Skip to content

Commit

Permalink
Merge pull request #904 from winpax/shadow-hooks
Browse files Browse the repository at this point in the history
Use shadow hooks instead of custom build files
  • Loading branch information
jewlexx authored Nov 27, 2024
2 parents 1165cb3 + 8635aef commit 682913e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 52 deletions.
68 changes: 40 additions & 28 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, io::Write};
use std::{error::Error, fs::File, io::Write};

use contribs::contributors::Contributors;
use dotenv::dotenv;
Expand Down Expand Up @@ -182,8 +182,10 @@ impl<'a> SprinklesVersion<'a> {
}
}

fn write_colours(output_file: &mut impl Write) -> Result<(), Box<dyn Error>> {
writeln!(output_file, "// This file is autogenerated")?;
fn write_colours(mut file: &File) -> std::io::Result<()> {
writeln!(file, "pub mod colours {{")?;
writeln!(file, "#![allow(unused_imports)]")?;
writeln!(file, "// This file is autogenerated")?;

for colour in COLOURS {
let output = COLOURS_TXT
Expand All @@ -194,9 +196,11 @@ fn write_colours(output_file: &mut impl Write) -> Result<(), Box<dyn Error>> {
.replace("#println_bright", &format!("println_bright_{colour}"))
.replace("#println", &format!("println_{colour}"));

output_file.write_all(output.as_bytes())?;
file.write_all(output.as_bytes())?;
}

writeln!(file, "}}")?;

Ok(())
}

Expand All @@ -210,9 +214,28 @@ fn main() -> Result<(), Box<dyn Error>> {

SprinklesVersion::from_doc(&lockfile).print_variables();

shadow_rs::new()?;
shadow_rs::new_hook(append_shadow_hooks)?;

println!("cargo:rerun-if-changed=sfsu.exe.manifest");
let mut res = winres::WindowsResource::new();
res.set_manifest(WIN_MANIFEST);

res.compile().expect("Failed to compile Windows resources");

let libgit2_version = git2::Version::get();

let (major, minor, patch) = libgit2_version.libgit2_version();

println!(
"cargo:rustc-env=LIBGIT2_VERSION={}.{}.{}",
major, minor, patch
);

Ok(())
}

std::fs::write(out_path.clone() + "/contributors.rs", {
fn append_shadow_hooks(mut file: &File) -> shadow_rs::SdResult<()> {
let sfsu_contribs = {
let contributors = get_contributors(("winpax", "sfsu"));

match contributors {
Expand All @@ -222,9 +245,11 @@ fn main() -> Result<(), Box<dyn Error>> {
}
_ => "pub const CONTRIBUTORS: [(&str, &str); 0] = [];".to_string(),
}
})?;
};

std::fs::write(out_path.clone() + "/sprinkles_contributors.rs", {
writeln!(file, "pub mod sfsu {{\n{sfsu_contribs}\n}}")?;

let sprinkles_contribs = {
let contributors = get_contributors(("winpax", "sprinkles"));

match contributors {
Expand All @@ -234,30 +259,17 @@ fn main() -> Result<(), Box<dyn Error>> {
}
_ => "pub const CONTRIBUTORS: [(&str, &str); 0] = [];".to_string(),
}
})?;

std::fs::write(out_path.clone() + "/packages.rs", get_packages(&lockfile))?;

let mut colours_file = std::fs::File::create(out_path.clone() + "/colours.rs")?;
write_colours(&mut colours_file)?;

println!("cargo:rerun-if-changed=sfsu.exe.manifest");
let mut res = winres::WindowsResource::new();
res.set_manifest(WIN_MANIFEST);
};

if let Err(error) = res.compile() {
eprint!("{error}");
std::process::exit(1);
}
writeln!(file, "pub mod sprinkles {{\n{sprinkles_contribs}\n}}")?;

let libgit2_version = git2::Version::get();
let lockfile = LOCKFILE
.parse::<DocumentMut>()
.expect("Failed to parse Cargo.lock");

let (major, minor, patch) = libgit2_version.libgit2_version();
writeln!(file, "{}", get_packages(&lockfile))?;

println!(
"cargo:rustc-env=LIBGIT2_VERSION={}.{}.{}",
major, minor, patch
);
write_colours(file)?;

Ok(())
}
32 changes: 11 additions & 21 deletions src/commands/credits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ use ratatui::{
use serde::Serialize;
use sprinkles::contexts::ScoopContext;

mod contributors {
include!(concat!(env!("OUT_DIR"), "/contributors.rs"));

pub mod sprinkles {
include!(concat!(env!("OUT_DIR"), "/sprinkles_contributors.rs"));
}
}

mod packages {
include!(concat!(env!("OUT_DIR"), "/packages.rs"));
}
use crate::shadow;

mod titles {
use shadow_rs::formatcp;
Expand All @@ -49,7 +39,7 @@ mod titles {
pub const SPRINKLES_CONTRIBUTORS: &str = "🍨 And in sprinkles 🍨";
pub const PACKAGES: &str = formatcp!(
"📦 And all the incredible {} crates we use 📦",
super::packages::PACKAGES.len()
crate::shadow::PACKAGES.len()
);
}

Expand Down Expand Up @@ -145,18 +135,18 @@ impl super::Command for Args {
version: &'a str,
}

let contributors = contributors::CONTRIBUTORS
let contributors = shadow::sfsu::CONTRIBUTORS
.into_iter()
.map(|(name, url)| Contributor { name, url })
.collect_vec();

let sprinkles_contributors = contributors::sprinkles::CONTRIBUTORS
let sprinkles_contributors = shadow::sprinkles::CONTRIBUTORS
.into_iter()
.map(|(name, url)| Contributor { name, url })
.collect_vec();

let packages = if self.packages {
packages::PACKAGES
shadow::PACKAGES
.into_iter()
.map(|(name, version)| Package { name, version })
.collect_vec()
Expand Down Expand Up @@ -184,7 +174,7 @@ impl super::Command for Args {
println!("{}", titles::SFSU_CONTRIBUTORS);
println!();

for (name, url) in contributors::CONTRIBUTORS {
for (name, url) in shadow::sfsu::CONTRIBUTORS {
let url = Url::new(name, url.to_string());
println!("{url}");
}
Expand All @@ -193,7 +183,7 @@ impl super::Command for Args {
println!("{}", titles::SPRINKLES_CONTRIBUTORS);
println!();

for (name, url) in contributors::sprinkles::CONTRIBUTORS {
for (name, url) in shadow::sprinkles::CONTRIBUTORS {
let url = Url::new(name, url.to_string());
println!("{url}");
}
Expand All @@ -203,7 +193,7 @@ impl super::Command for Args {
println!("{}", titles::PACKAGES);
println!();

for (name, version) in packages::PACKAGES {
for (name, version) in shadow::PACKAGES {
let url = Url::new(name, format!("https://crates.io/crates/{name}"));
println!("{url}: {version}");
}
Expand Down Expand Up @@ -240,7 +230,7 @@ impl Args {
]);

items.extend(
contributors::CONTRIBUTORS
shadow::sfsu::CONTRIBUTORS
.into_iter()
.map(|(name, url)| Text::from(format!("{name} ({url})"))),
);
Expand All @@ -252,7 +242,7 @@ impl Args {
]);

items.extend(
contributors::sprinkles::CONTRIBUTORS
shadow::sprinkles::CONTRIBUTORS
.into_iter()
.map(|(name, url)| Text::from(format!("{name} ({url})"))),
);
Expand All @@ -264,7 +254,7 @@ impl Args {
Text::raw(""),
]);

items.extend(packages::PACKAGES.into_iter().map(|(name, version)| {
items.extend(shadow::PACKAGES.into_iter().map(|(name, version)| {
Text::from(Line::from(vec![
Span::styled(name, Style::default()),
Span::raw(" - "),
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ use sprinkles::contexts::{AnyContext, User};
#[cfg(feature = "contexts")]
use sprinkles::contexts::Global;

shadow_rs::shadow!(shadow);

mod versions {
#![allow(clippy::needless_raw_string_hashes)]

use konst::eq_str;

include!(concat!(env!("OUT_DIR"), "/shadow.rs"));

// TODO: Move this into build script
pub const SFSU_LONG_VERSION: &str = {
use crate::shadow::{
BRANCH, BUILD_TIME, PKG_VERSION, RUST_CHANNEL, RUST_VERSION, SHORT_COMMIT, TAG,
};
use shadow_rs::formatcp;

const LIBGIT2_VERSION: &str = env!("LIBGIT2_VERSION");
Expand Down
2 changes: 1 addition & 1 deletion src/output/colours.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#![allow(unused_imports)]

include!(concat!(env!("OUT_DIR"), "/colours.rs"));
pub use crate::shadow::colours::*;

0 comments on commit 682913e

Please sign in to comment.