aboutsummaryrefslogtreecommitdiff
path: root/dev_tools/src/bin/ci.rs
blob: 3298452d6f0215195c2a86112bb19f45a534bbb1 (plain) (blame)
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
use std::io::Write as _;
use std::process::exit;

use tools::{
    cargo_tomls, eprintln_cargo_style, println_cargo_style, run_cmd, wprintln_cargo_style,
};

fn get_ignore_dirs() -> Vec<String> {
    vec![".temp".to_string()]
}

fn main() {
    #[cfg(windows)]
    let _ = colored::control::set_virtual_terminal(true);
    println!("{}", include_str!("../../../docs/res/ci_banner.txt"));

    let args: Vec<String> = std::env::args().collect();
    let auto_yes = args.iter().any(|a| a == "-y");

    let needs_commit_temp = !{ run_cmd!("git diff-index --quiet HEAD --").is_ok() };

    if needs_commit_temp {
        if auto_yes {
            run_cmd!("git add .").unwrap();
            run_cmd!("git commit -m \"[DO NOT PUSH] CI TEMP [DO NOT PUSH]\"").unwrap();
        } else {
            print!("Working tree is not clean, temporarily commit? [y/N]:");
            std::io::stdout().flush().unwrap();
            let mut input = String::new();
            std::io::stdin().read_line(&mut input).unwrap();
            let input = input.trim();
            if input == "y" || input == "Y" || input == "yes" || input == "Yes" {
                run_cmd!("git add .").unwrap();
                run_cmd!("git commit -m \"[DO NOT PUSH] CI TEMP [DO NOT PUSH]\"").unwrap();
            } else {
                eprintln_cargo_style!("Aborting.");
                exit(2)
            }
        }
    }

    if let Err(exit_code) = ci() {
        restore_workspace(needs_commit_temp).unwrap();
        exit(exit_code)
    }

    let is_worktree_clean = run_cmd!("git diff-index --quiet HEAD --").is_ok();
    if !is_worktree_clean {
        eprintln_cargo_style!("The repository was contaminated during CI, failing!");

        // Print git status
        println!();
        let _ = run_cmd!("git status");

        if needs_commit_temp {
            restore_workspace(true).unwrap();
        }
        exit(1)
    }

    println_cargo_style!("Done: All check passed!");

    if needs_commit_temp {
        restore_workspace(true).unwrap();
    }
}

fn restore_workspace(undo_commit: bool) -> Result<(), i32> {
    run_cmd!("git reset --hard --quiet")?;
    if undo_commit {
        run_cmd!("git reset --soft HEAD~1 --quiet")?;
        run_cmd!("git reset --quiet")?;
    }
    Ok(())
}

fn ci() -> Result<(), i32> {
    build_all()?;
    clippy_all()?;
    test_all()?;
    test_examples()?;
    test_docs_code_blocks()?;
    docs_refresh()?;

    run_cmd!("git add --renormalize .")?;

    Ok(())
}

fn test_examples() -> Result<(), i32> {
    println_cargo_style!("Testing: examples");
    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin test-examples")
}

fn test_docs_code_blocks() -> Result<(), i32> {
    println_cargo_style!("Testing: documentation code blocks");
    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin test-all-markdown-code")
}

fn build_all() -> Result<(), i32> {
    let ignore_dirs = get_ignore_dirs();
    let cargo_tomls = cargo_tomls();
    for cargo_toml in cargo_tomls {
        let path = cargo_toml.parent().unwrap_or(std::path::Path::new(""));
        let path_str = path.to_string_lossy();
        if ignore_dirs.iter().any(|d| path_str.contains(d.as_str())) {
            wprintln_cargo_style!("Skipping: {} (ignored dir)", cargo_toml.to_string_lossy());
            continue;
        }
        println_cargo_style!("Build: {}", cargo_toml.to_string_lossy());
        run_cmd!(
            "cargo build --manifest-path {}",
            cargo_toml.to_string_lossy()
        )?;
    }

    Ok(())
}

fn clippy_all() -> Result<(), i32> {
    let ignore_dirs = get_ignore_dirs();
    let cargo_tomls = cargo_tomls();
    for cargo_toml in cargo_tomls {
        let path = cargo_toml.parent().unwrap_or(std::path::Path::new(""));
        let path_str = path.to_string_lossy();
        if ignore_dirs.iter().any(|d| path_str.contains(d.as_str())) {
            println_cargo_style!("Skipping: {} (ignored dir)", cargo_toml.to_string_lossy());
            continue;
        }
        println_cargo_style!("Clippy: {}", cargo_toml.to_string_lossy());
        run_cmd!(
            "cargo clippy --manifest-path {} -- -D warnings",
            cargo_toml.to_string_lossy()
        )?;
    }

    Ok(())
}

fn test_all() -> Result<(), i32> {
    let ignore_dirs = get_ignore_dirs();
    let cargo_tomls = cargo_tomls();
    for cargo_toml in cargo_tomls {
        let path = cargo_toml.parent().unwrap_or(std::path::Path::new(""));
        let path_str = path.to_string_lossy();
        if ignore_dirs.iter().any(|d| path_str.contains(d.as_str())) {
            println_cargo_style!("Skipping: {} (ignored dir)", cargo_toml.to_string_lossy());
            continue;
        }
        println_cargo_style!("Testing: {}", cargo_toml.to_string_lossy());
        run_cmd!(
            "cargo test --manifest-path {}",
            cargo_toml.to_string_lossy()
        )?;
    }

    Ok(())
}

fn docs_refresh() -> Result<(), i32> {
    println_cargo_style!("Refresh: document at `./docs/`");

    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin docs-code-box-fix")?;
    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin docsify-sidebar-gen")?;
    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin refresh-docs")?;
    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin refresh-feature-mod")?;
    run_cmd!("cargo run --manifest-path dev_tools/Cargo.toml --bin sync-examples")?;

    Ok(())
}