Skip to content

Commit

Permalink
commit to work on chromebook
Browse files Browse the repository at this point in the history
  • Loading branch information
AverseABFun committed Dec 13, 2024
1 parent 9c3e95e commit b0d73c3
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 876 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ edition = "2021"

[dependencies]
anyhow = "1.0.94"
clap = { version = "4.5.23", features = ["derive"]}
clap = { version = "4.5.23", features = ["derive"] }
clap_complete = "4.5.38"
clap_mangen = "0.2.24"
dialoguer = "0.11.0"
filebuffer = "1.0.0"
hex = "0.4.3"
rand = "0.8.5"
regex = "1.11.1"
reqwest = {version = "0.12.9", features = ["blocking"]}
reqwest = { version = "0.12.9", features = ["blocking"] }
rust-embed = "8.5.0"
serde = {version="1.0.215", features = ["derive"]}
serde = { version = "1.0.215", features = ["derive"] }
sha2 = "0.10.8"
toml = "0.8.19"
url = "2.5.4"

[profile.release]
opt-level = "s"
debug-assertions = false # required to change the default behavior of --temp-dir
93 changes: 52 additions & 41 deletions src/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub fn download_all(
to_path: &str,
components: Vec<&str>,
for_targets: Vec<&str>,
quiet: bool,
) {
for channel in channels.clone() {
if !crate::targets::RELEASE_CHANNELS.contains(&channel) {
Expand Down Expand Up @@ -126,36 +127,35 @@ pub fn download_all(

let mut value = data.parse::<Value>().unwrap();
assert_eq!(value["manifest-version"].as_str(), Some("2"));
println!(
"Channel {} date {}",
channel,
value["date"].as_str().unwrap()
);
if !quiet {
println!(
"Channel {} date {}",
channel,
value["date"].as_str().unwrap()
);
}

for ele in for_targets.clone() {
if ele.contains("windows") {
let artifacts = value["artifacts"]["installer-msi"]["target"][ele][0].as_table_mut().unwrap();
let artifacts = value["artifacts"]["installer-msi"]["target"][ele][0]
.as_table_mut()
.unwrap();

let url =
Url::parse(artifacts["url"].as_str().unwrap())
.unwrap();
let url = Url::parse(artifacts["url"].as_str().unwrap()).unwrap();
let mirror = Path::new(to_path);
let file_name = url.path().replace("%20", " ");
let file = mirror.join(&file_name[1..]);

let hash_file = mirror.join(format!("{}.sha256", &file_name[1..]));
let hash_file_cont =
File::open(hash_file.clone()).ok().and_then(|mut f| {
let mut cont = String::new();
f.read_to_string(&mut cont).ok().map(|_| cont)
});
let hash_file_cont = File::open(hash_file.clone()).ok().and_then(|mut f| {
let mut cont = String::new();
f.read_to_string(&mut cont).ok().map(|_| cont)
});

let hash_file_missing = hash_file_cont.is_none();
let mut hash_file_cont =
hash_file_cont.or_else(|| file_sha256(file.as_path()));
let mut hash_file_cont = hash_file_cont.or_else(|| file_sha256(file.as_path()));

let chksum_upstream =
artifacts["hash-sha256"].as_str().unwrap();
let chksum_upstream = artifacts["hash-sha256"].as_str().unwrap();

let need_download = match hash_file_cont {
Some(ref chksum) => chksum_upstream != chksum,
Expand All @@ -166,7 +166,7 @@ pub fn download_all(
download(upstream_url, to_path, &file_name[1..]).unwrap();
hash_file_cont = file_sha256(file.as_path());
assert_eq!(Some(chksum_upstream), hash_file_cont.as_deref());
} else {
} else if !quiet {
println!("File {} already downloaded, skipping", file_name);
}

Expand All @@ -175,31 +175,30 @@ pub fn download_all(
.unwrap()
.write_all(hash_file_cont.unwrap().as_bytes())
.unwrap();
println!("Writing checksum for file {}", file_name);
if !quiet {
println!("Writing checksum for file {}", file_name);
}
}
} else if ele.contains("darwin") {
let artifacts = value["artifacts"]["installer-pkg"]["target"][ele].as_table_mut().unwrap();
let artifacts = value["artifacts"]["installer-pkg"]["target"][ele]
.as_table_mut()
.unwrap();

let url =
Url::parse(artifacts["url"].as_str().unwrap())
.unwrap();
let url = Url::parse(artifacts["url"].as_str().unwrap()).unwrap();
let mirror = Path::new(to_path);
let file_name = url.path().replace("%20", " ");
let file = mirror.join(&file_name[1..]);

let hash_file = mirror.join(format!("{}.sha256", &file_name[1..]));
let hash_file_cont =
File::open(hash_file.clone()).ok().and_then(|mut f| {
let mut cont = String::new();
f.read_to_string(&mut cont).ok().map(|_| cont)
});
let hash_file_cont = File::open(hash_file.clone()).ok().and_then(|mut f| {
let mut cont = String::new();
f.read_to_string(&mut cont).ok().map(|_| cont)
});

let hash_file_missing = hash_file_cont.is_none();
let mut hash_file_cont =
hash_file_cont.or_else(|| file_sha256(file.as_path()));
let mut hash_file_cont = hash_file_cont.or_else(|| file_sha256(file.as_path()));

let chksum_upstream =
artifacts["hash-sha256"].as_str().unwrap();
let chksum_upstream = artifacts["hash-sha256"].as_str().unwrap();

let need_download = match hash_file_cont {
Some(ref chksum) => chksum_upstream != chksum,
Expand All @@ -210,7 +209,7 @@ pub fn download_all(
download(upstream_url, to_path, &file_name[1..]).unwrap();
hash_file_cont = file_sha256(file.as_path());
assert_eq!(Some(chksum_upstream), hash_file_cont.as_deref());
} else {
} else if !quiet {
println!("File {} already downloaded, skipping", file_name);
}

Expand All @@ -219,7 +218,9 @@ pub fn download_all(
.unwrap()
.write_all(hash_file_cont.unwrap().as_bytes())
.unwrap();
println!("Writing checksum for file {}", file_name);
if !quiet {
println!("Writing checksum for file {}", file_name);
}
}
}
}
Expand Down Expand Up @@ -281,7 +282,7 @@ pub fn download_all(
download(upstream_url, to_path, &file_name[1..]).unwrap();
hash_file_cont = file_sha256(file.as_path());
assert_eq!(Some(chksum_upstream), hash_file_cont.as_deref());
} else {
} else if !quiet {
println!("File {} already downloaded, skipping", file_name);
}

Expand All @@ -290,7 +291,9 @@ pub fn download_all(
.unwrap()
.write_all(hash_file_cont.unwrap().as_bytes())
.unwrap();
println!("Writing checksum for file {}", file_name);
if !quiet {
println!("Writing checksum for file {}", file_name);
}
}

pkg_target.insert(
Expand All @@ -306,13 +309,17 @@ pub fn download_all(
let path = Path::new(to_path).join(&name);
create_dir_all(path.parent().unwrap()).unwrap();
let mut file = File::create(path.clone()).unwrap();
println!("Producing /{}", name);
if !quiet {
println!("Producing /{}", name);
}
file.write_all(output.as_bytes()).unwrap();

let sha256_new_file = file_sha256(&path).unwrap();
let sha256_new_file_path = Path::new(to_path).join(&sha256_name);
let mut file = File::create(sha256_new_file_path.clone()).unwrap();
println!("Producing /{}", sha256_name);
if !quiet {
println!("Producing /{}", sha256_name);
}
file.write_all(format!("{} channel-rust-{}.toml", sha256_new_file, channel).as_bytes())
.unwrap();

Expand All @@ -322,12 +329,16 @@ pub fn download_all(
let alt_path = Path::new(to_path).join(&alt_name);
create_dir_all(alt_path.parent().unwrap()).unwrap();
copy(path, alt_path).unwrap();
println!("Producing /{}", alt_name);
if !quiet {
println!("Producing /{}", alt_name);
}

let alt_sha256_new_file_name =
format!("dist/{}/channel-rust-{}.toml.sha256", date, channel);
let alt_sha256_new_file_path = Path::new(to_path).join(&alt_sha256_new_file_name);
copy(sha256_new_file_path, alt_sha256_new_file_path).unwrap();
println!("Producing /{}", alt_sha256_new_file_name);
if !quiet {
println!("Producing /{}", alt_sha256_new_file_name);
}
}
}
78 changes: 61 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use rand::{Rng, SeedableRng};
use rust_pkg_gen::resources::TemplateAssets;
use std::{fs, path::PathBuf};
use std::{fs, path::PathBuf, process::Stdio};

fn gen_char() -> u8 {
rand::rngs::StdRng::from_entropy().gen_range(65..90)
Expand Down Expand Up @@ -31,8 +31,30 @@ struct Cli {
help = "The equivalent to --yes for overwriting files"
)]
overwrite: bool,
#[arg(default_value = "rust-config.toml", help = "")]
#[arg(
default_value = "rust-config.toml",
help = "The path to the configuration file"
)]
path: PathBuf,
#[arg(
short = 'q',
long = "quiet",
default_value_t = false,
help = "Doesn't display any unnecessary text(still shows confirmation prompts; to remove, use --overwrite"
)]
quiet: bool,
#[arg(
long = "silent",
default_value_t = false,
help = "Equivalent to -y -q --overwrite"
)]
silent: bool,
#[arg(
long = "save-temp",
default_value_t = false,
help = "Saves all temporary files"
)]
save_temp: bool,
}

