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        )?;
48
49        create_dir_all(p.join(SourcePackageLayout::Tests.path()))?;
50        let mut w = std::fs::File::create(
51            p.join(SourcePackageLayout::Tests.path())
52                .join(format!("{name}_tests.move")),
53        )?;
54        writeln!(
55            w,
56            r#"/*
57#[test_only]
58module {name}::{name}_tests;
59// uncomment this line to import the module
60// use {name}::{name};
61
62const ENotImplemented: u64 = 0;
63
64#[test]
65fun test_{name}() {{
66    // pass
67}}
68
69#[test, expected_failure(abort_code = ::{name}::{name}_tests::ENotImplemented)]
70fun test_{name}_fail() {{
71    abort ENotImplemented
72}}
73*/"#
74        )?;
75
76        Ok(())
77    }
78}