Skip to content

Commit dc70ef5

Browse files
committed
Add update-version task (#172)
This commit adds an update version task to cargo make. The task is implemented as a rust script to work cross-platform and to allow retrieving the current version from Cargo.toml package version, for replacement in docs. It assumes that the version to be updated across all docs is the same.
1 parent 809ae1e commit dc70ef5

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

Makefile.toml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,100 @@ end
211211
"""
212212
]
213213

214+
[tasks.update-version]
215+
description = "Updates the package versions and version in docs"
216+
condition = { env_set = ["NEW_VERSION"] }
217+
script_runner = "@rust"
218+
script = '''
219+
//! ```cargo
220+
//! [dependencies]
221+
//! envmnt = "*"
222+
//! glob = "0.3.0"
223+
//! semver = "0.11.0"
224+
//! toml_edit = "0.2.0"
225+
//! ```
226+
extern crate glob;
227+
extern crate semver;
228+
229+
use std::fs::{File, OpenOptions};
230+
use std::io::{Read, Write};
231+
use std::path::Path;
232+
use glob::glob;
233+
use semver::Version;
234+
fn main() {
235+
let new_version = {
236+
let v = envmnt::get_or_panic("NEW_VERSION");
237+
v.parse::<Version>().expect("NEW_VERSION must be a valid semantic version")
238+
};
239+
let old_version = update_cargo_toml(&new_version);
240+
update_docs(&old_version, &new_version);
241+
}
242+
243+
fn update_docs(old_version: &str, new_version: &Version) {
244+
for entry in glob("docs/*.asciidoc").unwrap()
245+
.chain(glob("README.md").unwrap())
246+
.chain(glob("elasticsearch/src/lib.rs").unwrap()) {
247+
match entry {
248+
Ok(path) => {
249+
let mut content = read_file(&path);
250+
content = content.replace(
251+
&format!("elasticsearch = \"{}\"", old_version),
252+
&format!("elasticsearch = \"{}\"", new_version.to_string()));
253+
write_file(&path, content);
254+
}
255+
Err(e) => panic!("{:?}", e),
256+
}
257+
}
258+
}
259+
260+
fn update_cargo_toml(new_version: &Version) -> String {
261+
let mut old_version = String::new();
262+
for entry in glob("**/Cargo.toml").unwrap() {
263+
match entry {
264+
Ok(path) => {
265+
// skip workspace and target tomls
266+
if path.starts_with("target") || path.to_string_lossy() == "Cargo.toml" {
267+
continue;
268+
}
269+
270+
let content = read_file(&path);
271+
let mut toml = content.parse::<toml_edit::Document>().expect("Could not parse Cargo.toml");
272+
let name = toml["package"]["name"].as_str().expect("toml has name");
273+
274+
// store the version from the elasticsearch package to target replacement in docs
275+
if name == "elasticsearch" {
276+
old_version = toml["package"]["version"]
277+
.as_str()
278+
.expect("toml has version")
279+
.to_string();
280+
}
281+
282+
toml["package"]["version"] = toml_edit::value(new_version.to_string());
283+
write_file(&path, toml.to_string());
284+
},
285+
Err(e) => panic!("{:?}", e),
286+
}
287+
}
288+
old_version
289+
}
290+
291+
fn read_file<P: AsRef<Path>>(path: P) -> String {
292+
let mut file = File::open(path).unwrap();
293+
let mut raw_data = String::new();
294+
file.read_to_string(&mut raw_data).unwrap();
295+
raw_data
296+
}
297+
298+
fn write_file<P: AsRef<Path>>(path: P, content: String) {
299+
let mut file = OpenOptions::new()
300+
.write(true)
301+
.truncate(true)
302+
.open(path)
303+
.unwrap();
304+
file.write_all(content.as_bytes()).unwrap();
305+
}
306+
'''
307+
214308
[tasks.default]
215309
clear = true
216310
script_runner = "@duckscript"
@@ -225,6 +319,8 @@ script = ['''
225319
echo - test-generator: Generates and runs api_generator package tests
226320
echo - test: Runs elasticsearch package tests against a given Elasticsearch version
227321
echo
322+
echo - update-version: Updates the version
323+
echo pass NEW_VERSION environment variable for version
228324
echo - generate-release-notes: Generates release notes for elasticsearch crate.
229325
echo pass OLD_VERSION and NEW_VERSION environment variables to match release version GitHub labels e.g. v7.9.0-alpha.1
230326
echo - package: Packages the elasticsearch crate.

0 commit comments

Comments
 (0)