| 1 |
use std::fs::{self, File}; |
| 2 |
use std::io::Write; |
| 3 |
use std::path::Path; |
| 4 |
use std::process::Command; |
| 5 |
|
| 6 |
use anyhow::{ensure, Context, Result}; |
| 7 |
use skidder::{decompress, Metadata}; |
| 8 |
use tempfile::TempDir; |
| 9 |
|
| 10 |
use crate::collect_grammars; |
| 11 |
use crate::flags::RegenerateParser; |
| 12 |
use crate::import::import_compressed; |
| 13 |
|
| 14 |
impl RegenerateParser { |
| 15 |
pub fn run(self) -> Result<()> { |
| 16 |
let paths = if self.recursive { |
| 17 |
collect_grammars(&self.path)? |
| 18 |
} else { |
| 19 |
vec![self.path.clone()] |
| 20 |
}; |
| 21 |
let temp_dir = |
| 22 |
TempDir::new().context("failed to create temporary directory for decompression")?; |
| 23 |
|
| 24 |
File::create(temp_dir.path().join("grammar.js")) |
| 25 |
.context("failed to create temporary directory for decompression")?; |
| 26 |
let mut failed = Vec::new(); |
| 27 |
for grammar_dir in paths { |
| 28 |
let grammar_name = grammar_dir.file_name().unwrap().to_str().unwrap(); |
| 29 |
if grammar_name <= "dart" { |
| 30 |
continue; |
| 31 |
} |
| 32 |
println!("checking {grammar_name}"); |
| 33 |
|
| 34 |
let compressed = Metadata::read(&grammar_dir.join("metadata.json")) |
| 35 |
.with_context(|| format!("failed to read metadata for {grammar_name}"))? |
| 36 |
.parser_definition() |
| 37 |
.unwrap() |
| 38 |
.compressed; |
| 39 |
|
| 40 |
let src_path = grammar_dir.join("src"); |
| 41 |
let src_grammar_path = src_path.join("grammar.json"); |
| 42 |
let grammar_path = temp_dir.path().join("grammar.json"); |
| 43 |
if !src_grammar_path.exists() { |
| 44 |
eprintln!("grammar.json not found for {grammar_name}"); |
| 45 |
failed.push(grammar_name.to_owned()); |
| 46 |
continue; |
| 47 |
} |
| 48 |
if compressed { |
| 49 |
let dst = File::create(&grammar_path).with_context(|| { |
| 50 |
format!( |
| 51 |
"failed to create grammar.json file in temporary build directory {}", |
| 52 |
temp_dir.path().display() |
| 53 |
) |
| 54 |
})?; |
| 55 |
decompress_file(&src_grammar_path, dst).with_context(|| { |
| 56 |
format!("failed to decompress grammar.json for {grammar_name}") |
| 57 |
})?; |
| 58 |
} else { |
| 59 |
fs::copy(src_grammar_path, &grammar_path) |
| 60 |
.with_context(|| format!("failed to copy grammar.json for {grammar_name}"))?; |
| 61 |
} |
| 62 |
println!("running tree-sitter generate {}", grammar_path.display()); |
| 63 |
let res = Command::new("tree-sitter") |
| 64 |
.arg("generate") |
| 65 |
.arg("--no-bindings") |
| 66 |
.arg(&grammar_path) |
| 67 |
.current_dir(temp_dir.path()) |
| 68 |
.status() |
| 69 |
.with_context(|| { |
| 70 |
format!( |
| 71 |
"failed to execute tree-sitter generate {}", |
| 72 |
grammar_path.display() |
| 73 |
) |
| 74 |
})? |
| 75 |
.success(); |
| 76 |
if !res { |
| 77 |
eprintln!( |
| 78 |
"failed to execute tree-sitter generate {}", |
| 79 |
grammar_path.display() |
| 80 |
); |
| 81 |
failed.push(grammar_name.to_owned()); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
let new_parser_path = temp_dir.path().join("src").join("parser.c"); |
| 86 |
let old_parser_path = src_path.join("parser.c"); |
| 87 |
let mut old_parser = Vec::new(); |
| 88 |
decompress_file(&old_parser_path, &mut old_parser) |
| 89 |
.with_context(|| format!("failed to decompress parser for {grammar_name}"))?; |
| 90 |
let old_parser = String::from_utf8_lossy(&old_parser); |
| 91 |
let new_parser = fs::read_to_string(&new_parser_path) |
| 92 |
.context("tree-sitter cli did not generate parser.c")?; |
| 93 |
if old_parser.trim() == new_parser.trim() { |
| 94 |
continue; |
| 95 |
} |
| 96 |
failed.push(grammar_name.to_owned()); |
| 97 |
eprintln!("existing parser.c was outdated updating..."); |
| 98 |
if compressed { |
| 99 |
import_compressed(&new_parser_path, &old_parser_path).with_context(|| { |
| 100 |
format!("failed to compress new parser.c for {grammar_name}") |
| 101 |
})?; |
| 102 |
} else { |
| 103 |
fs::copy(&new_parser_path, &old_parser_path) |
| 104 |
.with_context(|| format!("failed to opy new parser.c for {grammar_name}"))?; |
| 105 |
} |
| 106 |
} |
| 107 |
ensure!( |
| 108 |
failed.is_empty(), |
| 109 |
"parser.c files is not up to date for {failed:?}!" |
| 110 |
); |
| 111 |
Ok(()) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
fn decompress_file(src: &Path, dst: impl Write) -> Result<()> { |
| 116 |
File::open(src) |
| 117 |
.map_err(anyhow::Error::from) |
| 118 |
.and_then(|mut reader| decompress(&mut reader, dst)) |
| 119 |
.with_context(|| format!("failed to decompress {}", src.display()))?; |
| 120 |
Ok(()) |
| 121 |
} |