diff --git a/Cargo.lock b/Cargo.lock index 496f310..72c29aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -973,7 +973,7 @@ dependencies = [ [[package]] name = "rust_realease" -version = "0.1.0" +version = "0.1.1" dependencies = [ "clap", "directories", diff --git a/Cargo.toml b/Cargo.toml index d66722c..86b912e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,14 @@ [package] name = "rust_realease" -version = "0.1.0" +version = "0.1.1" edition = "2021" publish = false +repository = "https://proxy.dandaedre.com/gitea/idle-org/rust_realease" + +[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 }" [dependencies] clap = { version = "4.5", features = ["derive"] } diff --git a/Cross.toml b/Cross.toml new file mode 100644 index 0000000..74bf6a5 --- /dev/null +++ b/Cross.toml @@ -0,0 +1,4 @@ +[build] + +[build.env] +passthrough = ["RUSTUP_TOOLCHAIN"] diff --git a/README.md b/README.md index df69133..320a1a2 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,16 @@ export GITEA_TOKEN rust_realease ./my-tool ``` +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 --dry-run +./release.sh ../gitea_token.txt +``` + For a remote repository, select the exact tag to check out: ```bash diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..819e3b0 --- /dev/null +++ b/release.sh @@ -0,0 +1,43 @@ +#!/bin/sh +set -eu + +usage() { + echo "usage: $0 [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 "$@" diff --git a/rust-release.toml b/rust-release.toml new file mode 100644 index 0000000..3b81392 --- /dev/null +++ b/rust-release.toml @@ -0,0 +1,3 @@ +[[repository.fallbacks]] +origin = "git@git.dandaedre.com:idle-org/rust_realease.git" +fallback-url = "https://proxy.dandaedre.com/gitea/idle-org/rust_realease" diff --git a/tests/release_wrapper.rs b/tests/release_wrapper.rs new file mode 100644 index 0000000..c9e2e2b --- /dev/null +++ b/tests/release_wrapper.rs @@ -0,0 +1,108 @@ +#![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.1\"\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")); +}