Skip to content

Commit

Permalink
commit to work on pc
Browse files Browse the repository at this point in the history
  • Loading branch information
AverseABFun committed Dec 12, 2024
1 parent c88ca4c commit 9c3e95e
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 14 deletions.
4 changes: 1 addition & 3 deletions rust-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ toolchains = [
"rustfmt",
"rustc",
"cargo",
], crate_id = "2021-nightly-complete" },
], crate_id = "2021-nightly-complete", platforms = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu"], targets = ["x86_64-unknown-linux-gnu"]},
]

[x64_package_linux_rust_pkg_gen.meta]
offline = true
platforms = ["x86_64-unknown-linux-gnu"]
targets = ["x86_64-unknown-linux-gnu"]

[x64_package_linux_rust_pkg_gen.crates]
"2021-nightly-complete" = { "syn" = "2.0.90" }
108 changes: 108 additions & 0 deletions src/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,23 @@ pub fn download_all(
targets: Vec<&str>,
to_path: &str,
components: Vec<&str>,
for_targets: Vec<&str>,
) {
for channel in channels.clone() {
if !crate::targets::RELEASE_CHANNELS.contains(&channel) {
return;
}
}
for target in targets.clone() {
if !crate::targets::TARGETS.contains(&target) {
return;
}
}
for target in for_targets.clone() {
if !crate::targets::TARGETS.contains(&target) {
return;
}
}
let mut all_targets = HashSet::new();

// All referenced files
Expand Down Expand Up @@ -116,6 +132,98 @@ pub fn download_all(
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 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_missing = hash_file_cont.is_none();
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 need_download = match hash_file_cont {
Some(ref chksum) => chksum_upstream != chksum,
None => true,
};

if need_download {
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 {
println!("File {} already downloaded, skipping", file_name);
}

if need_download || hash_file_missing {
File::create(hash_file)
.unwrap()
.write_all(hash_file_cont.unwrap().as_bytes())
.unwrap();
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 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_missing = hash_file_cont.is_none();
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 need_download = match hash_file_cont {
Some(ref chksum) => chksum_upstream != chksum,
None => true,
};

if need_download {
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 {
println!("File {} already downloaded, skipping", file_name);
}

if need_download || hash_file_missing {
File::create(hash_file)
.unwrap()
.write_all(hash_file_cont.unwrap().as_bytes())
.unwrap();
println!("Writing checksum for file {}", file_name);
}
}
}

let pkgs = value["pkg"].as_table_mut().unwrap();
let keys: Vec<String> = pkgs.keys().cloned().collect();
for pkg_name in keys {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub struct Toolchain {
pub profile: String,
pub components: Vec<String>,
pub crate_id: String,
pub platforms: Vec<String>,
pub targets: Vec<String>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct Meta {
pub offline: bool,
pub platforms: Vec<String>,
pub targets: Vec<String>,
}

pub type Crate = String;
Expand Down
17 changes: 10 additions & 7 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::path::PathBuf;
use std::{fs, path::PathBuf};

fn gen_char() -> u8 {
rand::rngs::StdRng::from_entropy().gen_range(65..90)
Expand Down Expand Up @@ -60,7 +60,7 @@ fn main() {
gen_char(),
];
let dir = if args.temp_dir.is_some() {
if args.temp_dir.clone().unwrap().exists() && !args.yes {
if args.temp_dir.clone().unwrap().exists() && !args.overwrite {
let confirmation = dialoguer::Confirm::new()
.with_prompt("Provided temporary directory already exists, overwrite?")
.default(false)
Expand Down Expand Up @@ -103,8 +103,8 @@ fn main() {
.replace("{?TOOLCHAIN.CHANNEL}", &toolchain.channel)
.replace("{?TOOLCHAIN.PROFILE}", &toolchain.profile)
.replace(
"{?META.TARGETS}",
&("\"".to_owned() + &cfg.meta.targets.join("\",\"") + "\""),
"{?TOOLCHAIN.TARGETS}",
&("\"".to_owned() + &toolchain.targets.join("\",\"") + "\""),
)
.replace(
"{?TOOLCHAIN.COMPONENTS}",
Expand All @@ -126,13 +126,16 @@ fn main() {
.unwrap();

rust_pkg_gen::copied::download_all(
vec!["nightly"],
vec![&toolchain.channel],
rust_pkg_gen::copied::DEFAULT_UPSTREAM_URL,
dir.join("tmp").to_str().unwrap(),
vec!["x86_64-unknown-linux-gnu"],
toolchain.targets.iter().map(|s| &**s).collect(),
dir.join("toolchain").to_str().unwrap(),
vec!["rustfmt", "rustc", "cargo"],
toolchain.components.iter().map(|s| &**s).collect(),
toolchain.platforms.iter().map(|s| &**s).collect()
);

fs::remove_dir_all(dir.join("tmp").to_str().unwrap()).unwrap();
}
}
}
5 changes: 4 additions & 1 deletion src/template/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env bash
cd "$(dirname "$0")"
cargo generate-lockfile --verbose
cargo vendor --verbose --locked ./crates
cargo vendor --verbose --locked ./crates
mv crates/* .
rm Cargo.lock Cargo.toml rust-toolchain.toml src/main.rs .cargo/config.toml build.sh
rm -d src .cargo
2 changes: 1 addition & 1 deletion src/template/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "{?TOOLCHAIN.CHANNEL}"
components = [ {?TOOLCHAIN.COMPONENTS} ]
targets = [ {?META.TARGETS} ]
targets = [ {?TOOLCHAIN.TARGETS} ]
profile = "{?TOOLCHAIN.PROFILE}"

0 comments on commit 9c3e95e

Please sign in to comment.