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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! [lies](https://docs.rs/lies/) implementation details.

const CARGO_ABOUT_REV : &'static str = "6b2a876910a98cfc48f68468b101bc2dfaed6cca";

extern crate proc_macro;

use proc_macro_hack::*;
use proc_macro::*;

use std::ffi::*;
use std::fs::*;
use std::io::{self, Write};
use std::path::*;
use std::process::*;

#[proc_macro_hack]
pub fn licenses_text(_input: TokenStream) -> TokenStream {
    use_cargo_about(include_bytes!("../templates/about.console.hbs"), "about.console.hbs")
}

#[proc_macro_hack]
pub fn licenses_ansi(_input: TokenStream) -> TokenStream {
    use_cargo_about(include_bytes!("../templates/about.ansi.hbs"), "about.ansi.hbs")
}

fn use_cargo_about(input_text: &[u8], input_name: &str) -> TokenStream {
    let cargo_about = ensure_cargo_about_installed(CARGO_ABOUT_REV);
    ensure_about_toml_exists();

    let tmp_template_path = std::env::temp_dir().join(format!("{}-{}-{}",
        get_env_path("CARGO_PKG_NAME"   ).display(),
        get_env_path("CARGO_PKG_VERSION").display(),
        input_name
    ));

    File::create(&tmp_template_path)
        .expect("Unable to create temporary .hbs file")
        .write_all(input_text)
        .expect("Unable to write entire temporary .hbs file");

    let output = match cmd_output(format!("{} about generate {}", cargo_about.display(), tmp_template_path.display()).as_str()) {
        Ok(o) => o,
        Err(err) => {
            eprintln!("Failed to '{} about generate {}'", cargo_about.display(), tmp_template_path.display());
            eprintln!("{}", err);
            exit(1);
        },
    };

    let output = reprocess(output.as_str());

    TokenStream::from(TokenTree::Literal(Literal::string(output.as_str())))
}

fn ensure_cargo_about_installed(rev: &str) -> PathBuf {
    let root            = [std::env::temp_dir().as_path(), Path::new(rev)].iter().collect::<PathBuf>();
    let expected_path   = [root.as_path(), Path::new("bin"), Path::new("cargo-about")].iter().collect::<PathBuf>();
    let version = cmd_output(format!("{} about --version", expected_path.display()).as_str()).ok();
    let version = version.as_ref().and_then(|output|{
        let ws = output.find(' ')?;
        let (_name, version) = output.split_at(ws);
        Some(version.trim()) // leading ' ', trailing '\n'
    });

    if version.map(|v| v < "0.0.1").unwrap_or(true) {
        eprintln!("Installing cargo-about {}", rev);
        if let Err(err) = cmd_run(format!("cargo install --git https://github.com/EmbarkStudios/cargo-about.git --rev {} --root {}", rev, root.display()).as_str()) {
            eprintln!("Failed to install cargo-about 0.0.1: {}", err);
            exit(1);
        }
    }

    expected_path
}

fn ensure_about_toml_exists() {
    let path = get_env_path("CARGO_MANIFEST_DIR").join("about.toml");
    if !path.exists() {
        let mut about = File::create(path).expect("about.toml does not exist, and cannot be opened for writing");
        about.write_all(include_bytes!("../templates/about.toml")).expect("Created but failed to fully write out about.toml");
    }
}

fn reprocess(text: &str) -> String {
    let mut lines = text.lines().map(|line| line
        .replace("&quot;", "\"")
        .replace("&amp;", "&")
        .replace("&copy;", "(c)")
    ).collect::<Vec<String>>();
    let lines_n = lines.len();

    for start_line in 0..lines_n {
        while lines[start_line].contains('\t') {
            // Find out the size of this "table"
            let mut max_col = 0;
            let mut end_line = start_line;
            while end_line < lines_n {
                if let Some(tab) = lines[end_line].find('\t') {
                    max_col = max_col.max(tab);
                    end_line += 1;
                } else {
                    break;
                }
            }

            max_col += 4; // Ensure minimum spacing

            // Fixup this "table"
            for line in start_line..end_line {
                let line = &mut lines[line];
                let tab = line.find('\t').unwrap(); // Already found it once
                let mut fixed = line[..tab].to_string();
                for _ in fixed.chars().count()..max_col {
                    fixed.push(' ');
                }
                fixed.push_str(&line[tab+1..]);
                *line = fixed;
            }
        }
    }

    lines.join("\n")
}



fn cmd(args: &str) -> Command {
    let wd = get_env_path("CARGO_MANIFEST_DIR");
    let mut args = args.split_whitespace();
    let exe = args.next().expect("cmd:  Expected a command");
    let mut cmd = Command::new(exe);
    cmd.current_dir(wd);
    for arg in args { cmd.arg(arg); }
    cmd
}

fn cmd_run(args: &str) -> io::Result<()> {
    let status = cmd(args).status()?;
    if !status.success() {
        Err(io::Error::new(io::ErrorKind::Other, format!("Failed to successfully run \"{}\": {:?}", args, status)))
    } else {
        Ok(())
    }
}

fn cmd_output(args: &str) -> io::Result<String> {
    let output = cmd(args).output()?;
    if !output.status.success() {
        let mut s = format!("Failed with {}: {}", output.status, args);
        for (channel,   output          ) in [
            ("stdout",  &output.stdout  ),
            ("stderr",  &output.stderr  ),
        ].iter().copied() {
            if !output.is_empty() {
                s.push_str("\n");
                s.push_str(channel);
                s.push_str(":\n");
                s.push_str("-------");
                s.push_str(&String::from_utf8_lossy(output));
            }
        }

        Err(io::Error::new(io::ErrorKind::Other, s))
    } else {
        String::from_utf8(output.stdout).map_err(|err| io::Error::new(
            io::ErrorKind::InvalidData,
            format!("{:?} output invalid UTF8: {}", args, err)
        ))
    }
}

fn get_env_path(name: &str) -> PathBuf {
    PathBuf::from(get_env_os(name))
}

fn get_env_os(name: &str) -> OsString {
    match std::env::var_os(name) {
        Some(v) => v,
        None => {
            if cfg!(windows) {
                eprintln!("%{}%: Not set", name);
                exit(1);
            } else {
                eprintln!("${{{}}}: Not set", name);
                exit(1);
            }
        },
    }
}