Skip to content

Commit

Permalink
Add build to .gitignore in sui move new (MystenLabs#20395)
Browse files Browse the repository at this point in the history
## Description 

- Adds `build/*` to `.gitignore` when invoking `sui move new` (extending
it if it already exists)
 - Splits out `move new` implementation into smaller functions

## Test plan 

I added to the `move new` unit tests; checking the case where the
`.gitignore` already exists, and extended the existing tests to check
that it was added.

Note that I didn't add tests to `sui move new`, only `move new`.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [x] CLI: When issuing `sui move new` command, a `.gitignore` file will
be added to the project's folder.
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
mdgeorge4153 authored Nov 26, 2024
1 parent 09d8ed3 commit edf7a29
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
61 changes: 46 additions & 15 deletions external-crates/move/crates/move-cli/src/base/new.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::anyhow;
use anyhow::{self, ensure, Context};
use clap::*;
use move_core_types::identifier::Identifier;
use move_package::source_package::layout::SourcePackageLayout;
use std::io::{BufRead, BufReader};
use std::{fmt::Display, fs::create_dir_all, io::Write, path::Path};

// TODO get a stable path to this stdlib
// pub const MOVE_STDLIB_PACKAGE_NAME: &str = "MoveStdlib";
// pub const MOVE_STDLIB_PACKAGE_PATH: &str = "{ \
// git = \"https://github.com/move-language/move.git\", \
// subdir = \"language/move-stdlib\", rev = \"main\" \
// }";
pub const MOVE_STDLIB_ADDR_NAME: &str = "std";
pub const MOVE_STDLIB_ADDR_VALUE: &str = "0x1";

Expand Down Expand Up @@ -43,17 +38,53 @@ impl New {
custom: &str, // anything else that needs to end up being in Move.toml (or empty string)
) -> anyhow::Result<()> {
// TODO warn on build config flags
let Self { name } = self;

if !Identifier::is_valid(&name) {
return Err(anyhow!(
"Invalid package name. Package name must start with a lowercase letter \
and consist only of lowercase letters, numbers, and underscores."
));
}
ensure!(
Identifier::is_valid(&self.name),
"Invalid package name. Package name must start with a lowercase letter \
and consist only of lowercase letters, numbers, and underscores."
);

let path = path.unwrap_or_else(|| Path::new(&name));
let path = path.unwrap_or_else(|| Path::new(&self.name));
create_dir_all(path.join(SourcePackageLayout::Sources.path()))?;

self.write_move_toml(path, deps, addrs, custom)?;
self.write_gitignore(path)?;
Ok(())
}

/// add `build/*` to `{path}/.gitignore` if it doesn't already have it
fn write_gitignore(&self, path: &Path) -> anyhow::Result<()> {
let gitignore_entry = "build/*";

let mut file = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(path.join(".gitignore"))
.context("Unexpected error creating .gitignore")?;

for line in BufReader::new(&file).lines().map_while(Result::ok) {
if line == gitignore_entry {
return Ok(());
}
}

writeln!(file, "{gitignore_entry}")?;
Ok(())
}

/// create default `Move.toml`
fn write_move_toml(
&self,
path: &Path,
deps: impl IntoIterator<Item = (impl Display, impl Display)>,
addrs: impl IntoIterator<Item = (impl Display, impl Display)>,
custom: &str, // anything else that needs to end up being in Move.toml (or empty string)
) -> anyhow::Result<()> {
let Self { name } = self;

let mut w = std::fs::File::create(path.join(SourcePackageLayout::Manifest.path()))?;
writeln!(
w,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Command `new -p package example`:
External Command `cat package/.gitignore`:
existing_ignore
build/*
External Command `ls -A package`:
.gitignore
Move.toml
sources
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# if `.gitignore` exists, everything should be created and .gitignore should be extended
new -p package example
>cat package/.gitignore
>ls -A package
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
existing_ignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# and `--dev` modes.
# alice = "0xB0B"

External Command `cat P1/.gitignore`:
build/*
Command `new P2 -p other_dir`:
External Command `cat other_dir/Move.toml`:
[package]
Expand Down Expand Up @@ -72,3 +74,5 @@ edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# and `--dev` modes.
# alice = "0xB0B"

External Command `cat other_dir/.gitignore`:
build/*
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
new P1
> cat P1/Move.toml
> cat P1/.gitignore

new P2 -p other_dir
> cat other_dir/Move.toml
> cat other_dir/.gitignore

0 comments on commit edf7a29

Please sign in to comment.