Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: workaround ValueError raised when using --defaults flag with copier 7.1 #256

Merged
merged 3 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/algokit/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 27 additions & 0 deletions src/algokit/core/init.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tests/init/test_init.test_init_ask_about_git.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
2 changes: 1 addition & 1 deletion tests/init/test_init.test_init_project_name.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧐