1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
use std::collections::BTreeMap; use std::fmt::{Display, Write}; #[derive(Debug, PartialEq, Default)] pub struct Writter { inner: String, } impl Writter { pub fn new() -> Self { Default::default() } pub fn newline(&mut self) { self.inner.push('\n') } pub fn variable(&mut self, key: &str, value: &str) { self.vari(key, value, 0); } pub fn pool(&mut self, name: &str, depth: u32) { writeln!(self.inner, "pool {}", name).unwrap(); self.vari("depth", depth, 1) } pub fn include(&mut self, path: &str) { writeln!(self.inner, "include {}", path).unwrap(); } pub fn done(self) -> String { self.inner } pub fn subninja(&mut self, path: &str) { writeln!(self.inner, "subninja {}", path).unwrap(); } pub fn default(&mut self, paths: &[&str]) { writeln!(self.inner, "default {}", paths.join(" ")).unwrap() } pub fn rule(&mut self, r: impl Into<Rule>) { macro_rules! write_if { ($name:expr, $val: expr) => { if let Some(s) = $val { self.vari($name, s, 1) } }; } let r: Rule = r.into(); writeln!(self.inner, "rule {}", r.name).unwrap(); self.vari("command", r.command, 1); write_if!("description", r.description); write_if!("depfile", r.depfile); if r.generator { self.vari("generator", "1", 1); } write_if!("pool", r.pool); if r.restat { self.vari("restat", "1", 1); } write_if!("rspfile", r.rspfile); write_if!("rspfile_content", r.rspfile_content); write_if!("deps", r.deps); } pub fn build(&mut self, b: impl Into<Build>) { let b = b.into(); assert!(!b.outputs.is_empty()); assert!(!b.rule.is_empty()); let outs = b.outputs.join(" "); let ins = b.inputs.join(" "); writeln!(self.inner, "build {}: {} {}", outs, b.rule, ins).unwrap(); if let Some(p) = b.pool { self.vari("pool", p, 1); } for (k, v) in b.variables { self.vari(k, v, 1); } } fn vari(&mut self, key: impl Display, value: impl Display, indent: usize) { writeln!(self.inner, "{}{} = {}", " ".repeat(indent), key, value).unwrap(); } } #[derive(Debug, PartialEq, Default)] #[non_exhaustive] pub struct Rule { pub name: String, pub command: String, pub description: Option<String>, pub depfile: Option<String>, pub generator: bool, pub pool: Option<String>, pub restat: bool, pub rspfile: Option<String>, pub rspfile_content: Option<String>, pub deps: Option<String>, } #[derive(Debug, PartialEq, Default)] #[non_exhaustive] pub struct Build { pub outputs: Vec<String>, pub rule: String, pub inputs: Vec<String>, pub variables: BTreeMap<String, String>, pub pool: Option<String>, } impl<X, Y> Into<Rule> for (X, Y) where X: Into<String>, Y: Into<String>, { fn into(self) -> Rule { let (name, command) = self; let name = name.into(); let command = command.into(); Rule { name, command, ..Default::default() } } } #[cfg(test)] mod tests { use super::*; use literally::bmap; #[test] fn it_works() { assert_eq!(2 + 2, 4); } fn test(c: impl Fn(&mut Writter)) { let mut n = Writter::new(); c(&mut n); insta::assert_snapshot!(n.done()); } #[test] fn basic() { test(|n| { n.variable("key", "value"); n.newline(); n.pool("pool", 5); n.include("foo.ninja"); n.subninja("bax.ninja"); }) } #[test] fn build() { test(|n| { n.build(Build { outputs: vec!["out".to_owned()], rule: "cc".to_owned(), inputs: vec!["in".to_owned()], variables: bmap! { "name".to_owned() => "value".to_owned() }, ..Default::default() }) }) } }