use anyhow::Result;
use super::{GeneratedFile, Mechanism, MechanismContext, StepResult};
pub struct InstallSh;
impl Mechanism for InstallSh {
fn id(&self) -> &'static str {
"install_sh"
}
fn description(&self) -> &'static str {
"scripts/install.sh — curl installer with SHA256 verify"
}
fn generate(&self, ctx: &MechanismContext<'_>) -> Result<StepResult> {
let p = &ctx.config.project;
let repo = ctx.config.github_slug();
let bin = &p.binary_name;
let env_prefix = bin.to_uppercase().replace('-', "_");
let script = format!(
r#"#!/usr/bin/env bash
# Install {bin} from GitHub Releases (no Rust toolchain required).
# Usage:
# curl -fsSL https://raw.githubusercontent.com/{repo}/main/scripts/install.sh | sh
# {env_prefix}_VERSION=v{version} sh scripts/install.sh
set -euo pipefail
REPO="${{{env_prefix}_REPO:-{repo}}}"
INSTALL_DIR="${{{env_prefix}_INSTALL_DIR:-${{HOME}}/.local/bin}}"
VERSION="${{{env_prefix}_VERSION:-}}"
detect_platform() {{
local os arch
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "${{os}}" in
linux) os="linux" ;;
darwin) os="macos" ;;
mingw*|msys*|cygwin*) os="windows" ;;
*) echo "error: unsupported OS: $(uname -s)" >&2; exit 1 ;;
esac
case "${{arch}}" in
x86_64|amd64) arch="x86_64" ;;
aarch64|arm64) arch="aarch64" ;;
*) echo "error: unsupported architecture: ${{arch}}" >&2; exit 1 ;;
esac
echo "${{os}}-${{arch}}"
}}
latest_tag() {{
curl -fsSL "https://api.github.com/repos/${{REPO}}/releases/latest" \
| sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -1
}}
download() {{
local url="$1" dest="$2"
curl -fsSL "${{url}}" -o "${{dest}}"
}}
verify_checksum() {{
local archive="$1" sums="$2"
local base actual expected
base="$(basename "${{archive}}")"
expected="$(awk -v f="${{base}}" '$2 == f {{ print $1; exit }}' "${{sums}}" || true)"
if [[ -z "${{expected}}" ]]; then
echo "warning: no checksum entry for ${{base}}; skipping verification" >&2
return 0
fi
if command -v sha256sum >/dev/null 2>&1; then
actual="$(sha256sum "${{archive}}" | awk '{{print $1}}')"
elif command -v shasum >/dev/null 2>&1; then
actual="$(shasum -a 256 "${{archive}}" | awk '{{print $1}}')"
else
echo "warning: no sha256 tool found; skipping verification" >&2
return 0
fi
if [[ "${{actual}}" != "${{expected}}" ]]; then
echo "error: checksum mismatch for ${{base}}" >&2
exit 1
fi
}}
main() {{
if [[ -z "${{VERSION}}" ]]; then
VERSION="$(latest_tag)"
fi
VERSION="${{VERSION#v}}"
local tag="v${{VERSION}}"
local platform archive_name ext
platform="$(detect_platform)"
if [[ "${{platform}}" == windows-* ]]; then
ext="zip"
else
ext="tar.gz"
fi
archive_name="{bin}-${{tag}}-${{platform}}.${{ext}}"
local tmp
tmp="$(mktemp -d)"
trap 'rm -rf "${{tmp}}"' EXIT
download "https://github.com/${{REPO}}/releases/download/${{tag}}/${{archive_name}}" \
"${{tmp}}/${{archive_name}}"
download "https://github.com/${{REPO}}/releases/download/${{tag}}/SHA256SUMS" \
"${{tmp}}/SHA256SUMS" || true
if [[ -f "${{tmp}}/SHA256SUMS" ]]; then
verify_checksum "${{tmp}}/${{archive_name}}" "${{tmp}}/SHA256SUMS"
fi
mkdir -p "${{INSTALL_DIR}}"
if [[ "${{ext}}" == "tar.gz" ]]; then
tar -xzf "${{tmp}}/${{archive_name}}" -C "${{tmp}}"
install -m 0755 "${{tmp}}/{bin}" "${{INSTALL_DIR}}/{bin}"
else
echo "error: use the Windows zip from Releases on Windows" >&2
exit 1
fi
echo "installed ${{INSTALL_DIR}}/{bin} (${{tag}})"
}}
main "$@"
"#,
version = p.version,
);
let path = format!(
"{}/install.sh",
ctx.config.paths.scripts_dir.trim_end_matches('/')
);
Ok(StepResult {
files: vec![GeneratedFile {
relative_path: path,
contents: script,
executable: true,
}],
notes: vec!["Document curl|sh install as the primary Linux path.".into()],
})
}
}