master
AdityaAngajala
Preserve source for adom-molecule-1.2.0
94877ba
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
//! adom-molecule is deprecated; 1.2.0 is its last release.
//!
//! Molecules live on the Adom wiki: the wiki's model registry builds a
//! molecule's model, footprint and symbol from the sources its page carries,
//! and Hydrogen reads them from there. Each old command says where its work
//! went, and `import` runs `adom-wiki model import` with the same arguments.
use clap::{Args, Parser, Subcommand};
use std::process::{Command, ExitCode, Stdio};
#[derive(Parser)]
#[command(
name = "adom-molecule",
version,
about = "Deprecated: molecules live on the Adom wiki. Publish one with the molecule-publish skill or adom-wiki, and bring it into Hydrogen with `adom-wiki model import`"
)]
struct Cli {
#[command(subcommand)]
command: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
/// Deprecated: runs `adom-wiki model import` with the same arguments
Import {
/// Wiki page reference: <owner>/<slug> or a bare slug
slug: String,
/// Hydrogen owner to import under: your username or an org you belong to
#[arg(long)]
owner: String,
/// Molecule name in Hydrogen
#[arg(long)]
name: Option<String>,
},
/// Deprecated: publish the sources on the wiki; its registry builds the model
Create(Unused),
/// Deprecated: the wiki's registry builds the model
Optimize(Unused),
/// Deprecated: the wiki's registry builds the model
OptimizeFusion(Unused),
/// Deprecated: Hydrogen lists your molecules
List(Unused),
/// Deprecated: Hydrogen deletes your molecules
Delete(Unused),
/// Deprecated: use adom-wiki
Health(Unused),
/// Deprecated: use adom-wiki
Install(Unused),
}
/// The arguments an old command was given. They are read and not used, so a
/// script calling the old command gets the notice, not a usage error.
#[derive(Args)]
struct Unused {
#[arg(trailing_var_arg = true, allow_hyphen_values = true, hide = true)]
args: Vec<String>,
}
const NOTICE: &str =
"adom-molecule is deprecated and no longer maintained; 1.2.0 is its last release. Molecules live on the Adom wiki.";
const MAKE: &str = "Publish the molecule's sources on its wiki page (the STEP with its .kicad_pcb or .brd, \
<slug>-fab.zip and export.json, or a circuit.json) with the molecule-publish skill or `adom-wiki pkg publish --wait`. \
The wiki's model registry builds the model, footprint and symbol from them. Then bring it into Hydrogen:\n \
adom-wiki model import <owner>/<slug> --owner <hydrogen-owner>";
const LIBRARY: &str = "Hydrogen lists and deletes your molecules at https://hydrogen.adom.inc/users/<you>/molecules \
(an org's at /orgs/<org>/molecules). Bring a wiki molecule in with:\n \
adom-wiki model import <owner>/<slug> --owner <hydrogen-owner>";
const TOOLS: &str = "Use adom-wiki: `adom-wiki doctor` checks it and your sign-in, `adom-wiki self-update` updates it, \
and `adom-wiki model import <owner>/<slug> --owner <hydrogen-owner>` brings a wiki molecule into Hydrogen.";
/// The `adom-wiki` arguments an old `import` stands for.
fn import_args(slug: &str, owner: &str, name: Option<&str>) -> Vec<String> {
let mut args = vec!["model", "import", slug, "--owner", owner];
if let Some(n) = name {
args.extend(["--name", n]);
}
args.into_iter().map(str::to_string).collect()
}
/// An argument as it would be typed in a shell: quoted when it holds
/// anything but letters, digits and `-_./@:=`.
fn shell_word(s: &str) -> String {
let plain = !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || "-_./@:=".contains(c));
if plain {
s.to_string()
} else {
format!("'{}'", s.replace('\'', r"'\''"))
}
}
/// Whether the `adom-wiki` on PATH has `model import` (1.8.0 and newer).
fn adom_wiki_imports() -> bool {
Command::new("adom-wiki")
.args(["model", "import", "--help"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
fn import(slug: &str, owner: &str, name: Option<&str>) -> ExitCode {
let args = import_args(slug, owner, name);
let typed = args.iter().map(|a| shell_word(a)).collect::<Vec<_>>().join(" ");
if !adom_wiki_imports() {
eprintln!(
"{NOTICE}\n`adom-molecule import` moved to `adom-wiki model import`, which needs adom-wiki 1.8.0 or newer \
(`adom-wiki self-update` updates an older one). Then run:\n adom-wiki {typed}"
);
return ExitCode::FAILURE;
}
eprintln!("{NOTICE}\nRunning `adom-wiki {typed}`; run that directly from now on.");
match Command::new("adom-wiki").args(&args).status() {
Ok(s) => ExitCode::from(s.code().unwrap_or(1).clamp(0, 255) as u8),
Err(e) => {
eprintln!("could not run adom-wiki: {e}");
ExitCode::FAILURE
}
}
}
fn main() -> ExitCode {
let where_now = match Cli::parse().command {
Cmd::Import { slug, owner, name } => return import(&slug, &owner, name.as_deref()),
Cmd::Create(_) | Cmd::Optimize(_) | Cmd::OptimizeFusion(_) => MAKE,
Cmd::List(_) | Cmd::Delete(_) => LIBRARY,
Cmd::Health(_) | Cmd::Install(_) => TOOLS,
};
eprintln!("{NOTICE}\n{where_now}");
ExitCode::FAILURE
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn import_maps_to_adom_wiki_model_import() {
assert_eq!(
import_args("drew2/e2e-kicad-solonoid", "drew2", None),
["model", "import", "drew2/e2e-kicad-solonoid", "--owner", "drew2"]
);
assert_eq!(
import_args("adom/foo", "acme", Some("Foo Board")),
["model", "import", "adom/foo", "--owner", "acme", "--name", "Foo Board"]
);
}
#[test]
fn shell_words_quote_only_what_needs_it() {
assert_eq!(shell_word("adom/foo-1.2"), "adom/foo-1.2");
assert_eq!(shell_word("Foo Board"), "'Foo Board'");
assert_eq!(shell_word("it's"), r"'it'\''s'");
assert_eq!(shell_word(""), "''");
}
#[test]
fn old_commands_parse_with_their_old_arguments() {
for argv in [
vec!["adom-molecule", "create", "--name", "X", "--owner", "adom", "--glb", "x.glb"],
vec!["adom-molecule", "optimize-fusion", "--owner", "a", "--name", "b", "--version", "v1"],
vec!["adom-molecule", "list"],
vec!["adom-molecule", "delete", "--owner", "a", "--name", "b"],
vec!["adom-molecule", "install", "--bin-dir", "/tmp"],
] {
assert!(Cli::try_parse_from(&argv).is_ok(), "{argv:?}");
}
}
}