Skip to content

Commit

Permalink
[ci] Openapi schema version is not updated on version update #1045 (#…
Browse files Browse the repository at this point in the history
…1053)

update openapi schema during release
  • Loading branch information
michaelvlach authored Mar 13, 2024
1 parent d5e23cb commit eb6f1b2
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
76 changes: 47 additions & 29 deletions agdb_ci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const RUST_RELEASE_PROJECTS: [&str; 4] = ["agdb", "agdb_derive", "agdb_api", "ag
const AGDB_PROJECT: &str = "agdb";
const CARGO_TOML: &str = "Cargo.toml";
const PACKAGE_JSON: &str = "package.json";
const OPENAPI_JSON: &str = "schema.json";
const IGNORE: [&str; 8] = [
"node_modules",
"tests",
Expand All @@ -24,8 +25,8 @@ struct CIError {

#[derive(Default)]
struct ProjectFiles {
cargo_tomls: Vec<std::path::PathBuf>,
package_jsons: Vec<std::path::PathBuf>,
tomls: Vec<std::path::PathBuf>,
jsons: Vec<std::path::PathBuf>,
}

impl<E: std::error::Error> From<E> for CIError {
Expand Down Expand Up @@ -62,9 +63,11 @@ fn project_files(path: &std::path::Path, files: &mut ProjectFiles) -> Result<(),
&& !IGNORE.contains(&dir.file_name().to_string_lossy().as_ref())
{
if dir.path().join(CARGO_TOML).exists() {
files.cargo_tomls.push(dir.path().join(CARGO_TOML));
files.tomls.push(dir.path().join(CARGO_TOML));
} else if dir.path().join(PACKAGE_JSON).exists() {
files.package_jsons.push(dir.path().join(PACKAGE_JSON));
files.jsons.push(dir.path().join(PACKAGE_JSON));
} else if dir.path().join(OPENAPI_JSON).exists() {
files.jsons.push(dir.path().join(OPENAPI_JSON));
}

project_files(&dir.path(), files)?;
Expand All @@ -74,15 +77,15 @@ fn project_files(path: &std::path::Path, files: &mut ProjectFiles) -> Result<(),
Ok(())
}

fn update_cargo_projects(
fn update_tomls(
current_version: &str,
new_version: &str,
cargo_tomls: &[std::path::PathBuf],
tomls: &[std::path::PathBuf],
) -> Result<(), CIError> {
for cargo_toml in cargo_tomls {
println!("Updating... {:?}", cargo_toml);
for toml in tomls {
println!("Updating... {:?}", toml);

let mut content = std::fs::read_to_string(cargo_toml)?.replace(
let mut content = std::fs::read_to_string(toml)?.replace(
&format!("\nversion = \"{current_version}\""),
&format!("\nversion = \"{new_version}\""),
);
Expand All @@ -99,44 +102,59 @@ fn update_cargo_projects(
);
}

std::fs::write(cargo_toml, content)?;
std::fs::write(toml, content)?;
}

Ok(())
}

fn update_typescript_projects(
fn update_jsons(
current_version: &str,
new_version: &str,
package_jsons: &[std::path::PathBuf],
jsons: &[std::path::PathBuf],
) -> Result<(), CIError> {
for package_json in package_jsons {
println!("Updating... {:?}", package_json);
for json in jsons {
println!("Updating... {:?}", json);

let mut content = std::fs::read_to_string(package_json)?.replace(
let mut content = std::fs::read_to_string(json)?.replace(
&format!("\"version\": \"{current_version}\""),
&format!("\"version\": \"{new_version}\""),
);

for project in TYPESCRIPT_PROJECTS {
if json.ends_with("package.json") {
content = content
.replace(
&format!("\"{project}\": \"{current_version}\""),
&format!("\"{project}\": \"{new_version}\""),
&format!("\"version\": \"{current_version}\""),
&format!("\"version\": \"{new_version}\""),
)
.replace(
&format!("\"{project}\": \"^{current_version}\""),
&format!("\"{project}\": \"^{new_version}\""),
&format!("\"version\": \"^{current_version}\""),
&format!("\"version\": \"^{new_version}\""),
);

for project in TYPESCRIPT_PROJECTS {
content = content
.replace(
&format!("\"{project}\": \"{current_version}\""),
&format!("\"{project}\": \"{new_version}\""),
)
.replace(
&format!("\"{project}\": \"^{current_version}\""),
&format!("\"{project}\": \"^{new_version}\""),
);
}
}

std::fs::write(package_json, content)?;
std::process::Command::new("bash")
.arg("-c")
.arg("npm install")
.current_dir(package_json.parent().expect("Parent directory not found"))
.spawn()?
.wait()?;
std::fs::write(json, content)?;

if json.ends_with("package.json") {
std::process::Command::new("bash")
.arg("-c")
.arg("npm install")
.current_dir(json.parent().expect("Parent directory not found"))
.spawn()?
.wait()?;
}
}
Ok(())
}
Expand All @@ -152,8 +170,8 @@ fn main() -> Result<(), CIError> {

let mut files = ProjectFiles::default();
project_files(std::path::Path::new("."), &mut files)?;
update_cargo_projects(&current_version, &new_version, &files.cargo_tomls)?;
update_typescript_projects(&current_version, &new_version, &files.package_jsons)?;
update_tomls(&current_version, &new_version, &files.tomls)?;
update_jsons(&current_version, &new_version, &files.jsons)?;

println!("DONE");

Expand Down
11 changes: 11 additions & 0 deletions agdb_ci/tests/test_data_before/project1/openapi/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.0.3",
"info": {
"title": "agdb_server",
"description": "Agnesoft Graph Database Server",
"license": {
"name": "Apache-2.0"
},
"version": "0.2.0"
}
}
11 changes: 11 additions & 0 deletions agdb_ci/tests/test_data_expected/project1/openapi/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"openapi": "3.0.3",
"info": {
"title": "agdb_server",
"description": "Agnesoft Graph Database Server",
"license": {
"name": "Apache-2.0"
},
"version": "0.3.0"
}
}

0 comments on commit eb6f1b2

Please sign in to comment.