iota_move/
new.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::{fs::create_dir_all, io::Write, path::Path};
6
7use clap::Parser;
8use move_cli::base::new;
9use move_package::source_package::layout::SourcePackageLayout;
10
11const IOTA_PKG_NAME: &str = "Iota";
12
13// Use testnet by default. Probably want to add options to make this
14// configurable later
15const IOTA_PKG_PATH: &str = "{ git = \"https://github.com/iotaledger/iota.git\", subdir = \"crates/iota-framework/packages/iota-framework\", rev = \"testnet\", override = true }";
16
17#[derive(Parser)]
18#[group(id = "iota-move-new")]
19pub struct New {
20    #[command(flatten)]
21    pub new: new::New,
22}
23
24impl New {
25    pub fn execute(self, path: Option<&Path>) -> anyhow::Result<()> {
26        let name = &self.new.name.to_lowercase();
27        let provided_name = &self.new.name.to_string();
28
29        self.new
30            .execute(path, [(IOTA_PKG_NAME, IOTA_PKG_PATH)], [(name, "0x0")], "")?;
31        let p = path.unwrap_or_else(|| Path::new(&provided_name));
32        let mut w = std::fs::File::create(
33            p.join(SourcePackageLayout::Sources.path())
34                .join(format!("{name}.move")),
35        )?;
36        writeln!(
37            w,
38            r#"/*
39/// Module: {name}
40module {name}::{name};
41*/
42
43// For Move coding conventions, see
44// https://docs.iota.org/developer/iota-101/move-overview/conventions
45
46"#,
47            name = name
48        )?;
49
50        create_dir_all(p.join(SourcePackageLayout::Tests.path()))?;
51        let mut w = std::fs::File::create(
52            p.join(SourcePackageLayout::Tests.path())
53                .join(format!("{name}_tests.move")),
54        )?;
55        writeln!(
56            w,
57            r#"/*
58#[test_only]
59module {name}::{name}_tests;
60// uncomment this line to import the module
61// use {name}::{name};
62
63const ENotImplemented: u64 = 0;
64
65#[test]
66fun test_{name}() {{
67    // pass
68}}
69
70#[test, expected_failure(abort_code = ::{name}::{name}_tests::ENotImplemented)]
71fun test_{name}_fail() {{
72    abort ENotImplemented
73}}
74*/"#,
75            name = name
76        )?;
77
78        Ok(())
79    }
80}