Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit b05ed5a

Browse files
committed
refactor nuget operations out of utility function
1 parent c043b91 commit b05ed5a

File tree

3 files changed

+401
-360
lines changed

3 files changed

+401
-360
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
function New-NugetPackage {
2+
[CmdletBinding()]
3+
Param(
4+
[Parameter(Mandatory = $true)]
5+
[string]$NuspecPath,
6+
7+
[Parameter(Mandatory = $true)]
8+
[string]$NugetPackageRoot,
9+
10+
[Parameter()]
11+
[string]$OutputPath = $NugetPackageRoot,
12+
13+
[Parameter(ParameterSetName = "UseNuget")]
14+
[string]$NugetExePath,
15+
16+
[Parameter(ParameterSetName = "UseDotnetCli")]
17+
[switch]$UseDotnetCli
18+
19+
)
20+
21+
if (-Not(Test-Path -Path $NuspecPath -PathType Leaf)) {
22+
throw "A nuspec file does not exist at $NuspecPath, provide valid path to a .nuspec"
23+
}
24+
25+
if (-Not(Test-Path -Path $NugetPackageRoot)) {
26+
throw "NugetPackageRoot $NugetPackageRoot does not exist"
27+
}
28+
29+
if ($PSCmdlet.ParameterSetName -eq "UseNuget") {
30+
if (-Not(Test-Path -Path $NuGetExePath)) {
31+
throw "Nuget.exe does not exist at $NugetExePath, provide a valid path to nuget.exe"
32+
}
33+
34+
$ArgumentList = @("pack")
35+
$ArgumentList += "`"$NuspecPath`""
36+
$ArgumentList += "-outputdirectory `"$OutputPath`""
37+
38+
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
39+
$processStartInfo.FileName = $NugetExePath
40+
$processStartInfo.RedirectStandardError = $true
41+
$processStartInfo.RedirectStandardOutput = $true
42+
$processStartInfo.UseShellExecute = $false
43+
$processStartInfo.Arguments = $ArgumentList
44+
45+
$process = New-Object System.Diagnostics.Process
46+
$process.StartInfo = $processStartInfo
47+
$process.Start() | Out-Null
48+
$process.WaitForExit()
49+
50+
if (-Not ($process.ExitCode -eq 0 )) {
51+
$stdErr = $process.StandardError.ReadToEnd()
52+
throw "nuget.exe failed to pack $stdErr"
53+
}
54+
}
55+
56+
if ($PSCmdlet.ParameterSetName -eq "UseDotnetCli") {
57+
#perform dotnet pack using a temporary project file.
58+
$dotnetCliPath = (Get-Command -Name "dotnet").Source
59+
$tempPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.Guid]::NewGuid()).Guid
60+
New-Item -ItemType Directory -Path $tempPath -Force | Out-Null
61+
62+
$CsprojContent = @"
63+
<Project Sdk="Microsoft.NET.Sdk">
64+
<PropertyGroup>
65+
<AssemblyName>NotUsed</AssemblyName>
66+
<Description>Temp project used for creating nupkg file.</Description>
67+
<TargetFramework>netcoreapp2.0</TargetFramework>
68+
<IsPackable>true</IsPackable>
69+
</PropertyGroup>
70+
</Project>
71+
"@
72+
$projectFile = New-Item -ItemType File -Path $tempPath -Name "Temp.csproj"
73+
Set-Content -Value $CsprojContent -Path $projectFile
74+
75+
#execution
76+
77+
$ArgumentList = @("pack")
78+
$ArgumentList += "`"$projectFile`""
79+
$ArgumentList += "/p:NuspecFile=`"$NuspecPath`""
80+
$ArgumentList += "--output `"$OutputPath`""
81+
82+
$processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
83+
$processStartInfo.FileName = $dotnetCliPath
84+
$processStartInfo.RedirectStandardError = $true
85+
$processStartInfo.RedirectStandardOutput = $true
86+
$processStartInfo.UseShellExecute = $false
87+
$processStartInfo.Arguments = $ArgumentList
88+
89+
$process = New-Object System.Diagnostics.Process
90+
$process.StartInfo = $processStartInfo
91+
$process.Start() | Out-Null
92+
$process.WaitForExit()
93+
94+
if (-Not ($process.ExitCode -eq 0 )) {
95+
if (Test-Path -Path $tempPath) {
96+
Remove-Item -Path $tempPath -Force -Recurse
97+
}
98+
$stdOut = $process.StandardOut.ReadToEnd()
99+
throw "dotnet cli failed to pack $stdOut"
100+
}
101+
102+
if (Test-Path -Path $tempPath) {
103+
Remove-Item -Path $tempPath -Force -Recurse
104+
}
105+
}
106+
107+
$stdOut = $process.StandardOutput.ReadLine()
108+
109+
Write-Verbose -Message $stdOut
110+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
function New-NuspecFile {
2+
[CmdletBinding()]
3+
Param(
4+
[Parameter(Mandatory = $true)]
5+
[string]$OutputPath,
6+
7+
[Parameter(Mandatory = $true)]
8+
[string]$Id,
9+
10+
[Parameter(Mandatory = $true)]
11+
[version]$Version,
12+
13+
[Parameter(Mandatory = $true)]
14+
[string]$Description,
15+
16+
[Parameter(Mandatory = $true)]
17+
[string[]]$Authors,
18+
19+
[Parameter()]
20+
[string[]]$Owners,
21+
22+
[Parameter()]
23+
[string]$ReleaseNotes,
24+
25+
[Parameter()]
26+
[bool]$RequireLicenseAcceptance,
27+
28+
[Parameter()]
29+
[string]$Copyright,
30+
31+
[Parameter()]
32+
[string[]]$Tags,
33+
34+
[Parameter()]
35+
[string]$LicenseUrl,
36+
37+
[Parameter()]
38+
[string]$ProjectUrl,
39+
40+
[Parameter()]
41+
[string]$IconUrl,
42+
43+
[Parameter()]
44+
[PSObject[]]$Dependencies,
45+
46+
[Parameter()]
47+
[PSObject[]]$Files
48+
49+
)
50+
51+
$nameSpaceUri = "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"
52+
[xml]$xml = New-Object System.Xml.XmlDocument
53+
54+
$xmlDeclaration = $xml.CreateXmlDeclaration("1.0", "utf-8", $null)
55+
$xml.AppendChild($xmlDeclaration) | Out-Null
56+
57+
#create top-level elements
58+
$packageElement = $xml.CreateElement("package", $nameSpaceUri)
59+
$metaDataElement = $xml.CreateElement("metadata", $nameSpaceUri)
60+
61+
#truncate tags if they exceed nuspec specifications for size.
62+
$Tags = $Tags -Join " "
63+
64+
if ($Tags.Length -gt 4000) {
65+
$Tags = $Tags.Substring(0, $Tags.LastIndexOf(" "))
66+
Write-Warning -Message "Nuspec 'Tag' list exceeded max 4000 characters and was truncated."
67+
}
68+
69+
$metaDataElementsHash = [ordered]@{
70+
id = $Id
71+
version = $Version
72+
description = $Description
73+
authors = $Authors -Join ","
74+
owners = $Owners -Join ","
75+
releaseNotes = $ReleaseNotes
76+
requireLicenseAcceptance = $RequireLicenseAcceptance.ToString().ToLower()
77+
copyright = $Copyright
78+
tags = $Tags
79+
licenseUrl = $LicenseUrl
80+
projectUrl = $ProjectUrl
81+
iconUrl = $IconUrl
82+
}
83+
84+
foreach ($key in $metaDataElementsHash.Keys) {
85+
$element = $xml.CreateElement($key, $nameSpaceUri)
86+
$elementInnerText = $metaDataElementsHash.item($key)
87+
$element.InnerText = $elementInnerText
88+
89+
$metaDataElement.AppendChild($element) | Out-Null
90+
}
91+
92+
93+
if ($Dependencies) {
94+
$dependenciesElement = $xml.CreateElement("dependencies", $nameSpaceUri)
95+
96+
foreach ($dependency in $Dependencies) {
97+
$element = $xml.CreateElement("dependency", $nameSpaceUri)
98+
$element.SetAttribute("id", $dependency.id)
99+
if ($dependency.version) { $element.SetAttribute("version", $dependency.version) }
100+
101+
$dependenciesElement.AppendChild($element) | Out-Null
102+
}
103+
$metaDataElement.AppendChild($dependenciesElement) | Out-Null
104+
}
105+
106+
if ($Files) {
107+
$filesElement = $xml.CreateElement("files", $nameSpaceUri)
108+
109+
foreach ($file in $Files) {
110+
$element = $xml.CreateElement("file", $nameSpaceUri)
111+
$element.SetAttribute("src", $file.src)
112+
if ($file.target) { $element.SetAttribute("target", $file.target) }
113+
if ($file.exclude) { $element.SetAttribute("exclude", $file.exclude) }
114+
115+
$filesElement.AppendChild($element) | Out-Null
116+
}
117+
}
118+
119+
$packageElement.AppendChild($metaDataElement) | Out-Null
120+
if ($filesElement) { $packageElement.AppendChild($filesElement) | Out-Null }
121+
122+
$xml.AppendChild($packageElement) | Out-Null
123+
124+
$xml.save("$OutputPath\$Id.nuspec")
125+
}

0 commit comments

Comments
 (0)