110 lines
4.4 KiB
Rust
110 lines
4.4 KiB
Rust
use std::fs;
|
|
use std::collections::HashMap;
|
|
use std::time::Duration;
|
|
use clap::{ArgMatches, SubCommand, App, Arg};
|
|
use crates_io_api::{SyncClient, CrateResponse};
|
|
use crate::cmd::{Command, CommandResult};
|
|
|
|
pub struct CommandImpl;
|
|
|
|
impl Command for CommandImpl {
|
|
|
|
fn name(&self) -> &str { "crate" }
|
|
|
|
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
|
SubCommand::with_name(self.name()).about("Crate subcommand")
|
|
.arg(Arg::with_name("add").short("A").long("add").multiple(true).takes_value(true).help("Add crates to project"))
|
|
}
|
|
|
|
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandResult {
|
|
let crate_io_client = SyncClient::new(
|
|
"cargotool (https://hatter.me/)", Duration::from_millis(1000)
|
|
)?;
|
|
|
|
let toml_str = match fs::read_to_string("Cargo.toml") {
|
|
Ok(s) => s, Err(e) => {
|
|
failure!("Read file Cargo.toml failed: {}", e);
|
|
return Ok(-1);
|
|
},
|
|
};
|
|
let mut toml: toml::Value = match toml_str.parse() {
|
|
Ok(t) => t, Err(e) => {
|
|
failure!("Parse Cargo.toml failed: {}", e);
|
|
return Ok(-1);
|
|
},
|
|
};
|
|
|
|
let mut exists_dependencies_map: HashMap<String, String> = HashMap::new();
|
|
if let toml::Value::Table(cargo_map) = &toml {
|
|
if let Some(toml::Value::Table(dependencies)) = cargo_map.get("dependencies") {
|
|
for (k, v) in dependencies.iter() {
|
|
match v {
|
|
toml::Value::String(s) => {
|
|
exists_dependencies_map.insert(k.clone(), s.clone());
|
|
},
|
|
toml::Value::Table(t) => {
|
|
if let Some(toml::Value::String(version)) = t.get("version") {
|
|
exists_dependencies_map.insert(k.clone(), version.clone());
|
|
}
|
|
},
|
|
_ => (),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut is_updated = false;
|
|
if let Some(add_crates) = sub_arg_matches.values_of("add") {
|
|
for c in add_crates {
|
|
if exists_dependencies_map.contains_key(c) {
|
|
warning!("Crate: {} exists!", c);
|
|
} else {
|
|
if let Some(crate_response) = find_create(&crate_io_client, c) {
|
|
if let toml::Value::Table(cargo_map) = &mut toml {
|
|
if let Some(toml::Value::Table(dependencies)) = cargo_map.get_mut("dependencies") {
|
|
is_updated = true;
|
|
let crate_data = crate_response.crate_data;
|
|
dependencies.insert(crate_data.id, toml::Value::String(crate_data.max_version));
|
|
success!("Adding crate: {}", c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if is_updated {
|
|
match toml::to_string_pretty(&toml) {
|
|
Err(e) => failure!("Generate toml failed: {}", e),
|
|
Ok(t) => match fs::write("Cargo.toml.new", t) {
|
|
Err(e) => failure!("Write to file: Carto.toml, failed: {}", e),
|
|
Ok(_) => success!("Write to file: Cargo.toml success!"),
|
|
},
|
|
}
|
|
}
|
|
|
|
Ok(0)
|
|
}
|
|
}
|
|
|
|
fn find_create(crate_io_client: &SyncClient, crate_name: &str) -> Option<CrateResponse> {
|
|
let c = match crate_io_client.get_crate(crate_name) {
|
|
Ok(c) => c, Err(e) => {
|
|
failure!("Crate not found or network error: {}", e);
|
|
return None;
|
|
},
|
|
};
|
|
let crate_data = &c.crate_data;
|
|
if crate_data.id != crate_name {
|
|
failure!("Crate not found: {}", crate_name);
|
|
return None;
|
|
}
|
|
if crate_data.downloads < 1000 {
|
|
failure!("Crate: {}, downloaded less then 1,000, is: {}, max version: {}", crate_name, crate_data.downloads, crate_data.max_version);
|
|
} else if crate_data.downloads < 10000 {
|
|
warning!("Crate: {}, downloaded less then 10,000, is: {}, max version: {}", crate_name, crate_data.downloads, crate_data.max_version);
|
|
} else {
|
|
success!("Crate: {}, downloaded more then 10,000, is: {}, max version: {}", crate_name, crate_data.downloads, crate_data.max_version);
|
|
}
|
|
Some(c)
|
|
} |