Skip to content

Commit

Permalink
Merge pull request #95 from wcampbell0x2a/add-config-json-writer
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a authored Oct 22, 2024
2 parents b0c2eb6 + f66a91c commit 3b6bf71
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 32 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,15 @@ Arguments:
[WORKSPACES]... list of Cargo.toml files to vendor depends

Options:
--build-std <VERSION> Cache build-std depends for nightly version
--skip-git-index Skip download of git index crates.io
-h, --help Print help
--build-std <VERSION> Cache build-std depends for nightly version
--git-index-url <GIT_INDEX_URL> hostname for git index crates.io
--skip-git-index Skip download of git index crates.io
-h, --help Print help
```

Example:
```console
$ zerus new-mirror ../deku/Cargo.toml ../adsb_deku/Cargo.toml
# configure crates.io-index to point to our host
$ cat crates.io-index/config.json
{
"dl": "http://[IP]/crates/{prefix}/{crate}/{version}/{crate}-{version}.crate",
"api": "http://[IP]/crates"
}
$ zerus new-mirror ../deku/Cargo.toml ../adsb_deku/Cargo.toml --git-index-url http://127.0.0.1
```


Expand Down
12 changes: 12 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,15 @@ fn fast_forward(
repo.checkout_head(Some(git2::build::CheckoutBuilder::default().force()))?;
Ok(())
}

pub fn write_config_json(url: &str, mut writer: impl Write) -> std::io::Result<()> {
let f = format!(
r#"{{
"dl": "{url}/crates/{{prefix}}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate",
"api": "{url}/crates"
}}
"#
);
writer.write_all(f.as_bytes())?;
Ok(())
}
33 changes: 30 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
mod build_std;
use build_std::prepare_build_std;
use git::write_config_json;
use guppy::errors::Error::CommandError;
use reqwest::StatusCode;

use std::fs::OpenOptions;
use std::path::{Path, PathBuf};
use std::{fs, iter};

use clap::Parser;
use clap::{Parser, ValueHint};
use guppy::graph::DependencyDirection;
use guppy::MetadataCommand;
use rayon::prelude::*;
Expand All @@ -16,6 +18,14 @@ use crate::git::{clone, pull};

mod git;

fn validate_url(url: &str) -> Result<String, String> {
if url.starts_with("http://") || url.starts_with("https://") {
Ok(url.to_string())
} else {
Err(String::from("The URL must start with http:// or https://"))
}
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
struct Crate {
name: String,
Expand All @@ -37,11 +47,18 @@ struct Args {
workspaces: Vec<String>,

/// Cache build-std depends for nightly version
#[clap(long, value_name = "VERSION")]
#[arg(long, value_name = "VERSION")]
build_std: Option<String>,

/// Hostname for git index crates.io
#[arg(long)]
#[arg(value_hint = ValueHint::Url, value_parser = validate_url)]
#[arg(conflicts_with = "skip_git_index")]
git_index_url: Option<String>,

/// Skip download of git index crates.io
#[clap(long)]
#[arg(long)]
#[arg(conflicts_with = "git_index_url")]
skip_git_index: bool,
}

Expand All @@ -68,6 +85,16 @@ fn main() {
}
println!("[-] Done syncing git index crates.io");
}

if let Some(url) = args.git_index_url {
let path = args.mirror_path.join("crates.io-index").join("config.json");
let file = OpenOptions::new()
.write(true)
.truncate(true)
.open(path)
.unwrap();
write_config_json(&url, file).unwrap();
}
}

/// # Returns
Expand Down
36 changes: 17 additions & 19 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs::{File, OpenOptions},
io::Write,
process::Output,
};

use assert_cmd::Command;
Expand All @@ -25,6 +26,7 @@ fn test_old_nightly_version() {
])
.output()
.unwrap();
assert_success(&output);

let rustup_home_output = std::process::Command::new("rustup")
.args(["show", "home"])
Expand Down Expand Up @@ -90,6 +92,7 @@ fn test_new_nightly_version() {
])
.output()
.unwrap();
assert_success(&output);

let rustup_home_output = std::process::Command::new("rustup")
.args(["show", "home"])
Expand Down Expand Up @@ -164,29 +167,14 @@ fn test_build_std(nightly_ver: &str, tmp_dir_path: std::path::PathBuf, port: u32
.env("RUST_LOG", "none")
.args([
tmp_dir_path.to_str().unwrap(),
// "--skip-git-index",
"--git-index-url",
&format!("http://127.0.0.1:{port}"),
"--build-std",
nightly_ver,
])
.output()
.unwrap();

// modify the config.json
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open(tmp_dir_path.join("crates.io-index/config.json"))
.unwrap();
file.write_all(
&format!(
r#"{{
"dl": "http://127.0.0.1:{port}/crates/{{prefix}}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate",
"api": "http://127.0.0.1:{port}/crates"
}}"#
)
.into_bytes(),
)
.unwrap();
assert_success(&output);

// Create a temp directory for a cargo project
let tmp_dir_cargo = Builder::new().tempdir_in("./").unwrap();
Expand Down Expand Up @@ -259,5 +247,15 @@ build-std-features = ["panic_immediate_abort"]
.current_dir(&tmp_dir_cargo_path.join("testing/"))
.output()
.unwrap();
assert!(output.status.success());
assert_success(&output);
}

fn assert_success(output: &Output) {
if !output.status.success() {
let stdout = String::from_utf8(output.stdout.clone()).unwrap();
println!("stdout: {}", stdout);
let stderr = String::from_utf8(output.stderr.clone()).unwrap();
println!("stderr: {}", stderr);
panic!("not success");
}
}

0 comments on commit 3b6bf71

Please sign in to comment.