Skip to content

Commit b02eb60

Browse files
committed
write scripts to tmp file
1 parent 6ce5787 commit b02eb60

4 files changed

Lines changed: 75 additions & 41 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sbuild-linter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "sbuild-linter"
33
description = "Linter for SBUILD package files"
4-
version = "0.1.0"
4+
version = "0.1.3"
55
authors.workspace = true
66
license.workspace = true
77
edition.workspace = true

sbuild-linter/src/build_config/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ pub mod visitor;
1313

1414
#[derive(Debug, Default)]
1515
pub struct BuildConfig {
16-
_disabled: bool,
17-
pkg: String,
18-
pkg_id: Option<String>,
19-
pkg_type: Option<String>,
16+
pub _disabled: bool,
17+
pub pkg: String,
18+
pub pkg_id: Option<String>,
19+
pub pkg_type: Option<String>,
2020
pub pkgver: Option<String>,
21-
app_id: Option<String>,
22-
build_util: Option<Vec<String>>,
23-
build_asset: Option<Vec<BuildAsset>>,
24-
category: Vec<String>,
25-
description: String,
26-
distro_pkg: Option<DistroPkg>,
27-
homepage: Option<Vec<String>>,
28-
maintainer: Option<Vec<String>>,
29-
icon: Option<String>,
30-
desktop: Option<String>,
31-
license: Option<Vec<String>>,
32-
note: Option<Vec<String>>,
33-
provides: Option<Vec<String>>,
34-
repology: Option<Vec<String>>,
35-
src_url: Vec<String>,
36-
tag: Option<Vec<String>>,
21+
pub app_id: Option<String>,
22+
pub build_util: Option<Vec<String>>,
23+
pub build_asset: Option<Vec<BuildAsset>>,
24+
pub category: Vec<String>,
25+
pub description: String,
26+
pub distro_pkg: Option<DistroPkg>,
27+
pub homepage: Option<Vec<String>>,
28+
pub maintainer: Option<Vec<String>>,
29+
pub icon: Option<String>,
30+
pub desktop: Option<String>,
31+
pub license: Option<Vec<String>>,
32+
pub note: Option<Vec<String>>,
33+
pub provides: Option<Vec<String>>,
34+
pub repology: Option<Vec<String>>,
35+
pub src_url: Vec<String>,
36+
pub tag: Option<Vec<String>>,
3737
pub x_exec: XExec,
3838
}
3939

sbuild-linter/src/main.rs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use std::{
22
collections::HashSet,
33
env,
44
fmt::Display,
5-
fs::File,
5+
fs::{self, File},
66
io::{BufRead, BufReader, BufWriter, Write},
7-
process::{Command, ExitStatus, Stdio},
7+
path::PathBuf,
8+
process::{Command, ExitStatus},
89
sync::LazyLock,
910
time::Instant,
1011
};
@@ -116,33 +117,51 @@ fn read_yaml(file_path: &str) -> Result<String, FileError> {
116117
Ok(yaml_content)
117118
}
118119

119-
fn run_shellcheck(script: &str, severity: &str) -> std::io::Result<ExitStatus> {
120-
Command::new("shellcheck")
120+
fn run_shellcheck(file_name: &str, script: &str, severity: &str) -> std::io::Result<ExitStatus> {
121+
let tmp = temp_file(file_name, &script);
122+
123+
let out = Command::new("shellcheck")
121124
.arg(format!("--severity={}", severity))
122-
.arg("-")
123-
.stdin(Stdio::piped())
124-
.stdout(Stdio::inherit())
125-
.stderr(Stdio::inherit())
126-
.spawn()
127-
.and_then(|mut child| {
128-
child.stdin.as_mut().unwrap().write_all(script.as_bytes())?;
129-
child.wait()
130-
})
125+
.arg(&tmp)
126+
.status();
127+
128+
fs::remove_file(tmp).expect("Failed to delete temporary script file");
129+
out
131130
}
132131

133-
fn shellcheck(script: &str) -> std::io::Result<()> {
134-
if !run_shellcheck(script, "error")?.success() {
132+
fn shellcheck(file_name: &str, script: &str) -> std::io::Result<()> {
133+
if !run_shellcheck(file_name, script, "error")?.success() {
135134
return Err(std::io::Error::new(
136135
std::io::ErrorKind::Other,
137136
"Shellcheck emitted errors.",
138137
));
139138
}
140139

141-
let _ = run_shellcheck(script, "warning");
140+
let _ = run_shellcheck(file_name, script, "warning");
142141

143142
Ok(())
144143
}
145144

145+
fn temp_file(file_name: &str, script: &str) -> PathBuf {
146+
let tmp_dir = env::temp_dir();
147+
let tmp_file_path = tmp_dir.join(format!("sbuild-{}", file_name));
148+
{
149+
let mut tmp_file =
150+
File::create(&tmp_file_path).expect("Failed to create temporary script file");
151+
tmp_file
152+
.write_all(script.as_bytes())
153+
.expect("Failed to write to temporary script file");
154+
155+
use std::os::unix::fs::PermissionsExt;
156+
let mut perms = fs::metadata(&tmp_file_path)
157+
.expect("Failed to read file metadata")
158+
.permissions();
159+
perms.set_mode(0o755);
160+
fs::set_permissions(&tmp_file_path, perms).expect("Failed to set executable permissions");
161+
}
162+
tmp_file_path
163+
}
164+
146165
fn is_pkgver_success(config: &BuildConfig, pkgver_path: &str) -> bool {
147166
let x_exec = &config.x_exec;
148167
let mut success = true;
@@ -164,7 +183,12 @@ fn is_pkgver_success(config: &BuildConfig, pkgver_path: &str) -> bool {
164183
None => {
165184
if let Some(ref pkgver) = x_exec.pkgver {
166185
let script = format!("#!/usr/bin/env {}\n{}", x_exec.shell, pkgver);
167-
let cmd = Command::new("sh").args(["-c", &script]).output();
186+
let tmp = temp_file(
187+
pkgver_path.split('/').last().unwrap_or(pkgver_path),
188+
&script,
189+
);
190+
let cmd = Command::new(&tmp).output();
191+
fs::remove_file(tmp).expect("Failed to delete temporary script file");
168192
if let Ok(cmd) = cmd {
169193
if cmd.status.success() {
170194
if !cmd.stderr.is_empty() {
@@ -234,7 +258,12 @@ fn is_shellcheck_success(config: &BuildConfig) -> bool {
234258
let mut success = true;
235259

236260
let script = format!("#!/usr/bin/env {}\n{}", x_exec.shell, x_exec.run);
237-
if shellcheck(&script).is_err() {
261+
if shellcheck(
262+
&config.pkg_id.clone().unwrap_or(config.pkg.clone()),
263+
&script,
264+
)
265+
.is_err()
266+
{
238267
eprintln!(
239268
"[{}] {} -> Shellcheck verification failed.",
240269
&*CROSS_MARK,
@@ -245,7 +274,12 @@ fn is_shellcheck_success(config: &BuildConfig) -> bool {
245274

246275
if let Some(ref pkgver) = x_exec.pkgver {
247276
let script = format!("#!/usr/bin/env {}\n{}", x_exec.shell, pkgver);
248-
if shellcheck(&script).is_err() {
277+
if shellcheck(
278+
&config.pkg_id.clone().unwrap_or(config.pkg.clone()),
279+
&script,
280+
)
281+
.is_err()
282+
{
249283
eprintln!(
250284
"[{}] {} -> Shellcheck verification failed.",
251285
&*CROSS_MARK,

0 commit comments

Comments
 (0)