diff options
author | Artyom Belousov <flygrounder@yandex.ru> | 2022-08-02 12:51:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-02 11:51:15 +0200 |
commit | c8a5adb412e98b07017ffa0edea5554b0a23b840 (patch) | |
tree | 3b5f61fd8926446626b4b63f5aeb7d20fdb611bd /build.rs | |
parent | b456b9c5191eca3844ab4a561b41a97396f7262f (diff) |
feat: Add starship preset command (#4112)
* Add starship preset command
* Use ValueEnum for preset command
* Generate ValueEnum struct in build.rs
* Use absolute paths and refactor codegen
* Use dunce to canonicalize path
* Use raw string literal in include_bytes!
* Use .cloned()
* Apply fixes
* Fix path escaping
* Removed error message if stdout is unavailable
Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
Diffstat (limited to 'build.rs')
-rw-r--r-- | build.rs | 60 |
1 files changed, 58 insertions, 2 deletions
@@ -1,7 +1,11 @@ -use std::error::Error; +use std::fs::{self, File}; +use std::io::Write; -fn main() -> Result<(), Box<dyn Error>> { +use shadow_rs::SdResult; + +fn main() -> SdResult<()> { shadow_rs::new().map_err(|err| err.to_string())?; + shadow_rs::new_hook(gen_presets_hook)?; #[cfg(windows)] { @@ -13,3 +17,55 @@ fn main() -> Result<(), Box<dyn Error>> { Ok(()) } + +fn gen_presets_hook(mut file: &File) -> SdResult<()> { + println!("cargo:rerun-if-changed=docs/.vuepress/public/presets/toml"); + let paths = fs::read_dir("docs/.vuepress/public/presets/toml")?; + + let mut presets = String::new(); + let mut match_arms = String::new(); + for path in paths { + let unwrapped = path?; + let file_name = unwrapped.file_name(); + let full_path = dunce::canonicalize(unwrapped.path())?; + let full_path = full_path.to_str().expect("failed to convert to string"); + let name = file_name + .to_str() + .and_then(|v| v.strip_suffix(".toml")) + .expect("Failed to process filename"); + presets.push_str(format!("print::Preset(\"{}\"),\n", name).as_str()); + match_arms.push_str( + format!( + r#" +"{name}" => {{ + let mut stdout = io::stdout().lock(); + let _ = stdout.write_all(include_bytes!(r"{full_path}")); +}} +"# + ) + .as_str(), + ); + } + + writeln!( + file, + r#" +use std::io::{{self, Write}}; +use crate::print; + +pub fn get_preset_list<'a>() -> &'a [print::Preset] {{ + &[ + {presets} + ] +}} + +pub fn print_preset_content(name: &str) {{ + match name {{ + {match_arms} + _ => {{}} + }} +}} +"# + )?; + Ok(()) +} |