diff --git a/Cargo.lock b/Cargo.lock index cf72bf8..dd8dac2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,10 +3,15 @@ version = 3 [[package]] -name = "ascii" -version = "0.8.7" +name = "atty" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97be891acc47ca214468e09425d02cef3af2c94d0d82081cd02061f996802f14" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] [[package]] name = "autocfg" @@ -35,6 +40,38 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "3.0.0-beta.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "indexmap", + "lazy_static", + "os_str_bytes", + "strsim", + "termcolor", + "textwrap", + "unicode-width", + "vec_map", +] + +[[package]] +name = "clap_derive" +version = "3.0.0-beta.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "form_urlencoded" version = "1.0.1" @@ -60,6 +97,30 @@ dependencies = [ "url", ] +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + [[package]] name = "idna" version = "0.2.3" @@ -71,6 +132,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "jobserver" version = "0.1.22" @@ -80,6 +151,12 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.95" @@ -160,6 +237,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "os_str_bytes" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -172,14 +255,91 @@ version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rust-cli" version = "0.1.0" dependencies = [ - "ascii", + "clap", "git2", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" +dependencies = [ + "unicode-width", +] + [[package]] name = "tinyvec" version = "1.2.0" @@ -213,6 +373,24 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-segmentation" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + [[package]] name = "url" version = "2.2.2" @@ -230,3 +408,46 @@ name = "vcpkg" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "025ce40a007e1907e58d5bc1a594def78e5573bb0b1160bc389634e8f12e4faa" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index eff6eaf..6026921 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ edition = "2018" [dependencies] git2 = "0.13.20" -ascii = "0.8.6" +clap = "3.0.0-beta.2" diff --git a/src/main.rs b/src/main.rs index c8cea0f..70acb40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,16 @@ -//extern crate clap; +extern crate clap; +use clap::ArgMatches; +use clap::{App, Arg}; use git2::BranchType; use git2::Repository; -use std::env; -//use clap::{Arg, App, SubCommand}; +use std::fs::File; +use std::io::Write; //use ascii:: +const REPO_VERSION: &str = "repo_version"; +const TYPESCRIPT: &str = "typescript"; + fn get_newst_tag_test(repo_path: &str) -> (Option, String) { let repo = match Repository::open(repo_path) { Ok(repo) => repo, @@ -137,18 +142,90 @@ fn get_head_tag(repo_path: &str) -> (Option, String) { */ } - println!("head: {:?}, commit: {:?}", commit.id(), commit); - (None, String::from("commit_id")) + // println!("head: {:?}, commit: {:?}", commit.id(), commit); + (None, commit.id().to_string()) +} + +fn parse_arguments() -> ArgMatches { + let repo_version = App::new(REPO_VERSION) + .about( + "自动生成目的语言代码\n + 如typescript: export const VERSION = '0.0.1-beta@xxx'", + ) + .arg( + Arg::new("repo_url") + .index(1) + .about("代码仓库地址: 1. path/to/git/repo"), + ) + .arg(Arg::new("out_file").index(2).about("要生成的文件")) + .arg( + Arg::new("language") + .index(3) + .about("目的编程语言") + .default_value(TYPESCRIPT), // default typescript + ); + let matches = App::new("rust-cli") + .version("0.1.0") + .author("Daryl.Xu ") + .about("Some useful command for development") + .subcommand(repo_version) + // .subcommand(Subcommand::with_name("test") + // .about("controls testing features") + // .version("1.3") + // .author("Someone E. ") + // .arg_from_usage("-d, --debug 'Print debug information'")) + .get_matches(); + matches } fn main() { - let args: Vec = env::args().collect(); - let repo_url = &args[1].to_string(); - // let (tag_name, id) = get_head_tag("/home/daryl/git/words-picker"); - // let (tag_name, id) = get_head_tag("/home/daryl/git/rust-cli"); - let (version_tag, commit_id) = get_head_tag(repo_url); - match version_tag { - Some(tag) => println!("tag name: {} \ncommit-id:{}", tag, commit_id), - None => println!("have no tag, {}", commit_id), - }; + let app_matches = parse_arguments(); + + match app_matches.subcommand() { + Some((name, matches)) => { + println!("Subcommand calling: {}", name); + // TODO 把各种if替换成match + if name == REPO_VERSION { + // 执行 REPO_VERSION相关动作 + let repo_url = matches.value_of("repo_url").unwrap(); + let out_file = matches.value_of("out_file").unwrap(); + let language = matches.value_of("language").unwrap(); + + // Get the version + let (version_tag, commit_id) = get_head_tag(repo_url); + let version; + match version_tag { + Some(tag) => { + println!("tag name: {} \ncommit-id:{}", tag, commit_id); + version = format!("{}@{}", tag, commit_id); + }, + None => { + println!("have no tag, {}", commit_id); + version = format!("{}", commit_id) + }, + }; + + // Generate the code + let code; + if language == TYPESCRIPT { + code = format!("export const VERSION = '{}'\n", version); + } else { + panic!("Language '{}' Not support yet", language); + } + + // write the code to file + let mut file = match File::create(&out_file) { + Ok(file) => file, + Err(e) => { + panic!("Failed to create file: {}. \n error:{}", out_file, e); + } + }; + file.write_all(code.as_bytes()).unwrap(); + } else { + panic!("Subcommand '{}' is not support yet!", name); + } + + } + None => println!("Nothing to do!"), + } }