Showing posts with label Nuget. Show all posts
Showing posts with label Nuget. Show all posts

Friday, April 24, 2015

ConfigurationTransformations Nuget

As in my recent work I had to deal with some configuration transformations from C# code I've created a nuget that can be used if you want to apply a transformation programmatically. To do that you can install the ConfigurationTransformation package from the official nuget repository.
In order to use it you can call the following from the package manager console:
PM> Install-Package ConfigurationTransformations

and then just create an instance of the ConfigTransformer class to apply the transformation.
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string destinationFile = @"C:\temp\resultweb.config";
TransformationResult transformationResult = configTransformer.ApplyTransformation(sourceFile, transformationFile, destinationFile);
If you don't specify the destination file the source file content will be changed with the result transformated content.

The other option is to get the result content without saving it to a file:
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string transformedContent = configTransformer.GetTransformedFileContent(sourceFile, transformationFile);

Thursday, November 8, 2012

How to configure local Nuget Repository

After my last posts about Nuget packaging I wanted to share another useful experience with Nuget.
You can create a local repository to store all the packages you need and not to download those every time.
  1. To do this I have created a folder C:\NugetConfig\Repo and I have copied there the Newtonsoft.Json.4.5.10.nupkg package file
  2. To make the both solutions use this local repository all I have to do is to change the following settings in the NuGet.targets file:
    <ItemGroup Condition=" '$(PackageSources)' == '' ">
        <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
        <!--
            <PackageSource Include="https://nuget.org/api/v2/" />
            <PackageSource Include="https://my-nuget-source/nuget/" />
        -->
    </ItemGroup>
    
    and adding s new PackageSource location
    <ItemGroup Condition=" '$(PackageSources)' == '' ">
        <!-- Package sources used to restore packages. By default will used the registered sources under %APPDATA%\NuGet\NuGet.Config -->
        <!--
            <PackageSource Include="https://nuget.org/api/v2/" />
            <PackageSource Include="https://my-nuget-source/nuget/" />
        -->
        <PackageSource Include="C:\NugetConfig\Repo" />
    </ItemGroup>
    
    And that's it. This will make the solution search for the used packages in the given folder and you will get а meaningful error if the package could not be found.
  3. Furthermore you can add you local repository to the visual studio package sources
    so that you will be able to search and add packages from it to any new solution:
As usual you can find the code here NugetConfig-Local-Repo.zip.

Share Nuget packages between solutions part 2

Let's see how we can fix the problem explained here.
According to the latest release notes by now you should be able to change the packages folder by adding this setting in the Nuget.config file:
<configuration>
  <config>
    <add key="repositoryPath" value="C:\myteam\teampackages"></add>
  </config>
  ... 
</configuration>
However unfortunately I couldn't make this work :( So I found a workaround. I want to make the Second.sln use the package folder of First.sln. So I have made the following changes to the NuGet.targets file.
  1. I this section:
    <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
        <!-- Windows specific commands -->
        <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
        <PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
    </PropertyGroup>
    
    I have added the following row:
    <PackagesDir>$(SolutionDir)..\First\packages</PackagesDir> 
  2. Change the arguments of the NewGetCommand from:
    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(RequireConsentSwitch) -solutionDir "$(SolutionDir)</RestoreCommand>
    
    to:
    <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" -o "$(PackagesDir)" $(RequireConsentSwitch) -solutionDir "$(SolutionDir)</RestoreCommand>
    
    I have added -o "$(PackagesDir)" which should make the packages to be downloaded to the PackagesDir folder
  3. To make the configuration more flexible we can change the PackagesDir definition to
    <PackagesDir Condition="'$(PackagesDir)' == ''">$(SolutionDir)..\First\packages</PackagesDir> 
    this will allow us to predefine the PackagesDir value in the .csproj.user files.
  4. To test this we can add the C:\ NugetConfig\First\Dummy\Dummy.csproj.user with the following content:
    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <PackagesDir>C:\NugetConfig\packages</PackagesDir>
        </PropertyGroup>
    </Project>
    
    This will make the Nuget to get files in the C:\NugetConfig\packages folder.
And here is the code NugetConfig-part-2.zip

Share Nuget packages between solutions part 1

Many of you may use Nuget to add reference assemblies to your projects. Anyway sometimes we need to share a project between solutions. In this case if the relative path to this project from the different solutions is different you won't be able to use the default nuget packaging functionality.
Let me explain this with samples.
  1. Let's create an empty Console application named First in folder C:\NugetConfig\First
  2. Add a dummy class library project to this solution in the same folder.
  3. Add reference in the console application to the class library. You should have the following structure now:
  4. Now add Newtonsoft.Json package to the solution:
  5. Install the package to the Dummy project:
  6. Add a dummy class to the dummy project which references Newton Json:
    namespace Dummy
    {
        public class Dummy
        {
            public Newtonsoft.Json.Required Required { get; set; }
    
            public Dummy()
            {
                this.Required = Newtonsoft.Json.Required.Always;
            }
    
            public override string ToString()
            {
                return this.Required.ToString();
            }
        }
    } 
  7. Add a call to the dummy project in Program.cs
    using System;
    
    namespace First
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var dummy = new Dummy.Dummy();
                Console.WriteLine(dummy);
            }
        }
    }
    
  8. Now enable Nuget Package Restore on this solution:

    Now if we delete the Newtonsoft.Json folder from the packages folder and run the build the package is downloaded and the solution builds successfully
  9. Let's create a new project in another solution named Second in folder in folder C:\NugetConfig\Second
  10. Add reference to the Dummy project 
  11. Add a call to the dummy project in Program.cs
    using System;
    
    namespace Second
    {
        class Program
        {
            public static void Main(string[] args)
            {
                var dummy = new Dummy.Dummy();
                Console.WriteLine(dummy);
            }
        }
    }
    
  12. We can build the second solution now as we have already downloaded the needed nuget package. But let's delete the package from C:\NugetConfig\First\packages folder. 
  13. Now we won't be able to build the solution. Let's enable Nuget Package Restore on this solution as well.
  14. Trying to build the solution leads to this error:
So here is the actual problem:
In the Dummy project there is a reference to the NewtonJson
 <Reference Include="Newtonsoft.Json">
      <HintPath>..\packages\Newtonsoft.Json.4.5.10\lib\net40\Newtonsoft.Json.dll</HintPath>
 </Reference>
But our folder looks like this:

So we search for the Newtonsoft.Json.dll in folder C:\NugetConfig\First\packages folder as the HintPath is relative from the Dummy.csproj file.
But the build downloads the package to C:\NugetConfig\Second\packages

You can find the code here NugetConfig-part-1.zip

To see how we can resolve this issue go to the part 2.