-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: regenerate type sitter bindings with build.rs
- Loading branch information
1 parent
5931650
commit f9656e3
Showing
9 changed files
with
10,015 additions
and
8,709 deletions.
There are no files selected for viewing
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
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
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
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
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,38 @@ | ||
use std::{fmt::Display, fs::File, io::Write, path::Path}; | ||
|
||
use rust_format::{Formatter, RustFmt}; | ||
use type_sitter_gen::tree_sitter; | ||
|
||
fn main() { | ||
regenerate_node_types(); | ||
|
||
println!("cargo:rerun-if-changed=../vendor/tree-sitter-lura/src/parser.c"); | ||
} | ||
|
||
/// Regenerates the `src/generated/lura.rs` file from the `src/node-types.json` | ||
fn regenerate_node_types() { | ||
let input_path = Path::new("../vendor/tree-sitter-lura/src/node-types.json"); | ||
let target = Path::new("src/generated/lura.rs"); | ||
let node_types = type_sitter_gen::generate_nodes(input_path, &tree_sitter()).expect("failed to generate nodes"); | ||
clear_output_files(target); | ||
write_rust_file(target, node_types); | ||
} | ||
|
||
/// Clears the output file so we don't write a directory or an invalid file | ||
/// when the build script fails. | ||
fn clear_output_files(target: &Path) { | ||
if target.is_dir() { | ||
std::fs::remove_dir_all(target).expect("failed to remove directory"); | ||
} | ||
if target.exists() && target.is_file() { | ||
std::fs::remove_file(target).expect("failed to remove file"); | ||
} | ||
} | ||
|
||
/// Writes the generated Rust code to the output file, formatting it | ||
/// with rustfmt. | ||
fn write_rust_file(path: &Path, contents: impl Display) { | ||
let mut file = File::create(path).expect("failed to create file"); | ||
write!(file, "{}", contents).expect("failed to write file"); | ||
RustFmt::new().format_file(path).expect("failed to format file"); | ||
} |
Oops, something went wrong.