Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extract library #50

Merged
merged 3 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 3 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
[package]
authors = ["Jan Likar <likar.jan@gmail.com>"]
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"]
20 changes: 20 additions & 0 deletions cargo-clone-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
authors = ["Jan Likar <likar.jan@gmail.com>"]
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
5 changes: 3 additions & 2 deletions src/lib.rs → cargo-clone-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions tests/test_clone.rs → cargo-clone-core/tests/test_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ 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")),
)];
let source_id = SourceId::crates_io(&config).unwrap();
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());
Expand All @@ -38,17 +38,17 @@ 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")),
)];
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());
Expand All @@ -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());
Expand Down
24 changes: 24 additions & 0 deletions cargo-clone/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
authors = ["Jan Likar <likar.jan@gmail.com>"]
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"]
8 changes: 4 additions & 4 deletions src/main.rs → cargo-clone/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ pub fn execute(matches: clap::ArgMatches, config: &mut Config) -> Result<Option<
let crates = matches
.values_of("crate")
.unwrap()
.map(cargo_clone::parse_name_and_version)
.collect::<Result<Vec<cargo_clone::Crate>>>()?;
.map(cargo_clone_core::parse_name_and_version)
.collect::<Result<Vec<cargo_clone_core::Crate>>>()?;

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)
}
15 changes: 10 additions & 5 deletions tests/test_cli.rs → cargo-clone/tests/test_cli.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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();
let output_path = temp_dir.path().join("cargo-clone");

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("--")
Expand All @@ -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")
Expand All @@ -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("--")
Expand All @@ -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("--")
Expand Down