简体   繁体   中英

Distinguish project languages for Nuget source code packages

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 ?

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

For all visitors searching for a solution. With the powershell approach suggested by @DJ, 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM