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



假设我想构建一个Nuget包,将源代码文件放入安装到的Visual Studio项目中。

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

.ThePackage.nuspec
└ contentTheFile.cs
└ contentTheOtherFile.cs

有了这样的包,Nuget将自动将源代码文件直接添加到项目中。然而,它对两个文件都是这样做的,所以我找不到任何方法使其成为条件。

"为什么?">您可能会问-嗯,我实际上没有两个cs文件。我有一个用于C#,一个用于Visual Basic,用不同的语言做同样的事情。因此,我需要区分C#和Visual Basic项目文件。上面的内容方法具有这样的结构。。。

.ThePackage.nuspec
└ contentTheFile.cs
└ contentTheFile.vb

当然,会导致每个项目中的csvb文件混合。

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

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

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


nuget包有两个内容文件:

contentXXXXXX.cs
contentXXXXXX.vb

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

之后,我运行以下脚本再次删除未使用的文件。

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)

最新更新