diff --git a/Cargo.lock b/Cargo.lock index b438198..75870f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -159,7 +159,17 @@ version = "1.0.1" dependencies = [ "anyhow", "cargo", + "cargo-clone-core", "clap 2.34.0", + "tempdir", +] + +[[package]] +name = "cargo-clone-core" +version = "0.1.0" +dependencies = [ + "anyhow", + "cargo", "semver", "tempdir", "walkdir", diff --git a/Cargo.toml b/Cargo.toml index eda5340..e3fceeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,25 +1,7 @@ -[package] -authors = ["Jan Likar "] -description = "A cargo subcommand to fetch the source code of a Rust crate" -documentation = "https://github.com/JanLikar/cargo-clone" -homepage = "https://github.com/JanLikar/cargo-clone" -keywords = ["cargo", "subcommand", "clone"] -license = "Apache-2.0/MIT" -name = "cargo-clone" -readme = "README.md" -repository = "https://github.com/JanLikar/cargo-clone" -version = "1.0.1" -edition = "2021" +[workspace] +members = ["cargo-clone", "cargo-clone-core"] -[dependencies] +[workspace.dependencies] anyhow = "1.0.52" cargo = "0.61.1" -clap = "2.34.0" -semver = "1.0.4" -walkdir = "2.3.2" - -[dev-dependencies] tempdir = "0.3.7" - -[features] -vendored-openssl = ["cargo/vendored-openssl"] diff --git a/cargo-clone-core/Cargo.toml b/cargo-clone-core/Cargo.toml new file mode 100644 index 0000000..d385fd5 --- /dev/null +++ b/cargo-clone-core/Cargo.toml @@ -0,0 +1,20 @@ +[package] +authors = ["Jan Likar "] +name = "cargo-clone-core" +version = "0.1.0" +edition = "2021" +description = "A library to fetch the source code of a Rust crate" +documentation = "https://github.com/JanLikar/cargo-clone" +homepage = "https://github.com/JanLikar/cargo-clone" +keywords = ["cargo", "subcommand", "clone"] +license = "Apache-2.0/MIT" +repository = "https://github.com/JanLikar/cargo-clone" + +[dependencies] +anyhow.workspace = true +cargo.workspace = true +semver = "1.0.4" +walkdir = "2.3.2" + +[dev-dependencies] +tempdir.workspace = true diff --git a/src/lib.rs b/cargo-clone-core/src/lib.rs similarity index 97% rename from src/lib.rs rename to cargo-clone-core/src/lib.rs index c7fe3f7..7118cb9 100644 --- a/src/lib.rs +++ b/cargo-clone-core/src/lib.rs @@ -282,11 +282,12 @@ mod tests { #[test] fn test_clone_directory() { - let from = Path::new("tests/data"); + let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let from = PathBuf::from(manifest_dir).join("tests/data"); let to = TempDir::new("cargo-clone-tests").unwrap(); let to_path = to.path(); - clone_directory(&from, &to_path).unwrap(); + clone_directory(&from, to_path).unwrap(); assert!(to_path.join("Cargo.toml").exists()); assert!(!to_path.join("cargo-ok").exists()); diff --git a/tests/data/.cargo-ok b/cargo-clone-core/tests/data/.cargo-ok similarity index 100% rename from tests/data/.cargo-ok rename to cargo-clone-core/tests/data/.cargo-ok diff --git a/tests/data/Cargo.toml b/cargo-clone-core/tests/data/Cargo.toml similarity index 100% rename from tests/data/Cargo.toml rename to cargo-clone-core/tests/data/Cargo.toml diff --git a/tests/test_clone.rs b/cargo-clone-core/tests/test_clone.rs similarity index 73% rename from tests/test_clone.rs rename to cargo-clone-core/tests/test_clone.rs index eb889e3..923c204 100644 --- a/tests/test_clone.rs +++ b/cargo-clone-core/tests/test_clone.rs @@ -12,7 +12,7 @@ fn test_from_registry() { let config = Config::default().unwrap(); - let crates = vec![cargo_clone::Crate::new( + let crates = vec![cargo_clone_core::Crate::new( String::from("cargo-clone"), Some(String::from("0.2.0")), )]; @@ -20,9 +20,9 @@ fn test_from_registry() { let git = false; let directory = Some(output_path.to_str().unwrap()); - let opts = cargo_clone::CloneOpts::new(&crates, &source_id, directory, git); + let opts = cargo_clone_core::CloneOpts::new(&crates, &source_id, directory, git); - cargo_clone::clone(&opts, &config).unwrap(); + cargo_clone_core::clone(&opts, &config).unwrap(); assert!(output_path.exists()); assert!(output_path.join("Cargo.toml").exists()); @@ -38,7 +38,7 @@ fn test_dir_path() { let config = Config::default().unwrap(); - let crates = vec![cargo_clone::Crate::new( + let crates = vec![cargo_clone_core::Crate::new( String::from("cargo-clone"), Some(String::from("0.2.0")), )]; @@ -46,9 +46,9 @@ fn test_dir_path() { let git = false; let directory = Some(format!("{}/", output_path.to_str().unwrap())); - let opts = cargo_clone::CloneOpts::new(&crates, &source_id, directory.as_deref(), git); + let opts = cargo_clone_core::CloneOpts::new(&crates, &source_id, directory.as_deref(), git); - cargo_clone::clone(&opts, &config).unwrap(); + cargo_clone_core::clone(&opts, &config).unwrap(); assert!(output_path.join("cargo-clone").exists()); assert!(output_path.join("cargo-clone").join("Cargo.toml").exists()); @@ -64,16 +64,16 @@ fn test_multi_crates() { let config = Config::default().unwrap(); let crates = vec![ - cargo_clone::Crate::new(String::from("cargo-clone"), Some(String::from("0.2.0"))), - cargo_clone::Crate::new(String::from("tokio"), None), + cargo_clone_core::Crate::new(String::from("cargo-clone"), Some(String::from("0.2.0"))), + cargo_clone_core::Crate::new(String::from("tokio"), None), ]; let source_id = SourceId::crates_io(&config).unwrap(); let git = false; let directory = Some(format!("{}/", output_path.to_str().unwrap())); - let opts = cargo_clone::CloneOpts::new(&crates, &source_id, directory.as_deref(), git); + let opts = cargo_clone_core::CloneOpts::new(&crates, &source_id, directory.as_deref(), git); - cargo_clone::clone(&opts, &config).unwrap(); + cargo_clone_core::clone(&opts, &config).unwrap(); assert!(output_path.join("cargo-clone").exists()); assert!(output_path.join("cargo-clone").join("Cargo.toml").exists()); diff --git a/cargo-clone/Cargo.toml b/cargo-clone/Cargo.toml new file mode 100644 index 0000000..e3b4f70 --- /dev/null +++ b/cargo-clone/Cargo.toml @@ -0,0 +1,24 @@ +[package] +authors = ["Jan Likar "] +description = "A cargo subcommand to fetch the source code of a Rust crate" +documentation = "https://github.com/JanLikar/cargo-clone" +homepage = "https://github.com/JanLikar/cargo-clone" +keywords = ["cargo", "subcommand", "clone"] +license = "Apache-2.0/MIT" +name = "cargo-clone" +readme = "README.md" +repository = "https://github.com/JanLikar/cargo-clone" +version = "1.0.1" +edition = "2021" + +[dependencies] +cargo-clone-core = { path = "../cargo-clone-core", version = "0.1" } +anyhow.workspace = true +cargo.workspace = true +clap = "2.34.0" + +[dev-dependencies] +tempdir.workspace = true + +[features] +vendored-openssl = ["cargo/vendored-openssl"] diff --git a/src/main.rs b/cargo-clone/src/main.rs similarity index 94% rename from src/main.rs rename to cargo-clone/src/main.rs index d8f9b8d..e1bbd11 100644 --- a/src/main.rs +++ b/cargo-clone/src/main.rs @@ -126,12 +126,12 @@ pub fn execute(matches: clap::ArgMatches, config: &mut Config) -> Result>>()?; + .map(cargo_clone_core::parse_name_and_version) + .collect::>>()?; - let opts = cargo_clone::CloneOpts::new(&crates, &source_id, directory, use_git); + let opts = cargo_clone_core::CloneOpts::new(&crates, &source_id, directory, use_git); - cargo_clone::clone(&opts, config)?; + cargo_clone_core::clone(&opts, config)?; Ok(None) } diff --git a/tests/test_cli.rs b/cargo-clone/tests/test_cli.rs similarity index 83% rename from tests/test_cli.rs rename to cargo-clone/tests/test_cli.rs index 7a6bd04..916fc6b 100644 --- a/tests/test_cli.rs +++ b/cargo-clone/tests/test_cli.rs @@ -1,8 +1,13 @@ -use std::fs; use std::process::Command; +use std::{env, fs}; use tempdir::TempDir; +fn cargo_clone_cmd() -> String { + let target_dir = env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "../target".to_string()); + format!("{target_dir}/debug/cargo-clone") +} + #[test] fn test_cli() { let temp_dir = TempDir::new("cargo-clone-tests").unwrap(); @@ -10,7 +15,7 @@ fn test_cli() { assert!(!output_path.exists()); - let status = Command::new("target/debug/cargo-clone") + let status = Command::new(cargo_clone_cmd()) .arg("clone") .arg("cargo-clone") .arg("--") @@ -30,7 +35,7 @@ fn test_custom_index() { assert!(!output_path.exists()); - let status = Command::new("target/debug/cargo-clone") + let status = Command::new(cargo_clone_cmd()) .arg("clone") .arg("--index") .arg("https://github.com/rust-lang/crates.io-index") @@ -50,7 +55,7 @@ fn test_clone_into_existing() { let temp_dir = TempDir::new("cargo-clone-tests").unwrap(); let output_path = temp_dir.path(); - let status = Command::new("target/debug/cargo-clone") + let status = Command::new(cargo_clone_cmd()) .arg("clone") .arg("time") .arg("--") @@ -71,7 +76,7 @@ fn test_with_version() { assert!(!output_path.exists()); - let status = Command::new("target/debug/cargo-clone") + let status = Command::new(cargo_clone_cmd()) .arg("clone") .arg("tokei@6.1.2") .arg("--")