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
11#[derive(Parser)]
12#[group(id = "iota-move-new")]
13pub struct New {
14    #[command(flatten)]
15    pub new: new::New,
16}
17
18impl New {
19    pub fn execute(self, path: Option<&Path>) -> anyhow::Result<()> {
20        let name = &self.new.name.to_lowercase();
21        let provided_name = &self.new.name.to_string();
22
23        self.new
24            .execute(path, [] as [(&str, &str); 0], [(name, "0x0")], "")?;
25        let p = path.unwrap_or_else(|| Path::new(&provided_name));
26        let mut w = std::fs::File::create(
27            p.join(SourcePackageLayout::Sources.path())
28                .join(format!("{name}.move")),
29        )?;
30        writeln!(
31            w,
32            r#"/*
33/// Module: {name}
34module {name}::{name};
35*/
36
37// For Move coding conventions, see
38// https://docs.iota.org/developer/iota-101/move-overview/conventions
39
40"#
41        )?;
42
43        create_dir_all(p.join(SourcePackageLayout::Tests.path()))?;
44        let mut w = std::fs::File::create(
45            p.join(SourcePackageLayout::Tests.path())
46                .join(format!("{name}_tests.move")),
47        )?;
48        writeln!(
49            w,
50            r#"/*
51#[test_only]
52module {name}::{name}_tests;
53// uncomment this line to import the module
54// use {name}::{name};
55
56const ENotImplemented: u64 = 0;
57
58#[test]
59fun test_{name}() {{
60    // pass
61}}
62
63#[test, expected_failure(abort_code = ::{name}::{name}_tests::ENotImplemented)]
64fun test_{name}_fail() {{
65    abort ENotImplemented
66}}
67*/"#
68        )?;
69
70        Ok(())
71    }
72}