Skip to content

Commit 0b7aba5

Browse files
committed
Use api to publish crate file packaged from source
1 parent 5966cc7 commit 0b7aba5

File tree

3 files changed

+92
-16
lines changed

3 files changed

+92
-16
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,29 @@ stages:
9191
- pwsh: |
9292
$additionalOwners = @('heaths', 'hallipr')
9393
$crateName = '${{artifact.name}}'
94+
$token = $env:CARGO_REGISTRY_TOKEN
9495
95-
Write-Host "Publishing packae: '$crateName'"
96-
$manifestPath = "$(Pipeline.Workspace)/drop/$crateName/Cargo.toml"
97-
98-
$command = "cargo publish --manifest-path '$manifestPath'"
96+
Write-Host "Dry-run verifying '$crateName'"
9997
98+
$command = "cargo publish --manifest-path '$(Pipeline.Workspace)/drop/$crateName/Cargo.toml' --dry-run"
10099
Write-Host "> $command"
101100
Invoke-Expression $command
102101
103102
if (!$?) {
104-
Write-Error "Failed to publish package: '$crateName'"
103+
Write-Error "Dry-run failed for '$crateName'"
105104
exit 1
106105
}
107106
107+
Write-Host "Publishing package '$crateName'"
108+
109+
$cargoBin = '$(Pipeline.Workspace)/drop/$crateName.bin'
110+
111+
# https://doc.rust-lang.org/cargo/reference/registry-web-api.html#publish
112+
Invoke-WebRequest -Method Put -Uri 'https://crates.io/api/v1/crates/new' `
113+
-Headers @{ Accept = 'application/json'; Authorization = $token } `
114+
-ContentType 'application/json' `
115+
-InFile $cargoBin
116+
108117
$existingOwners = (cargo owner --list $crateName) -replace " \(.*", ""
109118
$missingOwners = $additionalOwners | Where-Object { $existingOwners -notcontains $_ }
110119

eng/scripts/Pack-Crates.ps1

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,64 @@ function Get-PackagesToBuild() {
126126
return $buildOrder
127127
}
128128

129+
# https://doc.rust-lang.org/cargo/reference/registry-web-api.html#publish
130+
# https://github.com/rust-lang/cargo/blob/5c87c14f9a162daf10d4133fdaab35c72d67b018/crates/crates-io/lib.rs#L42
131+
function Get-ApiMetadata($package) {
132+
$packagePath = Split-Path -Path $package.manifest_path -Parent
133+
$readmePath = $package.readme ? (Join-Path -Path $packagePath -ChildPath $package.readme) : $null
134+
135+
$jsonBody = [ordered]@{
136+
'name' = $package.name
137+
'vers' = $package.version
138+
'deps' = @()
139+
'features' = $package.features
140+
'authors' = $package.authors
141+
'description' = $package.description
142+
'documentation' = $package.documentation
143+
'homepage' = $package.homepage
144+
'readme' = if ($readmePath -and (Test-Path -Path $readmePath)) {
145+
Get-Content -Path $readmePath -Raw
146+
}
147+
else {
148+
$null
149+
}
150+
'readme_file' = $package.readme
151+
'keywords' = $package.keywords
152+
'categories' = $package.categories
153+
'license' = $package.license
154+
'license_file' = $package.license_file
155+
'repository' = $package.repository
156+
'links' = $package.links
157+
'rust_version' = $package.rust_version
158+
}
159+
160+
foreach ($dependency in $package.dependencies) {
161+
$jsonBody.deps += @{
162+
'name' = $dependency.name
163+
'version_req' = $dependency.req
164+
'features' = $dependency.features
165+
'optional' = $dependency.optional
166+
'default_features' = $dependency.default_features
167+
'target' = $dependency.target
168+
'kind' = $dependency.kind
169+
'explicit_name_in_toml' = $dependency.rename
170+
}
171+
}
172+
173+
return $jsonBody
174+
}
175+
176+
function New-ApiPutFile($crateMetadata, $crateFilePath) {
177+
$metadataBytes = [Text.Encoding]::Utf8.GetBytes($crateMetadata)
178+
$metadataLengthBytes = [BitConverter]::GetBytes([UInt32]$metadataBytes.Length)
179+
$crateBytes = [IO.File]::ReadAllBytes($crateFilePath)
180+
$crateLengthBytes = [BitConverter]::GetBytes([UInt32]$crateBytes.Length)
181+
182+
$bytes += $metadataLengthBytes + $metadataBytes + $crateLengthBytes + $crateBytes
183+
184+
return $bytes
185+
}
186+
129187
Push-Location $RepoRoot
130188
try {
131189
[array]$packages = Get-PackagesToBuild
@@ -159,27 +217,38 @@ try {
159217
}
160218

161219
foreach ($package in $packages) {
220+
Write-Host "`nProcessing package '$($package.name)'"
162221
$packageName = $package.name
163222
$packageVersion = $package.version
164223

165224
if ($OutputPath -and $package.OutputPackage) {
166-
$crateFilePath = "$RepoRoot/target/package/$packageName-$packageVersion.crate"
225+
$sourceCrateFile = "$RepoRoot/target/package/$packageName-$packageVersion.crate"
226+
167227
$targetDirectory = "$OutputPath/$packageName"
228+
$targetCrateFile = "$OutputPath/$packageName-$packageVersion.crate"
229+
$targetJsonFile = "$OutputPath/$packageName-$packageVersion.json"
230+
$targetBinFile = "$OutputPath/$packageName.bin"
168231

169232
if (Test-Path $targetDirectory) {
170233
Write-Host "Removing existing directory '$targetDirectory'"
171234
Remove-Item -Path $targetDirectory -Recurse -Force
172235
}
236+
New-Item -ItemType Directory -Path $targetDirectory -Force | Out-Null
173237

174-
Write-Host "Copying crate '$crateFilePath' to '$OutputPath'"
175-
Copy-Item -Path $crateFilePath -Destination $OutputPath -Force
238+
Write-Host "Copying crate file to '$targetCrateFile'"
239+
Copy-Item -Path $sourceCrateFile -Destination $targetCrateFile -Force
176240

177-
Write-Host "Exctracting crate '$crateFilePath' to '$targetDirectory'"
178-
New-Item -ItemType Directory -Path $targetDirectory -Force | Out-Null
179-
tar -xf $crateFilePath --directory $targetDirectory --strip-components=1
241+
$crateMetadata = Get-ApiMetadata $package | ConvertTo-Json -Depth 10
242+
243+
Write-Host "Writing crates.io request metadata to '$targetJsonFile'"
244+
$crateMetadata | Out-File -FilePath "$targetJsonFile" -Encoding utf8
245+
246+
$uploadBytes = New-ApiPutFile $crateMetadata $sourceCrateFile
247+
Write-Host "Writing crates.io request bundle to '$targetBinFile'"
248+
[IO.File]::WriteAllBytes($targetBinFile, $uploadBytes)
180249

181-
# Remove Cargo.toml.orig from the extracted directory
182-
Remove-Item -Path "$targetDirectory/Cargo.toml.orig" -Force -ErrorAction SilentlyContinue
250+
Write-Host "Exctracting crate file to '$targetDirectory'"
251+
tar -xf $sourceCrateFile --directory $targetDirectory --strip-components=1
183252
}
184253
}
185254
}

eng/scripts/Yank-Crates.ps1

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,4 @@ foreach ($crateName in $crateNames) {
3131
}
3232
}
3333

34-
if ($hasErrors) {
35-
exit 1
36-
}
34+
exit $hasErrors ? 1 : 0

0 commit comments

Comments
 (0)