From 0c1b80faabd1d06e883fecc7c85847005ef3c55c Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Sun, 5 Nov 2023 12:22:26 +0800 Subject: [PATCH] Impl per-target syntax mappings --- build/syntax_mapping.rs | 64 +++++++++++++++---- src/syntax_mapping/builtins/README.md | 6 +- .../builtins/bsd-family/.gitkeep | 0 src/syntax_mapping/builtins/common/.gitkeep | 0 .../99-unset-ambiguous-extensions.toml | 0 src/syntax_mapping/builtins/linux/.gitkeep | 0 .../builtins/{ => linux}/50-git-config.toml | 0 src/syntax_mapping/builtins/macos/.gitkeep | 0 .../builtins/unix-family/.gitkeep | 0 src/syntax_mapping/builtins/windows/.gitkeep | 0 10 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 src/syntax_mapping/builtins/bsd-family/.gitkeep create mode 100644 src/syntax_mapping/builtins/common/.gitkeep rename src/syntax_mapping/builtins/{ => common}/99-unset-ambiguous-extensions.toml (100%) create mode 100644 src/syntax_mapping/builtins/linux/.gitkeep rename src/syntax_mapping/builtins/{ => linux}/50-git-config.toml (100%) create mode 100644 src/syntax_mapping/builtins/macos/.gitkeep create mode 100644 src/syntax_mapping/builtins/unix-family/.gitkeep create mode 100644 src/syntax_mapping/builtins/windows/.gitkeep diff --git a/build/syntax_mapping.rs b/build/syntax_mapping.rs index bab38f16..c8882301 100644 --- a/build/syntax_mapping.rs +++ b/build/syntax_mapping.rs @@ -1,4 +1,9 @@ -use std::{convert::Infallible, env, fs, path::Path, str::FromStr}; +use std::{ + convert::Infallible, + env, fs, + path::{Path, PathBuf}, + str::FromStr, +}; use anyhow::{anyhow, bail}; use indexmap::IndexMap; @@ -181,19 +186,56 @@ impl MappingList { } } +/// Get the list of paths to all mapping definition files that should be +/// included for the current target platform. +fn get_def_paths() -> anyhow::Result> { + let source_subdirs = [ + "common", + #[cfg(target_family = "unix")] + "unix-family", + #[cfg(any( + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd", + target_os = "macos" + ))] + "bsd-family", + #[cfg(target_os = "linux")] + "linux", + #[cfg(target_os = "macos")] + "macos", + #[cfg(target_os = "windows")] + "windows", + ]; + + let mut toml_paths = vec![]; + for subdir in source_subdirs { + let wd = WalkDir::new(Path::new("src/syntax_mapping/builtins").join(subdir)); + let paths = wd + .into_iter() + .filter_map_ok(|entry| { + let path = entry.path(); + (path.is_file() && path.extension().map(|ext| ext == "toml").unwrap_or(false)) + .then(|| path.to_owned()) + }) + .collect::, _>>()?; + toml_paths.extend(paths); + } + + toml_paths.sort_by_key(|path| { + path.file_name() + .expect("file name should not terminate in ..") + .to_owned() + }); + + Ok(toml_paths) +} + fn read_all_mappings() -> anyhow::Result { let mut all_mappings = vec![]; - for entry in WalkDir::new("src/syntax_mapping/builtins") - .sort_by_file_name() - .into_iter() - .map(|entry| entry.unwrap_or_else(|err| panic!("failed to visit a file: {err}"))) - .filter(|entry| { - let path = entry.path(); - path.is_file() && path.extension().map(|ext| ext == "toml").unwrap_or(false) - }) - { - let toml_string = fs::read_to_string(entry.path())?; + for path in get_def_paths()? { + let toml_string = fs::read_to_string(path)?; let mappings = toml::from_str::(&toml_string)?.into_mapping_list(); all_mappings.extend(mappings.0); } diff --git a/src/syntax_mapping/builtins/README.md b/src/syntax_mapping/builtins/README.md index c6208b25..47a7a9e3 100644 --- a/src/syntax_mapping/builtins/README.md +++ b/src/syntax_mapping/builtins/README.md @@ -13,8 +13,10 @@ What defines "a single application" here is deliberately vague, since the file-splitting is purely for maintainability reasons. (Technically, we could just as well use a single TOML file.) So just use common sense. -At compile time, the build script will collect all the syntax mappings defined -by the TOML files within this directory, and embed them into the binary. +TOML files should reside in the corresponding subdirectory of the platform(s) +that they intend to target. At compile time, the build script will go through +each subdirectory that is applicable to the compilation target, collect the +syntax mappings defined by all TOML files, and embed them into the binary. ## File syntax diff --git a/src/syntax_mapping/builtins/bsd-family/.gitkeep b/src/syntax_mapping/builtins/bsd-family/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/syntax_mapping/builtins/common/.gitkeep b/src/syntax_mapping/builtins/common/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/syntax_mapping/builtins/99-unset-ambiguous-extensions.toml b/src/syntax_mapping/builtins/common/99-unset-ambiguous-extensions.toml similarity index 100% rename from src/syntax_mapping/builtins/99-unset-ambiguous-extensions.toml rename to src/syntax_mapping/builtins/common/99-unset-ambiguous-extensions.toml diff --git a/src/syntax_mapping/builtins/linux/.gitkeep b/src/syntax_mapping/builtins/linux/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/syntax_mapping/builtins/50-git-config.toml b/src/syntax_mapping/builtins/linux/50-git-config.toml similarity index 100% rename from src/syntax_mapping/builtins/50-git-config.toml rename to src/syntax_mapping/builtins/linux/50-git-config.toml diff --git a/src/syntax_mapping/builtins/macos/.gitkeep b/src/syntax_mapping/builtins/macos/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/syntax_mapping/builtins/unix-family/.gitkeep b/src/syntax_mapping/builtins/unix-family/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/syntax_mapping/builtins/windows/.gitkeep b/src/syntax_mapping/builtins/windows/.gitkeep new file mode 100644 index 00000000..e69de29b