diff --git a/build.rs b/build.rs index cbc8b6e3..9af1106c 100644 --- a/build.rs +++ b/build.rs @@ -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; @@ -182,8 +182,10 @@ impl<'a> SprinklesVersion<'a> { } } -fn write_colours(output_file: &mut impl Write) -> Result<(), Box> { - 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 @@ -194,9 +196,11 @@ fn write_colours(output_file: &mut impl Write) -> Result<(), Box> { .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(()) } @@ -210,9 +214,28 @@ fn main() -> Result<(), Box> { 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 { @@ -222,9 +245,11 @@ fn main() -> Result<(), Box> { } _ => "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 { @@ -234,30 +259,17 @@ fn main() -> Result<(), Box> { } _ => "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::() + .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(()) } diff --git a/src/commands/credits.rs b/src/commands/credits.rs index b0eb4bbd..b220d060 100644 --- a/src/commands/credits.rs +++ b/src/commands/credits.rs @@ -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; @@ -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() ); } @@ -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() @@ -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}"); } @@ -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}"); } @@ -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}"); } @@ -240,7 +230,7 @@ impl Args { ]); items.extend( - contributors::CONTRIBUTORS + shadow::sfsu::CONTRIBUTORS .into_iter() .map(|(name, url)| Text::from(format!("{name} ({url})"))), ); @@ -252,7 +242,7 @@ impl Args { ]); items.extend( - contributors::sprinkles::CONTRIBUTORS + shadow::sprinkles::CONTRIBUTORS .into_iter() .map(|(name, url)| Text::from(format!("{name} ({url})"))), ); @@ -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(" - "), diff --git a/src/main.rs b/src/main.rs index 99541dcd..fcfb79d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); diff --git a/src/output/colours.rs b/src/output/colours.rs index 55e24a2d..6dae9c8a 100644 --- a/src/output/colours.rs +++ b/src/output/colours.rs @@ -1,3 +1,3 @@ #![allow(unused_imports)] -include!(concat!(env!("OUT_DIR"), "/colours.rs")); +pub use crate::shadow::colours::*;