|
| 1 | +use std::process::{Command, Output}; |
| 2 | +use std::{ffi::OsStr, path::Path}; |
| 3 | +use std::{iter::IntoIterator, path::PathBuf}; |
| 4 | + |
| 5 | +use anyhow::Context; |
| 6 | + |
| 7 | +pub fn run_gh<I, S>(args: I) -> Command |
| 8 | +where |
| 9 | + I: IntoIterator<Item = S>, |
| 10 | + S: AsRef<OsStr>, |
| 11 | +{ |
| 12 | + let mut command = Command::new("gh"); |
| 13 | + command.args(args); |
| 14 | + command |
| 15 | +} |
| 16 | + |
| 17 | +pub fn get_current_pr() -> Result<usize, anyhow::Error> { |
| 18 | + let pr = run_gh([ |
| 19 | + "pr", |
| 20 | + "view", |
| 21 | + "--json", |
| 22 | + "number", |
| 23 | + "--template", |
| 24 | + "{{.number}}", |
| 25 | + ]) |
| 26 | + .output()?; |
| 27 | + String::from_utf8(pr.stdout)? |
| 28 | + .trim() |
| 29 | + .parse() |
| 30 | + .map_err(Into::into) |
| 31 | +} |
| 32 | + |
| 33 | +pub fn get_pr_run_id(pr: usize) -> Result<usize, anyhow::Error> { |
| 34 | + let run_id = run_gh([ |
| 35 | + "api", |
| 36 | + &format!("repos/:owner/:repo/actions/runs?event=pull_request&pr={pr}"), |
| 37 | + "--jq", |
| 38 | + r#"[.workflow_runs[] | select(.name == "Continuous integration")][0] | .id"#, |
| 39 | + ]) |
| 40 | + .output()?; |
| 41 | + String::from_utf8(run_id.stdout)? |
| 42 | + .trim() |
| 43 | + .parse() |
| 44 | + .map_err(Into::into) |
| 45 | +} |
| 46 | + |
| 47 | +pub fn get_release_run_id(event: &str) -> Result<usize, anyhow::Error> { |
| 48 | + let query = match event { |
| 49 | + "master" => "branch=master".to_owned(), |
| 50 | + _ => anyhow::bail!("unknown event"), |
| 51 | + }; |
| 52 | + let run_id = dbg!(run_gh([ |
| 53 | + "api", |
| 54 | + &format!("repos/:owner/:repo/actions/runs?{query}"), |
| 55 | + "--jq", |
| 56 | + r#"[.workflow_runs[] | select(.name == "release")][0] | .id"#, |
| 57 | + ]) |
| 58 | + .output()) |
| 59 | + .with_context(|| "couldn't run gh")?; |
| 60 | + String::from_utf8(run_id.stdout)? |
| 61 | + .trim() |
| 62 | + .parse() |
| 63 | + .map_err(Into::into) |
| 64 | +} |
| 65 | + |
| 66 | +fn find(dir: &Path, begins: &str) -> Result<Option<PathBuf>, anyhow::Error> { |
| 67 | + let find = |entry, begins: &str| -> Result<Option<PathBuf>, std::io::Error> { |
| 68 | + let entry: std::fs::DirEntry = entry?; |
| 69 | + let filename = entry.file_name(); |
| 70 | + let filename = filename.to_string_lossy(); |
| 71 | + if entry.metadata()?.is_file() && filename.starts_with(begins) { |
| 72 | + Ok(Some(entry.path())) |
| 73 | + } else { |
| 74 | + Ok(None) |
| 75 | + } |
| 76 | + }; |
| 77 | + let mut read_dir = std::fs::read_dir(dir)?; |
| 78 | + read_dir |
| 79 | + .find_map(|entry| find(entry, begins).transpose()) |
| 80 | + .transpose() |
| 81 | + .map_err(Into::into) |
| 82 | +} |
| 83 | + |
| 84 | +pub fn get_release_binary_artifact( |
| 85 | + reference: &str, |
| 86 | + output_dir: &Path, |
| 87 | +) -> Result<PathBuf, anyhow::Error> { |
| 88 | + let output_dir = output_dir.join(reference); |
| 89 | + if let Some(binary) = find(&output_dir, "svd2rust")? { |
| 90 | + return Ok(binary); |
| 91 | + } |
| 92 | + |
| 93 | + match reference { |
| 94 | + reference if reference.starts_with('v') || matches!(reference, "master" | "latest") => { |
| 95 | + let tag = if reference == "master" { |
| 96 | + Some("Unreleased") |
| 97 | + } else if reference == "latest" { |
| 98 | + None |
| 99 | + } else { |
| 100 | + Some(reference) |
| 101 | + }; |
| 102 | + run_gh([ |
| 103 | + "release", |
| 104 | + "download", |
| 105 | + "--pattern", |
| 106 | + "svd2rust-x86_64-unknown-linux-gnu.gz", |
| 107 | + "--dir", |
| 108 | + ]) |
| 109 | + .arg(&output_dir) |
| 110 | + .args(tag) |
| 111 | + .status()?; |
| 112 | + |
| 113 | + Command::new("tar") |
| 114 | + .arg("-xzf") |
| 115 | + .arg(output_dir.join("svd2rust-x86_64-unknown-linux-gnu.gz")) |
| 116 | + .arg("-C") |
| 117 | + .arg(&output_dir) |
| 118 | + .output() |
| 119 | + .expect("Failed to execute command"); |
| 120 | + } |
| 121 | + _ => { |
| 122 | + let run_id = get_release_run_id(reference)?; |
| 123 | + run_gh([ |
| 124 | + "run", |
| 125 | + "download", |
| 126 | + &run_id.to_string(), |
| 127 | + "-n", |
| 128 | + "svd2rust-x86_64-unknown-linux-gnu", |
| 129 | + "--dir", |
| 130 | + ]) |
| 131 | + .arg(&output_dir) |
| 132 | + .output()?; |
| 133 | + } |
| 134 | + } |
| 135 | + let binary = find(&output_dir, "svd2rust")?; |
| 136 | + binary.ok_or_else(|| anyhow::anyhow!("no binary found")) |
| 137 | +} |
| 138 | + |
| 139 | +pub fn get_pr_binary_artifact(pr: usize, output_dir: &Path) -> Result<PathBuf, anyhow::Error> { |
| 140 | + let output_dir = output_dir.join(format!("{pr}")); |
| 141 | + let run_id = get_pr_run_id(pr)?; |
| 142 | + run_gh([ |
| 143 | + "run", |
| 144 | + "download", |
| 145 | + &run_id.to_string(), |
| 146 | + "-n", |
| 147 | + "artifact-svd2rust-x86_64-unknown-linux-gnu", |
| 148 | + "--dir", |
| 149 | + ]) |
| 150 | + .arg(&output_dir) |
| 151 | + .output()?; |
| 152 | + let mut read_dir = std::fs::read_dir(output_dir)?; |
| 153 | + let binary = read_dir |
| 154 | + .find_map(|entry| { |
| 155 | + let find = |entry| -> Result<Option<PathBuf>, std::io::Error> { |
| 156 | + let entry: std::fs::DirEntry = entry?; |
| 157 | + let filename = entry.file_name(); |
| 158 | + let filename = filename.to_string_lossy(); |
| 159 | + if entry.metadata()?.is_file() && filename.starts_with("svd2rust-regress") { |
| 160 | + Ok(Some(entry.path())) |
| 161 | + } else { |
| 162 | + Ok(None) |
| 163 | + } |
| 164 | + }; |
| 165 | + find(entry).transpose() |
| 166 | + }) |
| 167 | + .transpose()?; |
| 168 | + binary.ok_or_else(|| anyhow::anyhow!("no binary found")) |
| 169 | +} |
0 commit comments