0

Let's say I want to build a Nuget package which puts source code files to the Visual Studio projects it gets installed to. Well this works pretty fine with the "content"-approach.

That means I can bring these files in the following folder structure to automatically add them to the file system and the VS-projects as well:

.\ThePackage.nuspec
    └ content\TheFile.cs
    └ content\TheOtherFile.cs

With a package like this, Nuget will automatically add the source code files to the projects directly. However, it does that with both files, so I found no way to make that conditional.

"Why?" you may ask - well, I don't really have two cs files. I have one for C# and one for Visual Basic doing the same in different languages. So I need to distinguish between C# and Visual Basic project files. The content approach above with a structure like this ...

.\ThePackage.nuspec
    └ content\TheFile.cs
    └ content\TheFile.vb

... will cause a mix with the cs and the vb file in each project, of course.

Is there a way to tell Nuget that I just want to have the cs file in C# projects and the vb file in Visual Basic projects without the need to provide two Nuget packages like ThePackage for C# and ThePackage for VB?

Waescher
  • 5,361
  • 3
  • 34
  • 51
  • You can add a init.ps1-file to your nuget-package that is executed on installation. There you can place some logic like detect what language is used in the project etc and removed/add the unwanted or wanted files – D.J. Oct 31 '17 at 22:48
  • 1
    I solved it - if you would post this as answer, I could give you some points. – Waescher Nov 03 '17 at 14:31

2 Answers2

1

You can add a init.ps1-file to your nuget-package that is executed on installation. There you can place some logic like detect what language is used in the project etc and removed/add the unwanted or wanted files

D.J.
  • 3,644
  • 2
  • 15
  • 22
  • 1
    I added my own answer to provide some source code (it's too small in the comments). Thanks again. – Waescher Nov 03 '17 at 14:45
1

For all visitors searching for a solution. With the powershell approach suggested by @D.J., I ended up with that script below.


The nuget package has two content files:

content\XXXXXX.cs
content\XXXXXX.vb

With this, both get installed by Nuget (to the file system and the VS project).

Afterwards, I run the following script to delete the unused file again.

param($installPath, $toolsPath, $package, $project)


# All XXXXXX code files (for C# and VB) have been added by nuget because they are ContentFiles.
# Now, try to detect the project language and remove the unnecessary file after the installation.


function RemoveUnnecessaryCodeFile($project)
{
    $projectFullName = $project.FullName
    $codeFile = ""
    $removeCodeFile = ""

    if ($projectFullName -like "*.csproj*")
    {
        $codeFile = "XXXXXX.cs"
        $removeCodeFile = "XXXXXX.vb"
        Write-Host "Identified as C# project, installing '$codeFile'"
    }

    if ($projectFullName -like "*.vbproj*")
    {
        $codeFile = "XXXXXX.vb"
        $removeCodeFile = "XXXXXX.cs"
        Write-Host "Identified as VB project, installing '$codeFile'"
    }

    if ($removeCodeFile -eq "")
    {
        Write-Host "Could not find a supported project file (*.csproj, *.vbproj). You will get both code files and have to clean up manually. Sorry :("
    }
    else
    {
        # Delete the unnecessary code file (like *.vb for C# projects)
        #   Remove() would only remove it from the VS project, whereas 
        #   Delete() additionally deletes it from disk as well
        $project.ProjectItems.Item($removeCodeFile).Delete()
    }
}

RemoveUnnecessaryCodeFile -Project ($project)
Waescher
  • 5,361
  • 3
  • 34
  • 51