Skip to content

Commit 4ea35e5

Browse files
committed
Improve svd2rust-regress
1 parent 90b406a commit 4ea35e5

File tree

9 files changed

+4766
-4512
lines changed

9 files changed

+4766
-4512
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,29 @@ jobs:
171171
uses: Swatinem/rust-cache@v2
172172

173173
- run: cargo fmt --all -- --check
174+
175+
artifact:
176+
name: Build svd2rust artifact
177+
if: github.event_name == 'pull_request'
178+
needs: [check]
179+
runs-on: ubuntu-latest
180+
steps:
181+
- uses: actions/checkout@v3
182+
183+
- uses: dtolnay/rust-toolchain@master
184+
with:
185+
toolchain: stable
186+
187+
- name: Cache Dependencies
188+
uses: Swatinem/rust-cache@v2
189+
190+
- name: Build svd2rust artifact
191+
run: cargo build --release
192+
193+
- run: mv target/release/svd2rust svd2rust-x86_64-unknown-linux-gnu-$(git rev-parse --short HEAD)
194+
195+
- name: Upload artifact
196+
uses: actions/upload-artifact@v3
197+
with:
198+
name: artifact-svd2rust-x86_64-unknown-linux-gnu
199+
path: svd2rust-x86_64-unknown-linux-gnu*

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,8 @@ version = "0.14.6"
6969
[dependencies.syn]
7070
version = "2.0"
7171
features = ["full","extra-traits"]
72+
73+
[workspace]
74+
members = ["ci/svd2rust-regress"]
75+
default-members = ["."]
76+
exclude = ["output"]

ci/svd2rust-regress/Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ version = "0.1.0"
55
authors = ["James Munns <james.munns@gmail.com>", "The svd2rust developers"]
66

77
[dependencies]
8-
clap = { version = "4.1", features = ["color", "derive"] }
8+
clap = { version = "4.1", features = ["color", "derive", "string"] }
99
svd2rust = { path = "../../" }
1010
reqwest = { version = "0.11", features= ["blocking"] }
1111
rayon = "1.4"
12-
error-chain = "0.12"
12+
anyhow = "1"
13+
thiserror = "1"
14+
serde = "1"
15+
serde_json = "1"
16+
prettyplease = "0.2"
17+
syn = "2"

ci/svd2rust-regress/src/errors.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

ci/svd2rust-regress/src/github.rs

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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

Comments
 (0)