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