tree-sitter/crates/xtask/src/fetch.rs
Max Brunsfeld 0fdf569571
Reorganize rust crates into a flat crates directory, simplify some CI steps (#4496)
* Move all rust crates (except lib) into crates dir, w/o nesting

* Remove stale path from .gitattributes

* Rename lib.rs files for easier navigation

* Rename mod.rs file for easier navigation

* Fix emscripten-version path

* Fix fixtures dir paths

* Use the default rustfmt settings

* Don't use nightly on CI
2025-06-06 14:25:37 -07:00

107 lines
3 KiB
Rust

use crate::{bail_on_err, root_dir, EMSCRIPTEN_VERSION};
use anyhow::Result;
use std::process::Command;
pub fn run_fixtures() -> Result<()> {
let grammars_dir = root_dir().join("test").join("fixtures").join("grammars");
[
("bash", "master"),
("c", "master"),
("cpp", "master"),
("embedded-template", "master"),
("go", "master"),
("html", "master"),
("java", "master"),
("javascript", "master"),
("jsdoc", "master"),
("json", "master"),
("php", "master"),
("python", "master"),
("ruby", "master"),
("rust", "master"),
("typescript", "master"),
]
.iter()
.try_for_each(|(grammar, r#ref)| {
let grammar_dir = grammars_dir.join(grammar);
let grammar_url = format!("https://github.com/tree-sitter/tree-sitter-{grammar}");
println!("Updating the {grammar} grammar...");
if !grammar_dir.exists() {
let mut command = Command::new("git");
command.args([
"clone",
"--depth",
"1",
&grammar_url,
&grammar_dir.to_string_lossy(),
]);
bail_on_err(
&command.spawn()?.wait_with_output()?,
&format!("Failed to clone the {grammar} grammar"),
)?;
}
std::env::set_current_dir(&grammar_dir)?;
let mut command = Command::new("git");
command.args(["fetch", "origin", r#ref, "--depth", "1"]);
bail_on_err(
&command.spawn()?.wait_with_output()?,
&format!("Failed to fetch the {grammar} grammar"),
)?;
let mut command = Command::new("git");
command.args(["reset", "--hard", "FETCH_HEAD"]);
bail_on_err(
&command.spawn()?.wait_with_output()?,
&format!("Failed to reset the {grammar} grammar"),
)?;
Ok(())
})
}
pub fn run_emscripten() -> Result<()> {
let emscripten_dir = root_dir().join("target").join("emsdk");
if emscripten_dir.exists() {
println!("Emscripten SDK already exists");
return Ok(());
}
println!("Cloning the Emscripten SDK...");
let mut command = Command::new("git");
command.args([
"clone",
"https://github.com/emscripten-core/emsdk.git",
&emscripten_dir.to_string_lossy(),
]);
bail_on_err(
&command.spawn()?.wait_with_output()?,
"Failed to clone the Emscripten SDK",
)?;
std::env::set_current_dir(&emscripten_dir)?;
let emsdk = if cfg!(windows) {
"emsdk.bat"
} else {
"./emsdk"
};
let mut command = Command::new(emsdk);
command.args(["install", EMSCRIPTEN_VERSION]);
bail_on_err(
&command.spawn()?.wait_with_output()?,
"Failed to install Emscripten",
)?;
let mut command = Command::new(emsdk);
command.args(["activate", EMSCRIPTEN_VERSION]);
bail_on_err(
&command.spawn()?.wait_with_output()?,
"Failed to activate Emscripten",
)
}