Skip to content

Commit

Permalink
chore: bump weekly (#286)
Browse files Browse the repository at this point in the history
* fix: invalid entry type fields in vue template (#279)

* fix: prevent unaffected rust files from getting formatted (#281)

* fix: preserve doc comments

* fix: unmodified files getting reformatted by unparse

* refactor map_all_files_rec function

* revert unparse function change

* fix: prevent scaffolding dead code (#280)

* fix: prevent scaffolding dead code

* fix clippy warnings

* improve readability of stringified rust code

* update coordinator functions

* refactor boolean checks

* feat: Add headless template (#267)

* feat: add initial headless template

* include headless template in ui framework choices

* add web-app instructions and prefer explicit hc commands in all package.json templates

* rename instructions template

* fix indentation in instructions

* update instructions

* remove gitkeep

* refactor UiFramework struct

* refactor tempalte config access

* remove assertion

* revert renaming

* revert integrity zome name

* choose non vanilla framework for non hello world examples

* simplify ui framework prompts; update instructions

* update instructions

* Update scripts and instructions

* color code ui frameworks

* templatify web-happ manifest

* fix bug writing invalid template config

* remove unnecessary print statement

* remove unnecessary return statement

* update ui framework try from filetree implementation

* refactor: refactor reserved words check

* update ansi colors for lit and svelte

* Add build happ instruction

* chore: update web-app instructions for headless template

* fix: fix vue example ui server not starting (#284)

* simplify templates_dir

* chore: bump weekly
  • Loading branch information
c12i committed May 16, 2024
1 parent 6606967 commit 941dc28
Show file tree
Hide file tree
Showing 33 changed files with 1,576 additions and 796 deletions.
150 changes: 75 additions & 75 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "holochain_scaffolding_cli"
version = "0.4000.0-dev.0"
version = "0.4000.0-dev.1"
description = "CLI to easily generate and modify holochain apps"
license = "CAL-1.0"
homepage = "https://developer.holochain.org"
Expand All @@ -18,8 +18,8 @@ name = "holochain_scaffolding_cli"
path = "src/lib.rs"

[dependencies]
holochain = { features = ["test_utils"], version = "0.4.0-dev.1"}
holochain_types = "0.4.0-dev.1"
holochain = { features = ["test_utils"], version = "0.4.0-dev.3"}
holochain_types = "0.4.0-dev.3"
holochain_util = { features = ["backtrace"], version = "0.4.0-dev.1" }
mr_bundle = "0.4.0-dev.1"

Expand Down
44 changes: 22 additions & 22 deletions flake.lock

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

65 changes: 28 additions & 37 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use crate::scaffold::collection::{scaffold_collection, CollectionType};
use crate::scaffold::dna::{scaffold_dna, DnaFileTree};
use crate::scaffold::entry_type::crud::{parse_crud, Crud};
use crate::scaffold::entry_type::definitions::{
parse_entry_type_reference, parse_referenceable, Cardinality, EntryTypeReference,
FieldDefinition, FieldType, Referenceable,
Cardinality, EntryTypeReference, FieldDefinition, FieldType, Referenceable,
};
use crate::scaffold::entry_type::{fields::parse_fields, scaffold_entry_type};
use crate::scaffold::example::{choose_example, Example};
Expand Down Expand Up @@ -46,7 +45,7 @@ use structopt::StructOpt;
pub struct HcScaffold {
#[structopt(short, long)]
/// The template to use for the scaffold command
/// Can either be an option from the built-in templates: "vanilla", "vue", "lit", "svelte"
/// Can either be an option from the built-in templates: "vanilla", "vue", "lit", "svelte", "headless"
/// Or a path to a custom template
template: Option<String>,

Expand Down Expand Up @@ -160,11 +159,11 @@ pub enum HcScaffoldCommand {
/// Name of the integrity zome in which you want to scaffold the link type
zome: Option<String>,

#[structopt(parse(try_from_str = parse_referenceable))]
#[structopt(parse(try_from_str = Referenceable::from_str))]
/// Entry type (or agent role) used as the base for the links
from_referenceable: Option<Referenceable>,

#[structopt(parse(try_from_str = parse_referenceable))]
#[structopt(parse(try_from_str = Referenceable::from_str))]
/// Entry type (or agent role) used as the target for the links
to_referenceable: Option<Referenceable>,

Expand Down Expand Up @@ -196,15 +195,14 @@ pub enum HcScaffoldCommand {
/// Collection name, just to differentiate it from other collections
collection_name: Option<String>,

#[structopt(parse(try_from_str = parse_entry_type_reference))]
#[structopt(parse(try_from_str = EntryTypeReference::from_str))]
/// Entry type that is going to be added to the collection
entry_type: Option<EntryTypeReference>,

#[structopt(long)]
/// Skips UI generation for this collection.
no_ui: bool,
},

Example {
/// Name of the example to scaffold. One of ['hello-world', 'forum'].
example: Option<Example>,
Expand All @@ -217,44 +215,37 @@ pub enum HcScaffoldCommand {
impl HcScaffold {
pub async fn run(self) -> anyhow::Result<()> {
let current_dir = std::env::current_dir()?;
let template_config = if let Some(t) = &self.template {
// Only read from config if the template is inbuilt and not a path
if Path::new(t).exists() {
None
} else {
get_template_config(&current_dir)?
}
} else {
None
};
let template_config = get_template_config(&current_dir)?;
let template = match (&template_config, &self.template) {
(Some(config), Some(template)) if &config.template != template => {
return Err(ScaffoldError::InvalidArguments(format!(
"The value {template} passed with `--template` does not match the template the web-app was scaffolded with: {}",
config.template
"The value {} passed with `--template` does not match the template the web-app was scaffolded with: {}",
template.italic(),
config.template.italic(),
)).into())
}
(Some(config), _) => Some(&config.template),
// Only read from config if the template is inbuilt and not a path
(Some(config), _) if !Path::new(&config.template).exists() => Some(&config.template),
(_, t) => t.as_ref(),
};

// Given a template either passed via the --template flag or retreived via the hcScaffold config,
// get the template file tree and the ui framework name or custom template path
let (template, template_file_tree) = match template {
Some(template) => {
let template_name_or_path;
let file_tree = match template.as_str() {
"lit" | "svelte" | "vanilla" | "vue" => {
let ui_framework = UiFramework::from_str(template)?;
template_name_or_path = ui_framework.to_string();
ui_framework.template_filetree()?
}
custom_template_path => {
template_name_or_path = custom_template_path.to_string();
let templates_dir = current_dir.join(PathBuf::from(custom_template_path));
load_directory_into_memory(&templates_dir)?
}
};
(template_name_or_path.to_owned(), file_tree)
}
Some(template) => match template.to_lowercase().as_str() {
"lit" | "svelte" | "vanilla" | "vue" | "headless" => {
let ui_framework = UiFramework::from_str(template)?;
(ui_framework.name(), ui_framework.template_filetree()?)
}
custom_template_path if Path::new(custom_template_path).exists() => {
let templates_dir = current_dir.join(custom_template_path);
(
custom_template_path.to_string(),
load_directory_into_memory(&templates_dir)?,
)
}
path => return Err(ScaffoldError::PathNotFound(PathBuf::from(path)).into()),
},
None => {
let ui_framework = match self.command {
HcScaffoldCommand::WebApp { .. } => UiFramework::choose()?,
Expand All @@ -267,7 +258,7 @@ impl HcScaffold {
UiFramework::try_from(&file_tree)?
}
};
(ui_framework.to_string(), ui_framework.template_filetree()?)
(ui_framework.name(), ui_framework.template_filetree()?)
}
};

Expand Down
44 changes: 24 additions & 20 deletions src/file_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use build_fs_tree::{dir, file, Build, FileSystemTree, MergeableFileSystemTree};
use ignore::WalkBuilder;
use include_dir::Dir;
Expand Down Expand Up @@ -183,23 +184,24 @@ fn find_map_files_rec<T, F: Fn(&PathBuf, &String) -> Option<T>>(
found_files
}

pub fn map_rust_files<F: Fn(PathBuf, syn::File) -> ScaffoldResult<syn::File> + Clone>(
pub fn map_rust_files<F: Fn(PathBuf, syn::File) -> ScaffoldResult<syn::File> + Copy>(
file_tree: &mut FileTree,
map_fn: F,
) -> ScaffoldResult<()> {
map_all_files(file_tree, |file_path, s| {
map_all_files(file_tree, |file_path, contents| {
if let Some(extension) = file_path.extension() {
if extension == "rs" {
let rust_file: syn::File = syn::parse_str(s.as_str()).map_err(|e| {
ScaffoldError::MalformedFile(file_path.clone(), format!("{}", e))
})?;
let new_file = map_fn(file_path, rust_file)?;

return Ok(unparse(&new_file));
let original_file: syn::File = syn::parse_str(&contents)
.map_err(|e| ScaffoldError::MalformedFile(file_path.clone(), e.to_string()))?;
let new_file = map_fn(file_path, original_file.clone())?;
// Only reformat the file via unparse if the contents of the newly modified
// file are different from the original
if new_file != original_file {
return Ok(unparse(&new_file));
}
}
}

Ok(s)
Ok(contents)
})
}

Expand Down Expand Up @@ -238,32 +240,34 @@ pub fn unflatten_file_tree(
Ok(file_tree)
}

pub fn map_all_files<F: Fn(PathBuf, String) -> ScaffoldResult<String> + Clone>(
pub fn map_all_files<F: Fn(PathBuf, String) -> ScaffoldResult<String> + Copy>(
file_tree: &mut FileTree,
map_fn: F,
) -> ScaffoldResult<()> {
map_all_files_rec(file_tree, PathBuf::new(), map_fn)?;
Ok(())
}

fn map_all_files_rec<F: Fn(PathBuf, String) -> ScaffoldResult<String> + Clone>(
fn map_all_files_rec<F: Fn(PathBuf, String) -> ScaffoldResult<String> + Copy>(
file_tree: &mut FileTree,
current_path: PathBuf,
map_fn: F,
) -> ScaffoldResult<()> {
if let Some(c) = file_tree.dir_content_mut() {
for (key, mut tree) in c.clone().into_iter() {
if let Some(dir) = file_tree.dir_content_mut() {
for (key, mut tree) in dir.clone().into_iter() {
let child_path = current_path.join(&key);
match tree.clone() {
FileTree::Directory(_dir_contents) => {
map_all_files_rec(&mut tree, child_path, map_fn.clone())?;
match &tree {
FileTree::Directory(_) => {
map_all_files_rec(&mut tree, child_path, map_fn)?;
}
FileTree::File(file_contents) => {
*tree.file_content_mut().unwrap() = map_fn(child_path, file_contents)?;
*tree
.file_content_mut()
.context("Failed to get mutable reference of file tree")? =
map_fn(child_path, file_contents.to_owned())?;
}
}

c.insert(key.clone(), tree.clone());
dir.insert(key, tree);
}
}

Expand Down
19 changes: 8 additions & 11 deletions src/reserved_words.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use convert_case::{Case, Casing};

use crate::error::{ScaffoldError, ScaffoldResult};

const RESERVED_WORDS: [&str; 27] = [
Expand Down Expand Up @@ -32,16 +30,15 @@ const RESERVED_WORDS: [&str; 27] = [
"Call",
];

// Returns an error if the given string is invalid due to it being a reserved word
/// Returns an error if the given string is invalid due to it being a reserved word
pub fn check_for_reserved_words(string_to_check: &str) -> ScaffoldResult<()> {
for w in RESERVED_WORDS {
if string_to_check
.to_case(Case::Lower)
.eq(&w.to_string().to_case(Case::Lower))
{
return Err(ScaffoldError::InvalidReservedWord(w.to_string()));
}
if RESERVED_WORDS
.iter()
.any(|w| string_to_check.eq_ignore_ascii_case(w))
{
return Err(ScaffoldError::InvalidReservedWord(
string_to_check.to_string(),
));
}

Ok(())
}

0 comments on commit 941dc28

Please sign in to comment.