Skip to content

Commit

Permalink
forc template implementation (FuelLabs#1614)
Browse files Browse the repository at this point in the history
* forc template resolves the HEAD and fetches the repo. Prints the requested directorie's path

* forc template works with and without template name

* forc init --template removed, pkg.rs unused pub modifier removed

* extra info added before copy, cargo-toml-lint, udeps fix, mdbook fix

* invoke CI

* Update scripts/mdbook-forc-documenter/examples/forc_template.md

Co-authored-by: John Adler <[email protected]>

* Update scripts/mdbook-forc-documenter/examples/forc_template.md

Co-authored-by: John Adler <[email protected]>

* default url provided, project_name is last parameter to pass without --project_name

* cargo fmt

* println to tracing::info

* local_repo_name to template_name

Co-authored-by: John Adler <[email protected]>
  • Loading branch information
kayagokalp and adlerjohn authored May 23, 2022
1 parent bebf631 commit 53361ee
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 398 deletions.
70 changes: 7 additions & 63 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- [forc run](./forc/commands/forc_run.md)
- [forc test](./forc/commands/forc_test.md)
- [forc update](./forc/commands/forc_update.md)
- [forc template](./forc/commands/forc_template.md)
- [Plugins](./forc/plugins.md)
- [forc explore](./forc_explore.md)
- [forc fmt](./forc_fmt.md)
Expand Down
1 change: 1 addition & 0 deletions docs/src/forc/commands/forc_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc template
8 changes: 4 additions & 4 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ pub(crate) fn fetch_deps(
/// Produce a unique ID for a particular fetch pass.
///
/// This is used in the temporary git directory and allows for avoiding contention over the git repo directory.
fn fetch_id(path: &Path, timestamp: std::time::Instant) -> u64 {
pub fn fetch_id(path: &Path, timestamp: std::time::Instant) -> u64 {
let mut hasher = hash_map::DefaultHasher::new();
path.hash(&mut hasher);
timestamp.hash(&mut hasher);
Expand Down Expand Up @@ -781,7 +781,7 @@ where
///
/// This clones the repository to a temporary directory in order to determine the commit at the
/// HEAD of the given git reference.
fn pin_git(fetch_id: u64, name: &str, source: SourceGit) -> Result<SourceGitPinned> {
pub fn pin_git(fetch_id: u64, name: &str, source: SourceGit) -> Result<SourceGitPinned> {
let commit_hash = with_tmp_git_repo(fetch_id, name, &source, |repo| {
// Resolve the reference to the commit ID.
let commit_id = source
Expand Down Expand Up @@ -856,7 +856,7 @@ fn pin_pkg(fetch_id: u64, pkg: &Pkg, path_map: &mut PathMap, sway_git_tag: &str)
/// ```
///
/// where `<repo_url_hash>` is a hash of the source repository URL.
fn git_commit_path(name: &str, repo: &Url, commit_hash: &str) -> PathBuf {
pub fn git_commit_path(name: &str, repo: &Url, commit_hash: &str) -> PathBuf {
let repo_dir_name = git_repo_dir_name(name, repo);
git_checkouts_directory()
.join(repo_dir_name)
Expand All @@ -866,7 +866,7 @@ fn git_commit_path(name: &str, repo: &Url, commit_hash: &str) -> PathBuf {
/// Fetch the repo at the given git package's URL and checkout the pinned commit.
///
/// Returns the location of the checked out commit.
fn fetch_git(fetch_id: u64, name: &str, pinned: &SourceGitPinned) -> Result<PathBuf> {
pub fn fetch_git(fetch_id: u64, name: &str, pinned: &SourceGitPinned) -> Result<PathBuf> {
let path = git_commit_path(name, &pinned.source.repo, &pinned.commit_hash);

// Checkout the pinned hash to the path.
Expand Down
2 changes: 1 addition & 1 deletion forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ clap = { version = "3.1", features = ["cargo", "derive", "env"] }
clap_complete = "3.1"
forc-pkg = { version = "0.13.0", path = "../forc-pkg" }
forc-util = { version = "0.13.0", path = "../forc-util" }
fs_extra = "1.2"
fuel-asm = "0.4"
fuel-gql-client = { version = "0.6", default-features = false }
fuel-tx = "0.9"
Expand All @@ -39,7 +40,6 @@ tokio = { version = "1.8.0", features = ["macros", "rt-multi-thread", "process"]
toml = "0.5"
toml_edit = "0.13"
tracing = "0.1"
ureq = { version = "2.4", features = ["json"] }
url = "2.2"
uwuify = { version = "^0.2", optional = true }
walkdir = "2.3"
Expand Down
8 changes: 0 additions & 8 deletions forc/src/cli/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@ use crate::ops::forc_init;
use anyhow::Result;
use clap::Parser;

const TEMPLATE_HELP: &str = r#"Initialize a new project from a template.
Example Templates:
- counter"#;

/// Create a new Forc project.
#[derive(Debug, Parser)]
pub struct Command {
/// Initialize a new project from a template
#[clap(short, long, help = TEMPLATE_HELP)]
pub template: Option<String>,
/// The default program type, excluding all flags or adding this flag creates a basic contract program.
#[clap(long)]
pub contract: bool,
Expand Down
1 change: 1 addition & 0 deletions forc/src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod json_abi;
pub mod parse_bytecode;
pub mod plugins;
pub mod run;
pub mod template;
pub mod test;
pub mod update;
23 changes: 23 additions & 0 deletions forc/src/cli/commands/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::ops::forc_template;
use anyhow::Result;
use clap::Parser;

/// Create a new Forc project from a git template.
#[derive(Debug, Parser)]
pub struct Command {
/// The template url, should be a git repo.
#[clap(long, short, default_value = "https://github.com/fuellabs/sway")]
pub url: String,

/// The name of the template that needs to be fetched and used from git repo provided.
#[clap(long, short)]
pub template_name: Option<String>,

/// The name of the project that will be created
pub project_name: String,
}

pub(crate) fn exec(command: Command) -> Result<()> {
forc_template::init(command)?;
Ok(())
}
5 changes: 4 additions & 1 deletion forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use self::commands::{
addr2line, build, clean, completions, deploy, init, json_abi, parse_bytecode, plugins, run,
test, update,
template, test, update,
};
use addr2line::Command as Addr2LineCommand;
use anyhow::{anyhow, Result};
Expand All @@ -14,6 +14,7 @@ pub use json_abi::Command as JsonAbiCommand;
use parse_bytecode::Command as ParseBytecodeCommand;
pub use plugins::Command as PluginsCommand;
pub use run::Command as RunCommand;
pub use template::Command as TemplateCommand;
use test::Command as TestCommand;
pub use update::Command as UpdateCommand;

Expand Down Expand Up @@ -45,6 +46,7 @@ enum Forc {
Update(UpdateCommand),
JsonAbi(JsonAbiCommand),
Plugins(PluginsCommand),
Template(TemplateCommand),
/// This is a catch-all for unknown subcommands and their arguments.
///
/// When we receive an unknown subcommand, we check for a plugin exe named
Expand Down Expand Up @@ -72,6 +74,7 @@ pub async fn run_cli() -> Result<()> {
Forc::Test(command) => test::exec(command),
Forc::Update(command) => update::exec(command).await,
Forc::JsonAbi(command) => json_abi::exec(command),
Forc::Template(command) => template::exec(command),
Forc::Plugin(args) => {
let output = plugin::execute_external_subcommand(args)?;
let code = output
Expand Down
Loading

0 comments on commit 53361ee

Please sign in to comment.