Skip to content

Commit

Permalink
feat(andax): add gitlab functions and rpm.changed(), bump to 0.1.23
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko committed Feb 11, 2024
1 parent 9e28013 commit c133446
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Cargo workspace
[package]
name = "anda"
version = "0.1.22"
version = "0.1.23"
edition = "2021"
description = "Andaman Build toolchain"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion andax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "andax"
version = "0.1.7"
version = "0.1.8"
edition = "2021"
license = "MIT"
description = "Andaman scripting runtime"
Expand Down
29 changes: 12 additions & 17 deletions andax/src/fns/rpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ lazy_static::lazy_static! {
/// Update RPM spec files
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RPMSpec {
/// Original spec file content
original: String,
/// Name of project
pub name: String,
/// AndaX chkupdate script of project
Expand All @@ -24,8 +26,6 @@ pub struct RPMSpec {
pub spec: PathBuf,
/// RPM spec file content
pub f: String,
/// Denotes if the spec file has been changed
pub changed: bool,
}

impl RPMSpec {
Expand All @@ -38,13 +38,8 @@ impl RPMSpec {
T: Into<PathBuf> + AsRef<Path>,
U: Into<PathBuf> + AsRef<Path>,
{
Self {
name,
chkupdate: chkupdate.into(),
changed: false,
f: fs::read_to_string(&spec).expect("Cannot read spec to string"),
spec: spec.into(),
}
let f = fs::read_to_string(&spec).expect("Cannot read spec to string");
Self { name, chkupdate: chkupdate.into(), original: f.clone(), f, spec: spec.into() }
}
/// Resets the release number to 1
pub fn reset_release(&mut self) {
Expand All @@ -54,9 +49,7 @@ impl RPMSpec {
pub fn release(&mut self, rel: &str) {
let m = RE_RELEASE.captures(self.f.as_str());
let Some(m) = m else { return error!("No `Release:` preamble for {}", self.name) };
self.f =
RE_RELEASE.replace(&self.f, format!("Release:{}{rel}%{{?dist}}\n", &m[1])).to_string();
self.changed = true;
self.f = RE_RELEASE.replace(&self.f, format!("Release:{}{rel}%?dist\n", &m[1])).to_string();
}
/// Sets the version in the spec file
pub fn version(&mut self, ver: &str) {
Expand All @@ -76,15 +69,13 @@ impl RPMSpec {
return error!("No `Version:` preamble for {}", self.name);
};
self.f = self.f.replace(&cap[0], &format!("%define{}{name}{}{val}", &cap[1], &cap[3]));
self.changed = true;
}
/// Change the value of a `%global` macro by the name
pub fn global(&mut self, name: &str, val: &str) {
let Some(cap) = RE_GLOBAL.captures_iter(self.f.as_str()).find(|cap| &cap[2] == name) else {
return error!("No `Version:` preamble for {}", self.name);
};
self.f = self.f.replace(&cap[0], &format!("%global{}{name}{}{val}", &cap[1], &cap[3]));
self.changed = true;
}
/// Change the `SourceN:` preamble value by `N`
pub fn source(&mut self, i: i64, p: &str) {
Expand All @@ -94,14 +85,13 @@ impl RPMSpec {
};
info!("{}: Source{i}: {p}", self.name);
self.f = self.f.replace(&cap[0], &format!("Source{i}:{}{p}\n", &cap[2]));
self.changed = true;
}
/// Write the updated spec file content
///
/// # Errors
/// - happens only if the writing part failed :3
pub fn write(self) -> std::io::Result<()> {
if self.changed {
if self.changed() {
fs::write(self.spec, self.f)?;
}
Ok(())
Expand All @@ -112,9 +102,13 @@ impl RPMSpec {
}
/// Override the spec file content manually
pub fn set(&mut self, ff: String) {
self.changed = true;
self.f = ff;
}
/// Check if file has been changed
#[must_use]
pub fn changed(&self) -> bool {
self.f == self.original
}
}

impl CustomType for RPMSpec {
Expand All @@ -127,6 +121,7 @@ impl CustomType for RPMSpec {
.with_fn("global", Self::global)
.with_fn("release", Self::reset_release)
.with_fn("release", Self::release)
.with_fn("changed", Self::changed)
.with_get_set("f", Self::get, Self::set);
}
}
74 changes: 74 additions & 0 deletions andax/src/fns/tsunagu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,80 @@ pub mod ar {
.map(|a| a.first().ok_or_else(|| E::from("gh_tag no tags")))??;
Ok(v["name"].as_str().unwrap_or("").to_string())
}
#[rhai_fn(return_raw, global)]
pub fn gh_commit(ctx: NativeCallContext, repo: &str) -> Res<String> {
let v: Value = ureq::get(&format!("https://api.github.com/repos/{repo}/commits/HEAD"))
.set("Authorization", &format!("Bearer {}", env("GITHUB_TOKEN")?))
.set("User-Agent", USER_AGENT)
.call()
.ehdl(&ctx)?
.into_json()
.ehdl(&ctx)?;
trace!("Got json from {repo}:\n{v}");
Ok(v["sha"].as_str().unwrap_or("").to_string())
}
#[rhai_fn(return_raw, global)]
pub fn gh_rawfile(ctx: NativeCallContext, repo: &str, branch: &str, file: &str) -> Res<String> {
ureq::get(&format!("https://raw.githubusercontent.com/{repo}/{branch}/{file}"))
.set("User-Agent", USER_AGENT)
.call()
.ehdl(&ctx)?
.into_string()
.ehdl(&ctx)
}

#[rhai_fn(return_raw, name = "gitlab", global)]
pub fn gitlab_domain(ctx: NativeCallContext, domain: &str, id: &str) -> Res<String> {
let v: Value = ureq::get(&format!("https://{domain}/api/v4/projects/{id}/releases/"))
.set("User-Agent", USER_AGENT)
.call()
.ehdl(&ctx)?
.into_json()
.ehdl(&ctx)?;
trace!("Got json from {id}:\n{v}");
Ok(v[0]["tag_name"].as_str().unwrap_or("").to_string())
}
#[rhai_fn(return_raw, global)]
pub fn gitlab(ctx: NativeCallContext, id: &str) -> Res<String> {
gitlab_domain(ctx, "gitlab.com", id)
}
#[rhai_fn(return_raw, name = "gitlab_tag", global)]
pub fn gitlab_tag_domain(ctx: NativeCallContext, domain: &str, id: &str) -> Res<String> {
let v: Value = ureq::get(&format!("https://{domain}/api/v4/projects/{id}/repository/tags"))
.set("User-Agent", USER_AGENT)
.call()
.ehdl(&ctx)?
.into_json()
.ehdl(&ctx)?;
trace!("Got json from {id}:\n{v}");
Ok(v[0]["name"].as_str().unwrap_or("").to_string())
}
#[rhai_fn(return_raw, global)]
pub fn gitlab_tag(ctx: NativeCallContext, id: &str) -> Res<String> {
gitlab_tag_domain(ctx, "gitlab.com", id)
}
#[rhai_fn(return_raw, name = "gitlab_tag", global)]
pub fn gitlab_commit_domain(
ctx: NativeCallContext,
domain: &str,
id: &str,
branch: &str,
) -> Res<String> {
let v: Value = ureq::get(&format!(
"https://{domain}/api/v4/projects/{id}/repository/branches/{branch}"
))
.set("User-Agent", USER_AGENT)
.call()
.ehdl(&ctx)?
.into_json()
.ehdl(&ctx)?;
trace!("Got json from {id}:\n{v}");
Ok(v["commit"]["id"].as_str().unwrap_or("").to_string())
}
#[rhai_fn(return_raw, global)]
pub fn gitlab_commit(ctx: NativeCallContext, id: &str, branch: &str) -> Res<String> {
gitlab_commit_domain(ctx, "gitlab.com", id, branch)
}

#[rhai_fn(return_raw, global)]
pub fn pypi(ctx: NativeCallContext, name: &str) -> Res<String> {
Expand Down
2 changes: 1 addition & 1 deletion rust-anda-git.spec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

%global crate anda

%global _version 0.1.22
%global _version 0.1.23

Name: rust-anda
Version: %{_version}.%{autogitversion}
Expand Down
2 changes: 1 addition & 1 deletion rust-anda.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%global crate anda

Name: rust-anda
Version: 0.1.22
Version: 0.1.23
Release: 1%{?dist}
Summary: Andaman Build toolchain

Expand Down
10 changes: 4 additions & 6 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use tracing::{debug, error, instrument, trace};
#[instrument(skip(cfg))]
pub fn update(
cfg: Manifest,
lbls: BTreeMap<String, String>,
global_lbls: BTreeMap<String, String>,
fls: BTreeMap<String, String>,
) -> Result<()> {
let mut handlers = vec![];
'p: for (name, mut proj) in cfg.project {
if let Some(scr) = &proj.update {
trace!(name, scr = scr.to_str(), "Th start");
let mut lbls = std::mem::take(&mut proj.labels);
lbls.extend(lbls.clone());
lbls.extend(global_lbls.clone());
for (k, v) in &fls {
if let Some(val) = lbls.get(k) {
if val == v {
Expand All @@ -45,10 +45,8 @@ pub fn update(
});
if let Some(sc) = sc {
let rpm: RPMSpec = sc.get_value("rpm").expect("No rpm object in rhai scope");
if rpm.changed {
if let Err(e) = rpm.write() {
error!("{name}: Failed to write RPM: {e}");
}
if let Err(e) = rpm.write() {
error!("{name}: Failed to write RPM: {e}");
}
}
})?);
Expand Down

0 comments on commit c133446

Please sign in to comment.