From 0b0d36e04fb79fee16a3cd4cf901e822db0014ee Mon Sep 17 00:00:00 2001 From: Daniel McGregor Date: Wed, 26 Apr 2023 10:27:56 +0800 Subject: [PATCH 1/3] fix: workaround ValueError raised when using --defaults flag with latest copier --- src/algokit/cli/init.py | 5 ++++- src/algokit/core/init.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/algokit/core/init.py diff --git a/src/algokit/cli/init.py b/src/algokit/cli/init.py index 698cf872..f779321e 100644 --- a/src/algokit/cli/init.py +++ b/src/algokit/cli/init.py @@ -212,14 +212,17 @@ def init_command( # to their repo to include py.typed file from copier.main import Worker # type: ignore[import] + from algokit.core.init import populate_default_answers + with Worker( src_path=template.url, dst_path=project_path, data=answers_dict, - defaults=use_defaults, quiet=True, vcs_ref=template.commit, ) as copier_worker: + if use_defaults: + populate_default_answers(copier_worker) expanded_template_url = copier_worker.template.url_expanded logger.debug(f"final clone URL = {expanded_template_url}") copier_worker.run_copy() diff --git a/src/algokit/core/init.py b/src/algokit/core/init.py new file mode 100644 index 00000000..9699bdc2 --- /dev/null +++ b/src/algokit/core/init.py @@ -0,0 +1,27 @@ +from copier.main import MISSING, AnswersMap, Question, Worker # type: ignore[import] + + +def populate_default_answers(worker: Worker) -> None: + """Helper function to pre-populate Worker.data with default answers, based on Worker.answers implementation. + Used as a work-around for the behaviour of Worker(default=True, ...) which in >=7.1 raises an error instead of + prompting if no default is provided""" + answers = AnswersMap( + default=worker.template.default_answers, + user_defaults=worker.user_defaults, + init=worker.data, + last=worker.subproject.last_answers, + metadata=worker.template.metadata, + ) + + for var_name, details in worker.template.questions_data.items(): + if var_name in worker.data: + continue + question = Question( + answers=answers, + jinja_env=worker.jinja_env, + var_name=var_name, + **details, + ) + default_value = question.get_default() + if default_value is not MISSING: + worker.data[var_name] = default_value From f607ff8737cec70fe370f44b6da1877f47f07afb Mon Sep 17 00:00:00 2001 From: Daniel McGregor Date: Wed, 26 Apr 2023 10:26:26 +0800 Subject: [PATCH 2/3] test: update approvals due to reordered logs --- tests/init/test_init.test_init_ask_about_git.approved.txt | 2 +- tests/init/test_init.test_init_input_template_url.approved.txt | 2 +- ...raction_required_no_git_no_network_no_bootstrap.approved.txt | 2 +- ...minimal_interaction_required_yes_git_no_network.approved.txt | 2 +- ...interaction_required_defaults_no_git_no_network.approved.txt | 2 +- tests/init/test_init.test_init_project_name.approved.txt | 2 +- .../test_init.test_init_project_name_not_empty.approved.txt | 2 +- ...init.test_init_project_name_reenter_folder_name.approved.txt | 2 +- tests/init/test_init.test_init_template_selection.approved.txt | 2 +- tests/init/test_init.test_init_use_existing_folder.approved.txt | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/init/test_init.test_init_ask_about_git.approved.txt b/tests/init/test_init.test_init_ask_about_git.approved.txt index c14b599a..2fb4f17e 100644 --- a/tests/init/test_init.test_init_ask_about_git.approved.txt +++ b/tests/init/test_init.test_init_ask_about_git.approved.txt @@ -4,8 +4,8 @@ Please inspect the template repository, and pay particular attention to the valu DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/myapp DEBUG: Running 'git rev-parse --show-toplevel' in '{current_working_directory}/myapp' diff --git a/tests/init/test_init.test_init_input_template_url.approved.txt b/tests/init/test_init.test_init_input_template_url.approved.txt index f180fc14..1752284e 100644 --- a/tests/init/test_init.test_init_input_template_url.approved.txt +++ b/tests/init/test_init.test_init_input_template_url.approved.txt @@ -27,8 +27,8 @@ Valid examples: DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/myapp 🙌 Project initialized at `myapp`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_minimal_interaction_required_no_git_no_network_no_bootstrap.approved.txt b/tests/init/test_init.test_init_minimal_interaction_required_no_git_no_network_no_bootstrap.approved.txt index a323e791..82faaf9f 100644 --- a/tests/init/test_init.test_init_minimal_interaction_required_no_git_no_network_no_bootstrap.approved.txt +++ b/tests/init/test_init.test_init_minimal_interaction_required_no_git_no_network_no_bootstrap.approved.txt @@ -4,7 +4,7 @@ Please inspect the template repository, and pay particular attention to the valu DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! 🙌 Project initialized at `myapp`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt b/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt index 741136fb..18e07fb5 100644 --- a/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt +++ b/tests/init/test_init.test_init_minimal_interaction_required_yes_git_no_network.approved.txt @@ -4,8 +4,8 @@ Please inspect the template repository, and pay particular attention to the valu DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/myapp DEBUG: Running 'git rev-parse --show-toplevel' in '{current_working_directory}/myapp' diff --git a/tests/init/test_init.test_init_no_interaction_required_defaults_no_git_no_network.approved.txt b/tests/init/test_init.test_init_no_interaction_required_defaults_no_git_no_network.approved.txt index fcafa2a0..18e4d031 100644 --- a/tests/init/test_init.test_init_no_interaction_required_defaults_no_git_no_network.approved.txt +++ b/tests/init/test_init.test_init_no_interaction_required_defaults_no_git_no_network.approved.txt @@ -3,8 +3,8 @@ Please inspect the template repository, and pay particular attention to the valu DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/myapp 🙌 Project initialized at `myapp`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_project_name.approved.txt b/tests/init/test_init.test_init_project_name.approved.txt index 942ed175..36c5c648 100644 --- a/tests/init/test_init.test_init_project_name.approved.txt +++ b/tests/init/test_init.test_init_project_name.approved.txt @@ -4,8 +4,8 @@ DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle ? Name of project / directory to create the project in: DEBUG: project path = {current_working_directory}/FAKE_PROJECT Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/FAKE_PROJECT 🙌 Project initialized at `FAKE_PROJECT`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_project_name_not_empty.approved.txt b/tests/init/test_init.test_init_project_name_not_empty.approved.txt index 942ed175..36c5c648 100644 --- a/tests/init/test_init.test_init_project_name_not_empty.approved.txt +++ b/tests/init/test_init.test_init_project_name_not_empty.approved.txt @@ -4,8 +4,8 @@ DEBUG: template source = {test_parent_directory}/copier-helloworld.bundle ? Name of project / directory to create the project in: DEBUG: project path = {current_working_directory}/FAKE_PROJECT Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/FAKE_PROJECT 🙌 Project initialized at `FAKE_PROJECT`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_project_name_reenter_folder_name.approved.txt b/tests/init/test_init.test_init_project_name_reenter_folder_name.approved.txt index 17f7d286..45ac6f08 100644 --- a/tests/init/test_init.test_init_project_name_reenter_folder_name.approved.txt +++ b/tests/init/test_init.test_init_project_name_reenter_folder_name.approved.txt @@ -7,8 +7,8 @@ WARNING: Re-using existing directory, this is not recommended because if project ? Name of project / directory to create the project in: DEBUG: project path = {current_working_directory}/FAKE_PROJECT_2 Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/FAKE_PROJECT_2 🙌 Project initialized at `FAKE_PROJECT_2`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_template_selection.approved.txt b/tests/init/test_init.test_init_template_selection.approved.txt index 5e9f77a9..92ac97cd 100644 --- a/tests/init/test_init.test_init_template_selection.approved.txt +++ b/tests/init/test_init.test_init_template_selection.approved.txt @@ -10,8 +10,8 @@ beaker_with_version DEBUG: template source = gh:robdmoore/copier-helloworld DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = https://github.com/robdmoore/copier-helloworld.git No git tags found in template; using HEAD as ref +DEBUG: final clone URL = https://github.com/robdmoore/copier-helloworld.git Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/myapp 🙌 Project initialized at `myapp`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/init/test_init.test_init_use_existing_folder.approved.txt b/tests/init/test_init.test_init_use_existing_folder.approved.txt index 8f74f179..fc3e73f0 100644 --- a/tests/init/test_init.test_init_use_existing_folder.approved.txt +++ b/tests/init/test_init.test_init_use_existing_folder.approved.txt @@ -5,8 +5,8 @@ WARNING: Re-using existing directory, this is not recommended because if project ? Continue anyway? (y/N) DEBUG: project path = {current_working_directory}/myapp Starting template copy and render... -DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle No git tags found in template; using HEAD as ref +DEBUG: final clone URL = {test_parent_directory}/copier-helloworld.bundle Template render complete! Executed `algokit bootstrap all` in {current_working_directory}/myapp 🙌 Project initialized at `myapp`! For template specific next steps, consult the documentation of your selected template 🧐 From 7398e584bf60d6f11c4e0afb31c52696abe2e868 Mon Sep 17 00:00:00 2001 From: Daniel McGregor Date: Wed, 26 Apr 2023 12:29:28 +0800 Subject: [PATCH 3/3] doc: add link to copier source --- src/algokit/core/init.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/algokit/core/init.py b/src/algokit/core/init.py index 9699bdc2..00593d69 100644 --- a/src/algokit/core/init.py +++ b/src/algokit/core/init.py @@ -2,7 +2,9 @@ def populate_default_answers(worker: Worker) -> None: - """Helper function to pre-populate Worker.data with default answers, based on Worker.answers implementation. + """Helper function to pre-populate Worker.data with default answers, based on Worker.answers implementation (see + https://github.com/copier-org/copier/blob/v7.1.0/copier/main.py#L363). + Used as a work-around for the behaviour of Worker(default=True, ...) which in >=7.1 raises an error instead of prompting if no default is provided""" answers = AnswersMap(