-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code-generation facilities for adding new rustdoc versions. (#36)
- Loading branch information
1 parent
f6080c7
commit bbb3760
Showing
7 changed files
with
604 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[workspace] | ||
|
||
[package] | ||
name = "generator" | ||
publish = false | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
fs-err = "2.11.0" | ||
handlebars = "5.1.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::{collections::BTreeMap, env, path::Path}; | ||
|
||
use handlebars::Handlebars; | ||
|
||
fn main() { | ||
let versions: Vec<i64> = env::args() | ||
.skip(1) // skip the binary's own name, only keep explicit args | ||
.map(|value| { | ||
value | ||
.parse() | ||
.unwrap_or_else(|e| panic!("invalid version \"{value}\": {e}")) | ||
}) | ||
.collect(); | ||
let mut handlebars = Handlebars::new(); | ||
let mut args = BTreeMap::new(); | ||
args.insert("version_numbers", versions.as_slice()); | ||
|
||
let template_and_output = [ | ||
("../../template/parser.template.rs", "../../src/parser.rs"), | ||
("../../template/query.template.rs", "../../src/query.rs"), | ||
( | ||
"../../template/versioned.template.rs", | ||
"../../src/versioned.rs", | ||
), | ||
]; | ||
for (template_file, target) in template_and_output { | ||
let output = materialize(&mut handlebars, Path::new(template_file), &args); | ||
fs_err::write(Path::new(target), output).expect("failed to write file"); | ||
} | ||
} | ||
|
||
fn materialize( | ||
handlebars: &mut Handlebars, | ||
template_file: &Path, | ||
args: &BTreeMap<&str, &[i64]>, | ||
) -> String { | ||
let template = fs_err::read_to_string(template_file).expect("failed to read file"); | ||
handlebars | ||
.render_template(&template, args) | ||
.expect("failed to render template") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Script requirements: | ||
# - python | ||
# - yq | ||
|
||
# Fail on first error, on undefined variables, and on failures in pipelines. | ||
set -euo pipefail | ||
|
||
# Go to the repo root directory. | ||
cd "$(git rev-parse --show-toplevel)" | ||
|
||
CURRENT_VERSIONS="$(yq '.features.default' Cargo.toml -o json | \ | ||
python -m json.tool --compact | \ | ||
sed 's/,/, /g' | \ | ||
sed 's/\[//g' | \ | ||
sed 's/]//g')" | ||
NEXT_VERSION_NUMBER="$(yq '.features.default.[-1] | sub("v(\d+)", "${1}") | to_number | (. + 1)' Cargo.toml -o json -r)" | ||
|
||
ALL_VERSIONS="$(yq '.features.default[] | sub("v(\d+)", "${1}")' Cargo.toml -o json -r) ${NEXT_VERSION_NUMBER}" | ||
|
||
# Generate the new Rust source for the specified versions. | ||
pushd crates/generator | ||
cargo run -- $ALL_VERSIONS | ||
popd | ||
|
||
# Reformat the generated Rust source code. | ||
cargo fmt | ||
|
||
# Update the Cargo.toml file to add the new dependency and feature number. | ||
|
||
# '1h;2,$H;$!d;g' means "look two lines at a time": | ||
# https://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string | ||
sed -e '1h;2,$H;$!d;g' \ | ||
-e "s/\n\[features\]/trustfall-rustdoc-adapter-v${NEXT_VERSION_NUMBER} = { package = \"trustfall-rustdoc-adapter\", version = \">=${NEXT_VERSION_NUMBER}.0.0,<${NEXT_VERSION_NUMBER}.1.0\", optional = true }\n\n[features]/" \ | ||
-i Cargo.toml | ||
|
||
DEFAULT_MATCHER="default = \[${CURRENT_VERSIONS}\]" | ||
sed -e "s/$DEFAULT_MATCHER/default = [${CURRENT_VERSIONS}, \"v${NEXT_VERSION_NUMBER}\"]/" \ | ||
-i Cargo.toml | ||
|
||
echo "v${NEXT_VERSION_NUMBER} = [\"dep:trustfall-rustdoc-adapter-v${NEXT_VERSION_NUMBER}\"]" >>Cargo.toml | ||
|
||
# Ensure cargo regenerates the lockfile. | ||
cargo check |
Oops, something went wrong.