区分Nuget源代码包的项目语言

时间:2021-12-22 11:45:00

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.

假设我想构建一个Nuget包,它将源代码文件放到它安装到的Visual Studio项目中。这对于“内容” - 方法来说效果很好。

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:

这意味着我可以将这些文件放在以下文件夹结构中,以自动将它们添加到文件系统和VS项目中:

.\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.

使用这样的包,Nuget会自动将源代码文件直接添加到项目中。但是,它对两个文件都这样做,所以我发现没有办法使这个条件。

"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 ...

“为什么?”你可能会问 - 好吧,我真的没有两个cs文件。我有一个用于C#,一个用于Visual Basic在不同语言中做同样的事情。所以我需要区分C#和Visual Basic项目文件。上面的内容方法有这样的结构......

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

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

当然,...会导致每个项目中的cs和vb文件混合使用。

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?

有没有办法告诉Nuget我只想在C#项目中使用cs文件和在Visual Basic项目中使用vb文件而不需要提供两个Nuget包,如ThePackage for C#和ThePackage for VB?

2 个解决方案

#1


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

您可以将init.ps1文件添加到在安装时执行的nuget-package。在那里,您可以放置​​一些逻辑,例如检测项目中使用的语言等,并删除/添加不需要的或想要的文件

#2


1  

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

对于寻找解决方案的所有访问者。使用@ D.J.建议的powershell方法,我最终得到了下面的脚本。


The nuget package has two content files:

nuget包有两个内容文件:

content\XXXXXX.cs
content\XXXXXX.vb

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

有了这个,两者都由Nuget安装(到文件系统和VS项目)。

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)

#1


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

您可以将init.ps1文件添加到在安装时执行的nuget-package。在那里,您可以放置​​一些逻辑,例如检测项目中使用的语言等,并删除/添加不需要的或想要的文件

#2


1  

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

对于寻找解决方案的所有访问者。使用@ D.J.建议的powershell方法,我最终得到了下面的脚本。


The nuget package has two content files:

nuget包有两个内容文件:

content\XXXXXX.cs
content\XXXXXX.vb

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

有了这个,两者都由Nuget安装(到文件系统和VS项目)。

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)