Skip to content

Commit

Permalink
Add code-generation facilities for adding new rustdoc versions. (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi authored Jun 14, 2024
1 parent f6080c7 commit bbb3760
Show file tree
Hide file tree
Showing 7 changed files with 604 additions and 0 deletions.
290 changes: 290 additions & 0 deletions crates/generator/Cargo.lock

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

11 changes: 11 additions & 0 deletions crates/generator/Cargo.toml
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"
41 changes: 41 additions & 0 deletions crates/generator/src/main.rs
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")
}
45 changes: 45 additions & 0 deletions scripts/add_next_rustdoc_version.sh
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
Loading

0 comments on commit bbb3760

Please sign in to comment.