fn generate_crates(
Expand All @@ -47,7 +69,13 @@ fn generate_crates(
}

fn main() {
let args = Cli::parse();
let mut args = Cli::parse();

if args.silent {
args.quiet = true;
args.yes = true;
args.overwrite = true;
}

let path = &PathBuf::from(args.path);

Expand All @@ -62,18 +90,25 @@ fn main() {
let dir = if args.temp_dir.is_some() {
if args.temp_dir.clone().unwrap().exists() && !args.overwrite {
let confirmation = dialoguer::Confirm::new()
.with_prompt("Provided temporary directory already exists, overwrite?")
.with_prompt("Temporary directory already exists, overwrite?")
.default(false)
.interact()
.unwrap();
if !confirmation {
println!("Aborting.");
if !args.quiet {
println!("Aborting.");
}
std::process::exit(1)
} else {
println!("Overwriting.");
if !args.quiet {
println!("Overwriting.");
}
std::fs::remove_dir_all(args.temp_dir.clone().unwrap()).unwrap();
}
}
if args.overwrite {
std::fs::remove_dir_all(args.temp_dir.clone().unwrap()).unwrap();
}
args.temp_dir.unwrap()
} else {
std::env::temp_dir().join(PathBuf::from(String::from_utf8_lossy(chars).as_ref()))
Expand Down Expand Up @@ -115,15 +150,21 @@ fn main() {
.unwrap();
}

std::process::Command::new("bash")
.arg(
dir.join(PathBuf::from("crates"))
.join(PathBuf::from("build.sh")),
)
.spawn()
.unwrap()
.wait()
.unwrap();
let mut build_c = std::process::Command::new("bash");
let mut build = build_c.arg(
dir.join(PathBuf::from("crates"))
.join(PathBuf::from("build.sh")),
);
if args.quiet {
build = build
.stdout(Stdio::null())
.stderr(Stdio::null())
.stdin(Stdio::null());
}
if args.save_temp {
build = build.arg("save")
}
build.spawn().unwrap().wait().unwrap();

rust_pkg_gen::copied::download_all(
vec![&toolchain.channel],
Expand All @@ -132,10 +173,13 @@ fn main() {
toolchain.targets.iter().map(|s| &**s).collect(),
dir.join("toolchain").to_str().unwrap(),
toolchain.components.iter().map(|s| &**s).collect(),
toolchain.platforms.iter().map(|s| &**s).collect()
toolchain.platforms.iter().map(|s| &**s).collect(),
args.quiet,
);

fs::remove_dir_all(dir.join("tmp").to_str().unwrap()).unwrap();
if !args.save_temp {
fs::remove_dir_all(dir.join("tmp").to_str().unwrap()).unwrap();
}
}
}
}
2 changes: 0 additions & 2 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ use rust_embed::Embed;
#[folder = "src/template/"]
#[prefix = "template/"]
pub struct TemplateAssets;

const RUSTUP_INIT_ASSET: &str = include_str!("rustup-init.sh");
Loading

0 comments on commit b0d73c3

Please sign in to comment.