Skip to content

Commit

Permalink
WIP - embed the init template files in the build
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Feb 8, 2024
1 parent a1e51d2 commit 3f14653
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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
28 changes: 27 additions & 1 deletion 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 @@ -127,6 +129,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 +144,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,6 +181,24 @@ fn init(
Ok(())
}

fn copy_template_files(project_path: &Path) -> Result<(), Error> {
// first create all of the nested directories
for file in TemplateFiles::iter() {
let to = project_path.join(file.as_ref());

fs::create_dir_all(&to.parent().unwrap())?;
}

// then copy the files
for file in TemplateFiles::iter() {
let to = project_path.join(file.as_ref());
let file_contents = TemplateFiles::get(file.as_ref()).unwrap().data;
let file_contents_as_utf8 = std::str::from_utf8(&file_contents).unwrap();
std::fs::write(to, file_contents_as_utf8);
}
Ok(())
}

fn copy_contents(from: &Path, to: &Path) -> Result<(), Error> {
let contents_to_exclude_from_copy = [
".git",
Expand Down

0 comments on commit 3f14653

Please sign in to comment.