Skip to content

Commit 5f60890

Browse files
committed
refactor: customize ast-grep dynamic in own crate
1 parent a05f03a commit 5f60890

File tree

13 files changed

+439
-32
lines changed

13 files changed

+439
-32
lines changed

.github/workflows/rust-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
- uses: dtolnay/rust-toolchain@stable
5454
id: toolchain
5555
- name: Run tests
56-
run: cargo test --release
56+
run: cargo test
5757
- name: Check schema generation
5858
run: |
5959
# Generate schema

Cargo.lock

Lines changed: 17 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ members = [
88
"crates/codemod-sandbox",
99
"crates/codemod-sandbox/build",
1010
"crates/telemetry",
11-
"xtask"
11+
"xtask",
12+
"crates/ast-grep-codemod-dynamic-lang"
1213
]
1314
resolver = "2"
1415

@@ -53,8 +54,6 @@ thiserror = "2.0"
5354
tokio = { version = "1.36", features = ["full"] }
5455
uuid = { version = "1.6", features = ["v4", "serde"] }
5556
walkdir = "2.4"
56-
ast-grep-dynamic = "0.38.6"
57-
5857

5958
wasm-bindgen = { git = "https://github.com/mohebifar/wasm-bindgen.git", branch = "wasi" }
6059
wasm-bindgen-cli-support = { git = "https://github.com/mohebifar/wasm-bindgen.git", branch = "wasi" }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "ast-grep-codemod-dynamic-lang"
3+
edition = "2021"
4+
authors.workspace = true
5+
description.workspace = true
6+
documentation.workspace = true
7+
repository.workspace = true
8+
license.workspace = true
9+
rust-version.workspace = true
10+
version.workspace = true
11+
12+
[dependencies]
13+
ast-grep-core = "0.38.6"
14+
ignore.workspace = true
15+
libloading = "0.8.8"
16+
serde.workspace = true
17+
target-triple = "0.1.4"
18+
thiserror.workspace = true
19+
tree-sitter = "0.25.6"
20+
21+
[dev-dependencies]
22+
serde_yaml.workspace = true
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use crate::{DynamicLang, DynamicLangError, Registration};
2+
use serde::{Deserialize, Serialize};
3+
4+
use std::collections::HashMap;
5+
use std::path::{Path, PathBuf};
6+
7+
#[derive(Serialize, Deserialize, Clone)]
8+
#[serde(untagged)]
9+
pub enum LibraryPath {
10+
Single(PathBuf),
11+
Platform(HashMap<String, PathBuf>),
12+
}
13+
14+
#[derive(Serialize, Deserialize, Clone)]
15+
#[serde(rename_all = "camelCase")]
16+
pub struct CustomLang {
17+
pub library_path: LibraryPath,
18+
/// the dylib symbol to load ts-language, default is `tree_sitter_{name}`
19+
pub language_symbol: Option<String>,
20+
pub meta_var_char: Option<char>,
21+
pub expando_char: Option<char>,
22+
pub extensions: Vec<String>,
23+
}
24+
25+
impl CustomLang {
26+
pub fn register(
27+
base: &Path,
28+
langs: HashMap<String, CustomLang>,
29+
) -> Result<(), DynamicLangError> {
30+
let registrations: Result<_, _> = langs
31+
.into_iter()
32+
.map(|(name, custom)| to_registration(name, custom, base))
33+
.collect();
34+
unsafe { DynamicLang::register(registrations?) }
35+
}
36+
}
37+
38+
fn to_registration(
39+
name: String,
40+
custom_lang: CustomLang,
41+
base: &Path,
42+
) -> Result<Registration, DynamicLangError> {
43+
let lib_path = match custom_lang.library_path {
44+
LibraryPath::Single(path) => path,
45+
LibraryPath::Platform(mut map) => {
46+
let target = target_triple::TARGET;
47+
map.remove(target)
48+
.ok_or(DynamicLangError::NotConfigured(target))?
49+
}
50+
};
51+
let path = base.join(lib_path);
52+
let sym = custom_lang
53+
.language_symbol
54+
.unwrap_or_else(|| format!("tree_sitter_{name}"));
55+
Ok(Registration {
56+
lang_name: name,
57+
lib_path: path,
58+
symbol: sym,
59+
meta_var_char: custom_lang.meta_var_char,
60+
expando_char: custom_lang.expando_char,
61+
extensions: custom_lang.extensions,
62+
})
63+
}
64+
65+
#[cfg(test)]
66+
mod test {
67+
use super::*;
68+
use serde_yaml::from_str;
69+
70+
#[test]
71+
fn test_custom_lang() {
72+
let yaml = r"
73+
libraryPath: a/b/c.so
74+
extensions: [d, e, f]";
75+
let cus: CustomLang = from_str(yaml).unwrap();
76+
assert_eq!(cus.language_symbol, None);
77+
assert_eq!(cus.extensions, vec!["d", "e", "f"]);
78+
}
79+
fn is_test_supported() -> bool {
80+
cfg!(all(target_os = "macos", target_arch = "aarch64"))
81+
|| cfg!(all(target_os = "linux", target_arch = "x86_64"))
82+
}
83+
84+
#[test]
85+
fn test_custom_lang_platform() {
86+
if !is_test_supported() {
87+
return;
88+
}
89+
let yaml = r"
90+
libraryPath:
91+
aarch64-apple-darwin: a/b/c.so
92+
x86_64-unknown-linux-gnu: a/b/c.so
93+
extensions: [d, e, f]";
94+
let cus: CustomLang = from_str(yaml).unwrap();
95+
assert_eq!(cus.language_symbol, None);
96+
assert_eq!(cus.extensions, vec!["d", "e", "f"]);
97+
let registration = to_registration("test_lang".to_string(), cus, Path::new(".")).unwrap();
98+
assert_eq!(registration.lang_name, "test_lang");
99+
assert_eq!(registration.lib_path.to_str(), Some("./a/b/c.so"));
100+
}
101+
102+
#[test]
103+
fn test_unsupport_platform() {
104+
let yaml = r"
105+
libraryPath:
106+
impossible-platform: a/b/c.so
107+
extensions: [d, e, f]";
108+
let cus: CustomLang = from_str(yaml).unwrap();
109+
let reg = to_registration("test_lang".to_string(), cus, Path::new("."));
110+
assert!(matches!(
111+
reg,
112+
Err(DynamicLangError::NotConfigured(target_triple::TARGET))
113+
));
114+
}
115+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy