xtask/
main.rs

1#![allow(clippy::needless_raw_string_hashes)]
2
3use std::{
4    env,
5    path::{Path, PathBuf},
6    process,
7};
8
9use anyhow::Result;
10use once_cell::sync::Lazy;
11
12mod add_lang;
13mod build_js_langs;
14mod codegen;
15mod fetch_queries;
16mod set_version;
17mod theme_svgs;
18mod update_langs;
19mod update_vite_example;
20
21mod schema {
22    include!("../../syntastica-macros/src/schema.rs");
23}
24
25pub static WORKSPACE_DIR: Lazy<PathBuf> = Lazy::new(|| {
26    Path::new(env!("CARGO_MANIFEST_DIR"))
27        .parent()
28        .unwrap()
29        .to_path_buf()
30});
31pub static LANGUAGE_CONFIG: Lazy<schema::LanguageConfig> = Lazy::new(|| {
32    toml::from_str(include_str!("../../syntastica-macros/languages.toml"))
33        .expect("invalid `languages.toml`")
34});
35
36fn main() {
37    if let Err(err) = try_main() {
38        eprintln!("error running task:\n{err:?}");
39        process::exit(1);
40    }
41}
42
43fn try_main() -> Result<()> {
44    match env::args().nth(1).unwrap_or_default().as_str() {
45        "--help" | "-h" | "" => println!(
46            "{}",
47            r###"
48Usage: Run with `cargo xtask <task>`, eg. `cargo xtask codegen`.
49
50    Tasks:
51        codegen:                              Run all codegen subtasks
52        codegen queries:                      Generate the `lib.rs` file for syntastica-queries
53        codegen parsers-dep:                  Generate parts of the `Cargo.toml` for syntastica-parsers
54        codegen parsers-gitdep:               Generate parts of the `Cargo.toml` for syntastica-parsers-gitdep
55        codegen parsers-git:                  Generate parts of the `Cargo.toml` for syntastica-parsers-git
56        codegen parser-lists:                 Generate the parser lists in all three syntastica-parsers READMEs
57        codegen js-list:                      Generate the theme list in the JavaScript bindings
58        codegen js-pkgs:                      Generate the JS packages for all supported languages
59        codegen theme-list:                   Generate the `THEMES` list and `from_str` function for syntastica-themes
60        set-version <version>:                Set the version of all syntastica crates
61        add-lang <group> <name> <url> [path]: Add boilerplate code for a new language called <name> with sources at <url>/[path] in the feature group <group>
62        update-langs                          Search for new versions of languages
63        fetch-queries                         Fetch latest upstream versions of forked queries
64        theme-svgs                            Create SVGs for all themes using Typst and the `custom_renderer` example
65        build-js-langs                        Build all JS language packages in the generated `syntastica-js/langs` directory
66        update-vite-example                   Update the vite example project to include all languages
67            "###
68            .trim(),
69        ),
70        "codegen" => codegen::run()?,
71        "set-version" => set_version::run()?,
72        "add-lang" => add_lang::run()?,
73        "update-langs" => update_langs::run()?,
74        "fetch-queries" => fetch_queries::run()?,
75        "theme-svgs" => theme_svgs::run()?,
76        "build-js-langs" => build_js_langs::run()?,
77        "update-vite-example" => update_vite_example::run()?,
78        task => eprintln!(
79            "unknown task '{task}', run `cargo xtask --help` to see a list of available tasks"
80        ),
81    }
82
83    Ok(())
84}