From 38c8de284dfdbbfbb1d33c42dc590f0c9d20b041 Mon Sep 17 00:00:00 2001 From: ThetaSinner Date: Fri, 3 Nov 2023 17:23:53 +0000 Subject: [PATCH 1/8] Merge pull request #95 from holochain/fix-merge-scope Fix bug in merge scope --- src/lib.rs | 4 -- src/templates.rs | 81 +++++++++++++++++++++++++-------------- tests/generate_web_app.rs | 50 ------------------------ 3 files changed, 53 insertions(+), 82 deletions(-) delete mode 100644 tests/generate_web_app.rs diff --git a/src/lib.rs b/src/lib.rs index b511a1ec0..f340eae15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,6 @@ //! //! Templates have this directory structure: //! -//! ``` //! coordinator-zome/ //! dna/ //! entry-type/ @@ -92,7 +91,6 @@ //! integrity-zome/ //! link-type/ //! web-app/ -//! ``` //! //! Each folder corresponds to the templates that are going to be created when running a specific command. This is the steps that are executed: //! @@ -123,7 +121,6 @@ //! //! The `field-types` folder is special. It has the following directory structure: //! -//! ``` //! ActionHash/ //! type.hbs //! AgentPubKey/ @@ -168,7 +165,6 @@ //! imports.hbs //! render.hbs //! type.hbs -//! ``` //! //! As you can see, the top-level folders are the rust types that are possible to use as the field types for an entry. The `type.hbs` file in each of the folders contains the typescript type for that rust type, so that it can be rendered in the frontend. //! diff --git a/src/templates.rs b/src/templates.rs index 663002dfc..644097606 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -93,6 +93,37 @@ pub fn register_concat_helper<'a>(mut h: Handlebars<'a>) -> Handlebars<'a> { #[derive(Clone, Copy)] pub struct MergeScope; +fn get_scope_open_and_close_char_indexes( + text: &String, + scope_opener: &String, +) -> Result<(usize, usize), RenderError> { + let mut index = text.find(scope_opener.as_str()).ok_or(RenderError::new( + "Given scope opener not found in the given parameter", + ))?; + + index = index + scope_opener.len() - 1; + let scope_opener_index = index.clone(); + let mut scope_count = 1; + + while scope_count > 0 { + index += 1; + match text.chars().nth(index) { + Some('{') => { + scope_count += 1; + } + Some('}') => { + scope_count -= 1; + } + None => { + return Err(RenderError::new("Malformed scopes")); + } + _ => {} + } + } + + Ok((scope_opener_index, index)) +} + impl HelperDef for MergeScope { fn call<'reg: 'rc, 'rc>( &self, @@ -125,35 +156,11 @@ impl HelperDef for MergeScope { ))? .to_string(); - let mut index = s.find(scope_opener.as_str()).ok_or(RenderError::new( - "Given scope opener not found in the given parameter", - ))?; - - index += scope_opener.len(); - let scope_opener_index = index.clone(); - index -= 1; - let mut scope_count = 1; - - while scope_count > 0 { - index += 1; - match s.chars().nth(index) { - Some('{') => { - scope_count += 1; - } - Some('}') => { - scope_count -= 1; - } - None => { - return Err(RenderError::new("Malformed scopes")); - } - _ => {} - } - } - - // Here index is the position of the correct closing '}' for the scope + let (scope_opener_index, scope_close_index) = + get_scope_open_and_close_char_indexes(&s, &scope_opener)?; out.write(&s[0..=scope_opener_index])?; - let previous_scope_content = &s[scope_opener_index..index].to_string(); + let previous_scope_content = &s[(scope_opener_index + 1)..scope_close_index].to_string(); let mut data = ctx .data() @@ -167,7 +174,7 @@ impl HelperDef for MergeScope { rc.set_context(Context::wraps(data)?); t.render(r, ctx, rc, out)?; - out.write(&s[index..])?; + out.write(&s[scope_close_index..])?; Ok(()) } } @@ -516,3 +523,21 @@ pub fn choose_or_get_template( Ok(chosen_template_name) } + +#[cfg(test)] +mod tests { + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn test_get_scope_open_and_close_char_indexes() { + let text = String::from("const s = {};"); + let scope_opener = String::from("const s = {"); + + let (scope_opener_index, scope_close_index) = + get_scope_open_and_close_char_indexes(&text, &scope_opener).unwrap(); + + assert_eq!(scope_opener_index, 10); + assert_eq!(scope_close_index, 11); + } +} diff --git a/tests/generate_web_app.rs b/tests/generate_web_app.rs deleted file mode 100644 index b3f10d6c1..000000000 --- a/tests/generate_web_app.rs +++ /dev/null @@ -1,50 +0,0 @@ -use assert_cmd::Command; -use temp_dir::TempDir; - -#[test] -fn scaffold_full_web_app_and_test_it() { - let tempdir = TempDir::new().unwrap(); - - let tempdir_path = tempdir.path().to_path_buf(); - - let mut cmd = Command::cargo_bin("hc-scaffold").unwrap(); - let cmd = cmd.current_dir(&tempdir_path); - let cmd = cmd.args(&[ - "web-app", - "forum", - "--setup-nix", - "true", - "--template", - "lit", - ]); - cmd.assert().success(); - - let apptempdir = tempdir_path.join("forum"); - - let mut cmd = Command::cargo_bin("hc-scaffold").unwrap(); - let cmd = cmd.current_dir(&apptempdir); - let cmd = cmd.args(&["dna", "forum"]); - cmd.assert().success(); - - let mut cmd = Command::cargo_bin("hc-scaffold").unwrap(); - let cmd = cmd.current_dir(&apptempdir); - let cmd = cmd.args(&[ - "zome", - "posts", - "--integrity", - "dnas/forum/zomes/integrity", - "--coordinator", - "dnas/forum/zomes/coordinator", - ]); - cmd.assert().success(); - - let mut cmd = Command::cargo_bin("hc-scaffold").unwrap(); - let cmd = cmd.current_dir(&apptempdir); - let cmd = cmd.args(&["entry-type", "post", "posts", "--crud", "crud", "--fields"]); - cmd.assert().success(); - - let mut cmd = Command::new("nix-shell"); - let cmd = cmd.current_dir(&apptempdir); - let cmd = cmd.args(&["--run", "echo HI"]); - cmd.assert().success(); -} From 123104835cd1bcaebf446952295ca4e2140ca825 Mon Sep 17 00:00:00 2001 From: ThetaSinner Date: Tue, 21 Nov 2023 14:23:55 +0000 Subject: [PATCH 2/8] Merge pull request #137 from emhoracek/update-vue-scaffold Update vue scaffolding --- .../{{pascal_case collection_name}}.vue.hbs | 2 +- .../Create{{pascal_case entry_type.name}}.vue.hbs | 2 +- ....name}}.vue{{\302\241if}}{{\302\241each}}.hbs" | 15 ++++++++------- ..._case entry_type.name}}.vue{{\302\241if}}.hbs" | 2 +- .../{{pascal_case entry_type.name}}Detail.vue.hbs | 2 +- ...to_referenceable.name}}.vue{{\302\241if}}.hbs" | 2 +- ...om_referenceable.name}}.vue{{\302\241if}}.hbs" | 2 +- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/templates/vue/collection/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{pascal_case collection_name}}.vue.hbs b/templates/vue/collection/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{pascal_case collection_name}}.vue.hbs index 527153f9b..7ad76e248 100644 --- a/templates/vue/collection/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{pascal_case collection_name}}.vue.hbs +++ b/templates/vue/collection/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{pascal_case collection_name}}.vue.hbs @@ -4,7 +4,7 @@
- Error fetching the {{lower_case (plural referenceable.name)}}: {{{{raw}}}} {{error.data.data}}.{{{{/raw}}}} + Error fetching the {{lower_case (plural referenceable.name)}}: {{{{raw}}}} {{error.data}}.{{{{/raw}}}}
<{{pascal_case referenceable.name}}Detail v-for="hash in hashes" diff --git a/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/Create{{pascal_case entry_type.name}}.vue.hbs b/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/Create{{pascal_case entry_type.name}}.vue.hbs index be20ee283..6d4e8cd01 100644 --- a/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/Create{{pascal_case entry_type.name}}.vue.hbs +++ b/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/Create{{pascal_case entry_type.name}}.vue.hbs @@ -127,7 +127,7 @@ export default defineComponent({ this.$emit('{{kebab_case entry_type.name}}-created', record.signed_action.hashed.hash); } catch (e: any) { const errorSnackbar = this.$refs['create-error'] as Snackbar; - errorSnackbar.labelText = `Error creating the {{lower_case entry_type.name}}: ${e.data.data}`; + errorSnackbar.labelText = `Error creating the {{lower_case entry_type.name}}: ${e.data}`; errorSnackbar.show(); } }, diff --git "a/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{#each entry_type.fields}}{{#if (and linked_from (not (eq linked_from.hash_type 'AgentPubKey') ) )}}{{pascal_case (plural ..\302\241entry_type.name)}}For{{pascal_case linked_from.name}}.vue{{\302\241if}}{{\302\241each}}.hbs" "b/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{#each entry_type.fields}}{{#if (and linked_from (not (eq linked_from.hash_type 'AgentPubKey') ) )}}{{pascal_case (plural ..\302\241entry_type.name)}}For{{pascal_case linked_from.name}}.vue{{\302\241if}}{{\302\241each}}.hbs" index 85116db5a..4b860b08b 100644 --- "a/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{#each entry_type.fields}}{{#if (and linked_from (not (eq linked_from.hash_type 'AgentPubKey') ) )}}{{pascal_case (plural ..\302\241entry_type.name)}}For{{pascal_case linked_from.name}}.vue{{\302\241if}}{{\302\241each}}.hbs" +++ "b/templates/vue/entry-type/ui/src/{{dna_role_name}}/{{coordinator_zome_manifest.name}}/{{#each entry_type.fields}}{{#if (and linked_from (not (eq linked_from.hash_type 'AgentPubKey') ) )}}{{pascal_case (plural ..\302\241entry_type.name)}}For{{pascal_case linked_from.name}}.vue{{\302\241if}}{{\302\241each}}.hbs" @@ -4,7 +4,7 @@
- Error fetching the {{lower_case (plural ../entry_type.name)}}: {{{{raw}}}} {{error.data.data}}.{{{{/raw}}}} + Error fetching the {{lower_case (plural ../entry_type.name)}}: {{{{raw}}}} {{error.data}}.{{{{/raw}}}}
<{{pascal_case ../entry_type.name}}Detail v-for="hash in hashes" @@ -20,7 +20,7 @@