Replace entire web.config with NuGet - visual-studio

Is there a way to have nuget replace the entire web.config when you install a package?
Let's say I create a custom package which contains a web.config. I want to install this custom package and have it completely overwrite the existing web.config in a given project instead of merging like it does by default. How can I accomplish this?

Seems to me you could use a XML-Document-Transform and xdt:Transform="Replace" the configuration section like so:
<?xml version="1.0"?>
<configuration xdt:Transform="Replace" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
...
<system.webServer>
...
</system.webServer>
</configuration>
I haven't done this myself, and I'm not sure that a transform can be applied to the configuration element. Let me know if that doesn't work and I'll delete this answer.

Related

NuGet packages getting installed to the wrong folder

I'm working on a Xamarin.Forms PCL mobile app in Visual Studio 2017, using project.json for package management (I'm not using PackageReference, since Visual Studio 2017 is required for that, and some of our team are still using Visual Studio 2015). I have multiple projects within the solution, and I have multiple branches of the project, like so:
MobileApp/
packages/ <<--- (I want nuget packages to be installed here)
Branches/
DevBranchSolution/
MobileApp.sln
nuget.config
ProjectA/
ProjectB/
I want all my (projects / solutions / branches) to be able to reference packages from a single location, so you'll notice I've added the packages folder at the root level in the MobileApp folder. I have a nuget.config file per solution that looks something like:
nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="..\..\packages" />
</config>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="CustomPackagesLocation" value="..\..\packages" />
</packageSources>
<disabledPackageSources />
</configuration>
In Visual Studio when I right-click on the solution, click "Manage NuGet Packages For Solution...", and install a package (e.g. Newtonsoft.Json), I would expect that it would install those package files inside my MobileApp/packages/ folder, the location I set in the solution's nuget.config. But it doesn't. Instead the files are getting put into the global NuGet packages location, which is %USERPROFILE%\.nuget\packages.
Why? Shouldn't my nuget.config file be overriding that? I have verified that when I go to Package Manager Settings, the location of CustomPackagesLocation is correct, but apparently the repositoryPath setting doesn't seem to affect anything.
I also noticed that inside the project.json.lock and Project.nuget.targets files, the package folder is set to the global NuGet packages location (the %USERPROFILE%/.nuget/packages one). Why? Where is it pulling this value from??
Why? Where is it pulling this value from??
The default packages directory for Project.json file is %USERPROFILE%/.nuget/packages and project.json project doesn't support repositoryPath config now. This is the reason why you have changed the repositoryPath, but NuGet still put packages into the global NuGet packages location. You can refer to the same issue on GitHub.
If you want to change packages default location for project.json, you can set "NUGET_PACKAGES" environment variable. Just Set "NUGET_PACKAGES" = "....\packages". Or you can place a NuGet.Config file next to the solution with the following content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value="..\..\packages" />
</config>
</configuration>
See NuGet.Config reference for details on config section.
I had a very similar problem where it wasn't using the packages folder for a class library. For some reason my csproj file had set for a few assemblies. I removed this line and did a update-package -reinstall -project myclasslibrary and it worked again just fine.
I'm not sure what set the HintPath in the first place.

Shared nuget.config file on TFS

