98 lines
3.0 KiB
Rust
98 lines
3.0 KiB
Rust
use {
|
|
clap::ArgMatches,
|
|
hpk::{cli::cli, Creator, Dependency, Message, Specs, Version},
|
|
ron::ser::{to_writer_pretty, PrettyConfig},
|
|
std::{env, error::Error, fs::File, io::{BufWriter, self}, path::PathBuf, sync::mpsc},
|
|
};
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
let matches = cli().get_matches();
|
|
match matches.subcommand() {
|
|
Some(("create", matches)) => create(matches)?,
|
|
Some(("init", matches)) => {
|
|
let specsfile = init(matches)?;
|
|
println!("Created specsfile {}", specsfile.display());
|
|
}
|
|
Some(("search", matches)) => search(matches)?,
|
|
Some(("install", matches)) => install(matches)?,
|
|
Some(("remove", matches)) => remove(matches)?,
|
|
Some(("upgrade", _)) => upgrade()?,
|
|
_ => {}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn create(matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
|
|
let dir = PathBuf::from(matches.get_one::<String>("directory").unwrap().to_string());
|
|
let outdir = if let Some(d) = matches.get_one::<String>("output") {
|
|
PathBuf::from(&d.to_string())
|
|
} else {
|
|
env::current_dir()?
|
|
};
|
|
let mut specs = if let Some(s) = matches.get_one::<String>("specs") {
|
|
let path = PathBuf::from(s.to_string());
|
|
let fd = File::open(path)?;
|
|
ron::de::from_reader(fd)?
|
|
} else {
|
|
Specs::default()
|
|
};
|
|
if let Some(n) = matches.get_one::<String>("name") {
|
|
specs.name = n.to_string();
|
|
}
|
|
if let Some(v) = matches.get_one::<String>("package-version") {
|
|
specs.version = v.to_string().parse::<Version>()?;
|
|
}
|
|
if let Some(r) = matches.get_one::<u8>("release") {
|
|
specs.release = *r;
|
|
}
|
|
if let Some(deps) = matches.get_many::<String>("dependencies") {
|
|
for d in deps {
|
|
let d = Dependency {
|
|
name: d.to_string(),
|
|
version: (None, None),
|
|
};
|
|
specs.dependencies.push(d);
|
|
}
|
|
}
|
|
let creator = Creator::new(&dir, specs)?;
|
|
let (sender, receiver) = mpsc::channel();
|
|
creator.create(&outdir, sender)?;
|
|
for msg in receiver.iter() {
|
|
match msg {
|
|
Message::MemberAdded(_s) => {}
|
|
Message::Success(s) => println!("{s}"),
|
|
Message::Failure(s) => {
|
|
eprint!("{s}");
|
|
return Err(io::Error::new(io::ErrorKind::Other, s).into());
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn init(matches: &ArgMatches) -> Result<PathBuf, Box<dyn Error>> {
|
|
let specsfile = PathBuf::from("package.specs");
|
|
let cfg = PrettyConfig::new().struct_names(true);
|
|
let buf = File::create(&specsfile)?;
|
|
let writer = BufWriter::new(buf);
|
|
let specs = Specs::try_from(matches)?;
|
|
to_writer_pretty(writer, &specs, cfg)?;
|
|
Ok(specsfile)
|
|
}
|
|
|
|
fn search(_matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn install(_matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn remove(_matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
|
|
unimplemented!();
|
|
}
|
|
|
|
fn upgrade() -> Result<(), Box<dyn Error>> {
|
|
unimplemented!();
|
|
}
|