1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! CLI for parsing an IDL from a Move package.
use std::{collections::BTreeMap, path::PathBuf};

use anyhow::*;
use json_cli::{CliTool, CliTypedResult};
use move_idl::IDLBuilder;
use move_package::BuildConfig;
use move_ts::{generate_index, CodegenContext};

/// Parses a Move workspace into a set of IDLs.
#[derive(clap::Parser)]
#[clap(name = "move-tsgen", author, version)]
pub struct MoveTSGenTool {
    /// Path to the root of the Move workspace.
    #[clap(default_value = ".")]
    pub root: PathBuf,
    /// Output directory for the generated files.
    #[clap(short, long, default_value = "./build/ts/")]
    pub out_dir: PathBuf,

    /// Whether to generate module TypeScript files for dependencies.
    #[clap(short, long)]
    pub with_dependencies: bool,
}

#[async_trait::async_trait]
impl CliTool<()> for MoveTSGenTool {
    async fn execute(self) -> CliTypedResult<()> {
        let mut additional_named_addresses = BTreeMap::new();
        additional_named_addresses
            .insert("Std".to_string(), static_address::static_address!("0x1"));
        let build_config_std = BuildConfig {
            generate_docs: true,
            generate_abis: true,
            additional_named_addresses,
            ..Default::default()
        };
        let idl = IDLBuilder::load_with_config(&self.root, build_config_std)?.gen()?;

        std::fs::create_dir_all(&self.out_dir)?;

        let relevant_modules = if self.with_dependencies {
            let mut idl_mut = idl.clone();
            let mut modules = idl_mut.modules;
            modules.append(&mut idl_mut.dependencies);
            modules
        } else {
            idl.clone().modules
        };

        let ctx = CodegenContext::new(&idl);
        for (name, module_idl) in relevant_modules.iter() {
            let gen = ctx.get_module_generator(module_idl);

            let module_dir = &self.out_dir.join(name.name());
            std::fs::create_dir_all(module_dir)?;

            if gen.has_entrypoints() {
                std::fs::write(
                    module_dir.join("entry").with_extension("ts"),
                    gen.generate_entrypoint_module(&ctx)?.to_string(),
                )?;
            }

            std::fs::write(
                module_dir.join("idl").with_extension("ts"),
                gen.generate_idl_module()?.to_string(),
            )?;

            if let Some(errors_module) = gen.generate_errors_module()? {
                std::fs::write(
                    module_dir.join("errors").with_extension("ts"),
                    errors_module.to_string(),
                )?;
            }

            let ts = ctx.generate(module_idl)?;
            std::fs::write(
                module_dir.join("index").with_extension("ts"),
                ts.to_string(),
            )?;
        }

        std::fs::write(
            self.out_dir.join("index").with_extension("ts"),
            generate_index(
                &relevant_modules
                    .into_iter()
                    .map(|(name, _)| name.name().to_string())
                    .collect::<Vec<_>>(),
            )?
            .to_string(),
        )?;

        Ok(())
    }
}