Move开发环境搭建

Move开发环境搭建

Move开发环境构建常规有两种方式,第一种是通过源码的方式构建,第二种是通过 docker 的方式构建。本文主要介绍 第一种方式

源码构建

操作系统要求

建议采用如下的操作系统:

  • MacOS

  • Ubuntu

涉及工具

  • cargo

  • Move-cli

  • Move-analyzer

  • VSCode

  • VSCode Move插件

安装Cargos

curl https://sh.rustup.rs -sSf | sh

Tip: 等待安装完成即可

安装Move-cli

这里的工作目录是 ~/workspace/github/

  • 拉取move最新代码

~#  git clone https://github.com/move-language/move.git
  • 安装Move-cli

~# cargo install --path move/language/tools/move-cli
  • 验证安装结果

~# move -h
move-cli 0.1.0
Diem Association <opensource@diem.com>
MoveCLI is the CLI that will be executed by the `move-cli` command The `cmd` argument is added here
rather than in `Move` to make it easier for other crates to extend `move-cli`

USAGE:
    move [OPTIONS] <SUBCOMMAND>

OPTIONS:
        --abi                          Generate ABIs for packages
        --arch <ARCHITECTURE>
    -d, --dev                          Compile in 'dev' mode. The 'dev-addresses' and
                                       'dev-dependencies' fields will be used if this flag is set.
                                       This flag is useful for development of packages that expose
                                       named addresses that are not set to a specific value
        --doc                          Generate documentation for packages
        --fetch-deps-only              Only fetch dependency repos to MOVE_HOME
        --force                        Force recompilation of all packages
    -h, --help                         Print help information
        --install-dir <INSTALL_DIR>    Installation directory for compiled artifacts. Defaults to
                                       current directory
    -p, --path <PACKAGE_PATH>          Path to a package which the command should be run with
                                       respect to
        --test                         Compile in 'test' mode. The 'dev-addresses' and
                                       'dev-dependencies' fields will be used along with any code in
                                       the 'tests' directory
    -v                                 Print additional diagnostics if available
    -V, --version                      Print version information

SUBCOMMANDS:
    build           Build the package at `path`. If no path is provided defaults to current
                        directory
    coverage        Inspect test coverage for this package. A previous test run with the
                        `--coverage` flag must have previously been run
    disassemble     Disassemble the Move bytecode pointed to
    docgen          Generating javadoc style documentation for packages using the Move Prover
    errmap          Generate error map for the package and its dependencies at `path` for use by
                        the Move explanation tool
    experimental    (Experimental) Run static analyses on Move source or bytecode
    help            Print this message or the help of the given subcommand(s)
    info            Print address information
    movey-login
    new             Create a new Move package with name `name` at `path`. If `path` is not
                        provided the package will be created in the directory `name`
    prove           Run the Move Prover on the package at `path`. If no path is provided
                        defaults to current directory. Use `.. prove .. -- <options>` to pass on
                        options to the prover
    sandbox         Execute a sandbox command
    test            Run Move unit tests in this package

Move-analyzer安装

moe-analyzer 主要提供语法检查、高亮等功能,vscode的插件也需要用到该命令,否则会报错。

~# cargo install --path move/language/tools/move-analyzer

VScode 插件安装

目前用到的插件有2个,一个是 move-analyzermove syntax

!image-20220812155330720

!image-20220812155406299

代码示例

  • 代码目录结构如下:

├── Move.toml                 # move 配置文件,存放项目信息、依赖、命名地址等内容
└── sources                   # 源码目录,用于存放模块、脚本等内容
    ├── modules               # 模块目录
    │   └── Coin.move        # 具体模块
    └── scripts               # 脚本目录
        └── test-coin.move    # 具体脚本

Move.toml 配置内容

[package]
name = "example1"
version = "0.0.0"

[addresses]
std="0x1"

[dependencies]
MoveNursery = { git = "https://github.com/move-language/move.git", subdir="language/move-stdlib/nursery", rev = "main"}
MoveStdlib = { git = "https://github.com/move-language/move.git", subdir="language/move-stdlib", rev = "main"}

Coin.move 代码内容

address 0x2 {
    module Coin {

        struct Coin {
            value: u64,
        }

        public fun mint(value: u64): Coin {
            Coin { value }
        }
        public fun value(coin: &Coin): u64 {
            coin.value
        }

        public fun burn(coin: Coin): u64 {
            let Coin { value } = coin;
            value
        }

    }
}

test-coin.move 代码内容

script {
    use std::debug;
    use 0x2::Coin;

    fun main() {
        let coin = Coin::mint(1000000);

        debug::print(&Coin::value(&coin));

        Coin::burn(coin);
    }
}
  • 构建测试

# move sandbox publish
# move sandbox run sources/scripts/test-coin.move              
[debug] 1000000

关于作者

Mov社区成员:欢迎大家加入 Move 社区共建 Move社区,加入 Discord Move社区

Twitter: Follow Me