Skip to content

Commit c15eaf2

Browse files
committed
Refactor all packing to Pack-Crates.ps1
1 parent 530dddf commit c15eaf2

File tree

5 files changed

+81
-169
lines changed

5 files changed

+81
-169
lines changed

eng/pipelines/templates/jobs/pack.yml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,37 @@ jobs:
6969
- ${{ else }}:
7070
- pwsh: |
7171
$artifacts = '${{ convertToJson(parameters.Artifacts) }}' | ConvertFrom-Json
72-
$requireDependencies = $true
72+
$isReleaseBuild = $true
7373
$artifactsToBuild = $artifacts | Where-Object { $_.releaseInBatch -eq 'True' }
7474
7575
if (!$artifactsToBuild) {
7676
Write-Host "No packages to release. Building all packages in the service directory with no dependency validation."
7777
$artifactsToBuild = $artifacts
78-
$requireDependencies = $false
78+
$isReleaseBuild = $false
7979
}
8080
8181
$packageNames = $artifactsToBuild.name
8282
83-
if ($requireDependencies) {
84-
Write-Host "Building crates with dependency validation for release"
85-
& $(Build.SourcesDirectory)/eng/scripts/Pack-ReleaseCrates.ps1 `
86-
-OutputPath '$(Build.ArtifactStagingDirectory)' `
87-
-PackageNames $packageNames `
88-
-OutBuildOrderFile '$(Build.ArtifactStagingDirectory)/release-order.json'
83+
Write-Host "##vso[task.setvariable variable=PackageNames]$($packageNames -join ',')"
84+
if ($isReleaseBuild) {
85+
Write-Host "##vso[task.setvariable variable=AdditionalPackageParams]-Release"
8986
} else {
90-
Write-Host "Building crates without validating dependencies"
91-
& $(Build.SourcesDirectory)/eng/scripts/Pack-Crates.ps1 `
92-
-OutputPath '$(Build.ArtifactStagingDirectory)' `
93-
-PackageNames $packageNames
87+
Write-Host "##vso[task.setvariable variable=AdditionalPackageParams]"
9488
}
89+
displayName: Configure crate packing
90+
condition: and(succeeded(), ne(variables['NoPackagesChanged'],'true'))
91+
92+
- task: Powershell@2
9593
displayName: Pack Crates
9694
condition: and(succeeded(), ne(variables['NoPackagesChanged'],'true'))
95+
inputs:
96+
pwsh: true
97+
filePath: $(Build.SourcesDirectory)/eng/scripts/Pack-Crates.ps1
98+
arguments: >
99+
-OutputPath '$(Build.ArtifactStagingDirectory)'
100+
-PackageNames $(PackageNames)
101+
$(AdditionalPackageParams)
102+
97103
98104
- template: /eng/common/pipelines/templates/steps/publish-1es-artifact.yml
99105
parameters:

eng/pipelines/templates/stages/archetype-rust-release.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ stages:
9595
# and use ArtifactIndex to select the right one
9696
$index = [int]'$(ArtifactIndex)'
9797
$artifacts = Get-Content '$(Pipeline.Workspace)/drop/release-order.json' | ConvertFrom-Json
98+
99+
# Force $artifacts to be an array (PowerShell unrolls single-item arrays)
100+
if ($artifacts -isnot [Array]) {
101+
$artifacts = @($artifacts)
102+
}
103+
98104
if ($index -ge $artifacts.Count) {
99105
Write-Error "ArtifactIndex $index is out of range (0..$($artifacts.Count - 1))"
100106
exit 1

eng/scripts/Pack-Common.ps1

Lines changed: 0 additions & 15 deletions
This file was deleted.

eng/scripts/Pack-Crates.ps1

Lines changed: 57 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ param(
88
[string[]]$PackageNames,
99
[Parameter(ParameterSetName = 'PackageInfo')]
1010
[string]$PackageInfoDirectory,
11-
[switch]$NoVerify
11+
[switch]$Release,
12+
[switch]$NoVerify,
13+
[string]$OutBuildOrderFile
1214
)
1315

1416
$ErrorActionPreference = 'Stop'
1517

1618
. ([System.IO.Path]::Combine($PSScriptRoot, '..', 'common', 'scripts', 'common.ps1'))
17-
. ([System.IO.Path]::Combine($PSScriptRoot, 'Pack-Common.ps1'))
1819

1920
Write-Host @"
2021
Packing crates with
@@ -61,8 +62,8 @@ function Get-OutputPackageNames($workspacePackages) {
6162
function Get-CargoPackages() {
6263
$metadata = Get-CargoMetadata
6364

64-
# path based depdenencies are assumed to be unreleased package versions
65-
# they must be included in this build and build before packages that depend on them
65+
# Path based dependencies are assumed to be unreleased package versions. In
66+
# non-release builds these should be packed as well.
6667
foreach ($package in $metadata.packages) {
6768
$package.UnreleasedDependencies = @()
6869
foreach ($dependency in $package.dependencies) {
@@ -80,9 +81,13 @@ function Get-PackagesToBuild() {
8081
$packages = Get-CargoPackages
8182
$outputPackageNames = Get-OutputPackageNames $packages
8283

83-
# We start with output packages, then recursively add unreleased dependencies to the list of packages that need to be built
8484
[array]$packagesToBuild = $packages | Where-Object { $outputPackageNames.Contains($_.name) }
8585

86+
if ($Release) {
87+
return $packagesToBuild
88+
}
89+
90+
# If not releasing, expand dependencies into list of packages to build
8691
$toProcess = $packagesToBuild
8792
while ($toProcess.Length -gt 0) {
8893
$package = $toProcess[0]
@@ -96,111 +101,77 @@ function Get-PackagesToBuild() {
96101
}
97102
}
98103

99-
$buildOrder = @()
100-
101-
# Then we order the packages to that dependencies are built first
102-
while ($packagesToBuild.Count -gt 0) {
103-
# Pick any package with no unreleased dependencies, add it to the build order and remove it from the list of other packages' unreleased dependencies
104-
$package = $packagesToBuild | Where-Object { $_.UnreleasedDependencies.Count -eq 0 } | Select-Object -First 1
105-
106-
if (-not $package) {
107-
Write-Error "These packages cannot be built because they depend on unreleased dependencies that aren't being built." -ErrorAction Continue
108-
foreach ($package in $packagesToBuild) {
109-
Write-Error " $($package.name) -> $($package.UnreleasedDependencies -join ', ')" -ErrorAction Continue
110-
}
111-
exit 1
112-
}
113-
114-
$package.OutputPackage = $outputPackageNames.Contains($package.name)
115-
$buildOrder += $package
116-
$packagesToBuild = @($packagesToBuild -ne $package)
117-
118-
foreach ($otherPackage in $packagesToBuild) {
119-
$otherPackage.UnreleasedDependencies = $otherPackage.UnreleasedDependencies -ne $package
120-
}
121-
}
122-
123-
return $buildOrder
104+
return $packagesToBuild
124105
}
125106

126-
function Initialize-VendorDirectory() {
127-
$path = "$RepoRoot/target/vendor"
128-
Invoke-LoggedCommand "cargo vendor $path" -GroupOutput | Out-Host
129-
return $path
107+
function Get-CargoMetadata() {
108+
cargo metadata --no-deps --format-version 1 --manifest-path "$RepoRoot/Cargo.toml" | ConvertFrom-Json -Depth 100 -AsHashtable
130109
}
131110

132-
function Add-CrateToLocalRegistry($LocalRegistryPath, $Package) {
133-
$packageName = $Package.name
134-
$packageVersion = $Package.version
135-
136-
# create an index entry for the package
137-
$packagePath = "$RepoRoot/target/package/$packageName-$packageVersion"
111+
function Create-ApiViewFile($package) {
112+
$packageName = $package.name
113+
$command = "cargo run --manifest-path $RepoRoot/eng/tools/generate_api_report/Cargo.toml -- --package $packageName"
114+
Invoke-LoggedCommand $command -GroupOutput | Out-Host
138115

139-
Write-Host "Copying package '$packageName' to vendor directory '$LocalRegistryPath'"
140-
Copy-Item -Path $packagePath -Destination $LocalRegistryPath -Recurse
116+
$packagePath = Split-Path -Path $package.manifest_path -Parent
141117

142-
#write an empty checksum file
143-
'{"files":{}}' | Out-File -FilePath "$LocalRegistryPath/$packageName-$packageVersion/.cargo-checksum.json" -Encoding utf8
118+
"$packagePath/review/$packageName.rust.json"
144119
}
145120

146-
Push-Location $RepoRoot
121+
$originalLocation = Get-Location
147122
try {
148-
$localRegistryPath = Initialize-VendorDirectory
123+
Set-Location $RepoRoot
149124

150125
[array]$packages = Get-PackagesToBuild
151-
152-
Write-Host "Building packages in the following order:"
126+
$packageParams = @()
153127
foreach ($package in $packages) {
154-
$packageName = $package.name
155-
$type = if ($package.OutputPackage) { "output" } else { "dependency" }
156-
Write-Host " $packageName ($type)"
128+
$packageParams += "--package", $package.name
157129
}
158130

159-
foreach ($package in $packages) {
160-
Write-Host ""
131+
if ($NoVerify) {
132+
$packageParams += "--no-verify"
133+
}
161134

162-
$packageName = $package.name
163-
$packageVersion = $package.version
135+
Write-Host "> cargo publish --locked --dry-run --allow-dirty $($packageParams -join ' ')"
136+
& cargo publish --locked --dry-run --allow-dirty @packageParams 2>&1 | Tee-Object -Variable packResult
137+
if ($LASTEXITCODE) {
138+
Write-Host "cargo publish failed with exit code $LASTEXITCODE"
139+
exit $LASTEXITCODE
140+
}
164141

165-
$command = "cargo publish --locked --dry-run --package $packageName --registry crates-io --config `"source.crates-io.replace-with='local'`" --config `"source.local.directory='$localRegistryPath'`" --allow-dirty"
142+
if ($OutputPath -and $package.OutputPackage) {
143+
$sourcePath = [System.IO.Path]::Combine($RepoRoot, "target", "package", "$packageName-$packageVersion")
144+
$targetPath = [System.IO.Path]::Combine($OutputPath, $packageName)
145+
$targetContentsPath = [System.IO.Path]::Combine($targetPath, "contents")
146+
$targetApiReviewFile = [System.IO.Path]::Combine($targetPath, "$packageName.rust.json")
166147

167-
if ($NoVerify) {
168-
$command += " --no-verify"
148+
if (Test-Path -Path $targetContentsPath) {
149+
Remove-Item -Path $targetContentsPath -Recurse -Force
169150
}
170151

171-
Invoke-LoggedCommand -Command $command -GroupOutput
152+
Write-Host "Copying package '$packageName' to '$targetContentsPath'"
153+
New-Item -ItemType Directory -Path $targetContentsPath -Force | Out-Null
154+
Copy-Item -Path $sourcePath/* -Destination $targetContentsPath -Recurse -Exclude "Cargo.toml.orig"
172155

156+
Write-Host "Creating API review file"
157+
$apiReviewFile = Create-ApiViewFile $package
158+
159+
Write-Host "Copying API review file to '$targetApiReviewFile'"
160+
Copy-Item -Path $apiReviewFile -Destination $targetApiReviewFile -Force
161+
}
173162

174-
# copy the package to the local registry
175-
Add-CrateToLocalRegistry `
176-
-LocalRegistryPath $localRegistryPath `
177-
-Package $package
178-
179-
if ($OutputPath -and $package.OutputPackage) {
180-
$sourcePath = "$RepoRoot/target/package/$packageName-$packageVersion"
181-
$targetPath = "$OutputPath/$packageName"
182-
$targetContentsPath = "$targetPath/contents"
183-
$targetApiReviewFile = "$targetPath/$packageName.rust.json"
184-
185-
if (Test-Path -Path $targetContentsPath) {
186-
Remove-Item -Path $targetContentsPath -Recurse -Force
163+
if ($OutBuildOrderFile) {
164+
$buildOrder = @()
165+
foreach ($line in $packResult) {
166+
if ($line -match '^\s*Packaging (\w*) ([\w\d\.-]*)') {
167+
$buildOrder += $matches[1]
187168
}
188-
189-
Write-Host "Copying package '$packageName' to '$targetContentsPath'"
190-
New-Item -ItemType Directory -Path $targetContentsPath -Force | Out-Null
191-
Copy-Item -Path $sourcePath/* -Destination $targetContentsPath -Recurse -Exclude "Cargo.toml.orig"
192-
193-
Write-Host "Creating API review file"
194-
$apiReviewFile = Create-ApiViewFile $package
195-
196-
Write-Host "Copying API review file to '$targetApiReviewFile'"
197-
Copy-Item -Path $apiReviewFile -Destination $targetApiReviewFile -Force
198169
}
199-
}
200170

201-
Write-Host "Removing local registry"
202-
Remove-Item -Path $localRegistryPath -Recurse -Force | Out-Null
171+
Write-Host "Build Order: $($buildOrder -join ', ')"
172+
$buildOrder | ConvertTo-Json -Depth 100 | Set-Content $OutBuildOrderFile
173+
}
203174
}
204175
finally {
205-
Pop-Location
176+
Set-Location $originalLocation
206177
}

eng/scripts/Pack-ReleaseCrates.ps1

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)