Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code-generation facilities for adding new rustdoc versions. #36

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading