-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNew-NuGetPackage.ps1
115 lines (95 loc) · 4.28 KB
/
New-NuGetPackage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<#
.SYNOPSIS
Creates a NuGet package from each project in the sln file in the current directory.
.DESCRIPTION
Uses "dotnet sln list" to get all projects in the current directory. This means the current directory must have
exactly one sln file in it. Then calls "dotnet pack" for each csproj file with the provided arguments. If there is
a nuspec file in the project's directory too, then it is used to generate the package description instead of the
regular auto-generation.
.NOTES
We go through the projects individually in a foreach loop, because the "-p:NuspecFile=" parameter can't be passed
to a solution.
.EXAMPLE
New-NugetPackage @("--configuration:Release", "--warnaserror")
Calls "dotnet pack project.csproj --configuration:Release --warnaserror" on each project.
#>
param([array] $Arguments)
<#
.SYNOPSIS
Updates the given project file with a GetPropertyValue target to retrieve MSBuild properties in a way that
Directory.Build.props files also take effect, then retrieves the property.
#>
function Get-ProjectProperty
{
param (
[string] $ProjectFilePath,
[string] $PropertyName
)
try
{
$projectFileContent = Get-Content $ProjectFilePath -ErrorAction Stop
$newTarget = @"
<Target Name="GetPropertyValue">
<Message Importance="High" Text="---Get-ProjectProperty---`$($PropertyName)---Get-ProjectProperty---" />
</Target>
"@
# Insert the new target XML string just before the closing </Project> tag.
$updatedProjectFileContent = $projectFileContent -replace '</Project>', "$newTarget`r`n</Project>"
# Write the updated content to a new temporary project file.
$extension = (Get-Item $ProjectFilePath).Extension
$temporaryProjectFilePath = $ProjectFilePath -replace "\$extension`$", ".GetProperty$extension"
Set-Content $temporaryProjectFilePath $updatedProjectFileContent -ErrorAction Stop
$buildOutput = dotnet msbuild $temporaryProjectFilePath /nologo /v:minimal /p:DesignTimeBuild=true /p:BuildProjectReferences=false /t:GetPropertyValue
Write-Output "BUILD OUTPUT: '$buildOutput'"
# Removing the temporary file.
Remove-Item $temporaryProjectFilePath
return [string]::IsNullOrEmpty($buildOutput) ? '' : $buildOutput.Trim().Split('---Get-ProjectProperty---')[1]
}
catch
{
Write-Error "::error::Failed to add the GetPropertyValue target: $($_.Exception.Message)."
}
}
$projects = (Test-Path *.sln) ? (dotnet sln list | Select-Object -Skip 2 | Get-Item) : (Get-ChildItem *.csproj)
foreach ($project in $projects)
{
Write-Output "Packing $($project.Name)..."
$isPackableProperty = Get-ProjectProperty -ProjectFilePath $project -PropertyName 'IsPackable'
$isPackable = $isPackableProperty -notlike '*false*'
$isRequired = "$isPackableProperty".Trim() -like 'true'
# Silently skip project if the project file has <IsPackable>false</IsPackable>.
if (-not $isPackable)
{
Write-Output "Skipping $($project.Name) because it has <IsPackable>false</IsPackable>."
continue
}
# Warn and skip (or throw if required) if the project doesn't specify a package license file.
$packageLicenseFileProperty = Get-ProjectProperty -ProjectFilePath $project -PropertyName 'PackageLicenseFile'
if ([string]::IsNullOrEmpty($packageLicenseFileProperty))
{
$messageType = $isRequired ? 'error' : 'warning'
Write-Output ("::$messageType file=$($project.FullName)::Packing was skipped because $($project.Name) doesn't " +
'have a <PackageLicenseFile> property. You can avoid this check by including the ' +
'<IsPackable>false</IsPackable> property.')
Write-Output "isPackableProperty: '$isPackableProperty'"
Write-Output (Get-Content $project)
if ($isRequired) { exit 1 }
continue
}
Push-Location $project.Directory
$nuspecFile = (Get-ChildItem *.nuspec).Name
if ($nuspecFile.Count -eq 1)
{
dotnet pack $project -p:NuspecFile="$nuspecFile" @Arguments
}
else
{
dotnet pack $project @Arguments
}
if ($LASTEXITCODE -ne 0)
{
Write-Output "::error file=$($project.FullName)::dotnet pack failed for the project $($project.Name)."
exit 1
}
Pop-Location
}