sastri 0.1.0

Scaffold multi-ecosystem packaging and CI/CD for Rust CLIs (Homebrew, Scoop, PyPI, npm, Hex, mise, asdf, …)
Documentation
use anyhow::Result;

use super::{GeneratedFile, Mechanism, MechanismContext, StepResult};

pub struct Asdf;

impl Mechanism for Asdf {
    fn id(&self) -> &'static str {
        "asdf"
    }

    fn description(&self) -> &'static str {
        "packaging/asdf-* plugin (list-all + install from Releases)"
    }

    fn generate(&self, ctx: &MechanismContext<'_>) -> Result<StepResult> {
        let p = &ctx.config.project;
        let bin = &p.binary_name;
        let repo = ctx.config.github_slug();
        let plugin_dir = format!(
            "{}/asdf-{bin}",
            ctx.config.paths.packaging_dir.trim_end_matches('/')
        );
        let asdf_repo = ctx
            .config
            .org
            .asdf_plugin_repo
            .clone()
            .unwrap_or_else(|| format!("{}/asdf-{bin}", p.github_owner));

        let list_all = format!(
            r#"#!/usr/bin/env bash
set -euo pipefail
# List release tags for {bin} (strip leading v)
curl -fsSL "https://api.github.com/repos/{repo}/releases" \
  | sed -n 's/.*"tag_name": *"v\?\([^"]*\)".*/\1/p' \
  | sort -V \
  | xargs
"#
        );

        let install = format!(
            r#"#!/usr/bin/env bash
set -euo pipefail
# asdf install {bin} <version>
version="${{ASDF_INSTALL_VERSION:?}}"
install_path="${{ASDF_INSTALL_PATH:?}}"
repo="${{ASDF_{env}_REPO:-{repo}}}"
tag="v${{version#v}}"

os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "${{os}}" in
  linux) platform_os="linux" ;;
  darwin) platform_os="macos" ;;
  *) echo "unsupported OS: ${{os}}" >&2; exit 1 ;;
esac
case "${{arch}}" in
  x86_64|amd64) platform_arch="x86_64" ;;
  aarch64|arm64) platform_arch="aarch64" ;;
  *) echo "unsupported arch: ${{arch}}" >&2; exit 1 ;;
esac

platform="${{platform_os}}-${{platform_arch}}"
archive="{bin}-${{tag}}-${{platform}}.tar.gz"
url="https://github.com/${{repo}}/releases/download/${{tag}}/${{archive}}"

tmp="$(mktemp -d)"
trap 'rm -rf "${{tmp}}"' EXIT
curl -fsSL "${{url}}" -o "${{tmp}}/${{archive}}"
tar -xzf "${{tmp}}/${{archive}}" -C "${{tmp}}"
mkdir -p "${{install_path}}/bin"
install -m 0755 "${{tmp}}/{bin}" "${{install_path}}/bin/{bin}"
"#,
            env = bin.to_uppercase().replace('-', "_"),
        );

        let readme = format!(
            r#"# asdf-{bin}

asdf plugin that installs `{bin}` from [GitHub Releases](https://github.com/{repo}/releases).

```bash
asdf plugin add {bin} https://github.com/{asdf_repo}.git
asdf install {bin} {version}
asdf global {bin} {version}
{bin} --version
```

Publish this directory to `{asdf_repo}` (or keep developing from the monorepo path).
"#,
            version = p.version,
        );

        Ok(StepResult {
            files: vec![
                GeneratedFile {
                    relative_path: format!("{plugin_dir}/bin/list-all"),
                    contents: list_all,
                    executable: true,
                },
                GeneratedFile {
                    relative_path: format!("{plugin_dir}/bin/install"),
                    contents: install,
                    executable: true,
                },
                GeneratedFile {
                    relative_path: format!("{plugin_dir}/bin/lib.sh"),
                    contents: "#!/usr/bin/env bash\n# shared helpers (optional)\n".into(),
                    executable: true,
                },
                GeneratedFile {
                    relative_path: format!("{plugin_dir}/README.md"),
                    contents: readme,
                    executable: false,
                },
                GeneratedFile {
                    relative_path: format!("{plugin_dir}/.gitignore"),
                    contents: "bin/*.local\n".into(),
                    executable: false,
                },
            ],
            notes: vec![format!(
                "Push packaging/asdf-{bin} to https://github.com/{asdf_repo}"
            )],
        })
    }
}