Compare commits
28 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
0bf23d70b5 | |
|
|
79bf210a15 | |
|
|
ee03436977 | |
|
|
5806ff2fbb | |
|
|
1abe422646 | |
|
|
af24eb9d52 | |
|
|
335abf6604 | |
|
|
531a059b51 | |
|
|
9d6e366dba | |
|
|
b87f71c0cc | |
|
|
d89098b72d | |
|
|
cc4fb03c26 | |
|
|
e62f38c97f | |
|
|
609876f00c | |
|
|
bbc4fb439e | |
|
|
ed2ce6e86c | |
|
|
41eef41f27 | |
|
|
4f841ec1fa | |
|
|
e8625989e0 | |
|
|
131d1fa8dc | |
|
|
685b6f6865 | |
|
|
edb0bf7a21 | |
|
|
e321a8798b | |
|
|
ad3716c78a | |
|
|
c092d99811 | |
|
|
7b30e92b2b | |
|
|
f8e87c30b8 | |
|
|
e968370299 |
|
|
@ -1,5 +1,6 @@
|
|||
/target/
|
||||
/dist/
|
||||
/coverage/
|
||||
/.venv/
|
||||
.env
|
||||
gitea_token.txt
|
||||
|
|
|
|||
|
|
@ -67,6 +67,15 @@ dependencies = [
|
|||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arbitrary"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed"
|
||||
dependencies = [
|
||||
"derive_arbitrary",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-waker"
|
||||
version = "1.1.2"
|
||||
|
|
@ -224,6 +233,12 @@ dependencies = [
|
|||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
|
||||
|
||||
[[package]]
|
||||
name = "crypto-common"
|
||||
version = "0.1.7"
|
||||
|
|
@ -234,6 +249,17 @@ dependencies = [
|
|||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_arbitrary"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b034bd7d5f032402a2479444dcc6f74e36a03f31854d41680fb240ef682a1ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "digest"
|
||||
version = "0.10.7"
|
||||
|
|
@ -304,6 +330,16 @@ version = "2.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
|
||||
|
||||
[[package]]
|
||||
name = "filetime"
|
||||
version = "0.2.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.12"
|
||||
|
|
@ -659,9 +695,9 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
|||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.103"
|
||||
version = "0.3.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102"
|
||||
checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"futures-util",
|
||||
|
|
@ -991,22 +1027,26 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "rust_realease"
|
||||
version = "0.1.3"
|
||||
version = "0.1.6-rc.5"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"console",
|
||||
"directories",
|
||||
"flate2",
|
||||
"libc",
|
||||
"regex",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"signal-hook",
|
||||
"similar",
|
||||
"tar",
|
||||
"tempfile",
|
||||
"toml",
|
||||
"toml_edit",
|
||||
"url",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1156,6 +1196,26 @@ version = "2.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
|
||||
dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.10"
|
||||
|
|
@ -1250,6 +1310,17 @@ dependencies = [
|
|||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tar"
|
||||
version = "0.4.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840"
|
||||
dependencies = [
|
||||
"filetime",
|
||||
"libc",
|
||||
"xattr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.27.0"
|
||||
|
|
@ -1531,9 +1602,9 @@ checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.126"
|
||||
version = "0.2.129"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4"
|
||||
checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
|
|
@ -1544,19 +1615,20 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.76"
|
||||
version = "0.4.79"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c62df1340f32221cb9c54d6a27b030e3dba64361d4a95bed55f9aacb44da291d"
|
||||
checksum = "3cbab34de2d982e9b48e18d216d04c4a6f641066ff19ffb699980f591ee3610e"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"tokio",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.126"
|
||||
version = "0.2.129"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1"
|
||||
checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
|
|
@ -1564,31 +1636,31 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.126"
|
||||
version = "0.2.129"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e"
|
||||
checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
"syn 3.0.5",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.126"
|
||||
version = "0.2.129"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24"
|
||||
checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.103"
|
||||
version = "0.3.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8622dcb61c0bcc9fffa6938bed81210af2da9a7e4a1a834b2e37a59b6dfb6141"
|
||||
checksum = "88261b9deccee56594c11a3460c462c41f58d148598fe70ad77070126a68aba4"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
|
|
@ -1716,6 +1788,16 @@ version = "0.6.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
|
||||
|
||||
[[package]]
|
||||
name = "xattr"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rustix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "yoke"
|
||||
version = "0.8.3"
|
||||
|
|
@ -1799,6 +1881,21 @@ dependencies = [
|
|||
"syn 3.0.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zip"
|
||||
version = "2.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1dcb24d0152526ae49b9b96c1dcf71850ca1e0b882e4e28ed898a93c41334744"
|
||||
dependencies = [
|
||||
"arbitrary",
|
||||
"crc32fast",
|
||||
"crossbeam-utils",
|
||||
"flate2",
|
||||
"indexmap",
|
||||
"memchr",
|
||||
"zopfli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zlib-rs"
|
||||
version = "0.6.7"
|
||||
|
|
@ -1810,3 +1907,15 @@ name = "zmij"
|
|||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
|
||||
[[package]]
|
||||
name = "zopfli"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"crc32fast",
|
||||
"log",
|
||||
"simd-adler32",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rust_realease"
|
||||
version = "0.1.3"
|
||||
version = "0.1.6-rc.5"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
repository = "https://proxy.dandaedre.com/gitea/idle-org/rust_realease"
|
||||
|
|
@ -25,3 +25,9 @@ flate2 = "1.1"
|
|||
sha2 = "0.10"
|
||||
similar = "2"
|
||||
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "multipart", "rustls-tls"] }
|
||||
tar = "0.4"
|
||||
zip = { version = "2.6", default-features = false, features = ["deflate"] }
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2"
|
||||
signal-hook = "0.3"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
[build]
|
||||
pre-build = [
|
||||
"apt-get update && apt-get install --assume-yes --no-install-recommends libudev-dev pkg-config",
|
||||
]
|
||||
|
||||
[build.env]
|
||||
passthrough = ["RUSTUP_TOOLCHAIN"]
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ pipeline {
|
|||
|
||||
environment {
|
||||
CARGO_TERM_COLOR = 'never'
|
||||
CARGO_TARPAULIN_VERSION = '0.37.5'
|
||||
BADGE_PUBLISH_ROOT = '/mnt/assets/coverage'
|
||||
}
|
||||
|
||||
stages {
|
||||
|
|
@ -17,7 +19,41 @@ pipeline {
|
|||
|
||||
stage('Cargo Tests') {
|
||||
steps {
|
||||
sh 'cargo test --locked --all'
|
||||
sh '''
|
||||
set -eu
|
||||
|
||||
cargo test --locked --all
|
||||
mkdir -p coverage
|
||||
: > "coverage/.tests-passed-${BUILD_NUMBER}"
|
||||
'''
|
||||
}
|
||||
}
|
||||
|
||||
stage('Coverage') {
|
||||
steps {
|
||||
sh '''
|
||||
set -eu
|
||||
|
||||
installed_tarpaulin=$(cargo tarpaulin --version 2>/dev/null || true)
|
||||
if [ "${installed_tarpaulin}" != "cargo-tarpaulin-tarpaulin ${CARGO_TARPAULIN_VERSION}" ]; then
|
||||
cargo install cargo-tarpaulin --locked --version "${CARGO_TARPAULIN_VERSION}"
|
||||
fi
|
||||
|
||||
cargo tarpaulin \
|
||||
--config tarpaulin.toml \
|
||||
--out Json \
|
||||
--output-dir coverage
|
||||
|
||||
mv coverage/tarpaulin-report.json coverage/summary.json
|
||||
coverage_percent=$(sed -n \
|
||||
's/^.*],"coverage":\\([0-9][0-9.]*\\),"covered":[0-9][0-9]*,"coverable":[0-9][0-9]*}$/\\1/p' \
|
||||
coverage/summary.json)
|
||||
scripts/render-coverage-badge.sh \
|
||||
"${coverage_percent}" \
|
||||
coverage/coverage.svg
|
||||
rm coverage/summary.json
|
||||
: > "coverage/.coverage-ready-${BUILD_NUMBER}"
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -27,8 +63,8 @@ pipeline {
|
|||
publishChecks(
|
||||
conclusion: 'SUCCESS',
|
||||
name: 'Rust CI',
|
||||
summary: 'Cargo tests passed.',
|
||||
text: 'Rust tests passed without cross compilation.',
|
||||
summary: 'Cargo tests, coverage, and badge generation passed.',
|
||||
text: 'Rust tests and the enforced line-coverage gate passed without cross compilation; current badges were generated.',
|
||||
title: 'CI Successful'
|
||||
)
|
||||
}
|
||||
|
|
@ -37,8 +73,8 @@ pipeline {
|
|||
publishChecks(
|
||||
conclusion: 'FAILURE',
|
||||
name: 'Rust CI',
|
||||
summary: 'Cargo tests failed.',
|
||||
text: 'Check the Jenkins log for the Cargo test failure.',
|
||||
summary: 'Cargo tests, coverage, or badge generation failed.',
|
||||
text: 'Check the Jenkins log for the Rust test, coverage, or badge-generation failure.',
|
||||
title: 'CI Failed'
|
||||
)
|
||||
}
|
||||
|
|
@ -52,5 +88,65 @@ pipeline {
|
|||
title: 'CI Unstable'
|
||||
)
|
||||
}
|
||||
|
||||
cleanup {
|
||||
sh '''
|
||||
set -eu
|
||||
|
||||
mkdir -p coverage
|
||||
test_status=failing
|
||||
if [ -f "coverage/.tests-passed-${BUILD_NUMBER}" ]; then
|
||||
test_status=passing
|
||||
fi
|
||||
scripts/render-test-badge.sh \
|
||||
"${test_status}" \
|
||||
coverage/test.svg
|
||||
|
||||
if [ ! -f "coverage/.coverage-ready-${BUILD_NUMBER}" ]; then
|
||||
scripts/render-coverage-badge.sh - coverage/coverage.svg
|
||||
fi
|
||||
|
||||
package_version=$(awk '
|
||||
$0 == "[package]" { in_package = 1; next }
|
||||
substr($0, 1, 1) == "[" { in_package = 0 }
|
||||
in_package && $1 == "version" && $2 == "=" {
|
||||
gsub(/^"|"$/, "", $3)
|
||||
print $3
|
||||
exit
|
||||
}
|
||||
' Cargo.toml)
|
||||
if [ -z "${package_version}" ]; then
|
||||
echo "could not determine package version" >&2
|
||||
exit 2
|
||||
fi
|
||||
scripts/render-version-badge.sh \
|
||||
"${package_version}" \
|
||||
coverage/version.svg
|
||||
|
||||
rm -f \
|
||||
coverage/summary.json \
|
||||
coverage/tarpaulin-report.json \
|
||||
"coverage/.tests-passed-${BUILD_NUMBER}" \
|
||||
"coverage/.coverage-ready-${BUILD_NUMBER}"
|
||||
'''
|
||||
|
||||
archiveArtifacts(
|
||||
allowEmptyArchive: false,
|
||||
artifacts: 'coverage/coverage.svg,coverage/test.svg,coverage/version.svg',
|
||||
fingerprint: true
|
||||
)
|
||||
|
||||
sh '''
|
||||
set -eu
|
||||
|
||||
if [ "${BRANCH_NAME:-}" = "main" ]; then
|
||||
scripts/publish-badges.sh \
|
||||
"${BADGE_PUBLISH_ROOT}" \
|
||||
coverage \
|
||||
test \
|
||||
version
|
||||
fi
|
||||
'''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
164
README.md
164
README.md
|
|
@ -1,144 +1,118 @@
|
|||
# rust_realease
|
||||
|
||||
`rust_realease` builds and publishes the binaries from a Rust package to a Gitea release. Give it a local project directory or a Git URL. It inspects Cargo metadata, checks the version tag, builds the selected targets with Cross, creates cargo-binstall-compatible `.tgz` archives, directly downloadable executables, or both, writes `SHA256SUMS`, uploads the assets, and verifies release downloads. It can also prepare a project for release with explicit commit, tag, and push options.
|
||||

|
||||

|
||||

|
||||
|
||||
## Install
|
||||
`rust_realease` builds Rust binary packages for multiple targets and publishes
|
||||
them as Gitea or GitHub release assets, including mirrored releases sent to
|
||||
multiple repositories. It is intended for maintainers who want
|
||||
repeatable Cross builds, checksums, cargo-binstall archives, and post-upload
|
||||
verification without maintaining a separate release pipeline.
|
||||
|
||||
Install Rust and Cargo, Git, Cross, and Docker or Podman. Install cargo-binstall when producing the default `tgz` artifacts and running end-to-end install verification. A working container backend is needed for release builds.
|
||||
## Prerequisites and installation
|
||||
|
||||
Install [Git](https://git-scm.com/),
|
||||
[Cross](https://github.com/cross-rs/cross), and a Cross-compatible Docker or
|
||||
Podman backend. The default archive format also requires
|
||||
[cargo-binstall](https://github.com/cargo-bins/cargo-binstall).
|
||||
|
||||
Install the latest prebuilt release:
|
||||
|
||||
```bash
|
||||
cargo install --path . --locked
|
||||
cargo binstall --git https://proxy.dandaedre.com/gitea/idle-org/rust_realease \
|
||||
rust_realease
|
||||
rust_realease --help
|
||||
```
|
||||
|
||||
The released `john-slop` binary can be installed directly from its Gitea repository with cargo-binstall:
|
||||
To build the current checkout instead:
|
||||
|
||||
```bash
|
||||
cargo binstall --git https://proxy.dandaedre.com/gitea/idle-org/john-slop \
|
||||
john-slop
|
||||
cargo install --path . --locked
|
||||
```
|
||||
|
||||
To run from this checkout without installing, use `cargo run -- SOURCE [OPTIONS]`.
|
||||
## Quick start
|
||||
|
||||
## Start with a project
|
||||
|
||||
Preview a local project or remote Git repository:
|
||||
Preview a local project without changing files, building binaries, or
|
||||
contacting a release service:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --dry-run
|
||||
rust_realease https://gitea.example.org/owner/my-tool.git --dry-run
|
||||
```
|
||||
|
||||
For a workspace, select the exact Cargo package name:
|
||||
The preview reports the selected package and binaries, expected version tag,
|
||||
metadata changes, build targets, artifact names, prerequisites, and release
|
||||
destination. It exits nonzero when something needs attention. Select a package
|
||||
explicitly when the source is a workspace:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-workspace --package my-tool --dry-run
|
||||
```
|
||||
|
||||
Each run handles one package and all of its Cargo binary targets. A dry run reports the package, version, expected tag, metadata changes, build targets, artifact names, prerequisites, and release destination. It writes no project files, builds no binaries, and changes no Git refs or releases. It exits nonzero when it finds requirements that still need attention.
|
||||
|
||||
Interactive terminals use colored status markers. Cross builds report elapsed
|
||||
time for every target and for the complete build. Redirected output and CI logs
|
||||
remain plain.
|
||||
|
||||
## Prepare a release
|
||||
|
||||
Preview the project changes first:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --prepare-repo --dry-run
|
||||
```
|
||||
|
||||
Apply the preparation locally:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --prepare-repo
|
||||
```
|
||||
|
||||
Preparation repairs supported `Cargo.toml` release metadata, updates known `.gitignore` and `Cross.toml` settings, and generates a missing `Cargo.lock`. It exits before building or publishing. Review and commit local changes yourself, or authorize the Git steps explicitly from a clean checkout:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --prepare-repo \
|
||||
--commit-preparation --create-tag --push-preparation
|
||||
```
|
||||
|
||||
If you only want to add or repair the recommended `[package.metadata.binstall]` entries in `Cargo.toml`, use the existing metadata-only path:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --fix-metadata --stop-after metadata
|
||||
```
|
||||
|
||||
Preview the same change without writing it by adding `--dry-run`. The generated values follow the archive layout produced by `rust_realease`, including the configured release tag prefix.
|
||||
|
||||
The preparation commit uses your configured Git author and signing settings. The tag is annotated and follows your Git tag-signing configuration (`git config tag.gpgsign true` to sign it); an existing tag is never moved. The branch and tag are pushed atomically without force. Use `--cross-image-tag main` only when you want preparation to set Cross images for the selected targets.
|
||||
|
||||
## Build local artifacts
|
||||
Build local artifacts through the package stage with:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --compile-only
|
||||
```
|
||||
|
||||
This runs host `cargo check` and `cargo test`, builds the selected targets with Cross, and writes the configured artifacts plus `SHA256SUMS` into a new `dist/release-*/` directory. It does not contact Gitea. The default targets are:
|
||||
This mode does not require a release token or contact Gitea or GitHub. Artifacts and
|
||||
`SHA256SUMS` are written under `dist/release-*/`. A later publication with the
|
||||
same clean commit and build settings reuses the completed target builds; pass
|
||||
`--recompile` to rebuild them instead.
|
||||
|
||||
- `x86_64-unknown-linux-musl`
|
||||
- `aarch64-unknown-linux-musl`
|
||||
- `x86_64-pc-windows-gnu`
|
||||
## Publish a release
|
||||
|
||||
Override the matrix with repeatable `--target TARGET` options. Use `--stop-after verify` for host checks alone.
|
||||
The package version must match a local tag, and the same tag must already
|
||||
exist on the remote. Publication also requires a clean checkout and an HTTPS
|
||||
Gitea or GitHub release destination.
|
||||
|
||||
Use `--artifact-format binary` when you only want raw executables, or `--artifact-format both` to publish raw executables alongside the default cargo-binstall archives:
|
||||
For one destination, store an access token in a single-line UTF-8 file:
|
||||
|
||||
```bash
|
||||
rust_realease ./my-tool --artifact-format binary --compile-only
|
||||
rust_realease ./my-tool --artifact-format both
|
||||
rust_realease ./my-tool --token-file ../gitea_token.txt
|
||||
```
|
||||
|
||||
## Publish to Gitea
|
||||
The token file takes precedence over the configured token environment
|
||||
variable. Without `--token-file`, the defaults are `GITEA_TOKEN` and
|
||||
`GITHUB_TOKEN`. Grant the token
|
||||
repository read and release write access; preparation pushes also require code
|
||||
write access. Tokens are redacted from diagnostics and are not passed to Cargo
|
||||
or Cross child processes.
|
||||
|
||||
The package version and release tag must agree. For version `0.1.0`, the default tag is `v0.1.0`. Before ordinary publication, the local tag must point to the checkout and the matching remote tag must exist. Publication requires a clean checkout and an HTTPS Gitea destination.
|
||||
Configure named `[[release.endpoints]]` entries to publish the same release to
|
||||
several repositories. Each can use its own `token-env`, or `--token-file` can
|
||||
point to a JSON object whose keys match the endpoint names. See the advanced
|
||||
guide for the complete configuration and token-file format.
|
||||
|
||||
Set a Gitea token in the environment and run the command without `--stop-after`:
|
||||
Use `--prepare-repo --dry-run` to preview supported metadata and repository
|
||||
preparation before publishing. See the advanced guide for explicit commit,
|
||||
tag, push, release-note, private-repository, and asset-replacement workflows.
|
||||
|
||||
```bash
|
||||
read -r -s -p 'Gitea token: ' GITEA_TOKEN
|
||||
printf '\n'
|
||||
export GITEA_TOKEN
|
||||
rust_realease ./my-tool
|
||||
```
|
||||
## Documentation
|
||||
|
||||
To publish `rust_realease` itself, install the current publisher and use the
|
||||
repository wrapper. It reads the token from a file without placing it in the
|
||||
process arguments and publishes both archive and standalone-binary assets:
|
||||
|
||||
```bash
|
||||
cargo install --path . --locked
|
||||
./release.sh ../gitea_token.txt --compile-only
|
||||
./release.sh ../gitea_token.txt --dry-run
|
||||
./release.sh ../gitea_token.txt
|
||||
```
|
||||
|
||||
For a remote repository, select the exact tag to check out:
|
||||
|
||||
```bash
|
||||
rust_realease https://gitea.example.org/owner/my-tool.git --tag v0.1.0
|
||||
```
|
||||
|
||||
If Git uses an internal repository URL but releases must use a public proxy, add an exact repository fallback to a project `rust-release.toml` or to the user configuration. The public fallback is used for Cargo metadata, Gitea publication, download verification, and cargo-binstall while the configured Git origin remains valid for cloning and preparation pushes. See [Advanced usage](docs/advanced-usage.md#configuration) for the file locations and an example.
|
||||
|
||||
The token account needs repository read access and release write access. `--push-preparation` additionally needs code write access. The token is read from `GITEA_TOKEN` by default and is kept out of child build processes. Public releases are verified by anonymous downloads. Private releases are downloaded with Gitea authentication; use `--skip-binstall-test` for them because cargo-binstall cannot install from their private attachment URLs. An existing asset with the same name stops the upload; `--replace-assets` explicitly permits replacing matching assets.
|
||||
|
||||
## More options
|
||||
|
||||
See [Advanced usage](docs/advanced-usage.md) for configuration, metadata, workspace behavior, stage controls, artifact layout, token handling, and troubleshooting. Run `rust_realease --help` for the complete option list.
|
||||
- [Advanced usage](docs/advanced-usage.md) covers sources, configuration,
|
||||
metadata preparation, pipeline stages, artifact formats, authentication,
|
||||
this repository's release procedure, and troubleshooting.
|
||||
- [Cross configuration](docs/advanced-usage.md#cross-configuration) explains
|
||||
target images, environment passthrough, and global or target-specific
|
||||
`pre-build` commands for native build dependencies.
|
||||
- `rust_realease --help` is the complete option reference.
|
||||
|
||||
## Development
|
||||
|
||||
From the repository root, run:
|
||||
|
||||
```bash
|
||||
cargo fmt --check
|
||||
cargo clippy --all-targets --all-features -- -D warnings
|
||||
cargo test --all
|
||||
cargo test --locked --all
|
||||
```
|
||||
|
||||
Jenkins runs Cargo tests only. Cross builds and live Gitea releases are separate release checks. The small [target_test_project fixture](https://proxy.dandaedre.com/gitea/idle-org/target_test_project) is maintained in its own repository for fast target matrix experiments. Clone it separately when needed; it is not part of this checkout.
|
||||
Jenkins enforces an 80% line-coverage floor. The opt-in real cargo-binstall
|
||||
fixture requires its external test repository and is ignored by the ordinary
|
||||
test suite; its setup is documented in [Advanced usage](docs/advanced-usage.md#disposable-release-fixture).
|
||||
|
||||
With `cargo-binstall` installed, run the opt-in isolated install fixture for all three targets with `cargo test real_binstall_installs_local_release_fixture -- --ignored`.
|
||||
## License and support
|
||||
|
||||
This project is licensed under the [MIT License](LICENSE). Report problems in
|
||||
the canonical [Gitea repository](https://proxy.dandaedre.com/gitea/idle-org/rust_realease/issues).
|
||||
|
|
|
|||
|
|
@ -11,6 +11,13 @@ rust_realease ./target_test_project --dry-run
|
|||
|
||||
Its public [v0.1.3 release](https://proxy.dandaedre.com/gitea/idle-org/target_test_project/releases/tag/v0.1.3) was used to validate preparation, all three default Cross targets, published checksums, and isolated cargo-binstall installs. Use a new version and tag for future publication tests; release assets with the same names are deliberately treated as collisions.
|
||||
|
||||
With cargo-binstall installed, run the opt-in isolated install fixture for all
|
||||
three targets with:
|
||||
|
||||
```bash
|
||||
cargo test --locked real_binstall_installs_local_release_fixture -- --ignored
|
||||
```
|
||||
|
||||
## Sources and package selection
|
||||
|
||||
`SOURCE` can be a local directory, an HTTP(S) Git URL, an SSH URL, or an SCP-style URL such as `git@host:owner/repo.git`. Remote sources are cloned into a managed checkout. Use `--keep-workdir` to retain that checkout and its artifacts; preparation retains remote checkouts automatically. Local sources are never deleted.
|
||||
|
|
@ -35,6 +42,18 @@ Missing automatically discovered files are ignored. A path explicitly passed wit
|
|||
[release]
|
||||
tag-prefix = "v"
|
||||
|
||||
[[release.endpoints]]
|
||||
name = "github"
|
||||
provider = "github"
|
||||
repository = "https://github.com/example/my-tool"
|
||||
token-env = "MY_TOOL_GITHUB_TOKEN"
|
||||
|
||||
[[release.endpoints]]
|
||||
name = "gitea-mirror"
|
||||
provider = "gitea"
|
||||
repository = "https://git.example.com/example/my-tool"
|
||||
token-env = "MY_TOOL_GITEA_TOKEN"
|
||||
|
||||
[build]
|
||||
targets = [
|
||||
"x86_64-unknown-linux-musl",
|
||||
|
|
@ -54,6 +73,21 @@ fallbacks = []
|
|||
token-env = "GITEA_TOKEN"
|
||||
```
|
||||
|
||||
`release.endpoints` is optional. When omitted, the release repository is taken
|
||||
from `--repository-url`, Cargo metadata, or Git origin as before. A
|
||||
`github.com` repository automatically selects GitHub and uses `GITHUB_TOKEN`;
|
||||
other hosts select Gitea and use `[gitea].token-env`. When endpoints are
|
||||
configured, the first is the primary repository used for Cargo release
|
||||
metadata and cargo-binstall verification. A later configuration layer replaces
|
||||
the complete endpoint list, while repository fallback lists continue to merge.
|
||||
Endpoint names must be unique and are also the stable keys used by JSON token
|
||||
files. GitHub endpoints currently target github.com; self-hosted GitHub
|
||||
Enterprise API layouts are not inferred.
|
||||
|
||||
`--repository-url` remains the single-destination override and cannot be mixed
|
||||
with configured endpoints. Put that repository in `release.endpoints` when it
|
||||
must participate in a multi-destination release.
|
||||
|
||||
Use `--tag-prefix ""` for an unprefixed tag such as `0.3.1`. `--tag TAG` must match the selected package version and configured prefix. `--unlocked` permits builds without the usual locked dependency resolution; the default uses `--locked`.
|
||||
|
||||
`package.format` accepts `"tgz"`, `"binary"`, or `"both"`. The default `"tgz"` produces cargo-binstall archives. `"binary"` produces only directly downloadable executables and does not require or verify cargo-binstall metadata. `"both"` produces both forms. The `--artifact-format` option overrides this setting for an individual run.
|
||||
|
|
@ -68,6 +102,77 @@ fallback-url = "https://proxy.dandaedre.com/gitea/idle-org/john-slop"
|
|||
|
||||
URLs are normalized, including removal of a trailing `.git`; SCP-style and SSH URLs normalize to HTTPS. The scheme remains significant for HTTP(S), so add separate rules if the same origin is used through both HTTP and SSH. Each rule matches one repository, not an entire host. The fallback must use HTTPS because it becomes the Gitea API and public download base. It is also written to `package.repository` during metadata repair. Git continues to clone and push through `origin`; the mapping tells the safety checks that the internal and public repositories are equivalent.
|
||||
|
||||
## Cross configuration
|
||||
|
||||
`rust_realease` runs Cross from the acquired project root, once for every
|
||||
target in `build.targets` (or supplied with `--target`). Put `Cross.toml` in
|
||||
that root to configure the container builds. Cross also accepts equivalent
|
||||
settings below `[workspace.metadata.cross]` in the root `Cargo.toml`; when both
|
||||
are present, `Cross.toml` takes precedence. Cross environment variables take
|
||||
precedence over both. See the
|
||||
[Cross configuration reference](https://github.com/cross-rs/cross/blob/main/docs/config_file.md)
|
||||
for every setting supported by Cross.
|
||||
|
||||
This repository uses the following configuration:
|
||||
|
||||
```toml
|
||||
[build]
|
||||
pre-build = [
|
||||
"apt-get update && apt-get install --assume-yes --no-install-recommends libudev-dev pkg-config",
|
||||
]
|
||||
|
||||
[build.env]
|
||||
passthrough = ["RUSTUP_TOOLCHAIN"]
|
||||
|
||||
[target.x86_64-unknown-linux-musl]
|
||||
image = "ghcr.io/cross-rs/x86_64-unknown-linux-musl:main"
|
||||
|
||||
[target.aarch64-unknown-linux-musl]
|
||||
image = "ghcr.io/cross-rs/aarch64-unknown-linux-musl:main"
|
||||
|
||||
[target.x86_64-pc-windows-gnu]
|
||||
image = "ghcr.io/cross-rs/x86_64-pc-windows-gnu:main"
|
||||
```
|
||||
|
||||
The tables have distinct jobs:
|
||||
|
||||
- `[build]` applies defaults to every target. Its `pre-build` commands run
|
||||
while Cross constructs a derived container image, before Cargo builds the
|
||||
package. Commands run as container-image build steps and must be valid for
|
||||
every configured base image.
|
||||
- `[build.env]` controls the environment exposed to builds. The
|
||||
`RUSTUP_TOOLCHAIN` passthrough keeps a caller-selected toolchain available
|
||||
inside Cross. Add only variables the container build actually needs.
|
||||
- `[target.TRIPLE]` selects settings for one target. Here, each `image` pins
|
||||
the corresponding image repository to its `main` tag.
|
||||
|
||||
For a dependency needed by only one target, put `pre-build` on that target
|
||||
instead of under `[build]`:
|
||||
|
||||
```toml
|
||||
[target.aarch64-unknown-linux-musl]
|
||||
image = "ghcr.io/cross-rs/aarch64-unknown-linux-musl:main"
|
||||
pre-build = [
|
||||
"dpkg --add-architecture $CROSS_DEB_ARCH",
|
||||
"apt-get update && apt-get install --assume-yes --no-install-recommends libudev-dev:$CROSS_DEB_ARCH pkg-config",
|
||||
]
|
||||
```
|
||||
|
||||
Cross supplies `CROSS_DEB_ARCH` when it builds a custom image, which avoids
|
||||
hard-coding Debian architecture names. A target-specific `pre-build` may also
|
||||
name an executable script relative to the workspace root when a list of shell
|
||||
commands becomes unwieldy. Treat all `pre-build` content as trusted code: it
|
||||
is executed by the container engine. `rust_realease` accepts `pre-build` in
|
||||
both supported configuration locations, validates the TOML, and leaves the
|
||||
commands for Cross to execute. Custom `dockerfile` and `runner` options remain
|
||||
unsupported by the publisher.
|
||||
|
||||
`--prepare-repo` creates `Cross.toml` when necessary and preserves existing
|
||||
settings. It always ensures `RUSTUP_TOOLCHAIN` is in
|
||||
`build.env.passthrough`; `--cross-image-tag TAG` additionally writes the image
|
||||
for every configured release target. Add project-specific `pre-build`
|
||||
commands directly to `Cross.toml` and commit them like other build inputs.
|
||||
|
||||
## Metadata and preparation
|
||||
|
||||
The publisher checks the selected member's repository and, when archives are enabled, its cargo-binstall metadata. `--fix-metadata` repairs supported entries in that member's `Cargo.toml` while preserving comments where practical. It does not commit, tag, or push. To preview the full preparation diff, use `--prepare-repo --dry-run`.
|
||||
|
|
@ -83,15 +188,15 @@ bin-dir = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }"
|
|||
|
||||
The generated `pkg-url` uses the configured tag prefix; the archive filename itself retains `-v{version}`. Unsupported binstall keys are reported as metadata conflicts. A preparation run may create one extra fix commit, as intended. `--commit-preparation` requires a clean worktree and index. `--create-tag` requires that commit option, and `--push-preparation` requires both. No option moves an existing tag or force pushes.
|
||||
|
||||
For repositories whose build scripts need a selected Rust toolchain inside Cross, preparation can add the supported `Cross.toml` environment setting. `--cross-image-tag TAG` explicitly selects `ghcr.io/cross-rs/TARGET:TAG` images. Existing unrelated Cross settings are preserved. Repository-provided Cross hooks such as `pre-build`, `dockerfile`, and `runner` are rejected by the release build.
|
||||
For repositories whose build scripts need a selected Rust toolchain inside Cross, preparation can add the supported `Cross.toml` environment setting. `--cross-image-tag TAG` explicitly selects `ghcr.io/cross-rs/TARGET:TAG` images. Existing unrelated Cross settings, including `pre-build`, are preserved.
|
||||
|
||||
## Pipeline stages
|
||||
|
||||
`--compile-only` runs through the package stage and then stops: it performs host checks, cross-compiles, and writes local artifacts without creating or uploading a release. It is the explicit artifact-build mode and conflicts with `--dry-run`, `--prepare-repo`, and `--stop-after`.
|
||||
`--compile-only` runs through the package stage and then stops: it performs host checks, cross-compiles, and writes local artifacts without requiring a Gitea token or creating or uploading a release. It is the explicit artifact-build mode and conflicts with `--dry-run`, `--prepare-repo`, and `--stop-after`. A later publication from the same clean commit reuses the completed build when its fingerprint still matches.
|
||||
|
||||
`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. Release builds use fresh target directories to avoid stale build-script output. `release` creates or reuses the Gitea release; `upload` uploads archives and checksums. The final stage downloads assets anonymously for public repositories or with Gitea authentication for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for public targets. `--skip-binstall-test` skips that install check, while retaining download verification.
|
||||
`--stop-after` can stop at `inspect`, `metadata`, `verify`, `build`, `package`, `release`, `upload`, or `binstall`. `verify` runs host `cargo check` and `cargo test`; `--skip-tests` skips only tests. A successful Cross build from a clean checkout records its Git commit, package, targets, profile, lock mode, container engine, active Rust toolchain, and binary list. A later run with the same inputs reuses those regular binary files from Cargo's target directory; dirty checkouts, changed inputs, missing files, and invalid cache records build in a fresh target directory instead. `--recompile` explicitly bypasses a matching completed build. `release` creates or reuses every configured release; `upload` uploads archives and checksums to each one. The final stage downloads assets anonymously for public repositories or with authenticated Gitea/GitHub API access for private repositories, checks their checksums and archive layout, and runs isolated `cargo-binstall` installs for a public primary endpoint. `--skip-binstall-test` skips that install check, while retaining download verification.
|
||||
|
||||
`--draft --stop-after upload` is useful when a release must stay private for review. Private repositories require `--skip-binstall-test`; cargo-binstall cannot fetch their attachment URLs, even when `GITEA_TOKEN` is set. `--prerelease` and `--draft` set properties when a release is created; reusing a release preserves its existing settings. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly.
|
||||
`--draft --stop-after upload` is useful when a release must stay private for review. Private repositories require `--skip-binstall-test`; cargo-binstall cannot fetch their attachment URLs, even when Gitea authentication is configured. `--prerelease` and `--draft` set properties when a release is created; reusing a release preserves its existing settings. `-m/--message TEXT` sets release notes, while `--message-file PATH` reads them from a UTF-8 file; they are mutually exclusive. Notes are included when creating a release and replace the body of an existing tagged release when explicitly supplied. Releases with macOS artifacts automatically append an artifact-specific quarantine-removal section unless the notes already contain a `## macOS Workaround` heading. Asset replacement is not transactional: if an upload fails after a matching asset is deleted, retry the publication explicitly.
|
||||
|
||||
## Artifact contract
|
||||
|
||||
|
|
@ -104,14 +209,90 @@ my-tool-x86_64-unknown-linux-musl-v0.1.0.tgz
|
|||
└── my-tool-helper
|
||||
```
|
||||
|
||||
Windows archive members have an `.exe` suffix. Raw assets are target-qualified to remain unambiguous, for example `my-tool-x86_64-unknown-linux-musl-v0.1.0` and `my-tool-x86_64-pc-windows-gnu-v0.1.0.exe`. `SHA256SUMS` lists every generated asset. Archive ownership and timestamps are normalized; identical binary bytes and modes produce identical archives. Separate builds can still produce different binaries.
|
||||
Windows archive members have an `.exe` suffix. Raw assets are target-qualified to remain unambiguous, for example `my-tool-x86_64-unknown-linux-musl-v0.1.0` and `my-tool-x86_64-pc-windows-gnu-v0.1.0.exe`. Apple raw assets omit the dotted version suffix, for example `my-tool-aarch64-apple-darwin`; use the versioned tgz archive by default. `SHA256SUMS` lists every generated asset. Archive ownership and timestamps are normalized; identical binary bytes and modes produce identical archives. Separate builds can still produce different binaries.
|
||||
|
||||
## Authentication and security
|
||||
|
||||
Authenticated Gitea API publication requires HTTPS. The token comes from the configured environment variable, never from a positional argument or URL. Child Cargo and Cross processes do not receive it. An HTTPS preparation push to the matching release repository can use the token through a temporary askpass helper; SSH pushes use normal Git credentials. Grant the token account code read and release write access, plus code write when using `--push-preparation`.
|
||||
Authenticated Gitea and GitHub API publication requires HTTPS. Pass
|
||||
`--token-file PATH` to read one token from a UTF-8 file; trailing line endings
|
||||
are ignored, while empty and multiline plaintext files are rejected. A
|
||||
plaintext file is valid only when publishing to one endpoint. For several
|
||||
endpoints, use a JSON object keyed by the configured endpoint names:
|
||||
|
||||
```json
|
||||
{
|
||||
"github": "github-token",
|
||||
"gitea-mirror": "gitea-token"
|
||||
}
|
||||
```
|
||||
|
||||
Every configured endpoint must have a token and unknown JSON keys are rejected,
|
||||
which catches misspelled endpoint names. Alternatively, omit `--token-file` and
|
||||
set each endpoint's `token-env`; this is the supported way to use several
|
||||
environment tokens. The legacy Gitea default is `GITEA_TOKEN`, while GitHub
|
||||
defaults to `GITHUB_TOKEN`. Tokens are never accepted as a positional argument
|
||||
or URL. Child Cargo and Cross processes do not receive them. An HTTPS
|
||||
preparation push uses the primary endpoint token supplied by the token file
|
||||
through a temporary askpass helper; SSH pushes use normal Git credentials.
|
||||
Grant each token repository read and release write access, plus code write when
|
||||
using `--push-preparation`.
|
||||
|
||||
Before building, every endpoint is authenticated and its repository visibility
|
||||
is checked. Release creation, asset upload, and downloaded checksum/archive
|
||||
verification then run for every endpoint in configuration order. A failure can
|
||||
therefore leave earlier endpoints updated; rerunning is safe when assets do not
|
||||
collide, or use `--replace-assets` when replacement is intended. Drafts must be
|
||||
published on every endpoint before final public verification.
|
||||
|
||||
Private Gitea repositories retain authenticated download verification. GitHub
|
||||
assets are verified through their asset-ID API URLs with redirect handling, as
|
||||
required for authenticated private downloads. Private primary repositories
|
||||
still require `--skip-binstall-test`, because cargo-binstall does not receive
|
||||
these release credentials.
|
||||
|
||||
Normal publication does not create a commit, tag, or push. Existing asset names cause an error unless `--replace-assets` is set; unrelated assets are not deleted. Private assets are verified with authenticated downloads and do not establish public installability.
|
||||
|
||||
## Releasing this repository
|
||||
|
||||
Install the publisher from the checkout, then invoke it directly. This
|
||||
repository publishes both archive and standalone binary assets. Its current
|
||||
version is a release candidate, so publication also uses `--prerelease`:
|
||||
|
||||
```bash
|
||||
cargo install --path . --locked
|
||||
rust_realease . --package rust_realease --artifact-format both --compile-only
|
||||
rust_realease . --package rust_realease --artifact-format both \
|
||||
--token-file ../gitea_token.txt --prerelease --dry-run
|
||||
rust_realease . --package rust_realease --artifact-format both \
|
||||
--token-file ../gitea_token.txt --prerelease
|
||||
```
|
||||
|
||||
Omit `--prerelease` for a stable version. The token-free compile command can
|
||||
run first; a later publication reuses its completed build when the commit and
|
||||
build inputs still match.
|
||||
|
||||
## CI badge pipeline
|
||||
|
||||
Jenkins runs the locked test suite and Tarpaulin configuration, enforces the
|
||||
80% line-coverage floor, and renders version, test, and coverage SVGs with the
|
||||
scripts in `scripts/`. It validates and archives exactly
|
||||
`coverage/version.svg`, `coverage/test.svg`, and `coverage/coverage.svg`.
|
||||
Every `main` build publishes those files during post-build cleanup under
|
||||
`/mnt/assets/coverage/idle-org/rust_realease/main/` for the public badge URLs
|
||||
used by the README; other branches do not publish them. A failed build renders
|
||||
the test badge as failing and the coverage badge as unavailable.
|
||||
|
||||
Run the same coverage command locally with:
|
||||
|
||||
```bash
|
||||
cargo tarpaulin --config tarpaulin.toml --out Json --output-dir coverage
|
||||
```
|
||||
|
||||
The asset server must mount the published directory read-only, disable
|
||||
directory listings, serve SVG with a restrictive content security policy, and
|
||||
revalidate the mutable URLs or use a short cache lifetime. Cross builds and
|
||||
live Gitea releases remain separate release checks.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **Missing tag:** prepare and push the expected tag explicitly, or use `--prepare-repo --commit-preparation --create-tag --push-preparation` from a clean checkout.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,329 @@
|
|||
# Automated macOS releases from Linux
|
||||
|
||||
`rust_realease` can build `aarch64-apple-darwin` and
|
||||
`x86_64-apple-darwin` on a Linux release host. Apple targets use a pinned
|
||||
`cargo-zigbuild` container; Linux and Windows targets continue to use Cross.
|
||||
One invocation may mix both backends and feeds every result into the existing
|
||||
archive, standalone binary, checksum, collision, upload, and download
|
||||
verification pipeline.
|
||||
|
||||
This is compile and packaging support for separate Mach-O executables. It does
|
||||
not create universal binaries or `.app` bundles, sign with Developer ID,
|
||||
notarize, or support macOS below 13.0.
|
||||
|
||||
## Agent workflow
|
||||
|
||||
1. Add either or both supported Apple triples to `build.targets`. Apple targets
|
||||
are opt-in; existing configurations retain their current target matrix.
|
||||
2. Keep project-specific Linux/Windows settings in `Cross.toml` or the existing
|
||||
`CROSS_CONFIG`. Apple targets do not use Cross configuration or require
|
||||
Darwin Cross images.
|
||||
3. Run `rust_realease SOURCE --package PACKAGE --dry-run`. Confirm each target's
|
||||
printed backend, Apple image digest, SDK path, deployment minimum, artifact
|
||||
names, and release destination. Dry-run does not start containers, compile,
|
||||
create artifacts, read a publication token, or contact a release API.
|
||||
4. Run the project's normal token-free compile-only command. This builds every
|
||||
selected target and packages all outputs before returning success. For
|
||||
`tiny-wb`, the entry point remains `./release.sh --compile-only`.
|
||||
5. Review `SHA256SUMS`, archive layout and modes, and the target-aware check
|
||||
report. Apple runtime checks are explicitly `skipped` on Linux.
|
||||
6. Perform native smoke/hardware testing on compatible macOS machines. Publish
|
||||
later with the normal release command; no separate Apple publisher or manual
|
||||
`build-macos.sh` step is needed.
|
||||
|
||||
`--target` overrides the configured target list and can be repeated. `--features`
|
||||
accepts a comma-separated list and overrides `build.features`:
|
||||
|
||||
```console
|
||||
rust_realease . --package my-tool \
|
||||
--target aarch64-apple-darwin \
|
||||
--target x86_64-apple-darwin \
|
||||
--features serial,ui \
|
||||
--artifact-format both --compile-only
|
||||
```
|
||||
|
||||
Package name, version and declared binary names come from Cargo metadata. The
|
||||
selected profile, lock policy, and features are applied identically to Cross
|
||||
and cargo-zigbuild.
|
||||
|
||||
## Configuration
|
||||
|
||||
The default backend mapping is fixed and target-aware:
|
||||
|
||||
| Target | Backend |
|
||||
| --- | --- |
|
||||
| `aarch64-apple-darwin` | pinned cargo-zigbuild container |
|
||||
| `x86_64-apple-darwin` | pinned cargo-zigbuild container |
|
||||
| every other valid Rust target | existing Cross command |
|
||||
|
||||
The complete supported syntax is:
|
||||
|
||||
```toml
|
||||
[build]
|
||||
targets = [
|
||||
"x86_64-unknown-linux-gnu",
|
||||
"x86_64-pc-windows-gnu",
|
||||
"aarch64-apple-darwin",
|
||||
"x86_64-apple-darwin",
|
||||
]
|
||||
profile = "release"
|
||||
locked = true
|
||||
features = ["serial", "ui"] # Optional; default is no explicit features.
|
||||
|
||||
[build.apple]
|
||||
image = "ghcr.io/rust-cross/cargo-zigbuild@sha256:9b4f6b3eb9e8f9fefb3960fe288ddba576627d66d9eff851ce7f62d2455564a5"
|
||||
deployment-target = "13.0"
|
||||
sdk-root = "/opt/MacOSX11.3.sdk"
|
||||
|
||||
[package]
|
||||
format = "both"
|
||||
```
|
||||
|
||||
The `[build.apple]` table is optional because these exact values are built-in
|
||||
defaults. Declaring it explicitly makes the compiler/SDK contract visible in a
|
||||
release policy. The image must use an immutable lowercase SHA-256 digest;
|
||||
mutable tags are rejected. `deployment-target` must parse as a macOS version
|
||||
at least 13.0. `sdk-root` must be an absolute path inside the image without
|
||||
parent traversal. Unknown settings, invalid feature names and unsupported
|
||||
Apple triples fail configuration validation.
|
||||
|
||||
Layered configuration follows the existing rules. A later `build.features`
|
||||
list replaces an earlier list. Individual values in `[build.apple]` override
|
||||
earlier values, so a user-level image pin cannot silently erase a project-level
|
||||
deployment target or SDK path. `--features` replaces the configured feature
|
||||
list when supplied.
|
||||
|
||||
## Container contract and security
|
||||
|
||||
The publisher invokes the selected Docker or rootless Podman engine directly:
|
||||
|
||||
```text
|
||||
ENGINE run --rm --user UID:GID [--userns=keep-id for Podman]
|
||||
-v ABSOLUTE_WORKSPACE:/io
|
||||
-v ABSOLUTE_FRESH_OUTPUT:/output
|
||||
-w /io
|
||||
-e HOME=/tmp
|
||||
-e CARGO_HOME=/output/cargo-home
|
||||
-e CARGO_TARGET_DIR=/output/target
|
||||
-e ZIG_GLOBAL_CACHE_DIR=/output/zig-cache
|
||||
-e MACOSX_DEPLOYMENT_TARGET=13.0
|
||||
-e SDKROOT=/opt/MacOSX11.3.sdk
|
||||
PINNED_IMAGE
|
||||
cargo zigbuild --locked --release --package PACKAGE --bin BINARY
|
||||
--target APPLE_TARGET --message-format=json-render-diagnostics
|
||||
```
|
||||
|
||||
Every argument is passed as a distinct process argument, so spaces and Unicode
|
||||
in checkout/output paths do not undergo shell interpretation. The source is
|
||||
mounted at `/io`; a fresh publisher-owned directory is mounted at `/output`.
|
||||
Cargo and Zig caches stay writable there, and generated files retain the
|
||||
caller's UID/GID. There are no interactive flags or container-engine socket
|
||||
mounts. Podman receives `--userns=keep-id`; Docker does not.
|
||||
|
||||
Only the listed environment variables enter the compiler container. Gitea's
|
||||
configured token variable and host variables whose names contain `TOKEN`,
|
||||
`PASSWORD`, `SECRET`, or `CREDENTIAL` are removed from compiler subprocesses.
|
||||
They do not appear in arguments or verbose logs. Private Cargo dependencies
|
||||
therefore need a separately designed non-secret-in-container acquisition
|
||||
strategy; do not add publication tokens to Apple settings.
|
||||
|
||||
`CROSS_CONTAINER_ENGINE=podman` or `docker` selects the engine for both
|
||||
backends. Otherwise the existing Docker-then-Podman discovery applies. The
|
||||
engine must answer `info`. Apple builds require a Linux host; neither a Mac nor
|
||||
host Xcode is required. `CROSS_CONFIG` continues to reach Cross for Linux and
|
||||
Windows and is irrelevant to the direct Apple container invocation.
|
||||
|
||||
The pinned image is the environment verified by the downstream reference:
|
||||
Rust 1.93.0, cargo-zigbuild 0.23.4, Zig 0.16.0, Apple Rust targets, and
|
||||
`SDKROOT=/opt/MacOSX11.3.sdk`. The SDK is supplied by the upstream
|
||||
`rust-cross/cargo-zigbuild` image. The digest identifies the actual image;
|
||||
the SDK directory name alone does not establish the deployment minimum. During
|
||||
the real verification below, LLVM reported an SDK load-command value of 26.4
|
||||
and a minimum of 13.0. The publisher validates the minimum independently.
|
||||
|
||||
## Build discovery and cache identity
|
||||
|
||||
Apple builds request Cargo JSON messages and use each `compiler-artifact`
|
||||
executable path. `/output` container paths are remapped to the exact mounted
|
||||
host directory and canonicalized beneath that fresh directory. The publisher
|
||||
does not reconstruct artifacts from a conventional checkout `target/` path.
|
||||
This supports invocation outside the checkout, paths with spaces, workspace
|
||||
package selection, custom profiles, and publisher-owned output directories.
|
||||
|
||||
A clean-commit build cache includes:
|
||||
|
||||
- commit, package, version, declared binaries and complete target list;
|
||||
- profile, lock policy and Cargo features;
|
||||
- container engine and active host Rust toolchain;
|
||||
- Apple image digest, deployment target and SDK root.
|
||||
|
||||
Cached output paths must remain regular files beneath the publisher cache and
|
||||
their SHA-256 hashes must still match. Cached Apple files are re-inspected as
|
||||
Mach-O before reuse. Changing any fingerprinted setting, changing a binary,
|
||||
using a dirty checkout, deleting an output, or passing `--recompile` forces a
|
||||
fresh build. A backend failure propagates immediately and cannot be reported as
|
||||
a successful build or create/upload a partial release.
|
||||
|
||||
## Artifacts and target-aware verification
|
||||
|
||||
Apple binary names have no `.exe` suffix. Versioned tgz archives remain the
|
||||
recommended download because their internal executable names are ordinary Cargo
|
||||
binary names. Standalone Apple asset names omit the version so Finder does not
|
||||
mistake the final dotted version component for a file extension. For version
|
||||
`{version}` and package `tiny-wb`:
|
||||
|
||||
```text
|
||||
tiny-wb-aarch64-apple-darwin-v{version}.tgz
|
||||
tiny-wb-aarch64-apple-darwin
|
||||
tiny-wb-x86_64-apple-darwin-v{version}.tgz
|
||||
tiny-wb-x86_64-apple-darwin
|
||||
```
|
||||
|
||||
Each tgz has the cargo-binstall layout:
|
||||
|
||||
```text
|
||||
tiny-wb-aarch64-apple-darwin-v{version}/
|
||||
tiny-wb
|
||||
```
|
||||
|
||||
The member and standalone file retain executable permission. Ownership and
|
||||
timestamps in tgz files remain normalized. A mixed four-target release using
|
||||
`format = "both"` produces eight payload assets and one `SHA256SUMS` asset.
|
||||
All local archives are checked before any release creation. Existing remote
|
||||
collision handling, replacement policy, upload, authenticated/private download
|
||||
handling, and checksum verification apply to Apple assets too.
|
||||
|
||||
When a release contains Apple artifacts, the publisher adds a
|
||||
`## macOS Workaround` section to the release body. It is generated from the
|
||||
artifacts being uploaded: named bundles are preferred, then canonical tgz
|
||||
archives, with standalone binaries used only when no archive exists. The shell
|
||||
snippet names the correct architecture, archive, extracted root, and executable
|
||||
members. It extracts into `~/Downloads`, applies
|
||||
`xattr -rd com.apple.quarantine` to the complete extracted tree, restores the
|
||||
executable bits, and starts a GUI executable when one has a `-gui` name. User
|
||||
notes supplied with `--message` or `--message-file` appear before this section.
|
||||
An existing `## macOS Workaround` heading prevents a duplicate section.
|
||||
|
||||
For every Apple executable, the Linux host parses the Mach-O header and load
|
||||
commands and requires:
|
||||
|
||||
- a little-endian 64-bit executable matching arm64 or x86-64 as requested;
|
||||
- macOS `LC_BUILD_VERSION` exactly matching `deployment-target`;
|
||||
- every declared loaded/re-exported/weak/upward dylib under `/usr/lib/` or
|
||||
`/System/Library/`;
|
||||
- at least one system dynamic dependency.
|
||||
|
||||
This detects architecture mistakes, a silently raised deployment minimum, and
|
||||
build-machine/container library leakage. It does not execute Mach-O files.
|
||||
`cargo-binstall` verification installs foreign binaries into an isolated
|
||||
directory but does not run them. Console output records macOS runtime as
|
||||
`skipped (no compatible macOS runner)`, never passed. A native Mac should test
|
||||
startup, the application's normal workflow, and relevant hardware.
|
||||
|
||||
## tiny-wb migration
|
||||
|
||||
Apply this configuration change in the downstream checkout; its existing
|
||||
unprefixed tags, release profile, locked mode, Gitea token setting, repository
|
||||
fallback and `release/Cross.toml` remain unchanged:
|
||||
|
||||
```diff
|
||||
[build]
|
||||
-targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu"]
|
||||
+targets = [
|
||||
+ "x86_64-unknown-linux-gnu",
|
||||
+ "x86_64-pc-windows-gnu",
|
||||
+ "aarch64-apple-darwin",
|
||||
+ "x86_64-apple-darwin",
|
||||
+]
|
||||
profile = "release"
|
||||
locked = true
|
||||
+
|
||||
+[build.apple]
|
||||
+image = "ghcr.io/rust-cross/cargo-zigbuild@sha256:9b4f6b3eb9e8f9fefb3960fe288ddba576627d66d9eff851ce7f62d2455564a5"
|
||||
+deployment-target = "13.0"
|
||||
+sdk-root = "/opt/MacOSX11.3.sdk"
|
||||
```
|
||||
|
||||
`release.sh` already passes `--artifact-format both` and selects
|
||||
`release/Cross.toml`; it needs no extra Apple build command. The publisher
|
||||
reads version and binaries from Cargo metadata, so do not put `0.1.4` or any
|
||||
other tested version in release configuration. `scripts/build-macos.sh` remains
|
||||
a useful diagnostic reference and can be retired from the release procedure
|
||||
after adopting a publisher version containing this feature.
|
||||
|
||||
Run:
|
||||
|
||||
```console
|
||||
./release.sh --compile-only
|
||||
```
|
||||
|
||||
No Gitea token is required. Publication remains a separate explicit invocation.
|
||||
|
||||
## Verification performed for this implementation
|
||||
|
||||
During the initial implementation verification, no release, upload, tag, push,
|
||||
Jenkins job, or global publisher installation was performed. A disposable clone
|
||||
of `tiny-wb` commit `8ed07cb` under `/tmp` was built with this checkout's
|
||||
publisher and rootless Podman.
|
||||
|
||||
Real two-target command:
|
||||
|
||||
```console
|
||||
CROSS_CONTAINER_ENGINE=podman ./target/debug/rust_realease \
|
||||
/tmp/tiny-wb-rust-release-macos --package tiny-wb \
|
||||
--target aarch64-apple-darwin --target x86_64-apple-darwin \
|
||||
--artifact-format both --compile-only --skip-tests
|
||||
```
|
||||
|
||||
It completed both targets in 95.7 seconds. A second identical invocation reused
|
||||
commit `8ed07cb60eb6` from the hash-validated cache. The first artifacts were:
|
||||
|
||||
```text
|
||||
/tmp/tiny-wb-rust-release-macos/dist/release-IxUtOI/
|
||||
tiny-wb-aarch64-apple-darwin-v0.1.4
|
||||
tiny-wb-aarch64-apple-darwin-v0.1.4.tgz
|
||||
tiny-wb-x86_64-apple-darwin-v0.1.4
|
||||
tiny-wb-x86_64-apple-darwin-v0.1.4.tgz
|
||||
SHA256SUMS
|
||||
```
|
||||
|
||||
`sha256sum -c SHA256SUMS` passed. `file` identified 64-bit arm64 and x86-64
|
||||
Mach-O executables. Both tgz files had one root directory and a mode-0755
|
||||
`tiny-wb`. `llvm-objdump` reported macOS minimum 13.0 and these dependencies for
|
||||
both architectures: IOKit, CoreFoundation, libSystem, libobjc, libiconv and
|
||||
libcharset, all under Apple system paths.
|
||||
|
||||
The real four-target command additionally set
|
||||
`CROSS_CONFIG=/tmp/tiny-wb-rust-release-macos/release/Cross.toml` and selected
|
||||
`x86_64-unknown-linux-gnu` plus `x86_64-pc-windows-gnu`. It completed all four
|
||||
builds in 169.7 seconds and produced exactly eight payload files in:
|
||||
|
||||
```text
|
||||
/tmp/tiny-wb-rust-release-macos/dist/release-3XAf1R/
|
||||
```
|
||||
|
||||
The automated suite also covers Docker/Podman argument differences, paths with
|
||||
spaces and Unicode, mixed routing, credential removal, failure propagation,
|
||||
dry-run, token-free compile-only operation, cache reuse/invalidation fields,
|
||||
Mach-O rejection cases, artifact names, permissions, archive layout and eight
|
||||
checksum entries. Native macOS execution, exact macOS versions/CPUs exercised by
|
||||
the maintainer's earlier successful testing, hardware behavior, Gatekeeper,
|
||||
At that point, signing, notarization, Jenkins and real publication remained
|
||||
external checks.
|
||||
|
||||
### Subsequent self-release
|
||||
|
||||
The implementation was then released as the `v0.1.6-rc.3` prerelease from
|
||||
commit `609876f00c7821dd5a9d36b364c85fd883d80939`. The repository's release
|
||||
configuration selected its three existing Cross targets plus
|
||||
`x86_64-apple-darwin`. The token-free compile-only phase completed all four
|
||||
targets in 374.9 seconds and validated the Apple Mach-O file before any remote
|
||||
release mutation.
|
||||
|
||||
The publication phase reused that hash-validated build, uploaded eight payload
|
||||
assets plus `SHA256SUMS`, downloaded every asset again, verified all checksums
|
||||
and archive layouts, and completed isolated cargo-binstall installation checks
|
||||
for all four targets. The Apple runtime check remained explicitly skipped
|
||||
because the publisher host was Linux. The commit and annotated tag were pushed
|
||||
atomically before release creation. No Jenkins job or global publisher
|
||||
installation was involved.
|
||||
|
|
@ -0,0 +1,326 @@
|
|||
# Portable application bundles (protocol 1)
|
||||
|
||||
Named bundles add application resources and runtime executables to the release
|
||||
workflow. They are **additional artifacts**: `package.format`,
|
||||
`--artifact-format`, canonical tgz filenames, and cargo-binstall metadata retain
|
||||
their existing meanings. No release was published to test this feature.
|
||||
|
||||
## Agent workflow
|
||||
|
||||
1. Keep `[package] format = "tgz"` for the existing cargo-binstall distribution.
|
||||
2. Add `[[package.bundles]]` for each additional distribution. Declare every
|
||||
static inclusion; select Cargo binaries; configure preparation if necessary.
|
||||
3. Run `rust_realease . --package PACKAGE --dry-run`. This validates static
|
||||
inputs, configuration, names, and selected targets, and prints commands and
|
||||
required outputs. Dynamic results remain unresolved. No preparation, dependency
|
||||
downloads, cache directories, or archives are created. Existing metadata/tag/
|
||||
prerequisite diagnostics still apply.
|
||||
4. Run `rust_realease . --package PACKAGE --compile-only`. This needs no Gitea
|
||||
token and creates no tag, push, release, or upload. `--stop-after package`
|
||||
does the same packaging work. `--skip-tests` only skips source tests;
|
||||
preparation and bundle validation still run.
|
||||
5. Read the stable local `dist/latest-artifacts.json`. Its `manifest` and
|
||||
`checksums` paths are relative to `dist/` and identify the latest successful
|
||||
bundle invocation; a failed invocation leaves the previous locator intact.
|
||||
Open that `artifact-manifest.json` (beside `SHA256SUMS`). It contains schema
|
||||
version 1, relative asset filenames,
|
||||
asset digests, and bundle file destinations, digests, modes, and check statuses.
|
||||
Resolve filenames relative to this directory; do not guess build paths.
|
||||
6. Extract and perform native application acceptance tests. Then use the existing
|
||||
explicit publication workflow. All artifacts are assembled and validated
|
||||
before release creation. Upload collision handling, `--replace-assets`, and
|
||||
download/checksum verification include bundles and the artifact manifest.
|
||||
Unrelated release assets remain untouched. Uploads are not transactional.
|
||||
|
||||
`--target TRIPLE` limits builds and bundle preparation to that target. A bundle's
|
||||
`targets` filters further; omitted/empty means all selected build targets. Every
|
||||
selected target must have an entry in `formats`. Supported formats are `zip` and
|
||||
`tgz` (gzip-compressed tar). Extensions are appended automatically.
|
||||
|
||||
## Configuration contract
|
||||
|
||||
```toml
|
||||
[package]
|
||||
format = "tgz"
|
||||
|
||||
[[package.bundles]]
|
||||
name = "portable"
|
||||
targets = ["x86_64-pc-windows-gnu", "x86_64-unknown-linux-gnu"]
|
||||
formats = { x86_64-pc-windows-gnu = "zip", x86_64-unknown-linux-gnu = "tgz" }
|
||||
archive-name = "{name}-{bundle}-{target}-v{version}"
|
||||
root = "{name}-{bundle}-{target}-v{version}"
|
||||
cargo-binaries = "all" # Or ["app", "app-gui"], using declared Cargo target names.
|
||||
files = [
|
||||
{ source = "assets", destination = "resources" },
|
||||
{ source = "LICENSE", destination = "LICENSES/app.txt", workspace-relative = true },
|
||||
{ source = "runtime/helper.dll", destination = "helper.dll" },
|
||||
]
|
||||
|
||||
[package.bundles.prepare]
|
||||
command = ["python3", "prepare-release.py", "--context", "{context-file}", "--manifest", "{manifest-file}"]
|
||||
inputs = ["prepare-release.py", "dependency-pins.json", "workspace:scripts/downloader.py"]
|
||||
timeout-seconds = 600
|
||||
required-destinations = ["helper{binary-ext}", "LICENSES/helper/LICENSE.txt"]
|
||||
```
|
||||
|
||||
Static sources are relative to the selected Cargo package's directory, even when
|
||||
configuration is at the workspace root. Set `workspace-relative = true` for
|
||||
workspace files. Directories are recursively copied as explicitly declared file
|
||||
trees; empty directories are not included. DLLs/shared libraries/resources use
|
||||
`kind = "file"` (the default); runnable programs use `kind = "executable"`.
|
||||
Executable trees apply that kind to every file, so declare mixed trees separately.
|
||||
|
||||
`inputs` lists regular preparation/smoke-test input files and validates their
|
||||
existence. Strings are package-relative; `workspace:PATH` explicitly selects the
|
||||
workspace. The hook may read these inputs but is responsible for interpreting
|
||||
pins. Hooks and bundles are never cached, so each invocation resolves dependency
|
||||
identity again. A verified Rust build cache may still be reused. Changing a
|
||||
license, adapter, pin, or downloaded dependency cannot cause a stale bundle hit.
|
||||
A new source commit can still invalidate the existing Rust build cache.
|
||||
|
||||
Template substitutions in `archive-name`, `root`, static destinations and required
|
||||
destinations are `{name}` (Cargo package), `{version}`, `{bundle}`, `{target}` and
|
||||
`{binary-ext}` (`.exe` on Windows, otherwise empty). Archive name and root must
|
||||
be single components; neither is a source path. Names must not collide with
|
||||
canonical artifacts or `SHA256SUMS`/`artifact-manifest.json`. Unknown config
|
||||
fields, formats, binary selections and template placeholders are errors. Layered
|
||||
configuration replaces the entire bundle list when a later layer specifies one.
|
||||
|
||||
## Host preparation contract
|
||||
|
||||
Preparation runs once per selected bundle/target after compilation or build-cache
|
||||
reuse, before archive creation. The working directory is the selected package
|
||||
root. `command` is an argv array, with no shell interpretation. Explicit shell
|
||||
commands can use `["sh", "-c", "..."]`. The only command substitutions are
|
||||
`{context-file}`, `{manifest-file}`, and `{bundle-directory}`; paths with spaces
|
||||
and Unicode stay single arguments.
|
||||
|
||||
The context JSON has these exact keys:
|
||||
|
||||
| Key | Meaning |
|
||||
| --- | --- |
|
||||
| `schema_version` | Integer `1` |
|
||||
| `package_name`, `package_version` | Selected Cargo package identity |
|
||||
| `bundle`, `target`, `profile`, `binary_ext` | Effective bundle/build settings |
|
||||
| `workspace_root`, `package_root` | Absolute source roots |
|
||||
| `source_commit`, `source_dirty` | Git HEAD and pre-build checkout dirty status |
|
||||
| `binaries` | Object mapping **all declared binary names** to absolute built executable paths |
|
||||
| `output_directory` | Fresh, initially empty directory for prepared files |
|
||||
| `cache_directory` | Persistent dependency cache, separated by bundle and target |
|
||||
| `manifest_path` | Absolute path where the hook must write its result JSON |
|
||||
| `bundle_directory` | Fresh tree populated after preparation, for optional smoke tests |
|
||||
|
||||
Bundle builds read executable paths from Cargo compiler-artifact JSON and remap
|
||||
Cross container `/target` paths to the exact host build directory. Cached
|
||||
builds use validated publisher cache paths. The hook must use `binaries`, never
|
||||
construct `target/TRIPLE/release/NAME`; this supports workspace members, custom
|
||||
profiles, `CARGO_TARGET_DIR` and fresh dirty-checkout builds.
|
||||
|
||||
The hook writes outputs only beneath `output_directory`, writes its result to
|
||||
`manifest_path`, and may maintain the supplied persistent cache. It must not
|
||||
construct archives, checksums, tags or releases. The host command is trusted
|
||||
project code, **not an OS filesystem/network sandbox**. Gitea's configured token
|
||||
variable and environment variables containing TOKEN, PASSWORD, SECRET or
|
||||
CREDENTIAL are removed from its environment. Output is streamed with target/
|
||||
bundle labels and existing secret redaction. Do not put credentials in command
|
||||
arguments or public provenance files.
|
||||
|
||||
Example result (replace hashes with actual lowercase SHA-256 hex digests):
|
||||
|
||||
```json
|
||||
{
|
||||
"schema_version": 1,
|
||||
"target": "x86_64-pc-windows-gnu",
|
||||
"files": [
|
||||
{"source": "helper.exe", "destination": "helper.exe", "kind": "executable", "sha256": "<64 hex characters>"},
|
||||
{"source": "notices/LICENSE.txt", "destination": "LICENSES/helper/LICENSE.txt", "kind": "file", "sha256": "<64 hex characters>"}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Every manifest source is a regular file relative to `output_directory`; enumerate
|
||||
files in directories individually. Every destination is relative to the archive
|
||||
root. Only declared files enter the archive. Required destinations must exist
|
||||
exactly, including case. The publisher verifies hashes and target/schema identity.
|
||||
Cargo executables cannot be replaced by static files or hook outputs.
|
||||
|
||||
Paths cannot be absolute, contain `..`, empty components, backslashes or colons.
|
||||
Symlinks, nonregular files, and hardlinked files are rejected, including directory
|
||||
traversal through symlinks. Windows destinations additionally reject reserved
|
||||
names, invalid characters and case-insensitive collisions. File/directory-prefix
|
||||
collisions also fail. Files are ordered deterministically; ownership and timestamps
|
||||
are normalized; modes are 0755 for executables and 0644 for data. Identical input
|
||||
bytes and settings produce identical archives. Each archive is written atomically
|
||||
and reopened to check its declared contents, modes and hashes. Partial staging is
|
||||
removed on failure; successful invocation directories are retained under `dist/`.
|
||||
|
||||
Executable validation currently supports little-endian Linux ELF (x86-64,
|
||||
aarch64, i686, riscv64gc) Windows PE (x86-64, aarch64, i686), and thin 64-bit macOS Mach-O executables
|
||||
(arm64 and x86-64). Unsupported
|
||||
executable targets fail explicitly. This checks format and architecture, not ABI,
|
||||
DLL completeness, license suitability or runtime behavior. For Apple named bundles, final verification also checks every executable against
|
||||
`build.apple.deployment-target`: a dependency may have an older minimum, but not
|
||||
a newer one, and only system dylib dependencies are accepted. Cargo outputs still
|
||||
have their exact configured minimum checked during build/cache validation.
|
||||
Foreign executables
|
||||
are never executed automatically by the publisher. Trusted preparation scripts
|
||||
must follow the same rule themselves.
|
||||
|
||||
Nonzero exit, timeout, SIGINT/SIGTERM cancellation on Unix, malformed JSON,
|
||||
missing outputs, hash mismatch or architecture mismatch abort before publication.
|
||||
Unix hooks run in their own process group; descendants are killed on completion,
|
||||
failure, timeout or cancellation. Native Windows host process-tree cancellation
|
||||
has not been validated; use a Unix release host for this workflow.
|
||||
|
||||
## Optional native smoke test
|
||||
|
||||
```toml
|
||||
[package.bundles.smoke-test]
|
||||
command = ["python3", "smoke-release.py", "{bundle-directory}"]
|
||||
inputs = ["smoke-release.py"]
|
||||
timeout-seconds = 60
|
||||
```
|
||||
|
||||
The publisher runs this command only when the target exactly matches `rustc -vV`'s
|
||||
host triple. The script should invoke programs by absolute path in the supplied
|
||||
tree, set any necessary environment explicitly, and return nonzero on failure.
|
||||
Checks are reported as `passed` or `skipped` with a reason in the artifact manifest;
|
||||
a failed command is reported with its status and stops packaging, so no successful
|
||||
artifact manifest is emitted for that invocation. A successful smoke test cannot
|
||||
silently modify declared payload bytes. Preparation may separately record native
|
||||
checks in its own provenance. Cross-compiling Windows is not Windows validation.
|
||||
|
||||
## Migration recipe: pptx-reporter
|
||||
|
||||
This recipe uses the existing `scripts/fetch_ffprobe.py` API:
|
||||
`fetch(target, cache=None, release="latest", expected_sha256=None)` returns
|
||||
`(binary_path, notices_directory)`. It retains that downloader's target selection,
|
||||
verified archive digests, LGPL/non-shared provider selection and provenance. Do not
|
||||
copy the host's `ffprobe` or change the publisher into a provider-specific downloader.
|
||||
|
||||
In the consumer workspace's `rust-release.toml`, keep the existing build targets,
|
||||
release and repository settings. Replace the separate packager comments with:
|
||||
|
||||
```toml
|
||||
[package]
|
||||
format = "tgz"
|
||||
|
||||
[[package.bundles]]
|
||||
name = "portable"
|
||||
targets = ["x86_64-pc-windows-gnu", "x86_64-unknown-linux-gnu"]
|
||||
formats = { x86_64-pc-windows-gnu = "zip", x86_64-unknown-linux-gnu = "tgz" }
|
||||
archive-name = "{name}-portable-{target}-v{version}"
|
||||
root = "{name}-portable-{target}-v{version}"
|
||||
cargo-binaries = "all"
|
||||
files = [
|
||||
{ source = "LICENSE", destination = "LICENSES/reporter-MIT.txt", workspace-relative = true },
|
||||
{ source = "assets/fonts/LICENSE", destination = "LICENSES/DejaVu.txt" },
|
||||
{ source = "docs/windows-quickstart.txt", destination = "README.txt", workspace-relative = true },
|
||||
]
|
||||
|
||||
[package.bundles.prepare]
|
||||
command = ["python3", "prepare-release.py", "--context", "{context-file}", "--manifest", "{manifest-file}"]
|
||||
inputs = ["prepare-release.py", "ffprobe-pins.json", "workspace:scripts/fetch_ffprobe.py"]
|
||||
timeout-seconds = 600
|
||||
required-destinations = ["ffprobe{binary-ext}", "LICENSES/ffmpeg/LICENSE.txt", "LICENSES/ffmpeg/provenance.json"]
|
||||
```
|
||||
|
||||
Create `crates/pptx-reporter/ffprobe-pins.json`, using actual immutable provider
|
||||
release tags and reviewed SHA-256 archive digests for **each target**:
|
||||
|
||||
```json
|
||||
{
|
||||
"x86_64-pc-windows-gnu": {"release": "<immutable release tag>", "sha256": "<Windows archive SHA-256>"},
|
||||
"x86_64-unknown-linux-gnu": {"release": "<immutable release tag>", "sha256": "<Linux archive SHA-256>"}
|
||||
}
|
||||
```
|
||||
|
||||
Create `crates/pptx-reporter/prepare-release.py`:
|
||||
|
||||
```python
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--context", type=Path, required=True)
|
||||
parser.add_argument("--manifest", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
context = json.loads(args.context.read_text())
|
||||
assert context["schema_version"] == 1
|
||||
sys.path.insert(0, str(Path(context["workspace_root"]) / "scripts"))
|
||||
from fetch_ffprobe import fetch
|
||||
|
||||
pins = json.loads((Path(context["package_root"]) / "ffprobe-pins.json").read_text())
|
||||
pin = pins[context["target"]]
|
||||
binary, notices = fetch(
|
||||
context["target"], Path(context["cache_directory"]),
|
||||
pin["release"], pin["sha256"],
|
||||
)
|
||||
out = Path(context["output_directory"])
|
||||
files = []
|
||||
|
||||
def include(source, relative, destination, kind="file"):
|
||||
if source.is_symlink() or not source.is_file():
|
||||
raise ValueError(f"Not a regular prepared input: {source}")
|
||||
copied = out / relative
|
||||
copied.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(source, copied) # Copy bytes, not cache links.
|
||||
files.append({
|
||||
"source": relative, "destination": destination, "kind": kind,
|
||||
"sha256": hashlib.sha256(copied.read_bytes()).hexdigest(),
|
||||
})
|
||||
|
||||
name = "ffprobe" + context["binary_ext"]
|
||||
include(binary, name, name, "executable")
|
||||
for path in sorted(notices.rglob("*")):
|
||||
if path.is_symlink():
|
||||
raise ValueError(f"Symlinked notice: {path}")
|
||||
if path.is_file():
|
||||
relative = path.relative_to(notices).as_posix()
|
||||
include(path, "notices/" + relative, "LICENSES/ffmpeg/" + relative)
|
||||
# The downloader preserves upstream archive directories, whose names vary.
|
||||
# Retain them all and add a stable alias for the required license destination.
|
||||
license_destination = "LICENSES/ffmpeg/LICENSE.txt"
|
||||
if not any(f["destination"] == license_destination for f in files):
|
||||
licenses = sorted(p for p in notices.rglob("*") if p.is_file()
|
||||
and p.name.lower().startswith(("license", "copying")))
|
||||
if not licenses:
|
||||
raise ValueError("Missing upstream license")
|
||||
include(licenses[0], "canonical-LICENSE.txt", license_destination)
|
||||
args.manifest.write_text(json.dumps({
|
||||
"schema_version": 1, "target": context["target"], "files": files,
|
||||
}) + "\n")
|
||||
```
|
||||
|
||||
The adapter preserves upstream notices and `provenance.json`, including provider
|
||||
URL, release/asset IDs, archive SHA-256, extracted hashes and the downloader's
|
||||
native version check. A stable `LICENSE.txt` alias is added while retaining the
|
||||
original upstream notice hierarchy. It never discovers Cargo output paths or creates archives.
|
||||
Python is a build-host dependency only. Ignore the target/cache/dist directories
|
||||
in Git; do not track downloaded executables or generated manifests.
|
||||
|
||||
Run:
|
||||
|
||||
```sh
|
||||
rust_realease . --package pptx-reporter --dry-run
|
||||
rust_realease . --package pptx-reporter --compile-only
|
||||
# To prepare/build just Linux:
|
||||
rust_realease . --package pptx-reporter --target x86_64-unknown-linux-gnu --compile-only
|
||||
```
|
||||
|
||||
Verify checksums and both full portable trees. On Linux, extract the tgz and run
|
||||
report generation using the adjacent FFprobe without relying on PATH. On a Windows
|
||||
machine, extract the ZIP, launch the GUI and generate a report without Python,
|
||||
Typst, PowerPoint or separately installed FFmpeg. These consumer application tests
|
||||
and Windows native validation are **not performed by the fixture test suite**.
|
||||
Remove `scripts/package-portable.py` only after completing that consumer migration.
|
||||
Keep Jenkins as checks/coverage/badges only; publishing remains explicit.
|
||||
|
||||
The canonical cargo-binstall archives still install the declared Rust binaries.
|
||||
They do **not** install the portable bundle's resources or FFprobe. Document the
|
||||
external FFprobe requirement for cargo-binstall users. Portable-tree validation,
|
||||
cargo-binstall installation checks and native-runtime checks are separate results.
|
||||
43
release.sh
43
release.sh
|
|
@ -1,43 +0,0 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "usage: $0 <gitea-token-file> [rust_realease options...]" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
[ "$#" -ge 1 ] || usage
|
||||
token_file=$1
|
||||
shift
|
||||
|
||||
if [ ! -f "$token_file" ] || [ ! -r "$token_file" ]; then
|
||||
echo "token file is not a readable regular file: $token_file" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
if ! command -v rust_realease >/dev/null 2>&1; then
|
||||
echo "rust_realease is not installed or not available in PATH" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
GITEA_TOKEN=
|
||||
IFS= read -r GITEA_TOKEN <"$token_file" || :
|
||||
if [ -z "$GITEA_TOKEN" ]; then
|
||||
echo "token file is empty: $token_file" >&2
|
||||
exit 2
|
||||
fi
|
||||
export GITEA_TOKEN
|
||||
|
||||
repository_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
manifest_version=$(sed -n 's/^version = "\([^"]*\)"/\1/p' "$repository_dir/Cargo.toml" | head -n 1)
|
||||
|
||||
set -- \
|
||||
"$repository_dir" \
|
||||
--package rust_realease \
|
||||
--artifact-format both \
|
||||
"$@"
|
||||
case "$manifest_version" in
|
||||
*-*) set -- "$@" --prerelease ;;
|
||||
esac
|
||||
|
||||
exec rust_realease "$@"
|
||||
|
|
@ -1,3 +1,18 @@
|
|||
[[repository.fallbacks]]
|
||||
origin = "git@git.dandaedre.com:idle-org/rust_realease.git"
|
||||
fallback-url = "https://proxy.dandaedre.com/gitea/idle-org/rust_realease"
|
||||
|
||||
[build]
|
||||
targets = [
|
||||
"x86_64-unknown-linux-musl",
|
||||
"aarch64-unknown-linux-musl",
|
||||
"x86_64-pc-windows-gnu",
|
||||
"x86_64-apple-darwin",
|
||||
]
|
||||
profile = "release"
|
||||
locked = true
|
||||
|
||||
[build.apple]
|
||||
image = "ghcr.io/rust-cross/cargo-zigbuild@sha256:9b4f6b3eb9e8f9fefb3960fe288ddba576627d66d9eff851ce7f62d2455564a5"
|
||||
deployment-target = "13.0"
|
||||
sdk-root = "/opt/MacOSX11.3.sdk"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,270 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
cat >&2 <<'EOF'
|
||||
usage: $0 <version|test|coverage> [options]
|
||||
|
||||
Render one deterministic SVG badge. Defaults:
|
||||
version --text version --value unknown --color #007ec6
|
||||
test --text tests --value passing --color #4c1
|
||||
coverage --text coverage --value - --color #9f9f9f --suffix %
|
||||
|
||||
Options:
|
||||
--text TEXT left-hand label
|
||||
--value VALUE right-hand value
|
||||
--color COLOR value background (#RGB or #RRGGBB)
|
||||
--size PX font size, from 6 through 24 (default: 11)
|
||||
--height PX badge height, from 14 through 64 (default: 20)
|
||||
--label-width PX fixed label width instead of automatic sizing
|
||||
--value-width PX fixed value width instead of automatic sizing
|
||||
--suffix TEXT text appended to the displayed value
|
||||
--output PATH output file (default: coverage/<kind>.svg)
|
||||
-h, --help show this help
|
||||
|
||||
Examples:
|
||||
./ci-coverage-badges.sh version --value 1.2.3
|
||||
./ci-coverage-badges.sh test --value failing
|
||||
./ci-coverage-badges.sh coverage --value 87.4
|
||||
./ci-coverage-badges.sh version --text release --value rc.12 --size 12 --color '#8a2be2'
|
||||
EOF
|
||||
exit "${1:-2}"
|
||||
}
|
||||
|
||||
[ "$#" -ge 1 ] || usage
|
||||
kind=$1
|
||||
shift
|
||||
|
||||
case "$kind" in
|
||||
version)
|
||||
text=version
|
||||
value=unknown
|
||||
color='#007ec6'
|
||||
suffix=
|
||||
output=coverage/version.svg
|
||||
;;
|
||||
test)
|
||||
text=tests
|
||||
value=passing
|
||||
color=
|
||||
suffix=
|
||||
output=coverage/test.svg
|
||||
;;
|
||||
coverage)
|
||||
text=coverage
|
||||
value=-
|
||||
color=
|
||||
suffix=%
|
||||
output=coverage/coverage.svg
|
||||
;;
|
||||
-h|--help) usage 0 ;;
|
||||
*)
|
||||
echo "unsupported badge kind: $kind" >&2
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
|
||||
font_size=11
|
||||
height=20
|
||||
label_width=
|
||||
value_width=
|
||||
color_set=false
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
option=$1
|
||||
case "$option" in
|
||||
--text|--value|--color|--size|--height|--label-width|--value-width|--suffix|--output)
|
||||
[ "$#" -ge 2 ] || {
|
||||
echo "missing value for $option" >&2
|
||||
exit 2
|
||||
}
|
||||
option_value=$2
|
||||
shift 2
|
||||
case "$option" in
|
||||
--text) text=$option_value ;;
|
||||
--value) value=$option_value ;;
|
||||
--color) color=$option_value; color_set=true ;;
|
||||
--size) font_size=$option_value ;;
|
||||
--height) height=$option_value ;;
|
||||
--label-width) label_width=$option_value ;;
|
||||
--value-width) value_width=$option_value ;;
|
||||
--suffix) suffix=$option_value ;;
|
||||
--output) output=$option_value ;;
|
||||
esac
|
||||
;;
|
||||
-h|--help) usage 0 ;;
|
||||
*)
|
||||
echo "unknown option: $option" >&2
|
||||
usage
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
validate_text() {
|
||||
field_name=$1
|
||||
field_value=$2
|
||||
if ! printf '%sX\n' "$field_value" | LC_ALL=C awk '
|
||||
BEGIN { valid = 1 }
|
||||
NR != 1 || $0 !~ /^[ -~]+X$/ || length($0) <= 1 { valid = 0 }
|
||||
END { exit(valid == 0 ? 1 : 0) }
|
||||
'; then
|
||||
echo "$field_name must contain printable ASCII characters: $field_value" >&2
|
||||
exit 2
|
||||
fi
|
||||
}
|
||||
|
||||
validate_integer() {
|
||||
field_name=$1
|
||||
field_value=$2
|
||||
minimum=$3
|
||||
maximum=$4
|
||||
case "$field_value" in
|
||||
''|*[!0-9]*)
|
||||
echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
if [ "$field_value" -lt "$minimum" ] || [ "$field_value" -gt "$maximum" ]; then
|
||||
echo "$field_name must be an integer from $minimum through $maximum: $field_value" >&2
|
||||
exit 2
|
||||
fi
|
||||
}
|
||||
|
||||
validate_text text "$text"
|
||||
validate_text value "$value"
|
||||
if [ -n "$suffix" ]; then
|
||||
validate_text suffix "$suffix"
|
||||
fi
|
||||
validate_integer size "$font_size" 6 24
|
||||
validate_integer height "$height" 14 64
|
||||
if [ -n "$label_width" ]; then
|
||||
validate_integer label-width "$label_width" 20 1000
|
||||
fi
|
||||
if [ -n "$value_width" ]; then
|
||||
validate_integer value-width "$value_width" 20 1000
|
||||
fi
|
||||
case "$output" in
|
||||
''|*/) echo "output must name an SVG file: $output" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
case "$kind" in
|
||||
test)
|
||||
if [ "$color_set" = false ]; then
|
||||
case "$value" in
|
||||
passing) color='#4c1' ;;
|
||||
failing) color='#e05d44' ;;
|
||||
*) color='#9f9f9f' ;;
|
||||
esac
|
||||
fi
|
||||
;;
|
||||
coverage)
|
||||
if [ "$value" = '-' ]; then
|
||||
[ "$color_set" = true ] || color='#9f9f9f'
|
||||
else
|
||||
if ! awk -v candidate="$value" 'BEGIN {
|
||||
valid = candidate ~ /^[0-9]+([.][0-9]+)?$/ && candidate >= 0 && candidate <= 100
|
||||
exit(valid ? 0 : 1)
|
||||
}'; then
|
||||
echo "coverage value must be - or a number from 0 through 100: $value" >&2
|
||||
exit 2
|
||||
fi
|
||||
value=$(awk -v candidate="$value" 'BEGIN { printf "%.0f", candidate }')
|
||||
if [ "$color_set" = false ]; then
|
||||
if [ "$value" -ge 90 ]; then
|
||||
color='#4c1'
|
||||
elif [ "$value" -ge 70 ]; then
|
||||
color='#dfb317'
|
||||
else
|
||||
color='#e05d44'
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$color" in
|
||||
\#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]|\#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]) ;;
|
||||
*) echo "color must be #RGB or #RRGGBB: $color" >&2; exit 2 ;;
|
||||
esac
|
||||
|
||||
display_value=$value$suffix
|
||||
if [ -z "$label_width" ]; then
|
||||
label_width=$(printf '%s\\n' "$text" | awk -v size="$font_size" '{
|
||||
width = int(length($0) * size * 0.64 + 14)
|
||||
print (width < 35 ? 35 : width)
|
||||
}')
|
||||
fi
|
||||
if [ -z "$value_width" ]; then
|
||||
value_width=$(printf '%s\\n' "$display_value" | awk -v size="$font_size" '{
|
||||
width = int(length($0) * size * 0.64 + 14)
|
||||
print (width < 35 ? 35 : width)
|
||||
}')
|
||||
fi
|
||||
|
||||
badge_width=$((label_width + value_width))
|
||||
label_center=$(awk -v width="$label_width" 'BEGIN { printf "%.1f", width / 2 }')
|
||||
value_center=$(awk -v left="$label_width" -v width="$value_width" 'BEGIN {
|
||||
printf "%.1f", left + width / 2
|
||||
}')
|
||||
vertical_center=$(awk -v badge_height="$height" 'BEGIN { printf "%.1f", badge_height / 2 }')
|
||||
|
||||
xml_escape() {
|
||||
sed \
|
||||
-e 's/&/\&/g' \
|
||||
-e 's/</\</g' \
|
||||
-e 's/>/\>/g' \
|
||||
-e 's/"/\"/g'
|
||||
}
|
||||
|
||||
sed_replacement_escape() {
|
||||
sed -e 's/[\\&|]/\\&/g'
|
||||
}
|
||||
|
||||
escaped_text=$(printf '%s' "$text" | xml_escape | sed_replacement_escape)
|
||||
escaped_value=$(printf '%s' "$display_value" | xml_escape | sed_replacement_escape)
|
||||
escaped_title=$(printf '%s: %s' "$text" "$display_value" | xml_escape | sed_replacement_escape)
|
||||
|
||||
output_dir=${output%/*}
|
||||
if [ "$output_dir" != "$output" ]; then
|
||||
mkdir -p "$output_dir"
|
||||
fi
|
||||
temporary="${output}.tmp.$$"
|
||||
trap 'rm -f "$temporary"' EXIT HUP INT TERM
|
||||
|
||||
sed \
|
||||
-e "s/@BADGE_WIDTH@/$badge_width/g" \
|
||||
-e "s/@HEIGHT@/$height/g" \
|
||||
-e "s/@LABEL_WIDTH@/$label_width/g" \
|
||||
-e "s/@VALUE_WIDTH@/$value_width/g" \
|
||||
-e "s/@LABEL_CENTER@/$label_center/g" \
|
||||
-e "s/@VALUE_CENTER@/$value_center/g" \
|
||||
-e "s/@VERTICAL_CENTER@/$vertical_center/g" \
|
||||
-e "s/@FONT_SIZE@/$font_size/g" \
|
||||
-e "s/@COLOR@/$color/g" \
|
||||
-e "s|@TITLE@|$escaped_title|g" \
|
||||
-e "s|@TEXT@|$escaped_text|g" \
|
||||
-e "s|@VALUE@|$escaped_value|g" \
|
||||
>"$temporary" <<'EOF'
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="@BADGE_WIDTH@" height="@HEIGHT@" role="img" aria-label="@TITLE@">
|
||||
<title>@TITLE@</title>
|
||||
<linearGradient id="s" x2="0" y2="100%">
|
||||
<stop offset="0" stop-color="#fff" stop-opacity=".7"/>
|
||||
<stop offset=".1" stop-color="#aaa" stop-opacity=".1"/>
|
||||
<stop offset=".9" stop-opacity=".3"/>
|
||||
<stop offset="1" stop-opacity=".5"/>
|
||||
</linearGradient>
|
||||
<clipPath id="r"><rect width="@BADGE_WIDTH@" height="@HEIGHT@" rx="3"/></clipPath>
|
||||
<g clip-path="url(#r)">
|
||||
<rect width="@LABEL_WIDTH@" height="@HEIGHT@" fill="#555"/>
|
||||
<rect x="@LABEL_WIDTH@" width="@VALUE_WIDTH@" height="@HEIGHT@" fill="@COLOR@"/>
|
||||
<rect width="@BADGE_WIDTH@" height="@HEIGHT@" fill="url(#s)"/>
|
||||
</g>
|
||||
<g fill="#fff" text-anchor="middle" dominant-baseline="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="@FONT_SIZE@">
|
||||
<text x="@LABEL_CENTER@" y="@VERTICAL_CENTER@">@TEXT@</text>
|
||||
<text x="@VALUE_CENTER@" y="@VERTICAL_CENTER@">@VALUE@</text>
|
||||
</g>
|
||||
</svg>
|
||||
EOF
|
||||
|
||||
mv "$temporary" "$output"
|
||||
trap - EXIT HUP INT TERM
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "usage: $0 <badge-publication-root> <badge> [badge...]" >&2
|
||||
echo "badges: coverage test version" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
[ "$#" -ge 2 ] || usage
|
||||
publication_root=$1
|
||||
shift
|
||||
|
||||
case "$publication_root" in
|
||||
/*) ;;
|
||||
*)
|
||||
echo "badge publication root must be an absolute path: $publication_root" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ ! -d "$publication_root" ] || [ ! -w "$publication_root" ]; then
|
||||
echo "badge publication root must be an existing writable directory: $publication_root" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
selected=' '
|
||||
for badge in "$@"; do
|
||||
case "$badge" in
|
||||
coverage|test|version) ;;
|
||||
*)
|
||||
echo "unsupported badge: $badge" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
case "$selected" in
|
||||
*" $badge "*)
|
||||
echo "badge selected more than once: $badge" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
selected="$selected$badge "
|
||||
|
||||
source="coverage/$badge.svg"
|
||||
if [ ! -f "$source" ] || [ ! -s "$source" ] || [ -L "$source" ]; then
|
||||
echo "required badge must be a regular nonempty file, not a symlink: $source" >&2
|
||||
exit 2
|
||||
fi
|
||||
done
|
||||
|
||||
owner_root="$publication_root/idle-org"
|
||||
project_root="$owner_root/rust_realease"
|
||||
badge_root="$project_root/main"
|
||||
|
||||
for directory in "$owner_root" "$project_root"; do
|
||||
if [ -L "$directory" ] || { [ -e "$directory" ] && [ ! -d "$directory" ]; }; then
|
||||
echo "badge destination component is not a directory: $directory" >&2
|
||||
exit 2
|
||||
fi
|
||||
mkdir -p "$directory"
|
||||
chmod 0755 "$directory"
|
||||
done
|
||||
|
||||
if [ -L "$badge_root" ]; then
|
||||
unlink "$badge_root"
|
||||
elif [ -e "$badge_root" ] && [ ! -d "$badge_root" ]; then
|
||||
echo "badge destination is not a directory: $badge_root" >&2
|
||||
exit 2
|
||||
fi
|
||||
mkdir -p "$badge_root"
|
||||
chmod 0755 "$badge_root"
|
||||
|
||||
temporary=
|
||||
trap 'if [ -n "$temporary" ]; then rm -f "$temporary"; fi' EXIT HUP INT TERM
|
||||
for badge in "$@"; do
|
||||
temporary=$(mktemp "$badge_root/.$badge.svg.XXXXXX")
|
||||
cp "coverage/$badge.svg" "$temporary"
|
||||
chmod 0644 "$temporary"
|
||||
mv -f "$temporary" "$badge_root/$badge.svg"
|
||||
temporary=
|
||||
echo "published $badge badge to $badge_root/$badge.svg"
|
||||
done
|
||||
trap - EXIT HUP INT TERM
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "usage: $0 <line-coverage-percent> <output.svg>" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
[ "$#" -eq 2 ] || usage
|
||||
percent=$1
|
||||
output=$2
|
||||
|
||||
script_dir=$(CDPATH= cd -P "$(dirname "$0")" && pwd)
|
||||
exec "$script_dir/make-badge.sh" coverage \
|
||||
--value "$percent" \
|
||||
--label-width 75 \
|
||||
--value-width 37 \
|
||||
--output "$output"
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "usage: $0 <passing|failing> <output.svg>" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
[ "$#" -eq 2 ] || usage
|
||||
status=$1
|
||||
output=$2
|
||||
case "$status" in
|
||||
passing|failing) ;;
|
||||
*)
|
||||
echo "test badge status must be passing or failing: $status" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
script_dir=$(CDPATH= cd -P "$(dirname "$0")" && pwd)
|
||||
exec "$script_dir/make-badge.sh" test \
|
||||
--value "$status" \
|
||||
--label-width 45 \
|
||||
--value-width 55 \
|
||||
--output "$output"
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
usage() {
|
||||
echo "usage: $0 <package-version> <output.svg>" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
[ "$#" -eq 2 ] || usage
|
||||
version=$1
|
||||
output=$2
|
||||
case "$version" in
|
||||
''|*[!0-9A-Za-z.+-]*)
|
||||
echo "package version contains unsupported badge characters: $version" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
script_dir=$(CDPATH= cd -P "$(dirname "$0")" && pwd)
|
||||
exec "$script_dir/make-badge.sh" version \
|
||||
--value "$version" \
|
||||
--label-width 59 \
|
||||
--output "$output"
|
||||
716
src/build.rs
716
src/build.rs
|
|
@ -1,16 +1,45 @@
|
|||
use crate::{config::Config, error::Error, policy, project::SelectedPackage, runner::Runner, ui};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
env, fs,
|
||||
path::{Path, PathBuf},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct TargetOutputs {
|
||||
pub target: String,
|
||||
pub binaries: Vec<(String, PathBuf)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
struct BuildFingerprint {
|
||||
format_version: u8,
|
||||
commit: String,
|
||||
package: String,
|
||||
version: String,
|
||||
binaries: Vec<String>,
|
||||
targets: Vec<String>,
|
||||
profile: String,
|
||||
locked: bool,
|
||||
features: Vec<String>,
|
||||
engine: String,
|
||||
toolchain: Option<String>,
|
||||
apple_image: String,
|
||||
apple_deployment_target: String,
|
||||
apple_sdk_root: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
struct BuildCache {
|
||||
#[serde(default)]
|
||||
outputs: Option<Vec<TargetOutputs>>,
|
||||
fingerprint: BuildFingerprint,
|
||||
target_dir: PathBuf,
|
||||
#[serde(default)]
|
||||
output_hashes: std::collections::BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
pub fn prerequisites(
|
||||
root: &Path,
|
||||
config: &Config,
|
||||
|
|
@ -19,20 +48,30 @@ pub fn prerequisites(
|
|||
runner: &Runner,
|
||||
) -> (Vec<String>, Option<String>) {
|
||||
let mut problems = Vec::new();
|
||||
if let Err(error) = validate_cross_config(root) {
|
||||
problems.push(error.to_string());
|
||||
let needs_build = needs_cross;
|
||||
let needs_cross = needs_build && config.targets.iter().any(|target| !is_apple(target));
|
||||
let needs_apple = needs_build && config.targets.iter().any(|target| is_apple(target));
|
||||
if needs_cross {
|
||||
if let Err(error) = validate_cross_config(root) {
|
||||
problems.push(error.to_string());
|
||||
}
|
||||
}
|
||||
for tool in ["git", "cargo"]
|
||||
.into_iter()
|
||||
.chain(needs_cross.then_some("cross"))
|
||||
.chain(needs_binstall.then_some("cargo-binstall"))
|
||||
{
|
||||
if executable(tool).is_none() {
|
||||
problems.push(format!("{tool} is missing"));
|
||||
}
|
||||
}
|
||||
if needs_cross && cross_program().is_none() {
|
||||
problems.push("cross is missing (looked for cross and cargo-cross)".into());
|
||||
}
|
||||
if needs_apple && env::consts::OS != "linux" {
|
||||
problems.push("Apple cross-compilation is supported from a Linux release host".into());
|
||||
}
|
||||
let mut engine = None;
|
||||
if needs_cross {
|
||||
if needs_build {
|
||||
let requested = env::var("CROSS_CONTAINER_ENGINE").ok();
|
||||
let candidate = requested.or_else(|| {
|
||||
["docker", "podman"]
|
||||
|
|
@ -63,11 +102,28 @@ pub fn prerequisites(
|
|||
|
||||
fn executable(name: &str) -> Option<PathBuf> {
|
||||
let path = env::var_os("PATH")?;
|
||||
env::split_paths(&path)
|
||||
executable_in(name, &path)
|
||||
}
|
||||
|
||||
fn executable_in(name: &str, path: &std::ffi::OsStr) -> Option<PathBuf> {
|
||||
env::split_paths(path)
|
||||
.map(|dir| dir.join(name))
|
||||
.find(|path| path.is_file())
|
||||
}
|
||||
|
||||
fn cross_program() -> Option<&'static str> {
|
||||
let path = env::var_os("PATH")?;
|
||||
cross_program_in(&path)
|
||||
}
|
||||
|
||||
fn cross_program_in(path: &std::ffi::OsStr) -> Option<&'static str> {
|
||||
env::split_paths(path).find_map(|directory| {
|
||||
["cross", "cargo-cross"]
|
||||
.into_iter()
|
||||
.find(|name| directory.join(name).is_file())
|
||||
})
|
||||
}
|
||||
|
||||
fn validate_cross_config(root: &Path) -> Result<(), Error> {
|
||||
let source = fs::read_to_string(root.join("Cargo.toml"))
|
||||
.map_err(|e| Error(format!("cannot inspect root Cargo.toml: {e}")))?;
|
||||
|
|
@ -78,7 +134,7 @@ fn validate_cross_config(root: &Path) -> Result<(), Error> {
|
|||
.and_then(|value| value.get("metadata"))
|
||||
.and_then(|value| value.get("cross"))
|
||||
{
|
||||
reject_hooks(cross)?;
|
||||
reject_unsupported_options(cross)?;
|
||||
}
|
||||
let path = root.join("Cross.toml");
|
||||
if path.exists() {
|
||||
|
|
@ -86,24 +142,24 @@ fn validate_cross_config(root: &Path) -> Result<(), Error> {
|
|||
.map_err(|e| Error(format!("cannot read {}: {e}", path.display())))?;
|
||||
let cross: toml::Value = toml::from_str(&source)
|
||||
.map_err(|e| Error(format!("invalid {}: {e}", path.display())))?;
|
||||
reject_hooks(&cross)?;
|
||||
reject_unsupported_options(&cross)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reject_hooks(value: &toml::Value) -> Result<(), Error> {
|
||||
fn reject_unsupported_options(value: &toml::Value) -> Result<(), Error> {
|
||||
match value {
|
||||
toml::Value::Table(table) => {
|
||||
for (key, child) in table {
|
||||
if matches!(key.as_str(), "pre-build" | "dockerfile" | "runner") {
|
||||
if matches!(key.as_str(), "dockerfile" | "runner") {
|
||||
return Err(Error(format!("Repository cross configuration contains unsupported executable option {key:?}.")));
|
||||
}
|
||||
reject_hooks(child)?;
|
||||
reject_unsupported_options(child)?;
|
||||
}
|
||||
}
|
||||
toml::Value::Array(array) => {
|
||||
for child in array {
|
||||
reject_hooks(child)?;
|
||||
reject_unsupported_options(child)?;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
|
|
@ -122,6 +178,11 @@ pub fn verify_source(
|
|||
if config.locked {
|
||||
flags.push("--locked");
|
||||
}
|
||||
let features;
|
||||
if !config.features.is_empty() {
|
||||
features = config.features.join(",");
|
||||
flags.extend(["--features", &features]);
|
||||
}
|
||||
runner.output("cargo", &[&["check"], flags.as_slice()].concat(), root)?;
|
||||
if !skip_tests {
|
||||
runner.output("cargo", &[&["test"], flags.as_slice()].concat(), root)?;
|
||||
|
|
@ -144,6 +205,9 @@ pub fn cross_arguments(
|
|||
if config.locked {
|
||||
args.push("--locked".into());
|
||||
}
|
||||
if !config.features.is_empty() {
|
||||
args.extend(["--features".into(), config.features.join(",")]);
|
||||
}
|
||||
args.extend([
|
||||
"--target".into(),
|
||||
target.to_owned(),
|
||||
|
|
@ -158,29 +222,149 @@ pub fn cross_arguments(
|
|||
args
|
||||
}
|
||||
|
||||
pub fn is_apple(target: &str) -> bool {
|
||||
matches!(target, "aarch64-apple-darwin" | "x86_64-apple-darwin")
|
||||
}
|
||||
|
||||
fn apple_arguments(
|
||||
root: &Path,
|
||||
output: &Path,
|
||||
project: &SelectedPackage,
|
||||
config: &Config,
|
||||
target: &str,
|
||||
engine: &str,
|
||||
) -> Result<Vec<String>, Error> {
|
||||
let root = fs::canonicalize(root)
|
||||
.map_err(|e| Error(format!("cannot resolve Apple source mount: {e}")))?;
|
||||
fs::create_dir_all(output)
|
||||
.map_err(|e| Error(format!("cannot create Apple output directory: {e}")))?;
|
||||
let output = fs::canonicalize(output)
|
||||
.map_err(|e| Error(format!("cannot resolve Apple output mount: {e}")))?;
|
||||
let (uid, gid) = current_user();
|
||||
let mut args = vec![
|
||||
"run".into(),
|
||||
"--rm".into(),
|
||||
"--user".into(),
|
||||
format!("{uid}:{gid}"),
|
||||
];
|
||||
if engine == "podman" {
|
||||
args.push("--userns=keep-id".into());
|
||||
}
|
||||
args.extend([
|
||||
"-v".into(),
|
||||
format!("{}:/io", root.display()),
|
||||
"-v".into(),
|
||||
format!("{}:/output", output.display()),
|
||||
"-w".into(),
|
||||
"/io".into(),
|
||||
"-e".into(),
|
||||
"HOME=/tmp".into(),
|
||||
"-e".into(),
|
||||
"CARGO_HOME=/output/cargo-home".into(),
|
||||
"-e".into(),
|
||||
"CARGO_TARGET_DIR=/output/target".into(),
|
||||
"-e".into(),
|
||||
"ZIG_GLOBAL_CACHE_DIR=/output/zig-cache".into(),
|
||||
"-e".into(),
|
||||
format!(
|
||||
"MACOSX_DEPLOYMENT_TARGET={}",
|
||||
config.apple.deployment_target
|
||||
),
|
||||
"-e".into(),
|
||||
format!("SDKROOT={}", config.apple.sdk_root),
|
||||
config.apple.image.clone(),
|
||||
"cargo".into(),
|
||||
"zigbuild".into(),
|
||||
]);
|
||||
if config.locked {
|
||||
args.push("--locked".into());
|
||||
}
|
||||
if config.profile == "release" {
|
||||
args.push("--release".into());
|
||||
} else {
|
||||
args.extend(["--profile".into(), config.profile.clone()]);
|
||||
}
|
||||
if !config.features.is_empty() {
|
||||
args.extend(["--features".into(), config.features.join(",")]);
|
||||
}
|
||||
args.extend([
|
||||
"--package".into(),
|
||||
project.name.clone(),
|
||||
"--target".into(),
|
||||
target.into(),
|
||||
"--message-format=json-render-diagnostics".into(),
|
||||
]);
|
||||
for binary in &project.binaries {
|
||||
args.extend(["--bin".into(), binary.clone()]);
|
||||
}
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn current_user() -> (u32, u32) {
|
||||
// SAFETY: getuid/getgid have no preconditions and cannot fail.
|
||||
unsafe { (libc::getuid(), libc::getgid()) }
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn current_user() -> (u32, u32) {
|
||||
(0, 0)
|
||||
}
|
||||
|
||||
pub fn build(
|
||||
root: &Path,
|
||||
project: &SelectedPackage,
|
||||
config: &Config,
|
||||
engine: &str,
|
||||
clean_commit: Option<&str>,
|
||||
recompile: bool,
|
||||
runner: &Runner,
|
||||
) -> Result<Vec<TargetOutputs>, Error> {
|
||||
let target_base = project.target_directory.join("binstall-publisher");
|
||||
fs::create_dir_all(&target_base)
|
||||
.map_err(|e| Error(format!("cannot create {}: {e}", target_base.display())))?;
|
||||
let toolchain = executable("rustup").and_then(|_| {
|
||||
runner
|
||||
.text("rustup", &["show", "active-toolchain"], root)
|
||||
.ok()
|
||||
.and_then(|line| line.split_whitespace().next().map(str::to_owned))
|
||||
});
|
||||
let fingerprint = clean_commit.map(|commit| BuildFingerprint {
|
||||
format_version: 2,
|
||||
commit: commit.to_owned(),
|
||||
package: project.name.clone(),
|
||||
version: project.version.clone(),
|
||||
binaries: project.binaries.clone(),
|
||||
targets: config.targets.clone(),
|
||||
profile: config.profile.clone(),
|
||||
locked: config.locked,
|
||||
features: config.features.clone(),
|
||||
engine: engine.to_owned(),
|
||||
toolchain: toolchain.clone(),
|
||||
apple_image: config.apple.image.clone(),
|
||||
apple_deployment_target: config.apple.deployment_target.clone(),
|
||||
apple_sdk_root: config.apple.sdk_root.clone(),
|
||||
});
|
||||
let cache_path = target_base.join("release-build.json");
|
||||
if !recompile {
|
||||
if let Some(expected) = fingerprint.as_ref() {
|
||||
if let Some(outputs) =
|
||||
cached_outputs(&target_base, &cache_path, expected, project, config)?
|
||||
{
|
||||
ui::success(format_args!(
|
||||
"Reusing completed release build for commit {} (use --recompile to rebuild)",
|
||||
short_commit(&expected.commit)
|
||||
));
|
||||
return Ok(outputs);
|
||||
}
|
||||
}
|
||||
}
|
||||
let fresh = tempfile::Builder::new()
|
||||
.prefix("build-")
|
||||
.tempdir_in(&target_base)
|
||||
.map_err(|e| Error(format!("cannot create fresh build directory: {e}")))?;
|
||||
let target_dir = fresh.keep();
|
||||
println!("Fresh build directory: {}", target_dir.display());
|
||||
let toolchain = executable("rustup")
|
||||
.and_then(|_| {
|
||||
runner
|
||||
.text("rustup", &["show", "active-toolchain"], root)
|
||||
.ok()
|
||||
})
|
||||
.and_then(|line| line.split_whitespace().next().map(str::to_owned));
|
||||
let mut environment = vec![("CROSS_CONTAINER_ENGINE", engine)];
|
||||
if let Some(toolchain) = toolchain.as_deref() {
|
||||
environment.push(("RUSTUP_TOOLCHAIN", toolchain));
|
||||
|
|
@ -191,7 +375,22 @@ pub fn build(
|
|||
&config.profile
|
||||
};
|
||||
let mut outputs = Vec::new();
|
||||
let cross = cross_program();
|
||||
let builds_started = Instant::now();
|
||||
let credential_names = env::vars_os()
|
||||
.filter_map(|(name, _)| {
|
||||
let name = name.to_string_lossy();
|
||||
let upper = name.to_ascii_uppercase();
|
||||
["TOKEN", "PASSWORD", "SECRET", "CREDENTIAL"]
|
||||
.iter()
|
||||
.any(|word| upper.contains(word))
|
||||
.then(|| name.into_owned())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let credential_removals = credential_names
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.collect::<Vec<_>>();
|
||||
for target in &config.targets {
|
||||
println!("Building {target}");
|
||||
let target_started = Instant::now();
|
||||
|
|
@ -199,15 +398,42 @@ pub fn build(
|
|||
// build scripts and proc macros isolated so a later image never tries
|
||||
// to execute artifacts produced by an earlier one.
|
||||
let target_build_dir = target_dir.join(target);
|
||||
let args = cross_arguments(project, config, target, &target_build_dir);
|
||||
let (program, args, reported_root) = if is_apple(target) {
|
||||
(
|
||||
engine,
|
||||
apple_arguments(root, &target_build_dir, project, config, target, engine)?,
|
||||
Path::new("/output"),
|
||||
)
|
||||
} else {
|
||||
let mut args = cross_arguments(project, config, target, &target_build_dir);
|
||||
if !config.bundles.is_empty() {
|
||||
args.push("--message-format=json-render-diagnostics".into());
|
||||
}
|
||||
(
|
||||
cross.ok_or_else(|| {
|
||||
Error("cross is missing (looked for cross and cargo-cross)".into())
|
||||
})?,
|
||||
args,
|
||||
Path::new("/target"),
|
||||
)
|
||||
};
|
||||
let borrowed: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||
runner.output_with("cross", &borrowed, root, &environment, &[])?;
|
||||
let messages =
|
||||
runner.output_with(program, &borrowed, root, &environment, &credential_removals)?;
|
||||
let reported = cargo_executables(&messages);
|
||||
let mut binaries = Vec::new();
|
||||
for name in &project.binaries {
|
||||
let binary = target_build_dir
|
||||
.join(target)
|
||||
.join(profile_dir)
|
||||
.join(policy::binary_filename(name, target));
|
||||
let binary = if is_apple(target) || !config.bundles.is_empty() {
|
||||
let reported = reported
|
||||
.get(name)
|
||||
.ok_or_else(|| Error(format!("Cargo did not report executable for {name}")))?;
|
||||
resolve_executable(reported, reported_root, &target_build_dir)?
|
||||
} else {
|
||||
target_build_dir
|
||||
.join(target)
|
||||
.join(profile_dir)
|
||||
.join(policy::binary_filename(name, target))
|
||||
};
|
||||
let metadata = fs::symlink_metadata(&binary).map_err(|_| {
|
||||
Error(format!(
|
||||
"Build did not produce a regular binary: {}",
|
||||
|
|
@ -220,6 +446,9 @@ pub fn build(
|
|||
binary.display()
|
||||
)));
|
||||
}
|
||||
if is_apple(target) {
|
||||
crate::macho::verify_path(&binary, target, &config.apple.deployment_target)?;
|
||||
}
|
||||
binaries.push((name.clone(), binary));
|
||||
}
|
||||
outputs.push(TargetOutputs {
|
||||
|
|
@ -231,35 +460,240 @@ pub fn build(
|
|||
target_started.elapsed().as_secs_f64()
|
||||
));
|
||||
}
|
||||
let label = if config.targets.iter().all(|target| !is_apple(target)) {
|
||||
"Cross builds"
|
||||
} else {
|
||||
"Release builds"
|
||||
};
|
||||
ui::success(format_args!(
|
||||
"Cross builds complete in {:.1}s",
|
||||
"{label} complete in {:.1}s",
|
||||
builds_started.elapsed().as_secs_f64()
|
||||
));
|
||||
if let Some(fingerprint) = fingerprint {
|
||||
let cache = BuildCache {
|
||||
outputs: Some(outputs.clone()),
|
||||
fingerprint,
|
||||
target_dir,
|
||||
output_hashes: outputs
|
||||
.iter()
|
||||
.flat_map(|output| {
|
||||
output.binaries.iter().map(|(name, path)| {
|
||||
Ok((
|
||||
format!("{}/{}", output.target, name),
|
||||
crate::package::checksum(path)?,
|
||||
))
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, Error>>()?,
|
||||
};
|
||||
let encoded = serde_json::to_vec_pretty(&cache)
|
||||
.map_err(|e| Error(format!("cannot encode release build cache: {e}")))?;
|
||||
let mut temporary = tempfile::NamedTempFile::new_in(&target_base)
|
||||
.map_err(|e| Error(format!("cannot create release build cache: {e}")))?;
|
||||
use std::io::Write as _;
|
||||
temporary
|
||||
.write_all(&encoded)
|
||||
.map_err(|e| Error(format!("cannot write release build cache: {e}")))?;
|
||||
temporary
|
||||
.persist(&cache_path)
|
||||
.map_err(|e| Error(format!("cannot save release build cache: {e}")))?;
|
||||
}
|
||||
Ok(outputs)
|
||||
}
|
||||
|
||||
fn cached_outputs(
|
||||
target_base: &Path,
|
||||
cache_path: &Path,
|
||||
expected: &BuildFingerprint,
|
||||
project: &SelectedPackage,
|
||||
config: &Config,
|
||||
) -> Result<Option<Vec<TargetOutputs>>, Error> {
|
||||
match fs::symlink_metadata(cache_path) {
|
||||
Ok(metadata) if metadata.file_type().is_file() => (),
|
||||
Ok(_) => return Ok(None),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(error) => {
|
||||
return Err(Error(format!(
|
||||
"cannot inspect release build cache {}: {error}",
|
||||
cache_path.display()
|
||||
)))
|
||||
}
|
||||
}
|
||||
let encoded =
|
||||
fs::read(cache_path).map_err(|e| Error(format!("cannot read release build cache: {e}")))?;
|
||||
let cache: BuildCache = match serde_json::from_slice(&encoded) {
|
||||
Ok(cache) => cache,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
if &cache.fingerprint != expected {
|
||||
return Ok(None);
|
||||
}
|
||||
let base = fs::canonicalize(target_base)
|
||||
.map_err(|e| Error(format!("cannot resolve build cache directory: {e}")))?;
|
||||
let target_dir = match fs::canonicalize(&cache.target_dir) {
|
||||
Ok(path) if path.starts_with(&base) => path,
|
||||
_ => return Ok(None),
|
||||
};
|
||||
if let Some(outputs) = cache.outputs {
|
||||
if outputs.len() != config.targets.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
for (output, target) in outputs.iter().zip(&config.targets) {
|
||||
if &output.target != target || output.binaries.len() != project.binaries.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
for ((name, path), expected_name) in output.binaries.iter().zip(&project.binaries) {
|
||||
let expected_hash = cache.output_hashes.get(&format!("{target}/{name}"));
|
||||
if name != expected_name
|
||||
|| !path.is_file()
|
||||
|| path.is_symlink()
|
||||
|| !fs::canonicalize(path).is_ok_and(|p| p.starts_with(&base))
|
||||
|| expected_hash.is_none_or(|hash| match crate::package::checksum(path) {
|
||||
Ok(actual) => &actual != hash,
|
||||
Err(_) => true,
|
||||
})
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
if is_apple(target)
|
||||
&& crate::macho::verify_path(path, target, &config.apple.deployment_target)
|
||||
.is_err()
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(Some(outputs));
|
||||
}
|
||||
let profile_dir = if config.profile == "dev" {
|
||||
"debug"
|
||||
} else {
|
||||
&config.profile
|
||||
};
|
||||
let mut outputs = Vec::new();
|
||||
for target in &config.targets {
|
||||
let mut binaries = Vec::new();
|
||||
for name in &project.binaries {
|
||||
let binary = target_dir
|
||||
.join(target)
|
||||
.join(target)
|
||||
.join(profile_dir)
|
||||
.join(policy::binary_filename(name, target));
|
||||
let resolved = match fs::canonicalize(&binary) {
|
||||
Ok(path) if path.starts_with(&base) => path,
|
||||
_ => return Ok(None),
|
||||
};
|
||||
match fs::symlink_metadata(&binary) {
|
||||
Ok(metadata) if metadata.file_type().is_file() => {
|
||||
binaries.push((name.clone(), resolved));
|
||||
}
|
||||
_ => return Ok(None),
|
||||
}
|
||||
}
|
||||
outputs.push(TargetOutputs {
|
||||
target: target.clone(),
|
||||
binaries,
|
||||
});
|
||||
}
|
||||
Ok(Some(outputs))
|
||||
}
|
||||
|
||||
fn resolve_executable(
|
||||
reported: &Path,
|
||||
reported_root: &Path,
|
||||
target_dir: &Path,
|
||||
) -> Result<PathBuf, Error> {
|
||||
// Cross mounts the selected --target-dir at /target inside its container.
|
||||
// Remap the reported artifact, not a guessed profile/binary layout.
|
||||
let path = match reported.strip_prefix(reported_root) {
|
||||
Ok(relative) => target_dir.join(relative),
|
||||
Err(_) => reported.to_path_buf(),
|
||||
};
|
||||
let base = fs::canonicalize(target_dir)?;
|
||||
let resolved = fs::canonicalize(&path)?;
|
||||
if !resolved.starts_with(base) || fs::symlink_metadata(&path)?.file_type().is_symlink() {
|
||||
return Err(Error(
|
||||
"Cargo executable is outside the fresh target directory or is a symlink".into(),
|
||||
));
|
||||
}
|
||||
Ok(resolved)
|
||||
}
|
||||
|
||||
fn cargo_executables(messages: &[u8]) -> std::collections::BTreeMap<String, PathBuf> {
|
||||
String::from_utf8_lossy(messages)
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
let value: serde_json::Value = serde_json::from_str(line).ok()?;
|
||||
if value["reason"] != "compiler-artifact"
|
||||
|| !value["target"]["kind"]
|
||||
.as_array()?
|
||||
.iter()
|
||||
.any(|k| k == "bin")
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Some((
|
||||
value["target"]["name"].as_str()?.to_owned(),
|
||||
PathBuf::from(value["executable"].as_str()?),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn short_commit(commit: &str) -> &str {
|
||||
commit.get(..12).unwrap_or(commit)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cross_command_scopes_package_and_uses_fresh_target_path() {
|
||||
let project = SelectedPackage {
|
||||
fn cross_command_honors_path_order_and_falls_back_to_cargo_cross() {
|
||||
let first = tempfile::tempdir().unwrap();
|
||||
let later = tempfile::tempdir().unwrap();
|
||||
let path = env::join_paths([first.path(), later.path()]).unwrap();
|
||||
|
||||
fs::write(first.path().join("cargo-cross"), "").unwrap();
|
||||
fs::write(later.path().join("cross"), "").unwrap();
|
||||
assert_eq!(cross_program_in(&path), Some("cargo-cross"));
|
||||
|
||||
fs::write(first.path().join("cross"), "").unwrap();
|
||||
assert_eq!(cross_program_in(&path), Some("cross"));
|
||||
}
|
||||
|
||||
fn test_project(target_directory: PathBuf) -> SelectedPackage {
|
||||
SelectedPackage {
|
||||
workspace_root: PathBuf::from("/workspace"),
|
||||
name: "cli".into(),
|
||||
version: "0.1.0".into(),
|
||||
binaries: vec!["cli".into(), "helper".into()],
|
||||
manifest: PathBuf::from("Cargo.toml"),
|
||||
repository: None,
|
||||
target_directory: PathBuf::from("target"),
|
||||
};
|
||||
let config = Config {
|
||||
target_directory,
|
||||
}
|
||||
}
|
||||
|
||||
fn test_config() -> Config {
|
||||
Config {
|
||||
bundles: Vec::new(),
|
||||
features: Vec::new(),
|
||||
apple: crate::config::AppleConfig::default(),
|
||||
targets: vec!["x86_64-pc-windows-gnu".into()],
|
||||
profile: "release".into(),
|
||||
locked: true,
|
||||
tag_prefix: "v".into(),
|
||||
token_env: "GITEA_TOKEN".into(),
|
||||
release_endpoints: Vec::new(),
|
||||
package_format: crate::config::PackageFormat::Tgz,
|
||||
repository_fallbacks: Vec::new(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cross_command_scopes_package_and_uses_fresh_target_path() {
|
||||
let project = test_project(PathBuf::from("target"));
|
||||
let config = test_config();
|
||||
let args = cross_arguments(
|
||||
&project,
|
||||
&config,
|
||||
|
|
@ -285,11 +719,223 @@ mod tests {
|
|||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cross_hooks_are_rejected() {
|
||||
fn apple_container_arguments_are_pinned_noninteractive_and_path_safe() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let root = temporary.path().join("checkout with spaces é");
|
||||
let output = temporary.path().join("output with spaces");
|
||||
fs::create_dir(&root).unwrap();
|
||||
let mut config = test_config();
|
||||
config.targets = vec!["aarch64-apple-darwin".into()];
|
||||
config.features = vec!["serial".into(), "ui".into()];
|
||||
let args = apple_arguments(
|
||||
&root,
|
||||
&output,
|
||||
&test_project(PathBuf::from("target")),
|
||||
&config,
|
||||
"aarch64-apple-darwin",
|
||||
"podman",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(args[0], "run");
|
||||
assert!(args.contains(&"--rm".into()));
|
||||
assert!(args.contains(&"--userns=keep-id".into()));
|
||||
assert!(args.contains(&format!("{}:/io", root.display())));
|
||||
assert!(args.contains(&format!("{}:/output", output.display())));
|
||||
assert!(args.contains(&config.apple.image));
|
||||
assert!(args.contains(&"MACOSX_DEPLOYMENT_TARGET=13.0".into()));
|
||||
assert!(args.contains(&"SDKROOT=/opt/MacOSX11.3.sdk".into()));
|
||||
assert!(args
|
||||
.windows(2)
|
||||
.any(|args| args == ["--features", "serial,ui"]));
|
||||
assert!(args.windows(2).any(|args| args == ["--package", "cli"]));
|
||||
assert!(args
|
||||
.windows(2)
|
||||
.any(|args| args == ["--target", "aarch64-apple-darwin"]));
|
||||
assert!(!args.iter().any(|arg| arg.contains("sock") || arg == "-it"));
|
||||
|
||||
let docker = apple_arguments(
|
||||
&root,
|
||||
&output,
|
||||
&test_project(PathBuf::from("target")),
|
||||
&config,
|
||||
"aarch64-apple-darwin",
|
||||
"docker",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!docker.contains(&"--userns=keep-id".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backend_mapping_only_routes_supported_apple_targets_to_zig() {
|
||||
assert!(is_apple("aarch64-apple-darwin"));
|
||||
assert!(is_apple("x86_64-apple-darwin"));
|
||||
assert!(!is_apple("x86_64-unknown-linux-gnu"));
|
||||
assert!(!is_apple("x86_64-pc-windows-gnu"));
|
||||
assert!(!is_apple("i686-apple-darwin"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completed_clean_build_is_reused_only_for_the_same_fingerprint() {
|
||||
let temporary = tempfile::tempdir().unwrap();
|
||||
let target_base = temporary.path().join("binstall-publisher");
|
||||
let target_dir = target_base.join("build-complete");
|
||||
let project = test_project(temporary.path().to_owned());
|
||||
let config = test_config();
|
||||
for binary in &project.binaries {
|
||||
let path = target_dir
|
||||
.join(&config.targets[0])
|
||||
.join(&config.targets[0])
|
||||
.join("release")
|
||||
.join(policy::binary_filename(binary, &config.targets[0]));
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, binary).unwrap();
|
||||
}
|
||||
let fingerprint = BuildFingerprint {
|
||||
format_version: 2,
|
||||
commit: "0123456789abcdef".into(),
|
||||
package: project.name.clone(),
|
||||
version: project.version.clone(),
|
||||
binaries: project.binaries.clone(),
|
||||
targets: config.targets.clone(),
|
||||
profile: config.profile.clone(),
|
||||
locked: config.locked,
|
||||
features: config.features.clone(),
|
||||
engine: "podman".into(),
|
||||
toolchain: Some("stable-x86_64-unknown-linux-gnu".into()),
|
||||
apple_image: config.apple.image.clone(),
|
||||
apple_deployment_target: config.apple.deployment_target.clone(),
|
||||
apple_sdk_root: config.apple.sdk_root.clone(),
|
||||
};
|
||||
let cache_path = target_base.join("release-build.json");
|
||||
fs::write(
|
||||
&cache_path,
|
||||
serde_json::to_vec(&BuildCache {
|
||||
outputs: None,
|
||||
fingerprint: fingerprint.clone(),
|
||||
target_dir,
|
||||
output_hashes: std::collections::BTreeMap::new(),
|
||||
})
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let outputs = cached_outputs(&target_base, &cache_path, &fingerprint, &project, &config)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(outputs.len(), 1);
|
||||
assert_eq!(outputs[0].binaries.len(), 2);
|
||||
|
||||
let different = BuildFingerprint {
|
||||
commit: "different".into(),
|
||||
..fingerprint.clone()
|
||||
};
|
||||
assert!(
|
||||
cached_outputs(&target_base, &cache_path, &different, &project, &config)
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
for incompatible in [
|
||||
BuildFingerprint {
|
||||
features: vec!["new-feature".into()],
|
||||
..fingerprint.clone()
|
||||
},
|
||||
BuildFingerprint {
|
||||
apple_deployment_target: "14.0".into(),
|
||||
..fingerprint.clone()
|
||||
},
|
||||
BuildFingerprint {
|
||||
apple_image: format!("example.invalid/compiler@sha256:{}", "a".repeat(64)),
|
||||
..fingerprint.clone()
|
||||
},
|
||||
BuildFingerprint {
|
||||
engine: "docker".into(),
|
||||
..fingerprint.clone()
|
||||
},
|
||||
] {
|
||||
assert!(
|
||||
cached_outputs(&target_base, &cache_path, &incompatible, &project, &config)
|
||||
.unwrap()
|
||||
.is_none()
|
||||
);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn cross_pre_build_is_allowed_but_other_executable_options_are_rejected() {
|
||||
let doc: toml::Value =
|
||||
toml::from_str("[target.x86_64-unknown-linux-musl]\npre-build=['echo unsafe']")
|
||||
toml::from_str("[target.x86_64-unknown-linux-musl]\npre-build=['apt-get update']")
|
||||
.unwrap();
|
||||
assert!(reject_hooks(&doc).is_err());
|
||||
assert!(reject_unsupported_options(&doc).is_ok());
|
||||
|
||||
for option in ["dockerfile = 'Dockerfile'", "runner = 'custom-runner'"] {
|
||||
let doc: toml::Value =
|
||||
toml::from_str(&format!("[target.x86_64-unknown-linux-musl]\n{option}")).unwrap();
|
||||
assert!(reject_unsupported_options(&doc).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_cross_configuration_and_missing_lockfile_are_reported() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
assert!(validate_cross_config(temp.path())
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("cannot inspect"));
|
||||
|
||||
fs::write(temp.path().join("Cargo.toml"), "not valid toml = [").unwrap();
|
||||
assert!(validate_cross_config(temp.path())
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("invalid root"));
|
||||
|
||||
fs::write(
|
||||
temp.path().join("Cargo.toml"),
|
||||
"[workspace.metadata.cross.target.demo]\nrunner = 'custom-runner'\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert!(validate_cross_config(temp.path())
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("runner"));
|
||||
|
||||
fs::write(temp.path().join("Cargo.toml"), "[workspace]\n").unwrap();
|
||||
fs::write(
|
||||
temp.path().join("Cross.toml"),
|
||||
"[build]\npre-build = ['apt-get update']\n",
|
||||
)
|
||||
.unwrap();
|
||||
validate_cross_config(temp.path()).unwrap();
|
||||
|
||||
fs::write(temp.path().join("Cross.toml"), "invalid = [").unwrap();
|
||||
assert!(validate_cross_config(temp.path())
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("invalid"));
|
||||
fs::remove_file(temp.path().join("Cross.toml")).unwrap();
|
||||
|
||||
let config = test_config();
|
||||
let (problems, engine) = prerequisites(
|
||||
temp.path(),
|
||||
&config,
|
||||
false,
|
||||
false,
|
||||
&Runner::new("TEST_TOKEN"),
|
||||
);
|
||||
assert!(engine.is_none());
|
||||
assert!(problems
|
||||
.iter()
|
||||
.any(|problem| problem.contains("Cargo.lock")));
|
||||
|
||||
let mut dev = test_config();
|
||||
dev.profile = "dev".into();
|
||||
dev.locked = false;
|
||||
let args = cross_arguments(
|
||||
&test_project(PathBuf::from("target")),
|
||||
&dev,
|
||||
"demo-target",
|
||||
Path::new("target-dir"),
|
||||
);
|
||||
assert!(args.windows(2).any(|pair| pair == ["--profile", "dev"]));
|
||||
assert!(!args.contains(&"--locked".into()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
764
src/cli.rs
764
src/cli.rs
|
|
@ -1,19 +1,22 @@
|
|||
use crate::{
|
||||
build,
|
||||
config::{self, Overrides, PackageFormat},
|
||||
config::{self, Overrides, PackageFormat, ReleaseEndpoint, ReleaseProvider},
|
||||
error::Error,
|
||||
gitea::Gitea,
|
||||
manifest,
|
||||
model::Stage,
|
||||
policy, prepare, project,
|
||||
release::ReleaseClient,
|
||||
runner::Runner,
|
||||
ui, verify,
|
||||
};
|
||||
use clap::Parser;
|
||||
use std::path::PathBuf;
|
||||
use std::{collections::BTreeMap, path::PathBuf};
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(about = "Build and publish Rust binary releases to Gitea", version)]
|
||||
#[command(
|
||||
about = "Build and publish Rust binary releases to Gitea and GitHub",
|
||||
version
|
||||
)]
|
||||
pub struct Cli {
|
||||
/// Local directory or Git URL.
|
||||
pub source: String,
|
||||
|
|
@ -27,6 +30,9 @@ pub struct Cli {
|
|||
pub target: Vec<String>,
|
||||
#[arg(long)]
|
||||
pub profile: Option<String>,
|
||||
/// Enable Cargo features for source checks and every release backend.
|
||||
#[arg(long, value_delimiter = ',')]
|
||||
pub features: Vec<String>,
|
||||
#[arg(long, conflicts_with = "unlocked")]
|
||||
pub locked: bool,
|
||||
#[arg(long)]
|
||||
|
|
@ -46,6 +52,15 @@ pub struct Cli {
|
|||
pub prerelease: bool,
|
||||
#[arg(long)]
|
||||
pub replace_assets: bool,
|
||||
/// Ignore a matching completed build and compile every release target again.
|
||||
#[arg(long)]
|
||||
pub recompile: bool,
|
||||
/// Release notes text.
|
||||
#[arg(long, short = 'm', conflicts_with = "message_file")]
|
||||
pub message: Option<String>,
|
||||
/// Read release notes from a UTF-8 file.
|
||||
#[arg(long, value_name = "PATH", conflicts_with = "message")]
|
||||
pub message_file: Option<PathBuf>,
|
||||
#[arg(long)]
|
||||
pub keep_workdir: bool,
|
||||
#[arg(long)]
|
||||
|
|
@ -70,6 +85,9 @@ pub struct Cli {
|
|||
pub cross_image_tag: Option<String>,
|
||||
#[arg(long)]
|
||||
pub repository_url: Option<String>,
|
||||
/// Read one plaintext token or a JSON object of endpoint tokens from a UTF-8 file.
|
||||
#[arg(long, value_name = "PATH")]
|
||||
pub token_file: Option<PathBuf>,
|
||||
#[arg(long)]
|
||||
pub config: Option<PathBuf>,
|
||||
/// Release tgz archives, raw binaries, or both.
|
||||
|
|
@ -94,7 +112,23 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
"--prepare-repo is a separate mode; omit --stop-after.".into(),
|
||||
));
|
||||
}
|
||||
let acquisition_runner = Runner::new("GITEA_TOKEN").with_verbose(args.verbose);
|
||||
let supplied_release_message = match args.message_file.as_deref() {
|
||||
Some(path) => Some(std::fs::read_to_string(path).map_err(|e| {
|
||||
Error(format!(
|
||||
"cannot read release message file {}: {e}",
|
||||
path.display()
|
||||
))
|
||||
})?),
|
||||
None => args.message.clone(),
|
||||
};
|
||||
let file_tokens = args
|
||||
.token_file
|
||||
.as_deref()
|
||||
.map(read_token_file)
|
||||
.transpose()?;
|
||||
let acquisition_runner = Runner::new("GITEA_TOKEN")
|
||||
.with_secret(file_tokens.as_ref().and_then(TokenFile::plain))
|
||||
.with_verbose(args.verbose);
|
||||
let checkout = project::acquire_project(
|
||||
&args.source,
|
||||
if args.prepare_repo {
|
||||
|
|
@ -114,14 +148,24 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
unlocked: args.unlocked,
|
||||
tag_prefix: args.tag_prefix,
|
||||
package_format: args.artifact_format,
|
||||
features: args.features,
|
||||
},
|
||||
)?;
|
||||
let runner = Runner::new(&config.token_env).with_verbose(args.verbose);
|
||||
let runner = Runner::new(&config.token_env)
|
||||
.with_token_envs(
|
||||
config
|
||||
.release_endpoints
|
||||
.iter()
|
||||
.map(|endpoint| endpoint.token_env.clone()),
|
||||
)
|
||||
.with_secret(file_tokens.as_ref().and_then(TokenFile::plain))
|
||||
.with_verbose(args.verbose);
|
||||
let root = checkout
|
||||
.root
|
||||
.to_str()
|
||||
.ok_or_else(|| Error("Project path is not UTF-8.".into()))?;
|
||||
let selected = project::inspect(root, args.package.as_deref(), &runner)?;
|
||||
crate::bundle::plan(&config, &selected, &selected.workspace_root)?;
|
||||
let publishing = !args.prepare_repo
|
||||
&& !args.dry_run
|
||||
&& args
|
||||
|
|
@ -171,9 +215,44 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
)
|
||||
})?;
|
||||
let normalized_candidate = project::normalize_repository(&candidate)?;
|
||||
let repository = config.resolve_repository(&candidate)?;
|
||||
let mut endpoints = config.release_endpoints.clone();
|
||||
if args.repository_url.is_some() && !endpoints.is_empty() {
|
||||
return Err(Error(
|
||||
"--repository-url cannot be combined with configured release.endpoints.".into(),
|
||||
));
|
||||
}
|
||||
let repository = if let Some(primary) = endpoints.first() {
|
||||
primary.repository.clone()
|
||||
} else {
|
||||
config.resolve_repository(&candidate)?
|
||||
};
|
||||
if endpoints.is_empty() {
|
||||
let provider = if url::Url::parse(&repository)
|
||||
.ok()
|
||||
.and_then(|url| url.host_str().map(str::to_owned))
|
||||
.as_deref()
|
||||
== Some("github.com")
|
||||
{
|
||||
ReleaseProvider::Github
|
||||
} else {
|
||||
ReleaseProvider::Gitea
|
||||
};
|
||||
endpoints.push(ReleaseEndpoint {
|
||||
name: "default".into(),
|
||||
provider,
|
||||
repository: repository.clone(),
|
||||
token_env: match provider {
|
||||
ReleaseProvider::Gitea => config.token_env.clone(),
|
||||
ReleaseProvider::Github => "GITHUB_TOKEN".into(),
|
||||
},
|
||||
});
|
||||
}
|
||||
if repository != normalized_candidate {
|
||||
println!("Repository fallback: {normalized_candidate} -> {repository}");
|
||||
if config.release_endpoints.is_empty() {
|
||||
println!("Repository fallback: {normalized_candidate} -> {repository}");
|
||||
} else {
|
||||
println!("Primary release repository: {repository}");
|
||||
}
|
||||
}
|
||||
if publishing {
|
||||
if let Some(origin) = project::origin_url(&checkout.root, &runner) {
|
||||
|
|
@ -207,6 +286,11 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
println!("{}: {actual} -> {:?}", issue.key, issue.expected);
|
||||
}
|
||||
if args.prepare_repo {
|
||||
let preparation_token = match file_tokens.as_ref() {
|
||||
Some(TokenFile::Plain(token)) => Some(token.as_str()),
|
||||
Some(TokenFile::Named(tokens)) => tokens.get(&endpoints[0].name).map(String::as_str),
|
||||
None => None,
|
||||
};
|
||||
return prepare::run(
|
||||
&checkout.root,
|
||||
&selected,
|
||||
|
|
@ -215,6 +299,7 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
prepare::Options {
|
||||
tag: &tag,
|
||||
image_tag: args.cross_image_tag.as_deref(),
|
||||
token: preparation_token,
|
||||
dry_run: args.dry_run,
|
||||
commit: args.commit_preparation,
|
||||
create_tag: args.create_tag,
|
||||
|
|
@ -236,6 +321,14 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
return Ok(());
|
||||
}
|
||||
for target in &config.targets {
|
||||
if build::is_apple(target) {
|
||||
println!(
|
||||
"{target} build backend: cargo-zigbuild container {} (SDKROOT={}, MACOSX_DEPLOYMENT_TARGET={})",
|
||||
config.apple.image, config.apple.sdk_root, config.apple.deployment_target
|
||||
);
|
||||
} else {
|
||||
println!("{target} build backend: Cross");
|
||||
}
|
||||
if config.package_format.archives() {
|
||||
println!(
|
||||
"{target} archive: {} ({})",
|
||||
|
|
@ -262,10 +355,15 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
println!("Binstall {key}: {value}");
|
||||
}
|
||||
}
|
||||
ui::detail(
|
||||
"Release destination",
|
||||
format_args!("{repository}/releases/tag/{tag}"),
|
||||
);
|
||||
for endpoint in &endpoints {
|
||||
ui::detail(
|
||||
"Release destination",
|
||||
format_args!(
|
||||
"{} ({}) {}/releases/tag/{tag}",
|
||||
endpoint.name, endpoint.provider, endpoint.repository
|
||||
),
|
||||
);
|
||||
}
|
||||
let needs_cross = args.stop_after != Some(Stage::Verify);
|
||||
let needs_binstall = config.package_format.archives()
|
||||
&& !args.skip_binstall_test
|
||||
|
|
@ -274,12 +372,18 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
.is_none_or(|stage| stage.order() >= Stage::Binstall.order());
|
||||
let publication = if publishing {
|
||||
project::validate_checkout_tag(&checkout.root, &tag, &runner)?;
|
||||
let api = Gitea::new(&repository, &config.token_env)?;
|
||||
let private = api.repository_private()?;
|
||||
if private && needs_binstall {
|
||||
return Err(Error("This repository is private; cargo-binstall cannot verify its private release downloads. Rerun with --skip-binstall-test to verify uploaded assets with Gitea authentication.".into()));
|
||||
validate_token_keys(file_tokens.as_ref(), &endpoints)?;
|
||||
let mut clients = Vec::new();
|
||||
for endpoint in &endpoints {
|
||||
let token = token_for_endpoint(file_tokens.as_ref(), endpoint, endpoints.len())?;
|
||||
let api = ReleaseClient::new(endpoint, &token)?;
|
||||
let private = api.repository_private()?;
|
||||
clients.push((endpoint.clone(), api, private));
|
||||
}
|
||||
Some((api, private))
|
||||
if clients.first().is_some_and(|(_, _, private)| *private) && needs_binstall {
|
||||
return Err(Error("The primary release repository is private; cargo-binstall cannot verify its private release downloads. Rerun with --skip-binstall-test to verify uploaded assets with authentication.".into()));
|
||||
}
|
||||
Some(clients)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
|
@ -316,18 +420,63 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
if args.stop_after == Some(Stage::Verify) {
|
||||
return Ok(());
|
||||
}
|
||||
let engine = engine.ok_or_else(|| Error("No usable Cross container backend.".into()))?;
|
||||
let outputs = build::build(&checkout.root, &selected, &config, &engine, &runner)?;
|
||||
let engine =
|
||||
engine.ok_or_else(|| Error("No usable Docker or Podman container backend.".into()))?;
|
||||
let commit = project::head_commit(&checkout.root, &runner)?;
|
||||
let checkout_clean = runner
|
||||
.text(
|
||||
"git",
|
||||
&["status", "--porcelain", "--untracked-files=normal"],
|
||||
&checkout.root,
|
||||
)?
|
||||
.is_empty();
|
||||
let outputs = build::build(
|
||||
&checkout.root,
|
||||
&selected,
|
||||
&config,
|
||||
&engine,
|
||||
checkout_clean.then_some(commit.as_str()),
|
||||
args.recompile,
|
||||
&runner,
|
||||
)?;
|
||||
if args.stop_after == Some(Stage::Build) {
|
||||
return Ok(());
|
||||
}
|
||||
let (artifacts, sums) = crate::package::package(
|
||||
&checkout.root,
|
||||
&selected.name,
|
||||
&selected.version,
|
||||
&outputs,
|
||||
config.package_format,
|
||||
)?;
|
||||
let (artifacts, sums) = if config.bundles.is_empty() {
|
||||
crate::package::package(
|
||||
&checkout.root,
|
||||
&selected.name,
|
||||
&selected.version,
|
||||
&outputs,
|
||||
config.package_format,
|
||||
)?
|
||||
} else {
|
||||
crate::bundle::assemble(
|
||||
&selected.workspace_root,
|
||||
&selected,
|
||||
&config,
|
||||
&outputs,
|
||||
&commit,
|
||||
!checkout_clean,
|
||||
&runner,
|
||||
)?
|
||||
};
|
||||
for artifact in &artifacts {
|
||||
if matches!(
|
||||
artifact.contents,
|
||||
crate::package::ArtifactContents::Archive { .. }
|
||||
| crate::package::ArtifactContents::Bundle { .. }
|
||||
) {
|
||||
verify::verify_archive(&artifact.path, artifact)?;
|
||||
}
|
||||
if build::is_apple(&artifact.target) {
|
||||
verify::verify_apple_artifact(
|
||||
&artifact.path,
|
||||
artifact,
|
||||
&config.apple.deployment_target,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
ui::detail(
|
||||
"Artifacts",
|
||||
sums.parent().map_or_else(
|
||||
|
|
@ -342,37 +491,70 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
artifact.path.display()
|
||||
));
|
||||
}
|
||||
for target in &config.targets {
|
||||
if build::is_apple(target) {
|
||||
println!("{target} checks: Mach-O architecture, macOS deployment minimum {}, system dynamic dependencies, archive layout and checksum passed; runtime skipped (no compatible macOS runner)", config.apple.deployment_target);
|
||||
}
|
||||
}
|
||||
if args.stop_after == Some(Stage::Package) {
|
||||
return Ok(());
|
||||
}
|
||||
let commit = project::head_commit(&checkout.root, &runner)?;
|
||||
let (api, private) =
|
||||
let clients =
|
||||
publication.ok_or_else(|| Error("Publication client was not initialized.".into()))?;
|
||||
let release = api.ensure_release(&tag, &commit, args.draft, args.prerelease)?;
|
||||
ui::success(format_args!("Release ready: {tag}"));
|
||||
let release_message =
|
||||
release_message_with_macos_workaround(supplied_release_message.as_deref(), &artifacts)?;
|
||||
let mut releases = Vec::new();
|
||||
for (endpoint, api, private) in clients {
|
||||
let release = api.ensure_release(
|
||||
&tag,
|
||||
&commit,
|
||||
args.draft,
|
||||
args.prerelease,
|
||||
release_message.as_deref(),
|
||||
supplied_release_message.is_some(),
|
||||
)?;
|
||||
ui::success(format_args!("Release ready on {}: {tag}", endpoint.name));
|
||||
releases.push((endpoint, api, private, release));
|
||||
}
|
||||
if args.stop_after == Some(Stage::Release) {
|
||||
return Ok(());
|
||||
}
|
||||
let mut paths: Vec<&std::path::Path> = artifacts.iter().map(|a| a.path.as_path()).collect();
|
||||
paths.push(sums.as_path());
|
||||
api.upload(release.id, &paths, args.replace_assets)?;
|
||||
for (endpoint, api, _, release) in &releases {
|
||||
api.upload(release.id, &paths, args.replace_assets)?;
|
||||
ui::success(format_args!("Assets uploaded to {}", endpoint.name));
|
||||
}
|
||||
if args.stop_after == Some(Stage::Upload) {
|
||||
return Ok(());
|
||||
}
|
||||
if release.draft {
|
||||
return Err(Error("Draft uploaded. Public verification requires publishing the draft; use --stop-after upload for draft workflows.".into()));
|
||||
if releases.iter().any(|(_, _, _, release)| release.draft) {
|
||||
return Err(Error("A draft was uploaded. Public verification requires publishing every draft; use --stop-after upload for draft workflows.".into()));
|
||||
}
|
||||
verify::verify_published(
|
||||
&repository,
|
||||
&tag,
|
||||
&artifacts,
|
||||
&sums,
|
||||
if private {
|
||||
for (endpoint, api, private, release) in &releases {
|
||||
let asset_urls = api.asset_urls(release.id)?;
|
||||
let authenticated = if *private || asset_urls.is_some() {
|
||||
Some(api.download_client())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)?;
|
||||
};
|
||||
if let Some(asset_urls) = asset_urls.as_ref() {
|
||||
verify::verify_published_with_asset_urls(
|
||||
&endpoint.repository,
|
||||
&tag,
|
||||
&artifacts,
|
||||
&sums,
|
||||
authenticated,
|
||||
Some(asset_urls),
|
||||
)?;
|
||||
} else {
|
||||
verify::verify_published(&endpoint.repository, &tag, &artifacts, &sums, authenticated)?;
|
||||
}
|
||||
ui::success(format_args!(
|
||||
"Published assets verified on {}",
|
||||
endpoint.name
|
||||
));
|
||||
}
|
||||
if config.package_format.archives() && !args.skip_binstall_test {
|
||||
verify::verify_binstall(&checkout.root, &selected, &config.targets, &runner)?;
|
||||
}
|
||||
|
|
@ -380,9 +562,255 @@ pub fn run(mut args: Cli) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn release_message_with_macos_workaround(
|
||||
supplied: Option<&str>,
|
||||
artifacts: &[crate::package::Artifact],
|
||||
) -> Result<Option<String>, Error> {
|
||||
const HEADING: &str = "## macOS Workaround";
|
||||
if supplied.is_some_and(|message| message.contains(HEADING)) {
|
||||
return Ok(supplied.map(str::to_owned));
|
||||
}
|
||||
let Some(workaround) = macos_workaround(artifacts)? else {
|
||||
return Ok(supplied.map(str::to_owned));
|
||||
};
|
||||
Ok(Some(
|
||||
match supplied.map(str::trim_end).filter(|s| !s.is_empty()) {
|
||||
Some(message) => format!("{message}\n\n{workaround}"),
|
||||
None => workaround,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn macos_workaround(artifacts: &[crate::package::Artifact]) -> Result<Option<String>, Error> {
|
||||
use crate::{bundle::Format, package::ArtifactContents};
|
||||
|
||||
let apple = artifacts
|
||||
.iter()
|
||||
.filter(|artifact| build::is_apple(&artifact.target))
|
||||
.collect::<Vec<_>>();
|
||||
if apple.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let has_bundle = apple
|
||||
.iter()
|
||||
.any(|artifact| matches!(artifact.contents, ArtifactContents::Bundle { .. }));
|
||||
let has_archive = apple
|
||||
.iter()
|
||||
.any(|artifact| matches!(artifact.contents, ArtifactContents::Archive { .. }));
|
||||
let selected = apple.into_iter().filter(|artifact| {
|
||||
if has_bundle {
|
||||
matches!(artifact.contents, ArtifactContents::Bundle { .. })
|
||||
} else if has_archive {
|
||||
matches!(artifact.contents, ArtifactContents::Archive { .. })
|
||||
} else {
|
||||
matches!(artifact.contents, ArtifactContents::Binary)
|
||||
}
|
||||
});
|
||||
let mut sections = Vec::new();
|
||||
for artifact in selected {
|
||||
let filename = artifact
|
||||
.path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.ok_or_else(|| Error("Invalid macOS artifact filename".into()))?;
|
||||
let platform = if artifact.target.starts_with("aarch64-") {
|
||||
"Apple Silicon"
|
||||
} else {
|
||||
"Intel Mac"
|
||||
};
|
||||
let commands = match &artifact.contents {
|
||||
ArtifactContents::Bundle {
|
||||
root,
|
||||
format,
|
||||
entries,
|
||||
} => archive_workaround(
|
||||
filename,
|
||||
root,
|
||||
*format,
|
||||
entries
|
||||
.iter()
|
||||
.filter(|entry| entry.mode & 0o111 != 0)
|
||||
.map(|entry| entry.destination.as_str()),
|
||||
),
|
||||
ArtifactContents::Archive { root, binaries } => archive_workaround(
|
||||
filename,
|
||||
root,
|
||||
Format::Tgz,
|
||||
binaries.iter().map(String::as_str),
|
||||
),
|
||||
ArtifactContents::Binary => {
|
||||
let quoted = shell_quote(filename);
|
||||
format!(
|
||||
"downloads=\"$HOME/Downloads\"\nexecutable=\"$downloads\"/{quoted}\nxattr -d com.apple.quarantine \"$executable\" 2>/dev/null || true\nchmod +x \"$executable\"\n\"$executable\""
|
||||
)
|
||||
}
|
||||
};
|
||||
sections.push(format!(
|
||||
"For **{platform}** (`{filename}`), run in Terminal:\n```sh\n{commands}\n```"
|
||||
));
|
||||
}
|
||||
if sections.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(format!(
|
||||
"## macOS Workaround\n\nIf macOS blocks these downloaded, unsigned executables, remove their quarantine attribute manually after verifying and trusting the release. Download the indicated archive into `~/Downloads`; the commands extract it there, remove quarantine recursively from everything in the extracted directory, restore executable permissions, and start the first executable.\n\n{}",
|
||||
sections.join("\n\n")
|
||||
)))
|
||||
}
|
||||
|
||||
fn archive_workaround<'a>(
|
||||
filename: &str,
|
||||
root: &str,
|
||||
format: crate::bundle::Format,
|
||||
executables: impl Iterator<Item = &'a str>,
|
||||
) -> String {
|
||||
let filename = shell_quote(filename);
|
||||
let root = shell_quote(root);
|
||||
let executables = executables.collect::<Vec<_>>();
|
||||
let extraction = match format {
|
||||
crate::bundle::Format::Tgz => "tar -xzf \"$archive\" -C \"$downloads\"",
|
||||
crate::bundle::Format::Zip => "ditto -x -k \"$archive\" \"$downloads\"",
|
||||
};
|
||||
let paths = executables
|
||||
.iter()
|
||||
.map(|path| format!("\"$bundle\"/{}", shell_quote(path)))
|
||||
.collect::<Vec<_>>();
|
||||
let chmod = if paths.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
format!("\nchmod +x {}", paths.join(" "))
|
||||
};
|
||||
let launch = paths
|
||||
.iter()
|
||||
.zip(executables.iter())
|
||||
.find(|(_, executable)| {
|
||||
executable
|
||||
.rsplit('/')
|
||||
.next()
|
||||
.is_some_and(|name| name.ends_with("-gui"))
|
||||
})
|
||||
.map(|(path, _)| path)
|
||||
.or_else(|| paths.first())
|
||||
.map_or_else(String::new, |path| format!("\n{path}"));
|
||||
format!(
|
||||
"downloads=\"$HOME/Downloads\"\narchive=\"$downloads\"/{filename}\n{extraction}\nbundle=\"$downloads\"/{root}\nxattr -rd com.apple.quarantine \"$bundle\"{chmod}{launch}"
|
||||
)
|
||||
}
|
||||
|
||||
fn shell_quote(value: &str) -> String {
|
||||
format!("'{}'", value.replace('\'', "'\"'\"'"))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum TokenFile {
|
||||
Plain(String),
|
||||
Named(BTreeMap<String, String>),
|
||||
}
|
||||
|
||||
impl TokenFile {
|
||||
fn plain(&self) -> Option<&str> {
|
||||
match self {
|
||||
Self::Plain(token) => Some(token),
|
||||
Self::Named(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_token_file(path: &std::path::Path) -> Result<TokenFile, Error> {
|
||||
if !path.is_file() {
|
||||
return Err(Error(format!(
|
||||
"token file is not a regular file: {}",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
let source = std::fs::read_to_string(path)
|
||||
.map_err(|e| Error(format!("cannot read token file {}: {e}", path.display())))?;
|
||||
let trimmed = source.trim_end_matches(['\r', '\n']);
|
||||
if trimmed.trim_start().starts_with('{') {
|
||||
let tokens: BTreeMap<String, String> = serde_json::from_str(trimmed)
|
||||
.map_err(|e| Error(format!("invalid JSON token file {}: {e}", path.display())))?;
|
||||
if tokens.is_empty()
|
||||
|| tokens
|
||||
.values()
|
||||
.any(|token| token.is_empty() || token.contains(['\r', '\n']))
|
||||
{
|
||||
return Err(Error(format!(
|
||||
"JSON token file must map endpoint names to nonempty single-line tokens: {}",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
return Ok(TokenFile::Named(tokens));
|
||||
}
|
||||
let mut token = source;
|
||||
while token.ends_with(['\r', '\n']) {
|
||||
token.pop();
|
||||
}
|
||||
if token.is_empty() {
|
||||
return Err(Error(format!("token file is empty: {}", path.display())));
|
||||
}
|
||||
if token.contains(['\r', '\n']) {
|
||||
return Err(Error(format!(
|
||||
"plaintext token file must contain exactly one token: {}",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
Ok(TokenFile::Plain(token))
|
||||
}
|
||||
|
||||
fn token_for_endpoint(
|
||||
file: Option<&TokenFile>,
|
||||
endpoint: &ReleaseEndpoint,
|
||||
endpoint_count: usize,
|
||||
) -> Result<String, Error> {
|
||||
match file {
|
||||
Some(TokenFile::Plain(token)) if endpoint_count == 1 => Ok(token.clone()),
|
||||
Some(TokenFile::Plain(_)) => Err(Error(
|
||||
"A plaintext token file can only be used with one release endpoint; use a JSON object keyed by endpoint name."
|
||||
.into(),
|
||||
)),
|
||||
Some(TokenFile::Named(tokens)) => tokens.get(&endpoint.name).cloned().ok_or_else(|| {
|
||||
Error(format!(
|
||||
"Token file has no token for release endpoint {:?}.",
|
||||
endpoint.name
|
||||
))
|
||||
}),
|
||||
None => std::env::var(&endpoint.token_env)
|
||||
.ok()
|
||||
.filter(|token| !token.is_empty())
|
||||
.ok_or_else(|| {
|
||||
Error(format!(
|
||||
"Set {} before publishing endpoint {:?}.",
|
||||
endpoint.token_env, endpoint.name
|
||||
))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_token_keys(
|
||||
file: Option<&TokenFile>,
|
||||
endpoints: &[ReleaseEndpoint],
|
||||
) -> Result<(), Error> {
|
||||
let Some(TokenFile::Named(tokens)) = file else {
|
||||
return Ok(());
|
||||
};
|
||||
if let Some(unknown) = tokens
|
||||
.keys()
|
||||
.find(|name| !endpoints.iter().any(|endpoint| &endpoint.name == *name))
|
||||
{
|
||||
return Err(Error(format!(
|
||||
"Token file contains unknown release endpoint {unknown:?}."
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
bundle::{Entry, Format},
|
||||
package::{Artifact, ArtifactContents},
|
||||
};
|
||||
use std::{fs, path::Path};
|
||||
|
||||
fn write(root: &Path, relative: &str, content: &str) {
|
||||
|
|
@ -477,4 +905,258 @@ mod tests {
|
|||
assert!(Cli::try_parse_from(arguments).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preparation_options_are_rejected_before_project_acquisition() {
|
||||
let error = run(Cli::parse_from([
|
||||
"rust_realease",
|
||||
"missing-project",
|
||||
"--commit-preparation",
|
||||
]))
|
||||
.unwrap_err();
|
||||
assert!(error
|
||||
.to_string()
|
||||
.contains("Preparation options require --prepare-repo"));
|
||||
|
||||
let error = run(Cli::parse_from([
|
||||
"rust_realease",
|
||||
"missing-project",
|
||||
"--prepare-repo",
|
||||
"--stop-after",
|
||||
"metadata",
|
||||
]))
|
||||
.unwrap_err();
|
||||
assert!(error
|
||||
.to_string()
|
||||
.contains("--prepare-repo is a separate mode"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_message_inputs_and_recompile_flag_parse() {
|
||||
let args = Cli::try_parse_from([
|
||||
"rust_realease",
|
||||
".",
|
||||
"--recompile",
|
||||
"-m",
|
||||
"Changes in this release",
|
||||
])
|
||||
.unwrap();
|
||||
assert!(args.recompile);
|
||||
assert_eq!(args.message.as_deref(), Some("Changes in this release"));
|
||||
assert!(Cli::try_parse_from([
|
||||
"rust_realease",
|
||||
".",
|
||||
"--message",
|
||||
"inline",
|
||||
"--message-file",
|
||||
"notes.md",
|
||||
])
|
||||
.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn macos_workaround_uses_actual_bundle_and_quarantines_its_complete_tree() {
|
||||
let artifacts = vec![
|
||||
Artifact {
|
||||
target: "aarch64-apple-darwin".into(),
|
||||
path: "dist/pptx-reporter-aarch64-apple-darwin".into(),
|
||||
contents: ArtifactContents::Binary,
|
||||
sha256: "0".repeat(64),
|
||||
},
|
||||
Artifact {
|
||||
target: "aarch64-apple-darwin".into(),
|
||||
path: "dist/pptx-reporter-portable-aarch64-apple-darwin-v0.1.2.tgz".into(),
|
||||
contents: ArtifactContents::Bundle {
|
||||
root: "pptx-reporter-portable-aarch64-apple-darwin-v0.1.2".into(),
|
||||
format: Format::Tgz,
|
||||
entries: vec![
|
||||
Entry {
|
||||
destination: "pptx-reporter".into(),
|
||||
sha256: "1".repeat(64),
|
||||
mode: 0o755,
|
||||
},
|
||||
Entry {
|
||||
destination: "pptx-reporter-gui".into(),
|
||||
sha256: "2".repeat(64),
|
||||
mode: 0o755,
|
||||
},
|
||||
Entry {
|
||||
destination: "resources/font.ttf".into(),
|
||||
sha256: "3".repeat(64),
|
||||
mode: 0o644,
|
||||
},
|
||||
],
|
||||
},
|
||||
sha256: "4".repeat(64),
|
||||
},
|
||||
];
|
||||
let message = release_message_with_macos_workaround(Some("Release notes"), &artifacts)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(message.starts_with("Release notes\n\n## macOS Workaround"));
|
||||
assert!(message.contains("tar -xzf \"$archive\" -C \"$downloads\""));
|
||||
assert!(message.contains("xattr -rd com.apple.quarantine \"$bundle\""));
|
||||
assert!(message
|
||||
.contains("chmod +x \"$bundle\"/'pptx-reporter' \"$bundle\"/'pptx-reporter-gui'"));
|
||||
assert!(message.contains("\n\"$bundle\"/'pptx-reporter-gui'\n```"));
|
||||
assert!(!message.contains("executable=\"$downloads\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn macos_workaround_is_not_duplicated_or_added_to_other_targets() {
|
||||
let linux = Artifact {
|
||||
target: "x86_64-unknown-linux-gnu".into(),
|
||||
path: "dist/demo.tgz".into(),
|
||||
contents: ArtifactContents::Archive {
|
||||
root: "demo".into(),
|
||||
binaries: vec!["demo".into()],
|
||||
},
|
||||
sha256: "0".repeat(64),
|
||||
};
|
||||
assert_eq!(
|
||||
release_message_with_macos_workaround(Some("Notes"), &[linux]).unwrap(),
|
||||
Some("Notes".into())
|
||||
);
|
||||
let existing = "Notes\n\n## macOS Workaround\n\nCustom instructions";
|
||||
assert_eq!(
|
||||
release_message_with_macos_workaround(Some(existing), &[]).unwrap(),
|
||||
Some(existing.into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_file_accepts_one_token_and_trailing_newlines() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("token");
|
||||
fs::write(&path, "secret-token\r\n").unwrap();
|
||||
assert_eq!(
|
||||
read_token_file(&path).unwrap().plain(),
|
||||
Some("secret-token")
|
||||
);
|
||||
|
||||
fs::write(&path, "first\nsecond\n").unwrap();
|
||||
assert!(read_token_file(&path).is_err());
|
||||
fs::write(&path, "\n").unwrap();
|
||||
assert!(read_token_file(&path).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_file_accepts_json_tokens_keyed_by_endpoint() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("tokens.json");
|
||||
fs::write(&path, r#"{"github":"github-token","mirror":"gitea-token"}"#).unwrap();
|
||||
let TokenFile::Named(tokens) = read_token_file(&path).unwrap() else {
|
||||
panic!("expected named tokens");
|
||||
};
|
||||
assert_eq!(
|
||||
tokens.get("github").map(String::as_str),
|
||||
Some("github-token")
|
||||
);
|
||||
assert_eq!(
|
||||
tokens.get("mirror").map(String::as_str),
|
||||
Some("gitea-token")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_file_option_parses_as_a_path() {
|
||||
let args =
|
||||
Cli::try_parse_from(["rust_realease", ".", "--token-file", "secrets/gitea-token"])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
args.token_file.as_deref(),
|
||||
Some(Path::new("secrets/gitea-token"))
|
||||
);
|
||||
}
|
||||
|
||||
fn endpoint(name: &str, token_env: &str) -> ReleaseEndpoint {
|
||||
ReleaseEndpoint {
|
||||
name: name.into(),
|
||||
provider: crate::config::ReleaseProvider::Gitea,
|
||||
repository: "https://example.com/team/demo".into(),
|
||||
token_env: token_env.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn endpoint_tokens_are_selected_and_validated() {
|
||||
let primary = endpoint("primary", "RUST_RELEASE_TEST_PRIMARY_TOKEN");
|
||||
let mirror = endpoint("mirror", "RUST_RELEASE_TEST_MIRROR_TOKEN");
|
||||
let plain = TokenFile::Plain("plain-secret".into());
|
||||
assert_eq!(
|
||||
token_for_endpoint(Some(&plain), &primary, 1).unwrap(),
|
||||
"plain-secret"
|
||||
);
|
||||
assert!(token_for_endpoint(Some(&plain), &primary, 2).is_err());
|
||||
|
||||
let named = TokenFile::Named(BTreeMap::from([
|
||||
("primary".into(), "one".into()),
|
||||
("mirror".into(), "two".into()),
|
||||
]));
|
||||
assert_eq!(token_for_endpoint(Some(&named), &mirror, 2).unwrap(), "two");
|
||||
assert!(token_for_endpoint(Some(&TokenFile::Named(BTreeMap::new())), &primary, 1).is_err());
|
||||
assert!(validate_token_keys(Some(&named), &[primary.clone(), mirror]).is_ok());
|
||||
assert!(validate_token_keys(Some(&named), &[primary]).is_err());
|
||||
assert!(validate_token_keys(None, &[]).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_file_rejects_non_files_and_invalid_json_values() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
assert!(read_token_file(temp.path()).is_err());
|
||||
let path = temp.path().join("tokens.json");
|
||||
for invalid in [
|
||||
"{",
|
||||
"{}",
|
||||
r#"{"primary":""}"#,
|
||||
"{\"primary\":\"line\\nbreak\"}",
|
||||
] {
|
||||
fs::write(&path, invalid).unwrap();
|
||||
assert!(read_token_file(&path).is_err(), "accepted {invalid:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn macos_workaround_covers_zip_archive_binary_and_shell_escaping() {
|
||||
let bundle = Artifact {
|
||||
target: "x86_64-apple-darwin".into(),
|
||||
path: "dist/demo portable.zip".into(),
|
||||
contents: ArtifactContents::Bundle {
|
||||
root: "demo's bundle".into(),
|
||||
format: Format::Zip,
|
||||
entries: vec![],
|
||||
},
|
||||
sha256: "0".repeat(64),
|
||||
};
|
||||
let archive = Artifact {
|
||||
target: "aarch64-apple-darwin".into(),
|
||||
path: "dist/demo.tgz".into(),
|
||||
contents: ArtifactContents::Archive {
|
||||
root: "demo".into(),
|
||||
binaries: vec!["demo".into()],
|
||||
},
|
||||
sha256: "1".repeat(64),
|
||||
};
|
||||
let binary = Artifact {
|
||||
target: "aarch64-apple-darwin".into(),
|
||||
path: "dist/demo-bin".into(),
|
||||
contents: ArtifactContents::Binary,
|
||||
sha256: "2".repeat(64),
|
||||
};
|
||||
let message = release_message_with_macos_workaround(None, &[bundle])
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(message.contains("ditto -x -k"));
|
||||
assert!(message.contains("demo'\"'\"'s bundle"));
|
||||
|
||||
let message = release_message_with_macos_workaround(None, &[archive])
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(message.contains("tar -xzf"));
|
||||
|
||||
let message = release_message_with_macos_workaround(None, &[binary])
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(message.contains("xattr -d com.apple.quarantine \"$executable\""));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
310
src/config.rs
310
src/config.rs
|
|
@ -7,6 +7,7 @@ use std::{fs, path::Path};
|
|||
pub const PROJECT_CONFIG_FILE: &str = "rust-release.toml";
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct FileConfig {
|
||||
build: Option<Build>,
|
||||
release: Option<Release>,
|
||||
|
|
@ -16,30 +17,56 @@ struct FileConfig {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Build {
|
||||
targets: Option<Vec<String>>,
|
||||
profile: Option<String>,
|
||||
locked: Option<bool>,
|
||||
features: Option<Vec<String>>,
|
||||
apple: Option<Apple>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||
struct Apple {
|
||||
image: Option<String>,
|
||||
deployment_target: Option<String>,
|
||||
sdk_root: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Release {
|
||||
#[serde(rename = "tag-prefix")]
|
||||
tag_prefix: Option<String>,
|
||||
endpoints: Option<Vec<FileReleaseEndpoint>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||
struct FileReleaseEndpoint {
|
||||
name: String,
|
||||
provider: ReleaseProvider,
|
||||
repository: String,
|
||||
token_env: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Package {
|
||||
bundles: Option<Vec<crate::bundle::Bundle>>,
|
||||
format: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Repository {
|
||||
#[serde(default)]
|
||||
fallbacks: Vec<FileRepositoryFallback>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct FileRepositoryFallback {
|
||||
origin: String,
|
||||
#[serde(rename = "fallback-url")]
|
||||
|
|
@ -47,6 +74,7 @@ struct FileRepositoryFallback {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Gitea {
|
||||
#[serde(rename = "token-env")]
|
||||
token_env: Option<String>,
|
||||
|
|
@ -60,15 +88,62 @@ pub struct RepositoryFallback {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
pub bundles: Vec<crate::bundle::Bundle>,
|
||||
pub targets: Vec<String>,
|
||||
pub profile: String,
|
||||
pub locked: bool,
|
||||
pub features: Vec<String>,
|
||||
pub apple: AppleConfig,
|
||||
pub tag_prefix: String,
|
||||
pub token_env: String,
|
||||
pub release_endpoints: Vec<ReleaseEndpoint>,
|
||||
pub package_format: PackageFormat,
|
||||
pub repository_fallbacks: Vec<RepositoryFallback>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum ReleaseProvider {
|
||||
Gitea,
|
||||
Github,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ReleaseProvider {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Gitea => "gitea",
|
||||
Self::Github => "github",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ReleaseEndpoint {
|
||||
pub name: String,
|
||||
pub provider: ReleaseProvider,
|
||||
pub repository: String,
|
||||
pub token_env: String,
|
||||
}
|
||||
|
||||
pub const DEFAULT_APPLE_IMAGE: &str = "ghcr.io/rust-cross/cargo-zigbuild@sha256:9b4f6b3eb9e8f9fefb3960fe288ddba576627d66d9eff851ce7f62d2455564a5";
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct AppleConfig {
|
||||
pub image: String,
|
||||
pub deployment_target: String,
|
||||
pub sdk_root: String,
|
||||
}
|
||||
|
||||
impl Default for AppleConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
image: DEFAULT_APPLE_IMAGE.into(),
|
||||
deployment_target: "13.0".into(),
|
||||
sdk_root: "/opt/MacOSX11.3.sdk".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn resolve_repository(&self, input: &str) -> Result<String, Error> {
|
||||
let normalized = project::normalize_repository(input)?;
|
||||
|
|
@ -109,6 +184,7 @@ pub struct Overrides {
|
|||
pub unlocked: bool,
|
||||
pub tag_prefix: Option<String>,
|
||||
pub package_format: Option<PackageFormat>,
|
||||
pub features: Vec<String>,
|
||||
}
|
||||
|
||||
pub fn load(
|
||||
|
|
@ -173,6 +249,21 @@ fn merge_build(current: &mut Option<Build>, later: Option<Build>) {
|
|||
if later.locked.is_some() {
|
||||
current.locked = later.locked;
|
||||
}
|
||||
if later.features.is_some() {
|
||||
current.features = later.features;
|
||||
}
|
||||
if let Some(later) = later.apple {
|
||||
let apple = current.apple.get_or_insert_with(Apple::default);
|
||||
if later.image.is_some() {
|
||||
apple.image = later.image;
|
||||
}
|
||||
if later.deployment_target.is_some() {
|
||||
apple.deployment_target = later.deployment_target;
|
||||
}
|
||||
if later.sdk_root.is_some() {
|
||||
apple.sdk_root = later.sdk_root;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -182,12 +273,18 @@ fn merge_release(current: &mut Option<Release>, later: Option<Release>) {
|
|||
if later.tag_prefix.is_some() {
|
||||
current.tag_prefix = later.tag_prefix;
|
||||
}
|
||||
if later.endpoints.is_some() {
|
||||
current.endpoints = later.endpoints;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_package(current: &mut Option<Package>, later: Option<Package>) {
|
||||
if let Some(later) = later {
|
||||
let current = current.get_or_insert_with(Package::default);
|
||||
if later.bundles.is_some() {
|
||||
current.bundles = later.bundles;
|
||||
}
|
||||
if later.format.is_some() {
|
||||
current.format = later.format;
|
||||
}
|
||||
|
|
@ -214,6 +311,7 @@ fn merge_gitea(current: &mut Option<Gitea>, later: Option<Gitea>) {
|
|||
|
||||
fn finish(file: FileConfig, overrides: Overrides) -> Result<Config, Error> {
|
||||
let build = file.build.unwrap_or_default();
|
||||
let apple = build.apple.clone().unwrap_or_default();
|
||||
let release = file.release.unwrap_or_default();
|
||||
let package = file.package.unwrap_or_default();
|
||||
let repository = file.repository.unwrap_or_default();
|
||||
|
|
@ -228,6 +326,67 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result<Config, Error> {
|
|||
))
|
||||
}
|
||||
};
|
||||
let mut release_endpoints = Vec::new();
|
||||
for endpoint in release.endpoints.clone().unwrap_or_default() {
|
||||
if endpoint.name.is_empty()
|
||||
|| !endpoint
|
||||
.name
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_alphanumeric() || b"_-".contains(&b))
|
||||
{
|
||||
return Err(Error(
|
||||
"release endpoint names must contain only letters, digits, underscores or hyphens."
|
||||
.into(),
|
||||
));
|
||||
}
|
||||
if release_endpoints
|
||||
.iter()
|
||||
.any(|existing: &ReleaseEndpoint| existing.name == endpoint.name)
|
||||
{
|
||||
return Err(Error(format!(
|
||||
"duplicate release endpoint name {:?}.",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
let repository = project::normalize_repository(&endpoint.repository)
|
||||
.map_err(|e| Error(format!("invalid release endpoint repository: {e}")))?;
|
||||
if !repository.starts_with("https://") {
|
||||
return Err(Error(format!(
|
||||
"release endpoint {:?} repository must use HTTPS.",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
if endpoint.provider == ReleaseProvider::Github
|
||||
&& url::Url::parse(&repository)
|
||||
.ok()
|
||||
.and_then(|url| url.host_str().map(str::to_owned))
|
||||
.as_deref()
|
||||
!= Some("github.com")
|
||||
{
|
||||
return Err(Error(format!(
|
||||
"GitHub endpoint {:?} must use a github.com repository URL.",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
let token_env = endpoint
|
||||
.token_env
|
||||
.unwrap_or_else(|| match endpoint.provider {
|
||||
ReleaseProvider::Gitea => "GITEA_TOKEN".into(),
|
||||
ReleaseProvider::Github => "GITHUB_TOKEN".into(),
|
||||
});
|
||||
if !valid_env(&token_env) {
|
||||
return Err(Error(format!(
|
||||
"release endpoint {:?} token-env must be an environment variable name.",
|
||||
endpoint.name
|
||||
)));
|
||||
}
|
||||
release_endpoints.push(ReleaseEndpoint {
|
||||
name: endpoint.name,
|
||||
provider: endpoint.provider,
|
||||
repository,
|
||||
token_env,
|
||||
});
|
||||
}
|
||||
let mut repository_fallbacks = Vec::new();
|
||||
for fallback in repository.fallbacks {
|
||||
let origin = project::normalize_repository(&fallback.origin)
|
||||
|
|
@ -244,6 +403,7 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result<Config, Error> {
|
|||
});
|
||||
}
|
||||
let mut config = Config {
|
||||
bundles: package.bundles.unwrap_or_default(),
|
||||
targets: if overrides.targets.is_empty() {
|
||||
build.targets.unwrap_or_else(|| {
|
||||
[
|
||||
|
|
@ -266,11 +426,24 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result<Config, Error> {
|
|||
} else {
|
||||
build.locked.unwrap_or(true)
|
||||
},
|
||||
features: if overrides.features.is_empty() {
|
||||
build.features.unwrap_or_default()
|
||||
} else {
|
||||
overrides.features
|
||||
},
|
||||
apple: AppleConfig {
|
||||
image: apple.image.unwrap_or_else(|| DEFAULT_APPLE_IMAGE.into()),
|
||||
deployment_target: apple.deployment_target.unwrap_or_else(|| "13.0".into()),
|
||||
sdk_root: apple
|
||||
.sdk_root
|
||||
.unwrap_or_else(|| "/opt/MacOSX11.3.sdk".into()),
|
||||
},
|
||||
tag_prefix: overrides
|
||||
.tag_prefix
|
||||
.or(release.tag_prefix)
|
||||
.unwrap_or_else(|| "v".into()),
|
||||
token_env: gitea.token_env.unwrap_or_else(|| "GITEA_TOKEN".into()),
|
||||
release_endpoints,
|
||||
package_format: overrides.package_format.unwrap_or(package_format),
|
||||
repository_fallbacks,
|
||||
};
|
||||
|
|
@ -286,6 +459,19 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result<Config, Error> {
|
|||
}
|
||||
}
|
||||
config.targets = unique;
|
||||
let mut unique_features = Vec::new();
|
||||
for feature in config.features {
|
||||
if !valid_feature(&feature) {
|
||||
return Err(Error(format!(
|
||||
"invalid Cargo feature name in build.features: {feature:?}."
|
||||
)));
|
||||
}
|
||||
if !unique_features.contains(&feature) {
|
||||
unique_features.push(feature);
|
||||
}
|
||||
}
|
||||
config.features = unique_features;
|
||||
validate_apple(&config.apple)?;
|
||||
if !valid_profile(&config.profile) {
|
||||
return Err(Error("build.profile must be a Cargo profile name.".into()));
|
||||
}
|
||||
|
|
@ -307,7 +493,48 @@ fn finish(file: FileConfig, overrides: Overrides) -> Result<Config, Error> {
|
|||
Ok(config)
|
||||
}
|
||||
|
||||
fn valid_target(s: &str) -> bool {
|
||||
fn validate_apple(apple: &AppleConfig) -> Result<(), Error> {
|
||||
let digest = apple
|
||||
.image
|
||||
.rsplit_once("@sha256:")
|
||||
.map(|(_, digest)| digest);
|
||||
if digest.is_none_or(|digest| {
|
||||
digest.len() != 64
|
||||
|| !digest
|
||||
.bytes()
|
||||
.all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase())
|
||||
}) || apple.image.bytes().any(|b| b.is_ascii_whitespace())
|
||||
{
|
||||
return Err(Error(
|
||||
"build.apple.image must use an immutable @sha256:<64 lowercase hex> digest.".into(),
|
||||
));
|
||||
}
|
||||
let mut version = apple.deployment_target.split('.');
|
||||
let major = version.next().and_then(|value| value.parse::<u16>().ok());
|
||||
let minor = version.next().and_then(|value| value.parse::<u8>().ok());
|
||||
let patch = version
|
||||
.next()
|
||||
.map_or(Some(0), |value| value.parse::<u8>().ok());
|
||||
if major
|
||||
.zip(minor)
|
||||
.zip(patch)
|
||||
.is_none_or(|((major, _), _)| major < 13)
|
||||
|| version.next().is_some()
|
||||
{
|
||||
return Err(Error("build.apple.deployment-target must be a macOS version at least 13.0 (for example \"13.0\").".into()));
|
||||
}
|
||||
if !apple.sdk_root.starts_with('/')
|
||||
|| apple.sdk_root.contains(['\0', '\n', '\r'])
|
||||
|| apple.sdk_root.split('/').any(|part| part == "..")
|
||||
{
|
||||
return Err(Error(
|
||||
"build.apple.sdk-root must be an absolute container path without '..'.".into(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn valid_target(s: &str) -> bool {
|
||||
s.split('-').count() >= 3
|
||||
&& s.split('-').all(|part| {
|
||||
!part.is_empty() && part.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_')
|
||||
|
|
@ -320,6 +547,12 @@ fn valid_profile(s: &str) -> bool {
|
|||
.all(|b| b.is_ascii_alphanumeric() || b"_-".contains(&b))
|
||||
}
|
||||
|
||||
fn valid_feature(s: &str) -> bool {
|
||||
s.as_bytes().first().is_some_and(u8::is_ascii_alphanumeric)
|
||||
&& s.bytes()
|
||||
.all(|b| b.is_ascii_alphanumeric() || b"_+-".contains(&b))
|
||||
}
|
||||
|
||||
fn valid_env(s: &str) -> bool {
|
||||
s.as_bytes()
|
||||
.first()
|
||||
|
|
@ -396,4 +629,79 @@ mod tests {
|
|||
assert_eq!(config.profile, "project-profile");
|
||||
assert_eq!(config.targets.len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apple_settings_and_features_are_typed_layered_and_validated() {
|
||||
let config = parse(
|
||||
&format!(
|
||||
"[build]\ntargets=['aarch64-apple-darwin']\nfeatures=['serial','ui']\n[build.apple]\nimage='{DEFAULT_APPLE_IMAGE}'\ndeployment-target='13.2'\nsdk-root='/opt/MacOSX13.sdk'\n"
|
||||
),
|
||||
Overrides::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(config.features, ["serial", "ui"]);
|
||||
assert_eq!(config.apple.deployment_target, "13.2");
|
||||
assert_eq!(config.apple.sdk_root, "/opt/MacOSX13.sdk");
|
||||
|
||||
let overridden = parse(
|
||||
"[build]\ntargets=['x86_64-apple-darwin']\nfeatures=['old']\n",
|
||||
Overrides {
|
||||
features: vec!["new".into()],
|
||||
..Overrides::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(overridden.features, ["new"]);
|
||||
|
||||
for source in [
|
||||
"[build.apple]\nimage='cargo-zigbuild:latest'",
|
||||
&format!("[build.apple]\nimage='{DEFAULT_APPLE_IMAGE}'\ndeployment-target='12.0'"),
|
||||
&format!("[build.apple]\nimage='{DEFAULT_APPLE_IMAGE}'\nsdk-root='../sdk'"),
|
||||
"[build]\nfeatures=['bad feature']",
|
||||
] {
|
||||
assert!(parse(source, Overrides::default()).is_err(), "{source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_endpoints_are_typed_and_later_layers_replace_the_list() {
|
||||
let config = parse_sources(
|
||||
vec![
|
||||
(
|
||||
"user".into(),
|
||||
"[[release.endpoints]]\nname='old'\nprovider='gitea'\nrepository='https://git.example/team/demo'\n"
|
||||
.into(),
|
||||
),
|
||||
(
|
||||
"project".into(),
|
||||
"[[release.endpoints]]\nname='github'\nprovider='github'\nrepository='https://github.com/team/demo.git'\ntoken-env='DEMO_GITHUB_TOKEN'\n[[release.endpoints]]\nname='mirror'\nprovider='gitea'\nrepository='https://git.example/team/demo'\ntoken-env='DEMO_GITEA_TOKEN'\n"
|
||||
.into(),
|
||||
),
|
||||
],
|
||||
Overrides::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(config.release_endpoints.len(), 2);
|
||||
assert_eq!(config.release_endpoints[0].name, "github");
|
||||
assert_eq!(
|
||||
config.release_endpoints[0].provider,
|
||||
ReleaseProvider::Github
|
||||
);
|
||||
assert_eq!(
|
||||
config.release_endpoints[0].repository,
|
||||
"https://github.com/team/demo"
|
||||
);
|
||||
assert_eq!(config.release_endpoints[1].provider, ReleaseProvider::Gitea);
|
||||
|
||||
assert!(parse(
|
||||
"[[release.endpoints]]\nname='bad name'\nprovider='gitea'\nrepository='https://git.example/team/demo'",
|
||||
Overrides::default()
|
||||
)
|
||||
.is_err());
|
||||
assert!(parse(
|
||||
"[[release.endpoints]]\nname='enterprise'\nprovider='github'\nrepository='https://github.example/team/demo'",
|
||||
Overrides::default()
|
||||
)
|
||||
.is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,3 +8,9 @@ impl fmt::Display for Error {
|
|||
f.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Self(e.to_string())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
164
src/gitea.rs
164
src/gitea.rs
|
|
@ -9,7 +9,10 @@ use reqwest::{
|
|||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::{env, fs::File, path::Path, time::Duration};
|
||||
use std::{fs::File, path::Path, time::Duration};
|
||||
|
||||
#[cfg(test)]
|
||||
use std::env;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct RemoteRelease {
|
||||
|
|
@ -30,6 +33,7 @@ pub struct Gitea {
|
|||
}
|
||||
|
||||
impl Gitea {
|
||||
#[cfg(test)]
|
||||
pub fn new(repository: &str, token_env: &str) -> Result<Self, Error> {
|
||||
if !repository.starts_with("https://") {
|
||||
return Err(Error(
|
||||
|
|
@ -43,7 +47,7 @@ impl Gitea {
|
|||
Self::with_token(repository, &token)
|
||||
}
|
||||
|
||||
fn with_token(repository: &str, token: &str) -> Result<Self, Error> {
|
||||
pub(crate) fn with_token(repository: &str, token: &str) -> Result<Self, Error> {
|
||||
let parsed = url::Url::parse(repository)
|
||||
.map_err(|_| Error("Invalid release repository URL.".into()))?;
|
||||
let parts: Vec<_> = parsed
|
||||
|
|
@ -120,6 +124,8 @@ impl Gitea {
|
|||
commit: &str,
|
||||
draft: bool,
|
||||
prerelease: bool,
|
||||
message: Option<&str>,
|
||||
replace_existing_message: bool,
|
||||
) -> Result<RemoteRelease, Error> {
|
||||
let encoded = encode(tag);
|
||||
let remote = self
|
||||
|
|
@ -134,19 +140,33 @@ impl Gitea {
|
|||
"Remote tag commit differs from the checkout; refusing publication.".into(),
|
||||
));
|
||||
}
|
||||
let release = if let Some(existing) = self.request(
|
||||
let (release, existed) = if let Some(existing) = self.request(
|
||||
Method::GET,
|
||||
&format!("/releases/tags/{encoded}"),
|
||||
None,
|
||||
true,
|
||||
)? {
|
||||
existing
|
||||
(existing, true)
|
||||
} else {
|
||||
self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease})), false)?
|
||||
.ok_or_else(|| Error("Gitea release creation returned no response.".into()))?
|
||||
(
|
||||
self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease, "body": message.unwrap_or_default()})), false)?
|
||||
.ok_or_else(|| Error("Gitea release creation returned no response.".into()))?,
|
||||
false,
|
||||
)
|
||||
};
|
||||
serde_json::from_value(release)
|
||||
.map_err(|_| Error("Gitea release response is missing id or draft state.".into()))
|
||||
let release: RemoteRelease = serde_json::from_value(release)
|
||||
.map_err(|_| Error("Gitea release response is missing id or draft state.".into()))?;
|
||||
if existed && replace_existing_message {
|
||||
if let Some(message) = message {
|
||||
self.request(
|
||||
Method::PATCH,
|
||||
&format!("/releases/{}", release.id),
|
||||
Some(json!({"body": message})),
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Ok(release)
|
||||
}
|
||||
|
||||
pub fn repository_private(&self) -> Result<bool, Error> {
|
||||
|
|
@ -300,7 +320,10 @@ mod tests {
|
|||
assert!(n > 0, "client closed before body");
|
||||
bytes.extend_from_slice(&buffer[..n]);
|
||||
}
|
||||
requests.push(first);
|
||||
requests.push(format!(
|
||||
"{first}\n{}",
|
||||
String::from_utf8_lossy(&bytes[header_end..header_end + length])
|
||||
));
|
||||
let reason = if reply.status == 200 {
|
||||
"OK"
|
||||
} else if reply.status == 201 {
|
||||
|
|
@ -343,9 +366,13 @@ mod tests {
|
|||
},
|
||||
]);
|
||||
let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap();
|
||||
let release = api.ensure_release("v0.1.0", "abc", false, true).unwrap();
|
||||
let release = api
|
||||
.ensure_release("v0.1.0", "abc", false, true, Some("Release notes"), false)
|
||||
.unwrap();
|
||||
assert_eq!(release.id, 7);
|
||||
assert_eq!(handle.join().unwrap().len(), 3);
|
||||
let requests = handle.join().unwrap();
|
||||
assert_eq!(requests.len(), 3);
|
||||
assert!(requests[2].contains(r#""body":"Release notes""#));
|
||||
|
||||
let (host, handle) = server(vec![
|
||||
Reply {
|
||||
|
|
@ -360,15 +387,23 @@ mod tests {
|
|||
status: 200,
|
||||
body: r#"{"id":9,"draft":true}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "PATCH",
|
||||
path: "/gitea/api/v1/repos/team/demo/releases/9",
|
||||
status: 200,
|
||||
body: r#"{"id":9,"draft":true}"#,
|
||||
},
|
||||
]);
|
||||
let api = Gitea::with_token(&format!("{host}/gitea/team/demo"), "test-token").unwrap();
|
||||
assert_eq!(
|
||||
api.ensure_release("v0.1.0", "abc", false, false)
|
||||
api.ensure_release("v0.1.0", "abc", false, false, Some("Updated notes"), true,)
|
||||
.unwrap()
|
||||
.id,
|
||||
9
|
||||
);
|
||||
assert_eq!(handle.join().unwrap().len(), 2);
|
||||
let requests = handle.join().unwrap();
|
||||
assert_eq!(requests.len(), 3);
|
||||
assert!(requests[2].contains(r#""body":"Updated notes""#));
|
||||
let _ = base;
|
||||
}
|
||||
|
||||
|
|
@ -426,7 +461,7 @@ mod tests {
|
|||
}]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
let error = api
|
||||
.ensure_release("v0.1.0", "abc", false, false)
|
||||
.ensure_release("v0.1.0", "abc", false, false, None, false)
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(error.contains("HTTP 401"));
|
||||
|
|
@ -448,7 +483,7 @@ mod tests {
|
|||
}]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
let error = api
|
||||
.ensure_release("v0.1.0", "abc", false, false)
|
||||
.ensure_release("v0.1.0", "abc", false, false, None, false)
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(error.contains(expected), "{error}");
|
||||
|
|
@ -478,4 +513,103 @@ mod tests {
|
|||
.to_string()
|
||||
.contains("HTTPS"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constructor_rejects_missing_credentials_and_malformed_repositories() {
|
||||
let missing = "RUST_REALEASE_TEST_TOKEN_THAT_IS_NOT_SET";
|
||||
assert!(Gitea::new("https://example.com/team/demo", missing)
|
||||
.err()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains(missing));
|
||||
for repository in [
|
||||
"://",
|
||||
"mailto:demo@example.com",
|
||||
"https://example.com/only-one",
|
||||
] {
|
||||
assert!(
|
||||
Gitea::with_token(repository, "token").is_err(),
|
||||
"{repository}"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
Gitea::with_token("https://example.com/team/demo", "bad\ntoken")
|
||||
.err()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains("token value")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_api_responses_and_upload_failures_are_reported() {
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/api/v1/repos/team/demo/tags/v0.1.0",
|
||||
status: 200,
|
||||
body: "not-json",
|
||||
}]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
assert!(api
|
||||
.ensure_release("v0.1.0", "abc", false, false, None, false)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("invalid JSON"));
|
||||
handle.join().unwrap();
|
||||
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/api/v1/repos/team/demo",
|
||||
status: 200,
|
||||
body: "{}",
|
||||
}]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
assert!(api.repository_private().is_err());
|
||||
handle.join().unwrap();
|
||||
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/api/v1/repos/team/demo/releases/7/assets",
|
||||
status: 200,
|
||||
body: "{}",
|
||||
}]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
assert!(api.upload(7, &[Path::new("missing")], false).is_err());
|
||||
handle.join().unwrap();
|
||||
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/api/v1/repos/team/demo/releases/7/assets",
|
||||
status: 200,
|
||||
body: "[]",
|
||||
}]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
assert!(api.upload(7, &[Path::new("missing")], false).is_err());
|
||||
handle.join().unwrap();
|
||||
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let path = temp.path().join("asset.bin");
|
||||
std::fs::write(&path, b"asset").unwrap();
|
||||
let (host, handle) = server(vec![
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/api/v1/repos/team/demo/releases/7/assets",
|
||||
status: 200,
|
||||
body: "[]",
|
||||
},
|
||||
Reply {
|
||||
method: "POST",
|
||||
path: "/api/v1/repos/team/demo/releases/7/assets?name=asset.bin",
|
||||
status: 401,
|
||||
body: "{}",
|
||||
},
|
||||
]);
|
||||
let api = Gitea::with_token(&format!("{host}/team/demo"), "test-token").unwrap();
|
||||
assert!(api
|
||||
.upload(7, &[&path], false)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("HTTP 401"));
|
||||
handle.join().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,633 @@
|
|||
use crate::{error::Error, gitea::RemoteRelease};
|
||||
use reqwest::{
|
||||
blocking::{Body, Client},
|
||||
header::{HeaderMap, HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE},
|
||||
Method, StatusCode,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, Value};
|
||||
use std::{collections::BTreeMap, fs::File, path::Path, time::Duration};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Asset {
|
||||
id: u64,
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub struct Github {
|
||||
client: Client,
|
||||
base: String,
|
||||
uploads: String,
|
||||
}
|
||||
|
||||
impl Github {
|
||||
pub fn with_token(repository: &str, token: &str) -> Result<Self, Error> {
|
||||
Self::with_origins(
|
||||
repository,
|
||||
token,
|
||||
"https://api.github.com",
|
||||
"https://uploads.github.com",
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn with_origins(
|
||||
repository: &str,
|
||||
token: &str,
|
||||
api_origin: &str,
|
||||
upload_origin: &str,
|
||||
) -> Result<Self, Error> {
|
||||
let parsed = url::Url::parse(repository)
|
||||
.map_err(|_| Error("Invalid GitHub repository URL.".into()))?;
|
||||
if parsed.scheme() != "https" || parsed.host_str() != Some("github.com") {
|
||||
return Err(Error(
|
||||
"GitHub publishing requires an HTTPS github.com repository URL.".into(),
|
||||
));
|
||||
}
|
||||
let parts: Vec<_> = parsed
|
||||
.path_segments()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|part| !part.is_empty())
|
||||
.collect();
|
||||
if parts.len() != 2 {
|
||||
return Err(Error(
|
||||
"GitHub repository URL must end in owner/repository.".into(),
|
||||
));
|
||||
}
|
||||
let repository_path = format!("{}/{}", parts[0], parts[1]);
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
AUTHORIZATION,
|
||||
HeaderValue::from_str(&format!("Bearer {token}"))
|
||||
.map_err(|_| Error("Invalid GitHub token value.".into()))?,
|
||||
);
|
||||
headers.insert(
|
||||
ACCEPT,
|
||||
HeaderValue::from_static("application/vnd.github+json"),
|
||||
);
|
||||
headers.insert(
|
||||
"x-github-api-version",
|
||||
HeaderValue::from_static("2022-11-28"),
|
||||
);
|
||||
let client = Client::builder()
|
||||
.default_headers(headers)
|
||||
.user_agent("rust_realease/0.1")
|
||||
.timeout(Duration::from_secs(120))
|
||||
.redirect(reqwest::redirect::Policy::limited(10))
|
||||
.build()
|
||||
.map_err(|_| Error("Cannot initialize GitHub HTTP client.".into()))?;
|
||||
Ok(Self {
|
||||
client,
|
||||
base: format!("{api_origin}/repos/{repository_path}"),
|
||||
uploads: format!("{upload_origin}/repos/{repository_path}"),
|
||||
})
|
||||
}
|
||||
|
||||
fn request(
|
||||
&self,
|
||||
method: Method,
|
||||
path: &str,
|
||||
body: Option<Value>,
|
||||
missing_ok: bool,
|
||||
) -> Result<Option<Value>, Error> {
|
||||
let mut request = self
|
||||
.client
|
||||
.request(method.clone(), format!("{}{path}", self.base));
|
||||
if let Some(body) = body {
|
||||
request = request.json(&body);
|
||||
}
|
||||
let response = request
|
||||
.send()
|
||||
.map_err(|_| Error(format!("GitHub {method} {path}: connection failed.")))?;
|
||||
if missing_ok && response.status() == StatusCode::NOT_FOUND {
|
||||
return Ok(None);
|
||||
}
|
||||
if !response.status().is_success() {
|
||||
return Err(Error(format!(
|
||||
"GitHub {method} {path}: HTTP {}.",
|
||||
response.status().as_u16()
|
||||
)));
|
||||
}
|
||||
let bytes = response
|
||||
.bytes()
|
||||
.map_err(|_| Error(format!("GitHub {method} {path}: response failed.")))?;
|
||||
if bytes.is_empty() {
|
||||
return Ok(Some(Value::Null));
|
||||
}
|
||||
serde_json::from_slice(&bytes)
|
||||
.map(Some)
|
||||
.map_err(|_| Error(format!("GitHub {method} {path}: invalid JSON response.")))
|
||||
}
|
||||
|
||||
pub fn ensure_release(
|
||||
&self,
|
||||
tag: &str,
|
||||
commit: &str,
|
||||
draft: bool,
|
||||
prerelease: bool,
|
||||
message: Option<&str>,
|
||||
replace_existing_message: bool,
|
||||
) -> Result<RemoteRelease, Error> {
|
||||
let encoded = encode(tag);
|
||||
let reference = self
|
||||
.request(Method::GET, &format!("/git/ref/tags/{encoded}"), None, true)?
|
||||
.ok_or_else(|| {
|
||||
Error(format!(
|
||||
"Remote tag {tag:?} is missing. Push the tag explicitly before publishing."
|
||||
))
|
||||
})?;
|
||||
let mut remote_commit = reference
|
||||
.pointer("/object/sha")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_owned);
|
||||
if reference.pointer("/object/type").and_then(Value::as_str) == Some("tag") {
|
||||
let sha = remote_commit
|
||||
.as_deref()
|
||||
.ok_or_else(|| Error("GitHub tag response is missing its object SHA.".into()))?;
|
||||
let tag_object = self
|
||||
.request(Method::GET, &format!("/git/tags/{sha}"), None, false)?
|
||||
.ok_or_else(|| Error("GitHub annotated tag response is empty.".into()))?;
|
||||
remote_commit = tag_object
|
||||
.pointer("/object/sha")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::to_owned);
|
||||
}
|
||||
if remote_commit.as_deref() != Some(commit) {
|
||||
return Err(Error(
|
||||
"Remote tag commit differs from the checkout; refusing publication.".into(),
|
||||
));
|
||||
}
|
||||
let (release, existed) = if let Some(existing) = self.request(
|
||||
Method::GET,
|
||||
&format!("/releases/tags/{encoded}"),
|
||||
None,
|
||||
true,
|
||||
)? {
|
||||
(existing, true)
|
||||
} else {
|
||||
(
|
||||
self.request(Method::POST, "/releases", Some(json!({"tag_name": tag, "name": tag, "target_commitish": commit, "draft": draft, "prerelease": prerelease, "body": message.unwrap_or_default()})), false)?
|
||||
.ok_or_else(|| Error("GitHub release creation returned no response.".into()))?,
|
||||
false,
|
||||
)
|
||||
};
|
||||
let release: RemoteRelease = serde_json::from_value(release)
|
||||
.map_err(|_| Error("GitHub release response is missing id or draft state.".into()))?;
|
||||
if existed && replace_existing_message {
|
||||
if let Some(message) = message {
|
||||
self.request(
|
||||
Method::PATCH,
|
||||
&format!("/releases/{}", release.id),
|
||||
Some(json!({"body": message})),
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Ok(release)
|
||||
}
|
||||
|
||||
pub fn repository_private(&self) -> Result<bool, Error> {
|
||||
self.request(Method::GET, "", None, false)?
|
||||
.and_then(|repository| repository.get("private").and_then(Value::as_bool))
|
||||
.ok_or_else(|| Error("GitHub repository response is missing visibility.".into()))
|
||||
}
|
||||
|
||||
pub fn download_client(&self) -> &Client {
|
||||
&self.client
|
||||
}
|
||||
|
||||
fn assets(&self, release_id: u64) -> Result<Vec<Asset>, Error> {
|
||||
let response = self
|
||||
.request(
|
||||
Method::GET,
|
||||
&format!("/releases/{release_id}/assets?per_page=100"),
|
||||
None,
|
||||
false,
|
||||
)?
|
||||
.ok_or_else(|| Error("GitHub assets response is empty.".into()))?;
|
||||
serde_json::from_value(response)
|
||||
.map_err(|_| Error("GitHub assets response is invalid.".into()))
|
||||
}
|
||||
|
||||
pub fn asset_urls(&self, release_id: u64) -> Result<BTreeMap<String, String>, Error> {
|
||||
Ok(self
|
||||
.assets(release_id)?
|
||||
.into_iter()
|
||||
.map(|asset| {
|
||||
(
|
||||
asset.name,
|
||||
format!("{}/releases/assets/{}", self.base, asset.id),
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn upload(&self, release_id: u64, paths: &[&Path], replace: bool) -> Result<(), Error> {
|
||||
let existing = self.assets(release_id)?;
|
||||
for path in paths {
|
||||
let name = path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.ok_or_else(|| Error("Invalid upload filename.".into()))?;
|
||||
let collisions: Vec<_> = existing.iter().filter(|asset| asset.name == name).collect();
|
||||
if !collisions.is_empty() && !replace {
|
||||
return Err(Error(format!(
|
||||
"Release asset already exists: {name}. Use --replace-assets to replace it explicitly."
|
||||
)));
|
||||
}
|
||||
for asset in collisions {
|
||||
self.request(
|
||||
Method::DELETE,
|
||||
&format!("/releases/assets/{}", asset.id),
|
||||
None,
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
let input = File::open(path)
|
||||
.map_err(|e| Error(format!("cannot open asset {}: {e}", path.display())))?;
|
||||
let size = input
|
||||
.metadata()
|
||||
.map_err(|e| Error(format!("cannot inspect asset {}: {e}", path.display())))?
|
||||
.len();
|
||||
let mut url =
|
||||
url::Url::parse(&format!("{}/releases/{release_id}/assets", self.uploads))
|
||||
.map_err(|_| Error("Invalid GitHub upload URL.".into()))?;
|
||||
url.query_pairs_mut().append_pair("name", name);
|
||||
let response = self
|
||||
.client
|
||||
.post(url)
|
||||
.header(CONTENT_TYPE, "application/octet-stream")
|
||||
.header(CONTENT_LENGTH, size)
|
||||
.body(Body::new(input))
|
||||
.send()
|
||||
.map_err(|_| {
|
||||
Error(format!(
|
||||
"GitHub POST /releases/{release_id}/assets: connection failed."
|
||||
))
|
||||
})?;
|
||||
if !response.status().is_success() {
|
||||
return Err(Error(format!(
|
||||
"GitHub POST /releases/{release_id}/assets: HTTP {}.",
|
||||
response.status().as_u16()
|
||||
)));
|
||||
}
|
||||
println!("Uploaded {name}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn encode(segment: &str) -> String {
|
||||
url::form_urlencoded::byte_serialize(segment.as_bytes()).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::{
|
||||
io::{Read, Write},
|
||||
net::TcpListener,
|
||||
thread,
|
||||
};
|
||||
|
||||
struct Reply {
|
||||
method: &'static str,
|
||||
path: &'static str,
|
||||
status: u16,
|
||||
body: &'static str,
|
||||
}
|
||||
|
||||
fn server(replies: Vec<Reply>) -> (String, thread::JoinHandle<Vec<Vec<u8>>>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let address = format!("http://{}", listener.local_addr().unwrap());
|
||||
let handle = thread::spawn(move || {
|
||||
let mut bodies = Vec::new();
|
||||
for reply in replies {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut bytes = Vec::new();
|
||||
let mut buffer = [0_u8; 8192];
|
||||
let header_end = loop {
|
||||
let count = stream.read(&mut buffer).unwrap();
|
||||
bytes.extend_from_slice(&buffer[..count]);
|
||||
if let Some(index) = bytes.windows(4).position(|part| part == b"\r\n\r\n") {
|
||||
break index + 4;
|
||||
}
|
||||
};
|
||||
let headers = String::from_utf8_lossy(&bytes[..header_end]);
|
||||
let request_line = headers.lines().next().unwrap();
|
||||
assert_eq!(
|
||||
request_line,
|
||||
format!("{} {} HTTP/1.1", reply.method, reply.path)
|
||||
);
|
||||
assert!(headers
|
||||
.lines()
|
||||
.any(|line| line.eq_ignore_ascii_case("authorization: Bearer test-token")));
|
||||
assert!(headers.lines().any(|line| {
|
||||
line.eq_ignore_ascii_case("accept: application/vnd.github+json")
|
||||
}));
|
||||
let length = headers
|
||||
.lines()
|
||||
.find_map(|line| {
|
||||
line.to_ascii_lowercase()
|
||||
.strip_prefix("content-length: ")
|
||||
.map(str::to_owned)
|
||||
})
|
||||
.and_then(|value| value.parse::<usize>().ok())
|
||||
.unwrap_or(0);
|
||||
while bytes.len() < header_end + length {
|
||||
let count = stream.read(&mut buffer).unwrap();
|
||||
bytes.extend_from_slice(&buffer[..count]);
|
||||
}
|
||||
bodies.push(bytes[header_end..header_end + length].to_vec());
|
||||
write!(
|
||||
stream,
|
||||
"HTTP/1.1 {} OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
|
||||
reply.status,
|
||||
reply.body.len(),
|
||||
reply.body
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
bodies
|
||||
});
|
||||
(address, handle)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creates_release_and_uploads_raw_github_asset() {
|
||||
let (host, handle) = server(vec![
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/git/ref/tags/v1.0.0",
|
||||
status: 200,
|
||||
body: r#"{"object":{"type":"commit","sha":"abc"}}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/tags/v1.0.0",
|
||||
status: 404,
|
||||
body: "{}",
|
||||
},
|
||||
Reply {
|
||||
method: "POST",
|
||||
path: "/repos/team/demo/releases",
|
||||
status: 201,
|
||||
body: r#"{"id":7,"draft":false}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/7/assets?per_page=100",
|
||||
status: 200,
|
||||
body: "[]",
|
||||
},
|
||||
Reply {
|
||||
method: "POST",
|
||||
path: "/repos/team/demo/releases/7/assets?name=asset.bin",
|
||||
status: 201,
|
||||
body: "{}",
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/7/assets?per_page=100",
|
||||
status: 200,
|
||||
body: r#"[{"id":11,"name":"asset.bin"}]"#,
|
||||
},
|
||||
]);
|
||||
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
let release = api
|
||||
.ensure_release("v1.0.0", "abc", false, false, Some("notes"), false)
|
||||
.unwrap();
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let asset = temp.path().join("asset.bin");
|
||||
std::fs::write(&asset, b"asset bytes").unwrap();
|
||||
api.upload(release.id, &[asset.as_path()], false).unwrap();
|
||||
assert_eq!(
|
||||
api.asset_urls(release.id)
|
||||
.unwrap()
|
||||
.get("asset.bin")
|
||||
.map(String::as_str),
|
||||
Some(format!("{host}/repos/team/demo/releases/assets/11").as_str())
|
||||
);
|
||||
let bodies = handle.join().unwrap();
|
||||
assert!(String::from_utf8_lossy(&bodies[2]).contains("\"tag_name\":\"v1.0.0\""));
|
||||
assert_eq!(bodies[4], b"asset bytes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_github_and_malformed_repository_urls() {
|
||||
for repository in [
|
||||
"http://github.com/team/demo",
|
||||
"https://example.com/team/demo",
|
||||
"https://github.com/team/demo/extra",
|
||||
] {
|
||||
assert!(Github::with_token(repository, "token").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn updates_existing_annotated_release_and_replaces_colliding_asset() {
|
||||
let (host, handle) = server(vec![
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/git/ref/tags/v1.0.0",
|
||||
status: 200,
|
||||
body: r#"{"object":{"type":"tag","sha":"tag-object"}}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/git/tags/tag-object",
|
||||
status: 200,
|
||||
body: r#"{"object":{"sha":"abc"}}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/tags/v1.0.0",
|
||||
status: 200,
|
||||
body: r#"{"id":7,"draft":false}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "PATCH",
|
||||
path: "/repos/team/demo/releases/7",
|
||||
status: 200,
|
||||
body: "",
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo",
|
||||
status: 200,
|
||||
body: r#"{"private":true}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/7/assets?per_page=100",
|
||||
status: 200,
|
||||
body: r#"[{"id":11,"name":"asset.bin"}]"#,
|
||||
},
|
||||
Reply {
|
||||
method: "DELETE",
|
||||
path: "/repos/team/demo/releases/assets/11",
|
||||
status: 204,
|
||||
body: "",
|
||||
},
|
||||
Reply {
|
||||
method: "POST",
|
||||
path: "/repos/team/demo/releases/7/assets?name=asset.bin",
|
||||
status: 201,
|
||||
body: "{}",
|
||||
},
|
||||
]);
|
||||
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
let release = api
|
||||
.ensure_release("v1.0.0", "abc", false, false, Some("new notes"), true)
|
||||
.unwrap();
|
||||
assert_eq!(release.id, 7);
|
||||
assert!(api.repository_private().unwrap());
|
||||
let _ = api.download_client();
|
||||
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let asset = temp.path().join("asset.bin");
|
||||
std::fs::write(&asset, b"replacement").unwrap();
|
||||
api.upload(release.id, &[asset.as_path()], true).unwrap();
|
||||
|
||||
let bodies = handle.join().unwrap();
|
||||
assert!(String::from_utf8_lossy(&bodies[3]).contains("new notes"));
|
||||
assert_eq!(bodies[7], b"replacement");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_missing_mismatched_and_malformed_github_responses() {
|
||||
let cases = [
|
||||
(404, "{}", "missing"),
|
||||
(
|
||||
200,
|
||||
r#"{"object":{"type":"commit","sha":"other"}}"#,
|
||||
"differs",
|
||||
),
|
||||
(200, "not json", "invalid JSON"),
|
||||
(500, "{}", "HTTP 500"),
|
||||
];
|
||||
for (status, body, expected) in cases {
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/git/ref/tags/v1.0.0",
|
||||
status,
|
||||
body,
|
||||
}]);
|
||||
let api =
|
||||
Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
let error = api
|
||||
.ensure_release("v1.0.0", "abc", false, false, None, false)
|
||||
.unwrap_err();
|
||||
assert!(error.to_string().contains(expected), "{error}");
|
||||
handle.join().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_malformed_release_repository_and_asset_responses() {
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/git/ref/tags/v1.0.0",
|
||||
status: 200,
|
||||
body: r#"{"object":{"type":"tag"}}"#,
|
||||
}]);
|
||||
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
assert!(api
|
||||
.ensure_release("v1.0.0", "abc", false, false, None, false)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("object SHA"));
|
||||
handle.join().unwrap();
|
||||
|
||||
let (host, handle) = server(vec![
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/git/ref/tags/v1.0.0",
|
||||
status: 200,
|
||||
body: r#"{"object":{"type":"commit","sha":"abc"}}"#,
|
||||
},
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/tags/v1.0.0",
|
||||
status: 200,
|
||||
body: "{}",
|
||||
},
|
||||
]);
|
||||
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
assert!(api
|
||||
.ensure_release("v1.0.0", "abc", false, false, None, false)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("missing id"));
|
||||
handle.join().unwrap();
|
||||
|
||||
for (path, operation, expected) in [
|
||||
("/repos/team/demo", "repository", "visibility"),
|
||||
(
|
||||
"/repos/team/demo/releases/7/assets?per_page=100",
|
||||
"assets",
|
||||
"invalid",
|
||||
),
|
||||
] {
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path,
|
||||
status: 200,
|
||||
body: "{}",
|
||||
}]);
|
||||
let api =
|
||||
Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
let error = if operation == "repository" {
|
||||
api.repository_private().unwrap_err()
|
||||
} else {
|
||||
api.asset_urls(7).unwrap_err()
|
||||
};
|
||||
assert!(error.to_string().contains(expected), "{error}");
|
||||
handle.join().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_asset_collision_and_reports_upload_http_failure() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let asset = temp.path().join("asset.bin");
|
||||
std::fs::write(&asset, b"asset").unwrap();
|
||||
|
||||
let (host, handle) = server(vec![Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/7/assets?per_page=100",
|
||||
status: 200,
|
||||
body: r#"[{"id":11,"name":"asset.bin"}]"#,
|
||||
}]);
|
||||
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
assert!(api.upload(7, &[asset.as_path()], false).is_err());
|
||||
handle.join().unwrap();
|
||||
|
||||
let (host, handle) = server(vec![
|
||||
Reply {
|
||||
method: "GET",
|
||||
path: "/repos/team/demo/releases/7/assets?per_page=100",
|
||||
status: 200,
|
||||
body: "[]",
|
||||
},
|
||||
Reply {
|
||||
method: "POST",
|
||||
path: "/repos/team/demo/releases/7/assets?name=asset.bin",
|
||||
status: 500,
|
||||
body: "{}",
|
||||
},
|
||||
]);
|
||||
let api = Github::with_origins("https://github.com/team/demo", "test-token", &host, &host)
|
||||
.unwrap();
|
||||
let error = api.upload(7, &[asset.as_path()], false).unwrap_err();
|
||||
assert!(error.to_string().contains("HTTP 500"), "{error}");
|
||||
handle.join().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,192 @@
|
|||
use crate::error::Error;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
const MH_MAGIC_64_LE: [u8; 4] = [0xcf, 0xfa, 0xed, 0xfe];
|
||||
const LC_LOAD_DYLIB: u32 = 0xc;
|
||||
const LC_LOAD_WEAK_DYLIB: u32 = 0x8000_0018;
|
||||
const LC_REEXPORT_DYLIB: u32 = 0x8000_001f;
|
||||
const LC_LOAD_UPWARD_DYLIB: u32 = 0x8000_0023;
|
||||
const LC_BUILD_VERSION: u32 = 0x32;
|
||||
|
||||
pub fn verify_path(path: &Path, target: &str, minimum: &str) -> Result<(), Error> {
|
||||
let bytes = fs::read(path)
|
||||
.map_err(|e| Error(format!("cannot inspect Mach-O {}: {e}", path.display())))?;
|
||||
verify(&bytes, target, minimum).map_err(|e| Error(format!("{}: {e}", path.display())))
|
||||
}
|
||||
|
||||
pub fn verify(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
|
||||
verify_policy(bytes, target, minimum, true)
|
||||
}
|
||||
|
||||
/// Bundled runtime tools may support an older macOS version than the application.
|
||||
pub fn verify_compatible(bytes: &[u8], target: &str, minimum: &str) -> Result<(), Error> {
|
||||
verify_policy(bytes, target, minimum, false)
|
||||
}
|
||||
|
||||
fn verify_policy(bytes: &[u8], target: &str, minimum: &str, exact: bool) -> Result<(), Error> {
|
||||
if bytes.len() < 32 || bytes[..4] != MH_MAGIC_64_LE {
|
||||
return Err(Error(
|
||||
"expected a little-endian 64-bit Mach-O executable".into(),
|
||||
));
|
||||
}
|
||||
let expected_cpu = match target {
|
||||
"aarch64-apple-darwin" => 0x0100_000c,
|
||||
"x86_64-apple-darwin" => 0x0100_0007,
|
||||
_ => return Err(Error(format!("unsupported Apple target {target}"))),
|
||||
};
|
||||
if word(bytes, 4)? != expected_cpu || word(bytes, 12)? != 2 {
|
||||
return Err(Error(format!(
|
||||
"Mach-O architecture or file type does not match {target}"
|
||||
)));
|
||||
}
|
||||
let commands = word(bytes, 16)? as usize;
|
||||
let command_bytes = word(bytes, 20)? as usize;
|
||||
let end = 32usize
|
||||
.checked_add(command_bytes)
|
||||
.ok_or_else(|| Error("Mach-O load command size overflow".into()))?;
|
||||
if end > bytes.len() {
|
||||
return Err(Error("truncated Mach-O load commands".into()));
|
||||
}
|
||||
let expected_minimum = parse_version(minimum)?;
|
||||
let mut offset = 32usize;
|
||||
let mut found_minimum = None;
|
||||
let mut dependencies = Vec::new();
|
||||
for _ in 0..commands {
|
||||
if offset.checked_add(8).is_none_or(|header| header > end) {
|
||||
return Err(Error("truncated Mach-O load command".into()));
|
||||
}
|
||||
let command = word(bytes, offset)?;
|
||||
let size = word(bytes, offset + 4)? as usize;
|
||||
let command_end = offset
|
||||
.checked_add(size)
|
||||
.ok_or_else(|| Error("Mach-O load command overflow".into()))?;
|
||||
if size < 8 || command_end > end {
|
||||
return Err(Error("invalid Mach-O load command size".into()));
|
||||
}
|
||||
if command == LC_BUILD_VERSION {
|
||||
if size < 24 || word(bytes, offset + 8)? != 1 {
|
||||
return Err(Error("invalid or non-macOS LC_BUILD_VERSION".into()));
|
||||
}
|
||||
found_minimum = Some(word(bytes, offset + 12)?);
|
||||
} else if [
|
||||
LC_LOAD_DYLIB,
|
||||
LC_LOAD_WEAK_DYLIB,
|
||||
LC_REEXPORT_DYLIB,
|
||||
LC_LOAD_UPWARD_DYLIB,
|
||||
]
|
||||
.contains(&command)
|
||||
{
|
||||
if size < 24 {
|
||||
return Err(Error("invalid Mach-O dylib command".into()));
|
||||
}
|
||||
let name_offset = word(bytes, offset + 8)? as usize;
|
||||
if name_offset < 24 || name_offset >= size {
|
||||
return Err(Error("invalid Mach-O dylib name offset".into()));
|
||||
}
|
||||
let name = c_string(&bytes[offset + name_offset..command_end])?;
|
||||
if !name.starts_with("/usr/lib/") && !name.starts_with("/System/Library/") {
|
||||
return Err(Error(format!("non-system Mach-O dependency: {name}")));
|
||||
}
|
||||
dependencies.push(name.to_owned());
|
||||
}
|
||||
offset = command_end;
|
||||
}
|
||||
if offset != end {
|
||||
return Err(Error("Mach-O load command count/size mismatch".into()));
|
||||
}
|
||||
let found = found_minimum.ok_or_else(|| Error("Mach-O has no LC_BUILD_VERSION".into()))?;
|
||||
if (exact && found != expected_minimum) || (!exact && found > expected_minimum) {
|
||||
return Err(Error(format!(
|
||||
"Mach-O deployment minimum {} is incompatible with configured {minimum}",
|
||||
display_version(found)
|
||||
)));
|
||||
}
|
||||
if dependencies.is_empty() {
|
||||
return Err(Error(
|
||||
"Mach-O declares no dynamic system dependencies".into(),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn word(bytes: &[u8], offset: usize) -> Result<u32, Error> {
|
||||
let value = bytes
|
||||
.get(offset..offset + 4)
|
||||
.ok_or_else(|| Error("truncated Mach-O field".into()))?;
|
||||
Ok(u32::from_le_bytes(value.try_into().unwrap()))
|
||||
}
|
||||
|
||||
fn c_string(bytes: &[u8]) -> Result<&str, Error> {
|
||||
let end = bytes
|
||||
.iter()
|
||||
.position(|byte| *byte == 0)
|
||||
.ok_or_else(|| Error("unterminated Mach-O dylib name".into()))?;
|
||||
std::str::from_utf8(&bytes[..end]).map_err(|_| Error("non-UTF-8 Mach-O dylib name".into()))
|
||||
}
|
||||
|
||||
fn parse_version(value: &str) -> Result<u32, Error> {
|
||||
let mut fields = value.split('.');
|
||||
let major = fields.next().and_then(|part| part.parse::<u32>().ok());
|
||||
let minor = fields.next().and_then(|part| part.parse::<u32>().ok());
|
||||
let patch = fields
|
||||
.next()
|
||||
.map_or(Some(0), |part| part.parse::<u32>().ok());
|
||||
match (major, minor, patch, fields.next()) {
|
||||
(Some(major), Some(minor), Some(patch), None)
|
||||
if major < 65536 && minor < 256 && patch < 256 =>
|
||||
{
|
||||
Ok((major << 16) | (minor << 8) | patch)
|
||||
}
|
||||
_ => Err(Error(format!("invalid macOS deployment version {value:?}"))),
|
||||
}
|
||||
}
|
||||
|
||||
fn display_version(value: u32) -> String {
|
||||
format!("{}.{}.{}", value >> 16, (value >> 8) & 0xff, value & 0xff)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn fixture(cpu: u32, minimum: u32, dependency: &str) -> Vec<u8> {
|
||||
let name_size = dependency.len() + 1;
|
||||
let dylib_size = (24 + name_size).div_ceil(8) * 8;
|
||||
let mut bytes = vec![0_u8; 32 + 24 + dylib_size];
|
||||
bytes[..4].copy_from_slice(&MH_MAGIC_64_LE);
|
||||
bytes[4..8].copy_from_slice(&cpu.to_le_bytes());
|
||||
bytes[12..16].copy_from_slice(&2_u32.to_le_bytes());
|
||||
bytes[16..20].copy_from_slice(&2_u32.to_le_bytes());
|
||||
bytes[20..24].copy_from_slice(&(24_u32 + dylib_size as u32).to_le_bytes());
|
||||
bytes[32..36].copy_from_slice(&LC_BUILD_VERSION.to_le_bytes());
|
||||
bytes[36..40].copy_from_slice(&24_u32.to_le_bytes());
|
||||
bytes[40..44].copy_from_slice(&1_u32.to_le_bytes());
|
||||
bytes[44..48].copy_from_slice(&minimum.to_le_bytes());
|
||||
let offset = 56;
|
||||
bytes[offset..offset + 4].copy_from_slice(&LC_LOAD_DYLIB.to_le_bytes());
|
||||
bytes[offset + 4..offset + 8].copy_from_slice(&(dylib_size as u32).to_le_bytes());
|
||||
bytes[offset + 8..offset + 12].copy_from_slice(&24_u32.to_le_bytes());
|
||||
bytes[offset + 24..offset + 24 + dependency.len()].copy_from_slice(dependency.as_bytes());
|
||||
bytes
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_architecture_deployment_minimum_and_system_dependencies() {
|
||||
let arm = fixture(0x0100_000c, 13 << 16, "/usr/lib/libSystem.B.dylib");
|
||||
verify(&arm, "aarch64-apple-darwin", "13.0").unwrap();
|
||||
verify_compatible(&arm, "aarch64-apple-darwin", "14.0").unwrap();
|
||||
assert!(verify_compatible(&arm, "aarch64-apple-darwin", "12.0").is_err());
|
||||
assert!(verify(&arm, "x86_64-apple-darwin", "13.0").is_err());
|
||||
assert!(verify(&arm, "aarch64-apple-darwin", "14.0").is_err());
|
||||
|
||||
let intel = fixture(
|
||||
0x0100_0007,
|
||||
(13 << 16) | (2 << 8),
|
||||
"/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit",
|
||||
);
|
||||
verify(&intel, "x86_64-apple-darwin", "13.2").unwrap();
|
||||
let unsafe_dependency = fixture(0x0100_0007, 13 << 16, "/tmp/libInjected.dylib");
|
||||
assert!(verify(&unsafe_dependency, "x86_64-apple-darwin", "13.0").is_err());
|
||||
assert!(verify(&intel[..40], "x86_64-apple-darwin", "13.2").is_err());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +1,20 @@
|
|||
//! Entry point for the incremental Rust replacement.
|
||||
|
||||
mod build;
|
||||
mod bundle;
|
||||
mod cli;
|
||||
mod config;
|
||||
mod error;
|
||||
mod gitea;
|
||||
mod github;
|
||||
mod macho;
|
||||
mod manifest;
|
||||
mod model;
|
||||
mod package;
|
||||
mod policy;
|
||||
mod prepare;
|
||||
mod project;
|
||||
mod release;
|
||||
mod runner;
|
||||
mod ui;
|
||||
mod verify;
|
||||
|
|
|
|||
|
|
@ -17,7 +17,15 @@ pub struct Artifact {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ArtifactContents {
|
||||
Archive { root: String, binaries: Vec<String> },
|
||||
Bundle {
|
||||
root: String,
|
||||
format: crate::bundle::Format,
|
||||
entries: Vec<crate::bundle::Entry>,
|
||||
},
|
||||
Archive {
|
||||
root: String,
|
||||
binaries: Vec<String>,
|
||||
},
|
||||
Binary,
|
||||
}
|
||||
|
||||
|
|
@ -345,4 +353,48 @@ mod tests {
|
|||
assert!(package(temp.path(), "demo", "0.1.0", &outputs, PackageFormat::Tgz,).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_non_file_and_oversized_inputs() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let missing = temp.path().join("missing");
|
||||
let outputs = [TargetOutputs {
|
||||
target: "x86_64-unknown-linux-musl".into(),
|
||||
binaries: vec![("demo".into(), missing.clone())],
|
||||
}];
|
||||
assert!(
|
||||
package(temp.path(), "demo", "0.1.0", &outputs, PackageFormat::Tgz)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("cannot inspect binary")
|
||||
);
|
||||
assert!(checksum(&missing)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("cannot open"));
|
||||
|
||||
let directory = temp.path().join("directory");
|
||||
fs::create_dir(&directory).unwrap();
|
||||
let outputs = [TargetOutputs {
|
||||
target: "x86_64-unknown-linux-musl".into(),
|
||||
binaries: vec![("demo".into(), directory)],
|
||||
}];
|
||||
assert!(
|
||||
package(temp.path(), "demo", "0.1.0", &outputs, PackageFormat::Tgz)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("regular binary")
|
||||
);
|
||||
|
||||
let mut output = Vec::new();
|
||||
let long_name = format!("{}/{}", "p".repeat(156), "name");
|
||||
assert!(write_header(&mut output, &long_name, 0o755, 0, b'0')
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("USTAR limits"));
|
||||
assert!(octal(&mut [0_u8; 2], u64::MAX)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("Tar field"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,11 @@ pub fn binary_filename(binary: &str, target: &str) -> String {
|
|||
}
|
||||
|
||||
pub fn binary_asset_name(binary: &str, target: &str, version: &str) -> String {
|
||||
binary_filename(&format!("{binary}-{target}-v{version}"), target)
|
||||
if target.ends_with("-apple-darwin") {
|
||||
binary_filename(&format!("{binary}-{target}"), target)
|
||||
} else {
|
||||
binary_filename(&format!("{binary}-{target}-v{version}"), target)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn binstall_metadata(prefix: &str) -> [(&'static str, String); 3] {
|
||||
|
|
@ -52,6 +56,10 @@ mod tests {
|
|||
binary_asset_name("demo", "x86_64-pc-windows-gnu", "0.3.1"),
|
||||
"demo-x86_64-pc-windows-gnu-v0.3.1.exe"
|
||||
);
|
||||
assert_eq!(
|
||||
binary_asset_name("demo", "aarch64-apple-darwin", "0.3.1"),
|
||||
"demo-aarch64-apple-darwin"
|
||||
);
|
||||
let metadata = binstall_metadata("release-");
|
||||
assert_eq!(
|
||||
metadata[0].1,
|
||||
|
|
|
|||
113
src/prepare.rs
113
src/prepare.rs
|
|
@ -15,6 +15,7 @@ use toml_edit::{value, Array, DocumentMut, Item, Table};
|
|||
pub struct Options<'a> {
|
||||
pub tag: &'a str,
|
||||
pub image_tag: Option<&'a str>,
|
||||
pub token: Option<&'a str>,
|
||||
pub dry_run: bool,
|
||||
pub commit: bool,
|
||||
pub create_tag: bool,
|
||||
|
|
@ -354,7 +355,15 @@ pub fn run(
|
|||
project::validate_checkout_tag(root, options.tag, runner)?;
|
||||
}
|
||||
if let Some(branch) = branch {
|
||||
push_preparation(root, &branch, options.tag, repository, config, runner)?;
|
||||
push_preparation(
|
||||
root,
|
||||
&branch,
|
||||
options.tag,
|
||||
repository,
|
||||
config,
|
||||
options.token,
|
||||
runner,
|
||||
)?;
|
||||
}
|
||||
println!("Repository preparation complete. Review the changes, then run the publisher without --prepare-repo.");
|
||||
Ok(())
|
||||
|
|
@ -366,6 +375,7 @@ fn push_preparation(
|
|||
tag: &str,
|
||||
repository: &str,
|
||||
config: &Config,
|
||||
token: Option<&str>,
|
||||
runner: &Runner,
|
||||
) -> Result<(), Error> {
|
||||
let origin = runner.text("git", &["remote", "get-url", "--push", "origin"], root)?;
|
||||
|
|
@ -375,50 +385,53 @@ fn push_preparation(
|
|||
let branch_ref = format!("HEAD:refs/heads/{branch}");
|
||||
let tag_ref = format!("refs/tags/{tag}");
|
||||
if origin.starts_with("https://") {
|
||||
if let Ok(token) = std::env::var(&config.token_env) {
|
||||
if !token.is_empty() {
|
||||
let helper = tempfile::Builder::new()
|
||||
.prefix("rust-release-askpass-")
|
||||
.tempdir()
|
||||
.map_err(|e| Error(format!("cannot create askpass helper: {e}")))?;
|
||||
let script = helper.path().join("askpass");
|
||||
fs::write(&script, "#!/bin/sh\ncase \"$1\" in *sername*) printf 'token\\n';; *) printf '%s\\n' \"$PUBLISHER_GIT_TOKEN\";; esac\n").map_err(|e| Error(format!("cannot write askpass helper: {e}")))?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(&script, fs::Permissions::from_mode(0o700))
|
||||
.map_err(|e| Error(format!("cannot chmod askpass helper: {e}")))?;
|
||||
}
|
||||
let helper_path = script
|
||||
.to_str()
|
||||
.ok_or_else(|| Error("Askpass helper path is not UTF-8.".into()))?;
|
||||
return runner
|
||||
.output_with(
|
||||
"git",
|
||||
&[
|
||||
"-c",
|
||||
"credential.helper=",
|
||||
"-c",
|
||||
"core.hooksPath=/dev/null",
|
||||
"push",
|
||||
"--atomic",
|
||||
"origin",
|
||||
&branch_ref,
|
||||
&tag_ref,
|
||||
],
|
||||
root,
|
||||
&[
|
||||
("GIT_ASKPASS", helper_path),
|
||||
("GIT_TERMINAL_PROMPT", "0"),
|
||||
("PUBLISHER_GIT_TOKEN", &token),
|
||||
("GIT_TRACE", "0"),
|
||||
("GIT_TRACE_CURL", "0"),
|
||||
("GIT_TRACE2", "0"),
|
||||
],
|
||||
&[],
|
||||
)
|
||||
.map(|_| ());
|
||||
let environment_token = std::env::var(&config.token_env).ok();
|
||||
if let Some(token) = token.or_else(|| {
|
||||
environment_token
|
||||
.as_deref()
|
||||
.filter(|token| !token.is_empty())
|
||||
}) {
|
||||
let helper = tempfile::Builder::new()
|
||||
.prefix("rust-release-askpass-")
|
||||
.tempdir()
|
||||
.map_err(|e| Error(format!("cannot create askpass helper: {e}")))?;
|
||||
let script = helper.path().join("askpass");
|
||||
fs::write(&script, "#!/bin/sh\ncase \"$1\" in *sername*) printf 'token\\n';; *) printf '%s\\n' \"$PUBLISHER_GIT_TOKEN\";; esac\n").map_err(|e| Error(format!("cannot write askpass helper: {e}")))?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::set_permissions(&script, fs::Permissions::from_mode(0o700))
|
||||
.map_err(|e| Error(format!("cannot chmod askpass helper: {e}")))?;
|
||||
}
|
||||
let helper_path = script
|
||||
.to_str()
|
||||
.ok_or_else(|| Error("Askpass helper path is not UTF-8.".into()))?;
|
||||
return runner
|
||||
.output_with(
|
||||
"git",
|
||||
&[
|
||||
"-c",
|
||||
"credential.helper=",
|
||||
"-c",
|
||||
"core.hooksPath=/dev/null",
|
||||
"push",
|
||||
"--atomic",
|
||||
"origin",
|
||||
&branch_ref,
|
||||
&tag_ref,
|
||||
],
|
||||
root,
|
||||
&[
|
||||
("GIT_ASKPASS", helper_path),
|
||||
("GIT_TERMINAL_PROMPT", "0"),
|
||||
("PUBLISHER_GIT_TOKEN", token),
|
||||
("GIT_TRACE", "0"),
|
||||
("GIT_TRACE_CURL", "0"),
|
||||
("GIT_TRACE2", "0"),
|
||||
],
|
||||
&[],
|
||||
)
|
||||
.map(|_| ());
|
||||
}
|
||||
}
|
||||
runner.output_with(
|
||||
|
|
@ -460,11 +473,15 @@ mod tests {
|
|||
let runner = Runner::new("GITEA_TOKEN");
|
||||
let project = project::inspect(root.to_str().unwrap(), None, &runner).unwrap();
|
||||
let config = Config {
|
||||
features: Vec::new(),
|
||||
apple: crate::config::AppleConfig::default(),
|
||||
bundles: Vec::new(),
|
||||
targets: vec!["x86_64-unknown-linux-musl".into()],
|
||||
profile: "release".into(),
|
||||
locked: true,
|
||||
tag_prefix: "v".into(),
|
||||
token_env: "GITEA_TOKEN".into(),
|
||||
release_endpoints: Vec::new(),
|
||||
package_format: crate::config::PackageFormat::Tgz,
|
||||
repository_fallbacks: Vec::new(),
|
||||
};
|
||||
|
|
@ -521,11 +538,15 @@ mod tests {
|
|||
.unwrap();
|
||||
let project = project::inspect(root.to_str().unwrap(), None, &runner).unwrap();
|
||||
let config = Config {
|
||||
features: Vec::new(),
|
||||
apple: crate::config::AppleConfig::default(),
|
||||
bundles: Vec::new(),
|
||||
targets: vec!["x86_64-unknown-linux-musl".into()],
|
||||
profile: "release".into(),
|
||||
locked: true,
|
||||
tag_prefix: "v".into(),
|
||||
token_env: "GITEA_TOKEN".into(),
|
||||
release_endpoints: Vec::new(),
|
||||
package_format: crate::config::PackageFormat::Tgz,
|
||||
repository_fallbacks: Vec::new(),
|
||||
};
|
||||
|
|
@ -536,6 +557,7 @@ mod tests {
|
|||
Options {
|
||||
tag: "v0.1.0",
|
||||
image_tag: Some("main"),
|
||||
token: None,
|
||||
dry_run,
|
||||
commit,
|
||||
create_tag,
|
||||
|
|
@ -703,6 +725,7 @@ mod tests {
|
|||
let options = Options {
|
||||
tag: "v0.1.0",
|
||||
image_tag: None,
|
||||
token: None,
|
||||
dry_run: true,
|
||||
commit: true,
|
||||
create_tag: true,
|
||||
|
|
@ -753,6 +776,7 @@ mod tests {
|
|||
Options {
|
||||
tag: "v0.1.0",
|
||||
image_tag: None,
|
||||
token: None,
|
||||
dry_run: true,
|
||||
commit: true,
|
||||
create_tag: true,
|
||||
|
|
@ -811,6 +835,7 @@ mod tests {
|
|||
Options {
|
||||
tag: "v0.1.0",
|
||||
image_tag: None,
|
||||
token: None,
|
||||
dry_run: false,
|
||||
commit: true,
|
||||
create_tag: true,
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use std::{
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct SelectedPackage {
|
||||
pub workspace_root: PathBuf,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub binaries: Vec<String>,
|
||||
|
|
@ -220,6 +221,7 @@ pub fn inspect(
|
|||
));
|
||||
}
|
||||
Ok(SelectedPackage {
|
||||
workspace_root,
|
||||
name: selected.name,
|
||||
version: selected.version,
|
||||
binaries: binaries.iter().map(|binary| binary.name.clone()).collect(),
|
||||
|
|
@ -448,15 +450,44 @@ mod tests {
|
|||
normalize_repository("https://example.com/gitea/owner/demo.git").unwrap(),
|
||||
"https://example.com/gitea/owner/demo"
|
||||
);
|
||||
assert_eq!(
|
||||
normalize_repository("https://example.com:8443/owner/demo").unwrap(),
|
||||
"https://example.com:8443/owner/demo"
|
||||
);
|
||||
for value in [
|
||||
"https://token@example.com/owner/demo",
|
||||
"https://example.com/owner/demo?token=secret",
|
||||
"https://example.com/owner/../demo",
|
||||
"ftp://example.com/owner/demo",
|
||||
"not a repository URL",
|
||||
"git@:owner/demo",
|
||||
"https://example.com/owner/.git",
|
||||
] {
|
||||
assert!(normalize_repository(value).is_err(), "{value}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_source_and_inspection_errors_are_explicit() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let file = temp.path().join("not-a-directory");
|
||||
fs::write(&file, "content").unwrap();
|
||||
let runner = Runner::new("GITEA_TOKEN");
|
||||
assert!(
|
||||
acquire_project(file.to_str().unwrap(), None, false, &runner)
|
||||
.err()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.contains("must be a directory")
|
||||
);
|
||||
assert!(
|
||||
inspect(temp.path().join("missing").to_str().unwrap(), None, &runner,)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("requires a local project")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_source_is_retained_and_clone_checks_out_exact_tag() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,83 @@
|
|||
use crate::{
|
||||
config::{ReleaseEndpoint, ReleaseProvider},
|
||||
error::Error,
|
||||
gitea::{Gitea, RemoteRelease},
|
||||
github::Github,
|
||||
};
|
||||
use reqwest::blocking::Client;
|
||||
use std::{collections::BTreeMap, path::Path};
|
||||
|
||||
pub enum ReleaseClient {
|
||||
Gitea(Gitea),
|
||||
Github(Github),
|
||||
}
|
||||
|
||||
impl ReleaseClient {
|
||||
pub fn new(endpoint: &ReleaseEndpoint, token: &str) -> Result<Self, Error> {
|
||||
match endpoint.provider {
|
||||
ReleaseProvider::Gitea => {
|
||||
Gitea::with_token(&endpoint.repository, token).map(Self::Gitea)
|
||||
}
|
||||
ReleaseProvider::Github => {
|
||||
Github::with_token(&endpoint.repository, token).map(Self::Github)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ensure_release(
|
||||
&self,
|
||||
tag: &str,
|
||||
commit: &str,
|
||||
draft: bool,
|
||||
prerelease: bool,
|
||||
message: Option<&str>,
|
||||
replace_existing_message: bool,
|
||||
) -> Result<RemoteRelease, Error> {
|
||||
match self {
|
||||
Self::Gitea(api) => api.ensure_release(
|
||||
tag,
|
||||
commit,
|
||||
draft,
|
||||
prerelease,
|
||||
message,
|
||||
replace_existing_message,
|
||||
),
|
||||
Self::Github(api) => api.ensure_release(
|
||||
tag,
|
||||
commit,
|
||||
draft,
|
||||
prerelease,
|
||||
message,
|
||||
replace_existing_message,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn repository_private(&self) -> Result<bool, Error> {
|
||||
match self {
|
||||
Self::Gitea(api) => api.repository_private(),
|
||||
Self::Github(api) => api.repository_private(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn upload(&self, release_id: u64, paths: &[&Path], replace: bool) -> Result<(), Error> {
|
||||
match self {
|
||||
Self::Gitea(api) => api.upload(release_id, paths, replace),
|
||||
Self::Github(api) => api.upload(release_id, paths, replace),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn download_client(&self) -> &Client {
|
||||
match self {
|
||||
Self::Gitea(api) => api.download_client(),
|
||||
Self::Github(api) => api.download_client(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn asset_urls(&self, release_id: u64) -> Result<Option<BTreeMap<String, String>>, Error> {
|
||||
match self {
|
||||
Self::Gitea(_) => Ok(None),
|
||||
Self::Github(api) => api.asset_urls(release_id).map(Some),
|
||||
}
|
||||
}
|
||||
}
|
||||
165
src/runner.rs
165
src/runner.rs
|
|
@ -3,18 +3,40 @@ use regex::Regex;
|
|||
use std::{path::Path, process::Command, sync::OnceLock};
|
||||
|
||||
pub struct Runner {
|
||||
token_env: String,
|
||||
token_envs: Vec<String>,
|
||||
secrets: Vec<String>,
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
impl Runner {
|
||||
pub fn new(token_env: &str) -> Self {
|
||||
let mut token_envs = vec!["GITEA_TOKEN".to_owned(), "GITHUB_TOKEN".to_owned()];
|
||||
if !token_envs.iter().any(|name| name == token_env) {
|
||||
token_envs.push(token_env.to_owned());
|
||||
}
|
||||
Self {
|
||||
token_env: token_env.to_owned(),
|
||||
token_envs,
|
||||
secrets: Vec::new(),
|
||||
verbose: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_token_envs(mut self, names: impl IntoIterator<Item = String>) -> Self {
|
||||
for name in names {
|
||||
if !self.token_envs.contains(&name) {
|
||||
self.token_envs.push(name);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_secret(mut self, secret: Option<&str>) -> Self {
|
||||
if let Some(secret) = secret.filter(|secret| !secret.is_empty()) {
|
||||
self.secrets.push(secret.to_owned());
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_verbose(mut self, verbose: bool) -> Self {
|
||||
self.verbose = verbose;
|
||||
self
|
||||
|
|
@ -41,11 +63,10 @@ impl Runner {
|
|||
eprintln!("$ {}", self.redact(&command));
|
||||
}
|
||||
let mut process = Command::new(program);
|
||||
process
|
||||
.args(args)
|
||||
.current_dir(cwd)
|
||||
.env_remove("GITEA_TOKEN")
|
||||
.env_remove(&self.token_env);
|
||||
process.args(args).current_dir(cwd);
|
||||
for name in &self.token_envs {
|
||||
process.env_remove(name);
|
||||
}
|
||||
for name in removals {
|
||||
process.env_remove(name);
|
||||
}
|
||||
|
|
@ -77,6 +98,127 @@ impl Runner {
|
|||
Ok(output.stdout)
|
||||
}
|
||||
|
||||
pub fn hook(
|
||||
&self,
|
||||
argv: &[String],
|
||||
cwd: &Path,
|
||||
paths: (&Path, &Path, &Path),
|
||||
timeout: u64,
|
||||
label: &str,
|
||||
) -> Result<(), Error> {
|
||||
let (context, manifest, tree) = paths;
|
||||
use std::{
|
||||
io::{BufRead, BufReader},
|
||||
process::Stdio,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
let args = argv
|
||||
.iter()
|
||||
.map(|a| {
|
||||
a.replace("{context-file}", &context.to_string_lossy())
|
||||
.replace("{manifest-file}", &manifest.to_string_lossy())
|
||||
.replace("{bundle-directory}", &tree.to_string_lossy())
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let mut command = Command::new(&args[0]);
|
||||
command
|
||||
.args(&args[1..])
|
||||
.current_dir(cwd)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped());
|
||||
for name in &self.token_envs {
|
||||
command.env_remove(name);
|
||||
}
|
||||
for (name, _) in std::env::vars_os() {
|
||||
let upper = name.to_string_lossy().to_uppercase();
|
||||
if ["TOKEN", "PASSWORD", "SECRET", "CREDENTIAL"]
|
||||
.iter()
|
||||
.any(|s| upper.contains(s))
|
||||
{
|
||||
command.env_remove(name);
|
||||
}
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::process::CommandExt;
|
||||
command.process_group(0);
|
||||
}
|
||||
let cancelled = Arc::new(AtomicBool::new(false));
|
||||
#[cfg(unix)]
|
||||
let registrations = [signal_hook::consts::SIGINT, signal_hook::consts::SIGTERM]
|
||||
.into_iter()
|
||||
.map(|signal| signal_hook::flag::register(signal, Arc::clone(&cancelled)))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let result = (|| {
|
||||
let mut child = command
|
||||
.spawn()
|
||||
.map_err(|e| Error(format!("Cannot start hook [{label}] {args:?}: {e}")))?;
|
||||
let stdout = child.stdout.take().unwrap();
|
||||
let stderr = child.stderr.take().unwrap();
|
||||
let started = Instant::now();
|
||||
std::thread::scope(|scope| {
|
||||
let stream = |reader: &mut dyn BufRead| {
|
||||
let mut line = String::new();
|
||||
loop {
|
||||
line.clear();
|
||||
match reader.read_line(&mut line) {
|
||||
Ok(0) | Err(_) => break,
|
||||
Ok(_) => eprintln!("[{label}] {}", self.redact(line.trim_end())),
|
||||
}
|
||||
}
|
||||
};
|
||||
scope.spawn(move || stream(&mut BufReader::new(stdout)));
|
||||
scope.spawn(move || stream(&mut BufReader::new(stderr)));
|
||||
let outcome = loop {
|
||||
if cancelled.load(Ordering::Relaxed)
|
||||
|| started.elapsed() >= Duration::from_secs(timeout)
|
||||
{
|
||||
break Err(Error(format!(
|
||||
"Hook [{label}] {:?} {}",
|
||||
args,
|
||||
if cancelled.load(Ordering::Relaxed) {
|
||||
"cancelled"
|
||||
} else {
|
||||
"timed out"
|
||||
}
|
||||
)));
|
||||
}
|
||||
match child.try_wait() {
|
||||
Ok(Some(status)) => {
|
||||
break if status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(Error(format!(
|
||||
"Hook [{label}] {:?} failed with {status}",
|
||||
args
|
||||
)))
|
||||
}
|
||||
}
|
||||
Ok(None) => std::thread::sleep(Duration::from_millis(25)),
|
||||
Err(e) => break Err(Error(e.to_string())),
|
||||
}
|
||||
};
|
||||
// Kill the group even after the leader exits: descendants may retain pipes.
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
libc::kill(-(child.id() as i32), libc::SIGKILL);
|
||||
}
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
outcome
|
||||
})
|
||||
})();
|
||||
#[cfg(unix)]
|
||||
for id in registrations {
|
||||
signal_hook::low_level::unregister(id);
|
||||
}
|
||||
result.map_err(|e: Error| Error(self.redact(&e.to_string())))
|
||||
}
|
||||
|
||||
pub fn text(&self, program: &str, args: &[&str], cwd: &Path) -> Result<String, Error> {
|
||||
let bytes = self.output(program, args, cwd)?;
|
||||
String::from_utf8(bytes)
|
||||
|
|
@ -86,7 +228,10 @@ impl Runner {
|
|||
|
||||
pub fn redact(&self, text: &str) -> String {
|
||||
let mut without_token = text.to_owned();
|
||||
for name in ["GITEA_TOKEN", self.token_env.as_str()] {
|
||||
for secret in &self.secrets {
|
||||
without_token = without_token.replace(secret, "[REDACTED]");
|
||||
}
|
||||
for name in &self.token_envs {
|
||||
if let Ok(secret) = std::env::var(name) {
|
||||
if !secret.is_empty() {
|
||||
without_token = without_token.replace(&secret, "[REDACTED]");
|
||||
|
|
@ -107,13 +252,15 @@ mod tests {
|
|||
use super::*;
|
||||
#[test]
|
||||
fn redacts_token_and_urls_and_keeps_token_out_of_child() {
|
||||
let runner = Runner::new("RUST_RELEASE_TEST_SECRET");
|
||||
let runner =
|
||||
Runner::new("RUST_RELEASE_TEST_SECRET").with_secret(Some("token-loaded-from-file"));
|
||||
let secret = "unit-test-secret-value";
|
||||
std::env::set_var("RUST_RELEASE_TEST_SECRET", secret);
|
||||
let message = runner
|
||||
.redact("failed https://user:password@example.com/repo and unit-test-secret-value");
|
||||
assert!(!message.contains(secret));
|
||||
assert!(!message.contains("password"));
|
||||
assert_eq!(runner.redact("token-loaded-from-file"), "[REDACTED]");
|
||||
let child = runner.text("env", &[], Path::new(".")).unwrap();
|
||||
assert!(!child.contains("RUST_RELEASE_TEST_SECRET"));
|
||||
std::env::set_var("RUST_RELEASE_TEST_REMOVED", "remove-me");
|
||||
|
|
|
|||
263
src/verify.rs
263
src/verify.rs
|
|
@ -6,8 +6,9 @@ use crate::{
|
|||
runner::Runner,
|
||||
};
|
||||
use flate2::read::GzDecoder;
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::{blocking::Client, header::ACCEPT};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
fs::{self, File},
|
||||
io::{Read, Write},
|
||||
path::Path,
|
||||
|
|
@ -25,6 +26,14 @@ pub fn asset_url(repository: &str, tag: &str, filename: &str) -> String {
|
|||
}
|
||||
|
||||
pub fn verify_archive(path: &Path, artifact: &Artifact) -> Result<(), Error> {
|
||||
if let ArtifactContents::Bundle {
|
||||
root,
|
||||
format,
|
||||
entries,
|
||||
} = &artifact.contents
|
||||
{
|
||||
return crate::bundle::verify(path, root, *format, entries);
|
||||
}
|
||||
let ArtifactContents::Archive { root, binaries } = &artifact.contents else {
|
||||
return Err(Error(format!("{} is not an archive.", path.display())));
|
||||
};
|
||||
|
|
@ -93,6 +102,107 @@ pub fn verify_archive(path: &Path, artifact: &Artifact) -> Result<(), Error> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn verify_apple_artifact(
|
||||
path: &Path,
|
||||
artifact: &Artifact,
|
||||
deployment_target: &str,
|
||||
) -> Result<(), Error> {
|
||||
match &artifact.contents {
|
||||
ArtifactContents::Binary => {
|
||||
crate::macho::verify_path(path, &artifact.target, deployment_target)
|
||||
}
|
||||
ArtifactContents::Archive { binaries, .. } => {
|
||||
let gzip = GzDecoder::new(
|
||||
File::open(path)
|
||||
.map_err(|e| Error(format!("cannot open {}: {e}", path.display())))?,
|
||||
);
|
||||
let mut archive = tar::Archive::new(gzip);
|
||||
let mut checked = 0_usize;
|
||||
for entry in archive.entries().map_err(|e| {
|
||||
Error(format!(
|
||||
"cannot inspect Apple archive {}: {e}",
|
||||
path.display()
|
||||
))
|
||||
})? {
|
||||
let mut entry = entry.map_err(|e| Error(format!("invalid Apple archive: {e}")))?;
|
||||
if !entry.header().entry_type().is_file() {
|
||||
continue;
|
||||
}
|
||||
let name = entry
|
||||
.path()
|
||||
.map_err(|e| Error(format!("invalid Apple archive path: {e}")))?;
|
||||
let filename = name.file_name().and_then(|name| name.to_str());
|
||||
if !filename.is_some_and(|name| binaries.iter().any(|binary| binary == name)) {
|
||||
return Err(Error(format!(
|
||||
"unexpected Apple executable in {}",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
entry
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|e| Error(format!("cannot read Apple archive member: {e}")))?;
|
||||
crate::macho::verify(&bytes, &artifact.target, deployment_target)?;
|
||||
checked += 1;
|
||||
}
|
||||
if checked != binaries.len() {
|
||||
return Err(Error(format!(
|
||||
"{} does not contain every declared Apple binary",
|
||||
path.display()
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
ArtifactContents::Bundle {
|
||||
root,
|
||||
format,
|
||||
entries,
|
||||
} => {
|
||||
// Recheck the declared tree, hashes and modes before interpreting executable bytes.
|
||||
crate::bundle::verify(path, root, *format, entries)?;
|
||||
let mut expected = entries
|
||||
.iter()
|
||||
.filter(|entry| entry.mode & 0o111 != 0)
|
||||
.map(|entry| format!("{root}/{}", entry.destination))
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
let inspect = |reader: &mut dyn Read| -> Result<(), Error> {
|
||||
let mut bytes = Vec::new();
|
||||
reader
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|e| Error(format!("cannot read Apple bundle executable: {e}")))?;
|
||||
crate::macho::verify_compatible(&bytes, &artifact.target, deployment_target)
|
||||
};
|
||||
match format {
|
||||
crate::bundle::Format::Zip => {
|
||||
let mut archive = zip::ZipArchive::new(File::open(path)?)
|
||||
.map_err(|e| Error(format!("invalid Apple ZIP: {e}")))?;
|
||||
for name in &expected {
|
||||
let mut entry = archive
|
||||
.by_name(name)
|
||||
.map_err(|e| Error(format!("missing Apple executable {name}: {e}")))?;
|
||||
inspect(&mut entry)?;
|
||||
}
|
||||
expected.clear();
|
||||
}
|
||||
crate::bundle::Format::Tgz => {
|
||||
let mut archive = tar::Archive::new(GzDecoder::new(File::open(path)?));
|
||||
for entry in archive.entries()? {
|
||||
let mut entry = entry?;
|
||||
let name = entry.path()?.to_string_lossy().into_owned();
|
||||
if expected.remove(&name) {
|
||||
inspect(&mut entry)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !expected.is_empty() {
|
||||
return Err(Error("Missing Apple bundle executables".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn member_name(header: &[u8]) -> Result<String, Error> {
|
||||
let name = text_field(&header[..100])?;
|
||||
let prefix = text_field(&header[345..500])?;
|
||||
|
|
@ -127,6 +237,17 @@ pub fn verify_published(
|
|||
artifacts: &[Artifact],
|
||||
sums: &Path,
|
||||
authenticated_client: Option<&Client>,
|
||||
) -> Result<(), Error> {
|
||||
verify_published_with_asset_urls(repository, tag, artifacts, sums, authenticated_client, None)
|
||||
}
|
||||
|
||||
pub fn verify_published_with_asset_urls(
|
||||
repository: &str,
|
||||
tag: &str,
|
||||
artifacts: &[Artifact],
|
||||
sums: &Path,
|
||||
authenticated_client: Option<&Client>,
|
||||
asset_urls: Option<&BTreeMap<String, String>>,
|
||||
) -> Result<(), Error> {
|
||||
let downloads = tempfile::Builder::new()
|
||||
.prefix("rust-realease-download-")
|
||||
|
|
@ -149,8 +270,19 @@ pub fn verify_published(
|
|||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.ok_or_else(|| Error("Invalid asset filename.".into()))?;
|
||||
let mut response = client
|
||||
.get(asset_url(repository, tag, filename))
|
||||
let url = match asset_urls {
|
||||
Some(urls) => urls.get(filename).cloned().ok_or_else(|| {
|
||||
Error(format!(
|
||||
"{filename}: release API did not return the uploaded asset."
|
||||
))
|
||||
})?,
|
||||
None => asset_url(repository, tag, filename),
|
||||
};
|
||||
let mut request = client.get(url);
|
||||
if asset_urls.is_some() {
|
||||
request = request.header(ACCEPT, "application/octet-stream");
|
||||
}
|
||||
let mut response = request
|
||||
.send()
|
||||
.map_err(|_| Error(format!("{filename}: release download failed.")))?;
|
||||
if response.status() != reqwest::StatusCode::OK {
|
||||
|
|
@ -199,7 +331,7 @@ pub fn verify_published(
|
|||
}
|
||||
if let Some(artifact) = artifacts.iter().find(|a| a.path == local) {
|
||||
match &artifact.contents {
|
||||
ArtifactContents::Archive { .. } => {
|
||||
ArtifactContents::Archive { .. } | ArtifactContents::Bundle { .. } => {
|
||||
verify_archive(&path, artifact)?;
|
||||
println!("Verified URL, checksum and archive: {}", artifact.target);
|
||||
}
|
||||
|
|
@ -344,6 +476,7 @@ mod tests {
|
|||
let root = manifest.parent().unwrap().to_path_buf();
|
||||
let name = required("RUST_RELEASE_LIVE_NAME");
|
||||
let project = SelectedPackage {
|
||||
workspace_root: std::path::PathBuf::new(),
|
||||
name: name.clone(),
|
||||
version: required("RUST_RELEASE_LIVE_VERSION"),
|
||||
binaries: vec![name],
|
||||
|
|
@ -557,6 +690,7 @@ mod tests {
|
|||
}
|
||||
});
|
||||
let project = SelectedPackage {
|
||||
workspace_root: std::path::PathBuf::new(),
|
||||
name: "demo".into(),
|
||||
version: "0.1.0".into(),
|
||||
binaries: vec!["demo".into()],
|
||||
|
|
@ -607,6 +741,7 @@ mod tests {
|
|||
.to_string()
|
||||
.contains("unexpected members"));
|
||||
let project = SelectedPackage {
|
||||
workspace_root: std::path::PathBuf::new(),
|
||||
name: "demo".into(),
|
||||
version: "0.1.0".into(),
|
||||
binaries: vec!["demo".into()],
|
||||
|
|
@ -622,4 +757,124 @@ mod tests {
|
|||
"https://example.com/team/demo/releases/download/release%2B1/one.tgz"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn archive_validation_rejects_corrupt_metadata_and_payloads() {
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let binary = temp.path().join("demo");
|
||||
fs::write(&binary, b"binary").unwrap();
|
||||
let (artifacts, _) = package::package(
|
||||
temp.path(),
|
||||
"demo",
|
||||
"0.1.0",
|
||||
&outputs("x86_64-unknown-linux-musl", binary),
|
||||
PackageFormat::Tgz,
|
||||
)
|
||||
.unwrap();
|
||||
let artifact = &artifacts[0];
|
||||
let mut not_archive = artifact.clone();
|
||||
not_archive.contents = ArtifactContents::Binary;
|
||||
assert!(verify_archive(&artifact.path, ¬_archive)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("is not an archive"));
|
||||
|
||||
let invalid_gzip = temp.path().join("invalid.tgz");
|
||||
fs::write(&invalid_gzip, b"not gzip").unwrap();
|
||||
assert!(verify_archive(&invalid_gzip, artifact)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("invalid gzip"));
|
||||
|
||||
let mut valid_tar = Vec::new();
|
||||
GzDecoder::new(File::open(&artifact.path).unwrap())
|
||||
.read_to_end(&mut valid_tar)
|
||||
.unwrap();
|
||||
let write_tar = |name: &str, bytes: &[u8]| {
|
||||
let path = temp.path().join(name);
|
||||
let mut gzip = flate2::write::GzEncoder::new(
|
||||
File::create(&path).unwrap(),
|
||||
flate2::Compression::default(),
|
||||
);
|
||||
gzip.write_all(bytes).unwrap();
|
||||
gzip.finish().unwrap();
|
||||
path
|
||||
};
|
||||
let truncated = write_tar("truncated.tgz", &[0; 1024]);
|
||||
assert!(verify_archive(&truncated, artifact)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("truncated tar"));
|
||||
|
||||
for (name, index, byte, expected) in [
|
||||
("root.tgz", 0, b'x', "archive root"),
|
||||
("binary.tgz", 512, b'x', "archive binary"),
|
||||
("utf8.tgz", 0, 0xff, "non-UTF-8"),
|
||||
("numeric.tgz", 512 + 100, b'x', "numeric field"),
|
||||
] {
|
||||
let mut corrupt = valid_tar.clone();
|
||||
corrupt[index] = byte;
|
||||
let path = write_tar(name, &corrupt);
|
||||
let error = verify_archive(&path, artifact).unwrap_err().to_string();
|
||||
assert!(error.contains(expected), "{error}");
|
||||
}
|
||||
|
||||
let mut non_executable = valid_tar;
|
||||
non_executable[512 + 100..512 + 108].copy_from_slice(b"0000644\0");
|
||||
let path = write_tar("mode.tgz", &non_executable);
|
||||
assert!(verify_archive(&path, artifact)
|
||||
.unwrap_err()
|
||||
.to_string()
|
||||
.contains("not executable"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn published_verification_rejects_http_size_and_checksum_failures() {
|
||||
use std::{net::TcpListener, thread};
|
||||
|
||||
let temp = tempfile::tempdir().unwrap();
|
||||
let local = temp.path().join("demo.bin");
|
||||
let sums = temp.path().join("SHA256SUMS");
|
||||
fs::write(&local, b"local").unwrap();
|
||||
fs::write(&sums, b"unused").unwrap();
|
||||
let artifact = Artifact {
|
||||
target: "x86_64-unknown-linux-musl".into(),
|
||||
path: local,
|
||||
contents: ArtifactContents::Binary,
|
||||
sha256: String::new(),
|
||||
};
|
||||
|
||||
for (status, body, expected) in [
|
||||
(404, b"missing".as_slice(), "HTTP 404"),
|
||||
(200, b"too-long".as_slice(), "exceeds expected size"),
|
||||
(200, b"wrong".as_slice(), "SHA-256 differs"),
|
||||
] {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let repository = format!("http://{}/team/demo", listener.local_addr().unwrap());
|
||||
let response_body = body.to_vec();
|
||||
let handle = thread::spawn(move || {
|
||||
let (mut stream, _) = listener.accept().unwrap();
|
||||
let mut request = [0_u8; 4096];
|
||||
let _ = stream.read(&mut request).unwrap();
|
||||
write!(
|
||||
stream,
|
||||
"HTTP/1.1 {status} Result\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
response_body.len()
|
||||
)
|
||||
.unwrap();
|
||||
stream.write_all(&response_body).unwrap();
|
||||
});
|
||||
let error = verify_published(
|
||||
&repository,
|
||||
"v0.1.0",
|
||||
std::slice::from_ref(&artifact),
|
||||
&sums,
|
||||
None,
|
||||
)
|
||||
.unwrap_err()
|
||||
.to_string();
|
||||
assert!(error.contains(expected), "{error}");
|
||||
handle.join().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
[coverage]
|
||||
all-features = true
|
||||
engine = "Llvm"
|
||||
fail-under = 80.0
|
||||
locked = true
|
||||
run-types = ["Tests"]
|
||||
workspace = true
|
||||
|
||||
[report]
|
||||
out = ["Stdout"]
|
||||
|
||||
# This floor is backed by end-to-end release-flow and failure-path tests without
|
||||
# excluding production code or manufacturing process/platform-only failures.
|
||||
|
|
@ -0,0 +1,512 @@
|
|||
#![cfg(unix)]
|
||||
|
||||
use std::{
|
||||
env, fs,
|
||||
os::unix::fs::PermissionsExt,
|
||||
path::Path,
|
||||
process::{Command, Output},
|
||||
};
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn run(command: &mut Command) -> Output {
|
||||
command.output().expect("test command should start")
|
||||
}
|
||||
|
||||
fn git(repository: &Path, arguments: &[&str]) {
|
||||
let output = run(Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(repository)
|
||||
.args(arguments));
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"git {arguments:?} failed: {}",
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
fn executable(path: &Path, source: &str) {
|
||||
fs::write(path, source).expect("fake executable should be written");
|
||||
let mut permissions = fs::metadata(path)
|
||||
.expect("fake executable metadata should be readable")
|
||||
.permissions();
|
||||
permissions.set_mode(0o755);
|
||||
fs::set_permissions(path, permissions).expect("fake executable should be executable");
|
||||
}
|
||||
|
||||
fn publisher(project: &Path, fake_bin: &Path, cross_log: &Path, arguments: &[&str]) -> Output {
|
||||
let path = format!(
|
||||
"{}:{}",
|
||||
fake_bin.display(),
|
||||
env::var("PATH").expect("PATH should be available")
|
||||
);
|
||||
run(Command::new(env!("CARGO_BIN_EXE_rust_realease"))
|
||||
.arg(project)
|
||||
.args(arguments)
|
||||
.env("PATH", path)
|
||||
.env("CROSS_LOG", cross_log)
|
||||
.env_remove("GITEA_TOKEN"))
|
||||
}
|
||||
|
||||
fn assert_success(output: &Output) -> String {
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"publisher failed:\nstdout:\n{}\nstderr:\n{}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
String::from_utf8_lossy(&output.stdout).into_owned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compile_only_reuses_clean_commit_and_rebuilds_changed_or_forced_inputs() {
|
||||
let temporary = TempDir::new().expect("temporary directory should be created");
|
||||
let project = temporary.path().join("project");
|
||||
let fake_bin = temporary.path().join("bin");
|
||||
let cross_log = temporary.path().join("cross.log");
|
||||
fs::create_dir_all(project.join("src")).unwrap();
|
||||
fs::create_dir_all(&fake_bin).unwrap();
|
||||
fs::write(
|
||||
project.join("Cargo.toml"),
|
||||
"[package]\n\
|
||||
name = \"demo\"\n\
|
||||
version = \"0.1.0\"\n\
|
||||
edition = \"2021\"\n\
|
||||
repository = \"https://example.com/team/demo\"\n\
|
||||
[package.metadata.binstall]\n\
|
||||
pkg-url = \"{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.tgz\"\n\
|
||||
pkg-fmt = \"tgz\"\n\
|
||||
bin-dir = \"{ name }-{ target }-v{ version }/{ bin }{ binary-ext }\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(project.join("src/main.rs"), "fn main() {}\n").unwrap();
|
||||
fs::write(project.join(".gitignore"), "/target/\n/dist/\n").unwrap();
|
||||
fs::write(
|
||||
project.join("rust-release.toml"),
|
||||
"[build]\ntargets = [\"x86_64-unknown-linux-musl\"]\n",
|
||||
)
|
||||
.unwrap();
|
||||
executable(&fake_bin.join("docker"), "#!/bin/sh\n[ \"$1\" = info ]\n");
|
||||
executable(&fake_bin.join("cargo-binstall"), "#!/bin/sh\nexit 0\n");
|
||||
executable(
|
||||
&fake_bin.join("cargo-cross"),
|
||||
"#!/bin/sh\n\
|
||||
set -eu\n\
|
||||
target=\n\
|
||||
target_dir=\n\
|
||||
profile=debug\n\
|
||||
binaries=\n\
|
||||
while [ \"$#\" -gt 0 ]; do\n\
|
||||
case \"$1\" in\n\
|
||||
--target) shift; target=$1 ;;\n\
|
||||
--target-dir) shift; target_dir=$1 ;;\n\
|
||||
--release) profile=release ;;\n\
|
||||
--profile) shift; profile=$1 ;;\n\
|
||||
--bin) shift; binaries=\"$binaries $1\" ;;\n\
|
||||
esac\n\
|
||||
shift\n\
|
||||
done\n\
|
||||
output_dir=$target_dir/$target/$profile\n\
|
||||
mkdir -p \"$output_dir\"\n\
|
||||
for binary in $binaries; do printf 'compiled %s' \"$binary\" >\"$output_dir/$binary\"; done\n\
|
||||
printf 'cross invoked\\n' >>\"$CROSS_LOG\"\n",
|
||||
);
|
||||
|
||||
git(&project, &["init", "--quiet"]);
|
||||
git(&project, &["config", "user.name", "Release Test"]);
|
||||
git(&project, &["config", "user.email", "release@example.com"]);
|
||||
git(&project, &["config", "commit.gpgsign", "false"]);
|
||||
let lock = run(Command::new("cargo")
|
||||
.args(["generate-lockfile", "--manifest-path"])
|
||||
.arg(project.join("Cargo.toml")));
|
||||
assert!(lock.status.success());
|
||||
git(&project, &["add", "."]);
|
||||
git(&project, &["commit", "--quiet", "-m", "initial"]);
|
||||
git(&project, &["tag", "v0.1.0"]);
|
||||
|
||||
let dry_run = assert_success(&publisher(&project, &fake_bin, &cross_log, &["--dry-run"]));
|
||||
assert!(dry_run.contains("Dry run complete"));
|
||||
assert!(!cross_log.exists());
|
||||
|
||||
let first = assert_success(&publisher(
|
||||
&project,
|
||||
&fake_bin,
|
||||
&cross_log,
|
||||
&["--compile-only"],
|
||||
));
|
||||
assert!(first.contains("Cross builds complete"));
|
||||
assert_eq!(fs::read_to_string(&cross_log).unwrap().lines().count(), 1);
|
||||
|
||||
let second = assert_success(&publisher(
|
||||
&project,
|
||||
&fake_bin,
|
||||
&cross_log,
|
||||
&["--compile-only"],
|
||||
));
|
||||
assert!(second.contains("Reusing completed release build"));
|
||||
assert_eq!(fs::read_to_string(&cross_log).unwrap().lines().count(), 1);
|
||||
|
||||
fs::write(
|
||||
project.join("src/main.rs"),
|
||||
"fn main() { println!(\"changed\"); }\n",
|
||||
)
|
||||
.unwrap();
|
||||
let dirty_publication = publisher(&project, &fake_bin, &cross_log, &[]);
|
||||
assert!(!dirty_publication.status.success());
|
||||
assert!(String::from_utf8_lossy(&dirty_publication.stderr)
|
||||
.contains("Publication requires a clean checkout"));
|
||||
assert_success(&publisher(
|
||||
&project,
|
||||
&fake_bin,
|
||||
&cross_log,
|
||||
&["--compile-only"],
|
||||
));
|
||||
assert_eq!(fs::read_to_string(&cross_log).unwrap().lines().count(), 2);
|
||||
|
||||
git(&project, &["add", "src/main.rs"]);
|
||||
git(&project, &["commit", "--quiet", "-m", "change source"]);
|
||||
let changed = assert_success(&publisher(
|
||||
&project,
|
||||
&fake_bin,
|
||||
&cross_log,
|
||||
&["--compile-only"],
|
||||
));
|
||||
assert!(changed.contains("Cross builds complete"));
|
||||
assert_eq!(fs::read_to_string(&cross_log).unwrap().lines().count(), 3);
|
||||
|
||||
assert_success(&publisher(
|
||||
&project,
|
||||
&fake_bin,
|
||||
&cross_log,
|
||||
&["--compile-only", "--recompile"],
|
||||
));
|
||||
assert_eq!(fs::read_to_string(&cross_log).unwrap().lines().count(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_message_file_is_reported_before_project_acquisition() {
|
||||
let temporary = TempDir::new().unwrap();
|
||||
let output = run(Command::new(env!("CARGO_BIN_EXE_rust_realease"))
|
||||
.arg("missing-project")
|
||||
.args(["--message-file", "missing-notes.md"])
|
||||
.current_dir(temporary.path()));
|
||||
assert!(!output.status.success());
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("cannot read release message file"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bundle_cli_uses_reported_paths_and_reruns_preparation_on_cache_hits() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let root = tmp.path().join("workspace é");
|
||||
let bin = tmp.path().join("tools");
|
||||
let log = tmp.path().join("cross.log");
|
||||
fs::create_dir_all(root.join("member/src")).unwrap();
|
||||
fs::create_dir_all(root.join(".cargo")).unwrap();
|
||||
fs::create_dir(&bin).unwrap();
|
||||
fs::write(
|
||||
root.join("Cargo.toml"),
|
||||
"[workspace]\nmembers=['member']\nresolver='2'\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(
|
||||
root.join("member/Cargo.toml"),
|
||||
r#"[package]
|
||||
name='demo'
|
||||
version='0.1.0'
|
||||
edition='2021'
|
||||
repository='https://example.com/team/demo'
|
||||
[package.metadata.binstall]
|
||||
pkg-url='{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.tgz'
|
||||
pkg-fmt='tgz'
|
||||
bin-dir='{ name }-{ target }-v{ version }/{ bin }{ binary-ext }'
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("member/src/main.rs"), "fn main() {}\n").unwrap();
|
||||
fs::write(
|
||||
root.join(".cargo/config.toml"),
|
||||
"[build]\ntarget-dir='custom build dir'\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join(".gitignore"), "/custom build dir/\n/dist/\n").unwrap();
|
||||
fs::write(
|
||||
root.join("rust-release.toml"),
|
||||
r#"[build]
|
||||
targets=['x86_64-unknown-linux-gnu']
|
||||
[[package.bundles]]
|
||||
name='portable'
|
||||
formats={x86_64-unknown-linux-gnu='tgz'}
|
||||
archive-name='{name}-{bundle}-{target}-v{version}'
|
||||
root='{name}-{bundle}-{target}-v{version}'
|
||||
cargo-binaries='all'
|
||||
[package.bundles.prepare]
|
||||
command=['python3','prepare.py','{context-file}']
|
||||
inputs=['prepare.py']
|
||||
timeout-seconds=10
|
||||
required-destinations=['NOTICE']
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("member/prepare.py"),r#"import sys,json,pathlib,hashlib
|
||||
c=json.loads(pathlib.Path(sys.argv[1]).read_text())
|
||||
assert pathlib.Path(c['package_root']).name=='member'
|
||||
assert pathlib.Path(c['workspace_root']).name=='workspace é'
|
||||
assert 'actual output é' in c['binaries']['demo']
|
||||
assert pathlib.Path(c['binaries']['demo']).is_file()
|
||||
cache=pathlib.Path(c['cache_directory'])
|
||||
with (cache/'calls').open('a') as f: f.write(str(c['source_dirty'])+'\n')
|
||||
out=pathlib.Path(c['output_directory'])/'notice'
|
||||
out.write_bytes(b'notice')
|
||||
pathlib.Path(c['manifest_path']).write_text(json.dumps(dict(schema_version=1,target=c['target'],files=[dict(source='notice',destination='NOTICE',kind='file',sha256=hashlib.sha256(b'notice').hexdigest())])))
|
||||
"#).unwrap();
|
||||
executable(&bin.join("docker"), "#!/bin/sh\nexit 0\n");
|
||||
executable(&bin.join("cargo-binstall"), "#!/bin/sh\nexit 0\n");
|
||||
executable(
|
||||
&bin.join("cross"),
|
||||
r#"#!/usr/bin/env python3
|
||||
import sys,pathlib,json,os
|
||||
a=sys.argv
|
||||
assert '--message-format=json-render-diagnostics' in a
|
||||
out=pathlib.Path(a[a.index('--target-dir')+1])/'actual output é'
|
||||
out.mkdir(parents=True)
|
||||
binary=out/'demo'
|
||||
deps=out/'deps'
|
||||
deps.mkdir()
|
||||
backing=deps/'demo-build-id'
|
||||
elf=bytearray(64)
|
||||
elf[:4]=b'\x7fELF'
|
||||
elf[4]=2
|
||||
elf[5]=1
|
||||
elf[18:20]=(62).to_bytes(2,'little')
|
||||
backing.write_bytes(elf)
|
||||
backing.chmod(0o751)
|
||||
os.link(backing,binary)
|
||||
print(json.dumps(dict(reason='compiler-artifact',target=dict(name='demo',kind=['bin']),executable="/target/actual output é/demo")))
|
||||
with open(os.environ['CROSS_LOG'],'a') as f: f.write('build\n')
|
||||
"#,
|
||||
);
|
||||
git(&root, &["init", "-q"]);
|
||||
git(&root, &["config", "user.name", "Test"]);
|
||||
git(&root, &["config", "user.email", "test@example.com"]);
|
||||
git(&root, &["config", "commit.gpgsign", "false"]);
|
||||
assert!(run(Command::new("cargo")
|
||||
.arg("generate-lockfile")
|
||||
.current_dir(&root))
|
||||
.status
|
||||
.success());
|
||||
git(&root, &["add", "."]);
|
||||
git(&root, &["commit", "-qm", "fixture"]);
|
||||
git(&root, &["tag", "v0.1.0"]);
|
||||
assert_success(&publisher(
|
||||
&root,
|
||||
&bin,
|
||||
&log,
|
||||
&["--package", "demo", "--dry-run"],
|
||||
));
|
||||
assert!(!root.join("dist").exists());
|
||||
assert!(!log.exists());
|
||||
for _ in 0..2 {
|
||||
assert_success(&publisher(
|
||||
&root,
|
||||
&bin,
|
||||
&log,
|
||||
&["--package", "demo", "--compile-only", "--skip-tests"],
|
||||
));
|
||||
}
|
||||
assert_eq!(fs::read_to_string(&log).unwrap().lines().count(), 1);
|
||||
let calls = root
|
||||
.join("custom build dir/rust-release-bundle-cache/portable/x86_64-unknown-linux-gnu/calls");
|
||||
assert_eq!(fs::read_to_string(&calls).unwrap(), "False\nFalse\n");
|
||||
fs::write(
|
||||
root.join("member/src/main.rs"),
|
||||
"fn main() {println!(\"dirty\");}\n",
|
||||
)
|
||||
.unwrap();
|
||||
assert_success(&publisher(
|
||||
&root,
|
||||
&bin,
|
||||
&log,
|
||||
&[
|
||||
"--package",
|
||||
"demo",
|
||||
"--stop-after",
|
||||
"package",
|
||||
"--skip-tests",
|
||||
],
|
||||
));
|
||||
assert_eq!(fs::read_to_string(&log).unwrap().lines().count(), 2);
|
||||
assert!(fs::read_to_string(calls).unwrap().ends_with("True\n"));
|
||||
// Cancellation is delivered to a disposable publisher process, not the test runner.
|
||||
fs::write(
|
||||
root.join("member/prepare.py"),
|
||||
"import os,signal,time\nos.kill(os.getppid(), signal.SIGTERM)\ntime.sleep(5)\n",
|
||||
)
|
||||
.unwrap();
|
||||
let cancelled = publisher(
|
||||
&root,
|
||||
&bin,
|
||||
&log,
|
||||
&["--package", "demo", "--compile-only", "--skip-tests"],
|
||||
);
|
||||
assert!(!cancelled.status.success());
|
||||
assert!(String::from_utf8_lossy(&cancelled.stderr).contains("cancelled"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(target_os = "linux")]
|
||||
fn mixed_cross_and_apple_compile_only_routes_and_packages_eight_assets() {
|
||||
let temporary = TempDir::new().unwrap();
|
||||
let project = temporary.path().join("mixed checkout with spaces");
|
||||
let tools = temporary.path().join("tools");
|
||||
let log = temporary.path().join("routes.log");
|
||||
fs::create_dir_all(project.join("src")).unwrap();
|
||||
fs::create_dir(&tools).unwrap();
|
||||
fs::write(
|
||||
project.join("Cargo.toml"),
|
||||
"[package]\nname='demo'\nversion='2.3.4'\nedition='2021'\nrepository='https://example.com/team/demo'\n[features]\nserial=[]\n[package.metadata.binstall]\npkg-url='{ repo }/releases/download/{ version }/{ name }-{ target }-v{ version }.tgz'\npkg-fmt='tgz'\nbin-dir='{ name }-{ target }-v{ version }/{ bin }{ binary-ext }'\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(project.join("src/main.rs"), "fn main() {}\n").unwrap();
|
||||
fs::write(project.join(".gitignore"), "/target/\n/dist/\n").unwrap();
|
||||
fs::write(project.join("release.Cross.toml"), "[build]\n").unwrap();
|
||||
fs::write(
|
||||
project.join("rust-release.toml"),
|
||||
"[release]\ntag-prefix=''\n[build]\ntargets=['x86_64-unknown-linux-gnu','x86_64-pc-windows-gnu','aarch64-apple-darwin','x86_64-apple-darwin']\nprofile='release'\nlocked=true\nfeatures=['serial']\n[package]\nformat='both'\n",
|
||||
)
|
||||
.unwrap();
|
||||
executable(&tools.join("cargo-binstall"), "#!/bin/sh\nexit 0\n");
|
||||
executable(
|
||||
&tools.join("cross"),
|
||||
"#!/bin/sh\nset -eu\n[ -n \"${CROSS_CONFIG:-}\" ]\ntarget=\ntarget_dir=\nwhile [ $# -gt 0 ]; do case $1 in --target) shift; target=$1;; --target-dir) shift; target_dir=$1;; --features) shift; [ \"$1\" = serial ];; esac; shift; done\ncase $target in *windows*) suffix=.exe;; *) suffix=;; esac\nmkdir -p \"$target_dir/$target/release\"\nprintf cross >\"$target_dir/$target/release/demo$suffix\"\nprintf 'cross:%s\\n' \"$target\" >>\"$CROSS_LOG\"\n",
|
||||
);
|
||||
executable(
|
||||
&tools.join("docker"),
|
||||
r#"#!/usr/bin/env python3
|
||||
import json, os, pathlib, struct, sys
|
||||
if sys.argv[1:] == ['info']:
|
||||
raise SystemExit(0)
|
||||
assert 'RELEASE_TEST_SECRET' not in os.environ
|
||||
if os.environ.get('ENGINE_STATUS'):
|
||||
raise SystemExit(int(os.environ['ENGINE_STATUS']))
|
||||
args=sys.argv[1:]
|
||||
assert args[0:2] == ['run','--rm'] and '-it' not in args
|
||||
assert not any('sock' in arg for arg in args)
|
||||
assert '--userns=keep-id' not in args
|
||||
assert 'serial' in args and '--locked' in args and '--release' in args
|
||||
mount=next(args[i+1] for i,arg in enumerate(args) if arg=='-v' and args[i+1].endswith(':/output'))
|
||||
host=pathlib.Path(mount[:-len(':/output')])
|
||||
target=args[args.index('--target')+1]
|
||||
cpu=0x0100000c if target.startswith('aarch64') else 0x01000007
|
||||
dependency=b'/usr/lib/libSystem.B.dylib\0'
|
||||
dylib_size=(24+len(dependency)+7)//8*8
|
||||
data=bytearray(32+24+dylib_size)
|
||||
data[:4]=bytes.fromhex('cffaedfe')
|
||||
struct.pack_into('<I',data,4,cpu); struct.pack_into('<I',data,12,2)
|
||||
struct.pack_into('<II',data,16,2,24+dylib_size)
|
||||
struct.pack_into('<IIIIII',data,32,0x32,24,1,13<<16,0,0)
|
||||
struct.pack_into('<IIIIII',data,56,0xc,dylib_size,24,0,0,0)
|
||||
data[80:80+len(dependency)]=dependency
|
||||
binary=host/'target'/target/'release'/'demo'
|
||||
binary.parent.mkdir(parents=True)
|
||||
binary.write_bytes(data); binary.chmod(0o755)
|
||||
print(json.dumps({'reason':'compiler-artifact','target':{'name':'demo','kind':['bin']},'executable':f'/output/target/{target}/release/demo'}))
|
||||
with open(os.environ['CROSS_LOG'],'a') as stream: stream.write('zig:'+target+'\n')
|
||||
"#,
|
||||
);
|
||||
git(&project, &["init", "-q"]);
|
||||
git(&project, &["config", "user.name", "Test"]);
|
||||
git(&project, &["config", "user.email", "test@example.com"]);
|
||||
git(&project, &["config", "commit.gpgsign", "false"]);
|
||||
assert!(run(Command::new("cargo")
|
||||
.arg("generate-lockfile")
|
||||
.current_dir(&project))
|
||||
.status
|
||||
.success());
|
||||
git(&project, &["add", "."]);
|
||||
git(&project, &["commit", "-qm", "fixture"]);
|
||||
git(&project, &["tag", "2.3.4"]);
|
||||
|
||||
let path = format!("{}:{}", tools.display(), env::var("PATH").unwrap());
|
||||
let dry = run(Command::new(env!("CARGO_BIN_EXE_rust_realease"))
|
||||
.arg(&project)
|
||||
.args(["--dry-run"])
|
||||
.env("PATH", &path)
|
||||
.env("CROSS_CONTAINER_ENGINE", "docker")
|
||||
.env("CROSS_CONFIG", project.join("release.Cross.toml"))
|
||||
.env("CROSS_LOG", &log));
|
||||
let dry_stdout = assert_success(&dry);
|
||||
assert!(dry_stdout.contains("cargo-zigbuild container"));
|
||||
assert!(dry_stdout.contains("build backend: Cross"));
|
||||
assert!(!log.exists());
|
||||
|
||||
let output = run(Command::new(env!("CARGO_BIN_EXE_rust_realease"))
|
||||
.arg(&project)
|
||||
.args(["--compile-only", "--skip-tests"])
|
||||
.env("PATH", &path)
|
||||
.env("CROSS_CONTAINER_ENGINE", "docker")
|
||||
.env("CROSS_CONFIG", project.join("release.Cross.toml"))
|
||||
.env("CROSS_LOG", &log)
|
||||
.env("RELEASE_TEST_SECRET", "must-not-reach-compilers")
|
||||
.env_remove("GITEA_TOKEN"));
|
||||
let stdout = assert_success(&output);
|
||||
assert!(stdout.contains("runtime skipped (no compatible macOS runner)"));
|
||||
let routes = fs::read_to_string(&log).unwrap();
|
||||
assert_eq!(
|
||||
routes
|
||||
.lines()
|
||||
.filter(|line| line.starts_with("cross:"))
|
||||
.count(),
|
||||
2
|
||||
);
|
||||
assert_eq!(
|
||||
routes
|
||||
.lines()
|
||||
.filter(|line| line.starts_with("zig:"))
|
||||
.count(),
|
||||
2
|
||||
);
|
||||
let artifact_dir = fs::read_dir(project.join("dist"))
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.path())
|
||||
.find(|path| path.is_dir() && path.join("SHA256SUMS").is_file())
|
||||
.unwrap();
|
||||
let checksums = fs::read_to_string(artifact_dir.join("SHA256SUMS")).unwrap();
|
||||
assert_eq!(checksums.lines().count(), 8);
|
||||
for target in ["aarch64-apple-darwin", "x86_64-apple-darwin"] {
|
||||
assert!(artifact_dir
|
||||
.join(format!("demo-{target}-v2.3.4.tgz"))
|
||||
.is_file());
|
||||
let binary = artifact_dir.join(format!("demo-{target}"));
|
||||
assert!(binary.is_file());
|
||||
assert_ne!(
|
||||
fs::metadata(binary).unwrap().permissions().mode() & 0o111,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
let cached = run(Command::new(env!("CARGO_BIN_EXE_rust_realease"))
|
||||
.arg(&project)
|
||||
.args(["--compile-only", "--skip-tests"])
|
||||
.env("PATH", path)
|
||||
.env("CROSS_CONTAINER_ENGINE", "docker")
|
||||
.env("CROSS_CONFIG", project.join("release.Cross.toml"))
|
||||
.env("CROSS_LOG", &log));
|
||||
assert!(assert_success(&cached).contains("Reusing completed release build"));
|
||||
assert_eq!(fs::read_to_string(log).unwrap().lines().count(), 4);
|
||||
|
||||
let failed = run(Command::new(env!("CARGO_BIN_EXE_rust_realease"))
|
||||
.arg(&project)
|
||||
.args(["--compile-only", "--skip-tests", "--recompile"])
|
||||
.env(
|
||||
"PATH",
|
||||
format!("{}:{}", tools.display(), env::var("PATH").unwrap()),
|
||||
)
|
||||
.env("CROSS_CONTAINER_ENGINE", "docker")
|
||||
.env("CROSS_CONFIG", project.join("release.Cross.toml"))
|
||||
.env("CROSS_LOG", temporary.path().join("failed.log"))
|
||||
.env("ENGINE_STATUS", "42"));
|
||||
assert!(!failed.status.success());
|
||||
assert!(!String::from_utf8_lossy(&failed.stdout).contains("Packaged"));
|
||||
assert!(String::from_utf8_lossy(&failed.stderr).contains("exit status: 42"));
|
||||
}
|
||||
|
|
@ -1,108 +0,0 @@
|
|||
#![cfg(unix)]
|
||||
|
||||
use std::{
|
||||
env, fs,
|
||||
os::unix::fs::PermissionsExt,
|
||||
path::Path,
|
||||
process::{Command, Output},
|
||||
};
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
const DEVELOPMENT_ORIGIN: &str = "git@git.dandaedre.com:idle-org/rust_realease.git";
|
||||
const RELEASE_CONFIG: &str = "[[repository.fallbacks]]\n\
|
||||
origin = \"git@git.dandaedre.com:idle-org/rust_realease.git\"\n\
|
||||
fallback-url = \"https://proxy.dandaedre.com/gitea/idle-org/rust_realease\"\n";
|
||||
|
||||
fn run(command: &mut Command) -> Output {
|
||||
command.output().expect("test command should start")
|
||||
}
|
||||
|
||||
fn git(repository: &Path, arguments: &[&str]) -> Output {
|
||||
run(Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(repository)
|
||||
.args(arguments))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn release_wrapper_passes_the_token_and_preserves_the_ssh_origin() {
|
||||
let temporary = TempDir::new().expect("temporary directory should be created");
|
||||
let repository = temporary.path().join("repository");
|
||||
let fake_bin = temporary.path().join("bin");
|
||||
fs::create_dir_all(&repository).expect("repository directory should be created");
|
||||
fs::create_dir_all(&fake_bin).expect("fake binary directory should be created");
|
||||
|
||||
for name in ["release.sh", "rust-release.toml"] {
|
||||
fs::copy(
|
||||
Path::new(env!("CARGO_MANIFEST_DIR")).join(name),
|
||||
repository.join(name),
|
||||
)
|
||||
.expect("release file should be copied");
|
||||
}
|
||||
fs::write(
|
||||
repository.join("Cargo.toml"),
|
||||
"[package]\nname = \"rust_realease\"\nversion = \"0.1.3\"\n",
|
||||
)
|
||||
.expect("manifest should be written");
|
||||
fs::write(repository.join("token.txt"), "test-token\n").expect("token file should be written");
|
||||
|
||||
let fake_release = fake_bin.join("rust_realease");
|
||||
fs::write(
|
||||
&fake_release,
|
||||
"#!/bin/sh\n\
|
||||
[ \"$GITEA_TOKEN\" = test-token ] || exit 91\n\
|
||||
git -C \"$1\" remote get-url origin >\"$OBSERVED_ORIGIN\"\n\
|
||||
cat \"$1/rust-release.toml\" >\"$OBSERVED_CONFIG\"\n\
|
||||
printf '%s\\n' \"$@\" >\"$OBSERVED_ARGS\"\n",
|
||||
)
|
||||
.expect("fake release executable should be written");
|
||||
for path in [&fake_release, &repository.join("release.sh")] {
|
||||
let mut permissions = fs::metadata(path)
|
||||
.expect("script metadata should be readable")
|
||||
.permissions();
|
||||
permissions.set_mode(0o755);
|
||||
fs::set_permissions(path, permissions).expect("script should be executable");
|
||||
}
|
||||
|
||||
assert!(git(&repository, &["init", "--quiet"]).status.success());
|
||||
assert!(git(
|
||||
&repository,
|
||||
&["remote", "add", "origin", DEVELOPMENT_ORIGIN]
|
||||
)
|
||||
.status
|
||||
.success());
|
||||
|
||||
let observed_origin = temporary.path().join("observed-origin.txt");
|
||||
let observed_config = temporary.path().join("observed-config.toml");
|
||||
let observed_args = temporary.path().join("observed-args.txt");
|
||||
let path = format!(
|
||||
"{}:{}",
|
||||
fake_bin.display(),
|
||||
env::var("PATH").expect("PATH should be available")
|
||||
);
|
||||
let output = run(Command::new(repository.join("release.sh"))
|
||||
.arg(repository.join("token.txt"))
|
||||
.arg("--dry-run")
|
||||
.env("PATH", path)
|
||||
.env("OBSERVED_ORIGIN", &observed_origin)
|
||||
.env("OBSERVED_CONFIG", &observed_config)
|
||||
.env("OBSERVED_ARGS", &observed_args));
|
||||
|
||||
assert!(output.status.success());
|
||||
assert_eq!(
|
||||
fs::read_to_string(&observed_origin)
|
||||
.expect("fake release should record the origin")
|
||||
.trim(),
|
||||
DEVELOPMENT_ORIGIN
|
||||
);
|
||||
assert_eq!(
|
||||
fs::read_to_string(&observed_config).expect("fake release should record the configuration"),
|
||||
RELEASE_CONFIG
|
||||
);
|
||||
let arguments =
|
||||
fs::read_to_string(&observed_args).expect("fake release should record publisher arguments");
|
||||
assert!(arguments.lines().any(|argument| argument == "both"));
|
||||
assert!(arguments.lines().any(|argument| argument == "--dry-run"));
|
||||
assert!(!arguments.lines().any(|argument| argument == "--prerelease"));
|
||||
}
|
||||
Loading…
Reference in New Issue