microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
Guide/mdbook-openvmm-shim/src/main.rs
66lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use std::ffi::OsString; |
| 5 | |
| 6 | // certain plugins (e.g: mdbook-admonish) also "helpfully" update book.toml as |
| 7 | // part of an `install` operation, without any way to opt-out. |
| 8 | fn preserve_book_toml(f: impl FnOnce()) { |
| 9 | let old_book = std::fs::read("book.toml").unwrap(); |
| 10 | f(); |
| 11 | std::fs::write("book.toml", old_book).unwrap(); |
| 12 | } |
| 13 | |
| 14 | macro_rules! do_install { |
| 15 | ($cmd:expr, $args:expr) => { |
| 16 | std::process::Command::new(&$cmd) |
| 17 | .args($args) |
| 18 | .spawn() |
| 19 | .unwrap() |
| 20 | .wait() |
| 21 | .unwrap(); |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | fn main() { |
| 26 | let mut args = std::env::args_os().skip(1); |
| 27 | |
| 28 | let plugin = args.next().unwrap().into_string().unwrap(); |
| 29 | let args = args.collect::<Vec<OsString>>(); |
| 30 | |
| 31 | eprintln!("plugin={plugin}, args={args:?}"); |
| 32 | |
| 33 | let plugin_bin = { |
| 34 | if let Ok(path) = std::env::var(format!("SHIM_{}", plugin.replace('-', "_").to_uppercase())) |
| 35 | { |
| 36 | path |
| 37 | } else { |
| 38 | plugin.clone() |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | match plugin.as_ref() { |
| 43 | "mdbook-admonish" => { |
| 44 | if !std::fs::exists("mdbook-admonish.css").unwrap() { |
| 45 | preserve_book_toml(|| { |
| 46 | do_install!(plugin_bin, ["install"]); |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | "mdbook-mermaid" => { |
| 51 | if !std::fs::exists("mermaid.min.js").unwrap() { |
| 52 | do_install!(plugin_bin, ["install"]); |
| 53 | } |
| 54 | } |
| 55 | other => panic!("unknown plugin '{other}'"), |
| 56 | }; |
| 57 | |
| 58 | let status = std::process::Command::new(plugin_bin) |
| 59 | .args(args) |
| 60 | .spawn() |
| 61 | .unwrap() |
| 62 | .wait() |
| 63 | .unwrap(); |
| 64 | |
| 65 | std::process::exit(status.code().unwrap()) |
| 66 | } |
| 67 | |