Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Nov 26, 2023
1 parent fb0c11a commit 43a107d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
args: --all-features -- --nocapture

fmt:
name: Rustfmt
Expand Down
29 changes: 29 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::{
cell::OnceCell,
env,
fs::File,
io::Write,
path::{Path, PathBuf},
process::Command,
};

#[derive(Debug)]
struct Sitter {
path: PathBuf,
src: PathBuf,
lang: String,
version: OnceCell<String>,
}

impl Sitter {
Expand All @@ -23,6 +26,7 @@ impl Sitter {
path,
src,
lang: lang.to_owned(),
version: OnceCell::new(),
}
}
None => {
Expand All @@ -33,6 +37,7 @@ impl Sitter {
src: path.join("src"),
path,
lang,
version: OnceCell::new(),
}
}
};
Expand All @@ -48,6 +53,19 @@ impl Sitter {
self.src.clone()
}

fn version(&self) -> &str {
self.version.get_or_init(|| {
let output = Command::new("git")
.arg("rev-parse")
.arg(format!("HEAD:{}", self.src.display()))
.output()
.expect("git rev-parse")
.stdout;

String::from_utf8(output).expect("utf-8").trim().to_owned()
})
}

fn grammar(&self) -> Option<PathBuf> {
let p = self.path.join("grammar.js");
p.exists().then_some(p)
Expand Down Expand Up @@ -140,6 +158,8 @@ fn write_pepegsit(sitter @ Sitter { lang, .. }: &Sitter) -> std::io::Result<()>
let dest_path = Path::new(&env::var_os("OUT_DIR").unwrap()).join(format!("lang_{lang}.rs"));
let mut output = File::create(dest_path)?;

let version = sitter.version();

writeln!(
output,
r#"
Expand All @@ -155,6 +175,11 @@ fn write_pepegsit(sitter @ Sitter { lang, .. }: &Sitter) -> std::io::Result<()>
pub fn language() -> Language {{
unsafe {{ tree_sitter_{lang}() }}
}}
/// Get the commit hash or version of this grammar.
pub const fn version() -> &'static str {{
"{version}"
}}
"#
)?;

Expand Down Expand Up @@ -227,6 +252,10 @@ fn write_pepegsit(sitter @ Sitter { lang, .. }: &Sitter) -> std::io::Result<()>
output,
r#"
#[test]
fn test_print_version() {{
println!("{{}}", super::version());
}}
#[test]
fn test_can_load_grammar() {{
let mut parser = tree_sitter::Parser::new();
parser
Expand Down

0 comments on commit 43a107d

Please sign in to comment.