Skip to content

Commit

Permalink
Fix forc init --template counter (FuelLabs#1245)
Browse files Browse the repository at this point in the history
* only edit Cargo.toml on template init if it already exists

* init with default test harness
  • Loading branch information
JoshuaBatty authored Apr 14, 2022
1 parent 6f3439a commit 8333abe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
38 changes: 35 additions & 3 deletions forc/src/ops/forc_init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::InitCommand;
use crate::utils::defaults;
use anyhow::{anyhow, Context, Result};
use forc_util::validate_name;
use forc_util::{println_green, validate_name};
use serde::Deserialize;
use std::fs;
use std::fs::File;
Expand Down Expand Up @@ -113,6 +113,8 @@ pub(crate) fn init_new_project(project_name: String) -> Result<()> {
defaults::default_gitignore(),
)?;

println_green(&format!("Successfully created: {}", project_name));

Ok(())
}

Expand Down Expand Up @@ -149,8 +151,29 @@ pub(crate) fn init_from_git_template(project_name: String, example_url: &Url) ->

// Change the project name and authors of the Forc.toml file
edit_forc_toml(&out_dir, &project_name, &real_name)?;
// Change the project name and authors of the Cargo.toml file
edit_cargo_toml(&out_dir, &project_name, &real_name)?;

// If the example has a tests folder, edit the Cargo.toml
// Otherwise, create a basic tests template for the project
if out_dir.join("tests").exists() {
// Change the project name and authors of the Cargo.toml file
edit_cargo_toml(&out_dir, &project_name, &real_name)?;
} else {
// Create the tests directory, harness.rs and Cargo.toml file
fs::create_dir_all(out_dir.join("tests"))?;

fs::write(
out_dir.join("tests").join("harness.rs"),
defaults::default_test_program(&project_name),
)?;

fs::write(
out_dir.join("Cargo.toml"),
defaults::default_tests_manifest(&project_name),
)?;
}

println_green(&format!("Successfully created: {}", project_name));
println_green("Now try and run 'forc test'");

Ok(())
}
Expand Down Expand Up @@ -211,6 +234,14 @@ fn edit_forc_toml(out_dir: &Path, project_name: &str, real_name: &str) -> Result
manifest_toml["project"]["authors"] = toml_edit::value(authors);
manifest_toml["project"]["name"] = toml_edit::value(project_name);

// Remove explicit std entry from copied template
if let Some(project) = manifest_toml.get_mut("dependencies") {
let _ = project
.as_table_mut()
.context("Unable to get forc manifest as table")?
.remove("std");
}

let mut file = File::create(out_dir.join(constants::MANIFEST_FILE_NAME))?;
file.write_all(manifest_toml.to_string().as_bytes())?;
Ok(())
Expand Down Expand Up @@ -272,6 +303,7 @@ fn download_contents(url: &str, out_dir: &Path, responses: &[ContentResponse]) -
}
FileType::Dir => {
match &response.name.as_str() {
// Test directory no longer exists, make sure to create this from scratch!!
// Only download the directory and its contents if it matches src or tests
&constants::SRC_DIR | &constants::TEST_DIRECTORY => {
let dir = out_dir.join(&response.name);
Expand Down
8 changes: 4 additions & 4 deletions forc/src/utils/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ version = "0.1.0"
[dependencies]
fuel-gql-client = {{ version = "0.5", default-features = false }}
fuel-tx = "0.7"
fuels-abigen-macro = "0.8"
fuels-contract = "0.8"
fuels-core = "0.8"
fuels-signers = "0.8"
fuels-abigen-macro = "0.9"
fuels-contract = "0.9"
fuels-core = "0.9"
fuels-signers = "0.9"
rand = "0.8"
tokio = {{ version = "1.12", features = ["rt", "macros"] }}
Expand Down

0 comments on commit 8333abe

Please sign in to comment.