sui_move/
new.rs

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