I have this setup:
$/Shared/nuget.config
$/Shared/.nuget
$/SomeTeamProject/nuget.config
$/SomeTeamProject/SomeTeamProject.sln
Using VS.Net 2015 Update 3
nuget.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="$\..\..\Shared\.nuget" />
</config>
<packageRestore>
<add key="enabled" value="true" />
<add key="automatic" value="true"/>
</packageRestore>
</configuration>
I'd like to refer to the nuget.config file from Shared.
(I tried adding nuget.config to $/ but that's not allowed by TFS)
Added $/Shared/nuget.config as an existing item to SomeTeamProject but it doesn't seem to work.. It only works when I copy nuget.config from Shared to the SomeTeamProject folder.
It seems to ignore the referenced nuget.config file and creates a .packages folder in the SomeTeamProject folder..
A hard copy of nuget.config from the Shared Team Project works ok.
But a linked (add existing file) is not being used by NuGet for this solution:
The nuget.config file isn't used when you place it in "Shared" folder. You can refer to Configuring NuGet Behavior and specially Priority ordering section to see how the nuget.config file works.
And there is no way to simply add a link to get it work. The nuget only check the files in current folder or up level folder, it does not check the linked nuget.config file.

Variable Substitution in .config file with Octopus Deploy 3

I am using Octopus Deploy v3 for my deployment.
Within my project I have defined a variable called data.folder
I am trying to use this variable to set the value in a transformation file that was deployed using a package
I have the following .config file
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" >
<sitecore>
<sc.variable name="dataFolder">
<patch:attribute name="value">/Data</patch:attribute>
</sc.variable>
</sitecore>
</configuration>
and the following .ci.config file
<?xml version="1.0"?>
<configuration
xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:set="http://www.sitecore.net/xmlconfig/set/"
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<sitecore>
<sc.variable name="dataFolder" xdt:Transform="Replace" xdt:Location="Match(name)" set:value="#{data.folder}" />
</sitecore>
</configuration>
Both of these files are located in folder App_Config\Include
As you can see I have set the variable in the transformation file to include the variable "{data.folder}"
Within Octopus, I have created a package deployment step, and have set the following features:
Custom installation directory
Configuration Variables
Configuration transforms
Substitute variables in files
Within the Substitute variables in files I have included the target files
App_Config\Include\Z_Project.#{Octopus.Environment.Id}.config
I believe that I have followed http://docs.octopusdeploy.com/display/OD/Substitute+Variables+in+Files correctly, however when the deployment runs. the variable in the .ci.config file is not being set.
I am sure I have made a very basic mistake, but I have no idea what I have done wrong
What do I need to get the transform file to use the variable from Octopus
It sounds like you've got the process correct in terms of the steps, the variables etc, but I don't think the transformation in the .ci.config file is correct after previewing a transformation.
It ends up like this
<sitecore>
<sc.variable name="dataFolder" set:value="#{data.folder}"/>
</sitecore>
try using
<sitecore>
<sc.variable name="dataFolder" xdt:Transform="Replace" xdt:Location="Match(name)">
<patch:attribute name="value">#{data.folder}</patch:attribute>
</sc.variable>
</sitecore>
Running this through Slow Cheetah (Visual Studio Gallery) performs the transformation correctly and provided everything else is setup in Octopus Deploy correctly this should get the value injected before the transformation happens.
Hope this helps

Adding a Project to TFS and Ignore NuGet packages/lib Directory

My application has the following structure:
lib
package1
...
package2
...
repositories.config
src
MyProject.Web
packages.config
...
MyProject.sln
NuGet.config
My NuGet.config file contains the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="disableSourceControlIntegration" value="true" />
<add key="repositorypath" value="../lib" />
</config>
</configuration>
From my understanding the disableSourceControlIntegration config option is supposed to tell visual studio to add the packages to TFS and the repository path is used to change the default location to the packages directory.
I have this setup and working fine. However when I right click on the solution file and say add to source control I can see the lib directory is ready to check-in in source control explorer. I thought this folder would be ignored and simply exists locally and then when you do a build of the project is would download the nuget packages locally.
I'd appreciate it if someone could tell me what I'm doing wrong. Thanks
Your config should look like following. See I have solution tag instead of config.
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
Check this link out:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages

SlowCheetah transforms for non-web app.config

I am confused by the rapid pace and terrific work done by Sayed Hashimi and company on SlowCheetah, specifically whether or not SlowCheetah is able to transform files on Build instead of just Package and Publish.
I have SlowCheetah installed and am able to auto-generate the build configuration instances for app.config of a console application I am working with the development team to deploy. On build, a new "SlowCheetah" directory is created under the $(ProjectDir)\obj\x86\$(BuildConfig) directory. The obj\x86\STAGE\SlowCheetah directory, for instance, contains an app.config file with the transform completed as indicated when right clicking the STAGE transform and selecting "View Transforms".
The \bin\$(BuildConfig) directory, however, contains the untransformed app.config file as well as all of the templates for each of the $(BuildConfig) transformations - instead of replacing the app.config file in the bin\x86\STAGE directory with the one from SlowCheetah.
The build completes without error.
Is this the expected action, or is something still not working correctly?
#sayed-ibrahim-hashimi Each $(Configuration) has a transform. The transforms displays properly when viewing in Visual Studio, and is properly transformed in the \slowcheetah directory under the \obj directory tree. Examples of transforms are:
<?xml version="1.0" encoding="utf-8" ?>
<!-- For more information on using transformations see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform
<connectionStrings>
<add name="Portal" connectionString="Data Source=transformtestserver.domain.local;Initial Catalog=TestDB;User Id=TestUser;Password=testpw1;" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="DictionaryDatabase" connectionString="Data Source=transformtestserver.domain.local;Initial Catalog=TestDB;User Id=TestUser;Password=testpw2;" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="LoggingDatabase" connectionString="Data Source=transformtestserver.domain.local;Initial Catalog=TestDB;User Id=TestUser;Password=testpw3;" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
The problem may be related to the Nuget/solution configuration. The project I am working with is one part of a very large solution. I had a difficult time getting SlowCheetah installed and working - at one point finding Nuget said SlowCheetah was installed but the solution package directory did not contain SlowCheetah.
I setup a workaround in TeamCity to pickup the transformed app.config file from the \obj directory, but would really like to resolve the problem so I can rollout SC to the full development group. Is there a verbose log we can look at that might point out the details?
I faced with the same problem.
Local compilation in VS works great,
but on build Agent (TeamCity) the transformation magic not happend!
The solution I found relating to the location of the xml element in the proj file
<PropertyGroup Label="SlowCheetah">
<SlowCheetahToolsPath>$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.10.3\tools\))</SlowCheetahToolsPath>
<SlowCheetah_EnableImportFromNuGet Condition=" '$(SC_EnableImportFromNuGet)'=='' ">true</SlowCheetah_EnableImportFromNuGet>
<SlowCheetah_NuGetImportPath Condition=" '$(SlowCheetah_NuGetImportPath)'=='' ">$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\Properties\SlowCheetah\SlowCheetah.Transforms.targets ))</SlowCheetah_NuGetImportPath>
<SlowCheetahTargets Condition=" '$(SlowCheetah_EnableImportFromNuGet)'=='true' and Exists('$(SlowCheetah_NuGetImportPath)') ">$(SlowCheetah_NuGetImportPath)</SlowCheetahTargets>
</PropertyGroup>
I have noticed is that when the PropertyGroup is above all ItemGroup`s ellements
MsBuild.exe at last did the Transformation
Look for duplicate SlowCheetahTargets section - I had an older version and a newer version and the older one was pointing to a location that no longer existed in the nuget packages directory.
Delete the whole property group for the older / duplicated SlowCheetahTargets section and that should fix it.

Resources