diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-09 01:27:18 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-09 01:27:18 +0800 |
| commit | 3edb3c47c2a972093214296914f9d3e98e498612 (patch) | |
| tree | e4269c4f7655a2aa527e7b78eb89e3ba81000495 /tools | |
| parent | 4614aaf2fdfea30796388218b7437d7d24f3823a (diff) | |
feat: replace convert command with ast JSON output
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/tscn-to-bsn/src/cmd/cmd_ast.rs | 58 | ||||
| -rw-r--r-- | tools/tscn-to-bsn/src/cmd/cmd_convert.rs | 83 | ||||
| -rw-r--r-- | tools/tscn-to-bsn/src/cmd/mod.rs | 6 | ||||
| -rw-r--r-- | tools/tscn-to-bsn/src/helps/help.txt | 11 |
4 files changed, 66 insertions, 92 deletions
diff --git a/tools/tscn-to-bsn/src/cmd/cmd_ast.rs b/tools/tscn-to-bsn/src/cmd/cmd_ast.rs new file mode 100644 index 0000000..e2338de --- /dev/null +++ b/tools/tscn-to-bsn/src/cmd/cmd_ast.rs @@ -0,0 +1,58 @@ +use std::path::PathBuf; + +use mingling::{ + Grouped, Routable, + macros::{arg, buffer, chain, command, pack_err, r_println, renderer, routeify}, + picker::{EntryPicker, value::FilePath}, +}; + +use crate::{ + Entry, Next, + gd_res_structure::{TscnDoc, parse}, +}; + +#[derive(Grouped)] +pub struct StateConvert { + input: PathBuf, +} + +#[derive(Grouped)] +pub struct ResultConverted { + ast: TscnDoc, +} + +pack_err!(ErrorRequirePaths); + +#[command(routeify)] +pub fn ast(args: Entry) -> Next { + let tscn_file = args + .pick_or_route(&arg![FilePath], || ErrorRequirePaths::default().into()) + .to_result()?; + + StateConvert { + input: tscn_file.into(), + } + .into() +} + +#[chain(routeify)] +pub fn handle_state_convert(p: StateConvert) -> Next { + let i = p.input; + + let doc = parse(std::fs::read_to_string(&i)?.as_str())?; + + ResultConverted { ast: doc }.to_render() +} + +#[renderer] +pub fn render_converted_to_json(r: ResultConverted) -> String { + let doc = r.ast; + serde_json::to_string(&doc).unwrap_or_else(|_| "{}".to_string()) +} + +#[renderer(buffer)] +pub fn render_error_require_paths(_: ErrorRequirePaths) { + r_println!("Error: Require input file or output file"); + r_println!(""); + r_println!("Usage: tscn2bsn ast <TSCN>") +} diff --git a/tools/tscn-to-bsn/src/cmd/cmd_convert.rs b/tools/tscn-to-bsn/src/cmd/cmd_convert.rs deleted file mode 100644 index eaa4592..0000000 --- a/tools/tscn-to-bsn/src/cmd/cmd_convert.rs +++ /dev/null @@ -1,83 +0,0 @@ -use std::path::PathBuf; - -use mingling::{ - Grouped, Routable, - macros::{arg, buffer, chain, command, pack_err, r_println, renderer, routeify}, - picker::{EntryPicker, value::FilePath}, -}; - -use crate::{Entry, Next, gd_res_structure::parse}; - -#[derive(Grouped)] -pub struct StateConvert { - input: PathBuf, - output: PathBuf, -} - -#[derive(Grouped)] -pub struct ResultConvertedToRon { - input: PathBuf, - output: PathBuf, - len: usize, -} - -pack_err!(ErrorRequirePaths); - -#[command(routeify)] -pub fn convert(args: Entry) -> Next { - let (tscn_file, output_file) = args - .pick_or_route(&arg![input: FilePath, 'I'], || { - ErrorRequirePaths::default().into() - }) - .pick_or_route(&arg![output: PathBuf, 'O'], || { - ErrorRequirePaths::default().into() - }) - .to_result()?; - - StateConvert { - input: tscn_file.into(), - output: output_file, - } - .into() -} - -#[chain(routeify)] -pub fn handle_state_convert(p: StateConvert) -> Next { - let (i, o) = (p.input, p.output); - - // 1. Read file - let src = std::fs::read_to_string(&i)?; - - // 2. Parse into Tscn AST - let doc = parse(&src)?; - - // 3. Serialize to RON - let ron = ron::ser::to_string_pretty(&doc, ron::ser::PrettyConfig::default())?; - - // 4. Write output - std::fs::write(&o, ron)?; - - ResultConvertedToRon { - input: i, - output: o, - len: doc.nodes.len(), - } - .to_render() -} - -#[renderer(buffer)] -pub fn render_converted_to_ron(r: ResultConvertedToRon) { - r_println!( - "OK: {} -> {} ({} nodes)", - r.input.display(), - r.output.display(), - r.len - ); -} - -#[renderer(buffer)] -pub fn render_error_require_paths(_: ErrorRequirePaths) { - r_println!("Error: Require input file or output file"); - r_println!(""); - r_println!("Usage: tscn2bsn convert /I <TSCN> /O <RON>") -} diff --git a/tools/tscn-to-bsn/src/cmd/mod.rs b/tools/tscn-to-bsn/src/cmd/mod.rs index 1b0cdcd..7d71273 100644 --- a/tools/tscn-to-bsn/src/cmd/mod.rs +++ b/tools/tscn-to-bsn/src/cmd/mod.rs @@ -2,14 +2,14 @@ use mingling::{Program, macros::program_setup}; use crate::{ ThisProgram, - cmd::{cmd_build_cache::CMDBuildCache, cmd_convert::CMDConvert}, + cmd::{cmd_ast::CMDAst, cmd_build_cache::CMDBuildCache}, }; +pub mod cmd_ast; pub mod cmd_build_cache; -pub mod cmd_convert; #[program_setup] pub fn commands_setup(p: &mut Program<ThisProgram>) { - p.with_dispatcher(CMDConvert); + p.with_dispatcher(CMDAst); p.with_dispatcher(CMDBuildCache); } diff --git a/tools/tscn-to-bsn/src/helps/help.txt b/tools/tscn-to-bsn/src/helps/help.txt index 3f9c7c2..4c3a95c 100644 --- a/tools/tscn-to-bsn/src/helps/help.txt +++ b/tools/tscn-to-bsn/src/helps/help.txt @@ -4,12 +4,11 @@ A simple program for translating Godot 4's TSCN (*.tscn) files USAGE: tscn2bsn [] <ARGS..> Flags: - /H /Help Show this text - /P /ProjectPath <DIR_PATH> Godot project path + /H /Help Show this text + /P /ProjectPath <DIR_PATH> Godot project path Commands: - convert /I <TSCN_FILE> /O <RON_FILE> Parse TSCN text into a syntax tree and - output it to a RON file. + ast <TSCN_FILE> Analyze the AST of a *.tscn file, output as JSON - build-cache Collect information about the current - project and build a cache for analysis. + build-cache Collect information about the current + project and build a cache for analysis. |
