Skip to content

Commit

Permalink
Embed init template files in the build
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Feb 9, 2024
1 parent a1e51d2 commit 80fec59
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ ureq = {version = "2.9.1", features = ["json"]}

tempfile = "3.8.1"
toml_edit = "0.21.0"
rust-embed = "8.2.0"
# For hyper-tls
[target.'cfg(unix)'.dependencies]
openssl = { version = "0.10.55", features = ["vendored"] }
Expand Down
59 changes: 41 additions & 18 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::borrow::Cow;
use std::fs::read_to_string;
use std::path::Path;
use std::{env, fs, io};

use clap::builder::{PossibleValue, PossibleValuesParser, ValueParser};
use clap::{Parser, ValueEnum};
use rust_embed::RustEmbed;
use serde::Deserialize;
use std::num::NonZeroU32;
use std::sync::atomic::AtomicBool;
Expand Down Expand Up @@ -113,6 +115,9 @@ pub enum Error {

#[error("Failed to parse package.json file: {0}")]
JsonParseError(#[from] serde_json::Error),

#[error("Failed to convert file contents to string: {0}")]
ConvertFileContentsErr(#[from] std::str::Utf8Error),
}

impl Cmd {
Expand All @@ -127,6 +132,10 @@ impl Cmd {
}
}

#[derive(RustEmbed)]
#[folder = "src/utils/contract-init-template"]
struct TemplateFiles;

fn init(
project_path: &Path,
frontend_template: &String,
Expand All @@ -138,9 +147,11 @@ fn init(
.join("utils")
.join("contract-init-template");

println!("template_dir_path: {:?}", template_dir_path);

// create a project dir, and copy the contents of the base template (contract-init-template) into it
std::fs::create_dir_all(project_path)?;
copy_contents(template_dir_path.as_path(), project_path)?;
copy_template_files(project_path)?;

if !check_internet_connection() {
println!("⚠️ It doesn't look like you're connected to the internet. We're still able to initialize a new project, but additional examples and the frontend template will not be included.");
Expand Down Expand Up @@ -173,15 +184,35 @@ fn init(
Ok(())
}

fn copy_template_files(project_path: &Path) -> Result<(), Error> {
for item in TemplateFiles::iter() {
let to = project_path.join(item.as_ref());
if file_exists(&to.to_string_lossy()) {
println!(
"ℹ️ Skipped creating {} as it already exists",
&to.to_string_lossy()
);
continue;
}
fs::create_dir_all(&to.parent().unwrap())?;

let file = match TemplateFiles::get(item.as_ref()) {
Some(file) => file,
None => {
println!("⚠️ Failed to read file: {}", item.as_ref());
continue;
}
};

let file_contents = std::str::from_utf8(file.data.as_ref())?;

fs::write(to, file_contents)?;
}
Ok(())
}

fn copy_contents(from: &Path, to: &Path) -> Result<(), Error> {
let contents_to_exclude_from_copy = [
".git",
".github",
"Makefile",
"Cargo.lock",
".vscode",
"target",
];
let contents_to_exclude_from_copy = [".git", ".github", "Makefile", ".vscode", "target"];
for entry in fs::read_dir(from)? {
let entry = entry?;
let path = entry.path();
Expand Down Expand Up @@ -475,18 +506,10 @@ mod tests {
) {
let contract_dir = project_dir.join("contracts").join(contract_name);
assert!(!contract_dir.as_path().join("Makefile").exists());
assert!(!contract_dir.as_path().join("Cargo.lock").exists());
}

fn assert_base_excluded_paths_do_not_exist(project_dir: &Path) {
let excluded_paths = [
".git",
".github",
"Makefile",
"Cargo.lock",
".vscode",
"target",
];
let excluded_paths = [".git", ".github", "Makefile", ".vscode", "target"];
for path in &excluded_paths {
assert!(!project_dir.join(path).exists());
}
Expand Down

0 comments on commit 80fec59

Please sign in to comment.