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
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;

pub fn setup(workflow_dir: &Path) -> (Vec<String>, [(&'static str, &'static str); 2]) {
    // Ignore results, since maybe the folders do not exists atm
    let _ = std::fs::remove_dir_all(workflow_dir);
    let _ = std::fs::create_dir_all(workflow_dir);

    let metadata = cargo_metadata::MetadataCommand::new()
        .no_deps()
        .exec()
        .unwrap();
    let packages = metadata
        .workspace_members
        .into_iter()
        .map(|w| w.repr)
        .map(|p| p[..p.find(' ').unwrap()].to_string())
        .collect::<Vec<_>>();

    let images = [("cassandra", "cassandra"), ("scylladb", "scylladb/scylla")];

    (packages, images)
}

pub fn write_template(
    path_to_workflow: &Path,
    container: &str,
    image: &str,
    template: &str,
) -> File {
    let mut yml = File::create(path_to_workflow.join(format!("{}.yml", container))).unwrap();

    let template = template
        .replace("$IMAGE$", image)
        .replace("$CONTAINER$", container);

    writeln!(yml, "# This file is generated, don't edit\n").unwrap();
    writeln!(yml, "{}", template).unwrap();

    yml
}

pub fn write_tests(yml: &mut File, whitespace: &str, package: &str) {
    writeln!(yml, "{}- name: Build {}", whitespace, package).unwrap();
    writeln!(
        yml,
        "{}  run: cargo build --package {} --verbose",
        whitespace, package
    )
    .unwrap();

    writeln!(yml, "{}- name: Run tests {}", whitespace, package).unwrap();
    writeln!(
        yml,
        "{}  run: cargo test --package {} --verbose -- --test-threads=1",
        whitespace, package
    )
    .unwrap();

    writeln!(yml, "{}- name: Check clippy {}", whitespace, package).unwrap();
    writeln!(
        yml,
        "{}  run: cargo +nightly clippy --package {} -- -D warnings",
        whitespace, package
    )
    .unwrap();

    writeln!(yml, "{}- name: Check fmt {}", whitespace, package).unwrap();
    writeln!(
        yml,
        "{}  run: cargo +nightly fmt --package {} -- --check",
        whitespace, package
    )
    .unwrap();

    writeln!(yml).unwrap();

    // Format and fix project directly
    execute_cargo_command("fmt", package);
    execute_cargo_command("fix", package);

    // TODO: This does not work yet
    // Command::new("cargo")
    //     .env("TEST_CDRS_DB_KEYSPACE_KEY", "test_keyspace_for_testing")
    //     .args(&["build".to_string(), "--package".to_string(), format!("{}", package)])
    //     .output()
    //     .unwrap();
    //
    // // Use clippy to check for any errors
    // let clippy_out = Command::new("cargo")
    //     .env("TEST_CDRS_DB_KEYSPACE_KEY", "test_keyspace_for_testing")
    //     .args(&[
    //         "+nightly".to_string(),
    //         "clippy".to_string(),
    //         "--package".to_string(),
    //         format!("{}", package),
    //         "--".to_string(),
    //         "-D".to_string(),
    //         "warnings".to_string(),
    //     ])
    //     .output()
    //     .unwrap();
    //
    // if !clippy_out.stderr.is_empty() && !clippy_out.status.success() {
    //     let error = String::from_utf8(clippy_out.stderr).unwrap();
    //
    //     panic!("{}", error);
    // }
}

fn execute_cargo_command(command: &str, package: &str) {
    Command::new("cargo")
        .args(&[
            "+nightly".to_string(),
            command.to_string(),
            "--package".to_string(),
            package.to_string(),
        ])
        .output()
        .unwrap();
}