Browse Source

Update cookiecutter commands

Peter Justin 7 years ago
parent
commit
1fb08e5542
3 changed files with 41 additions and 28 deletions
  1. 14 7
      flaskbb/cli/plugins.py
  2. 16 9
      flaskbb/cli/themes.py
  3. 11 12
      flaskbb/cli/utils.py

+ 14 - 7
flaskbb/cli/plugins.py

@@ -9,6 +9,7 @@
     :license: BSD, see LICENSE for more details.
 """
 import sys
+import os
 
 import click
 from flask import current_app
@@ -16,7 +17,7 @@ from flask.cli import with_appcontext
 
 from flaskbb.extensions import db
 from flaskbb.cli.main import flaskbb
-from flaskbb.cli.utils import validate_plugin, check_cookiecutter
+from flaskbb.cli.utils import validate_plugin, get_cookiecutter
 from flaskbb.plugins.models import PluginRegistry, PluginStore
 from flaskbb.plugins.utils import remove_zombie_plugins_from_db
 
@@ -142,20 +143,26 @@ def cleanup():
 
 
 @plugins.command("new")
-@click.argument("plugin_name", callback=check_cookiecutter)
 @click.option("--template", "-t", type=click.STRING,
               default="https://github.com/sh4nks/cookiecutter-flaskbb-plugin",
               help="Path to a cookiecutter template or to a valid git repo.")
-@click.option("--out-dir", "-o", type=click.STRING,
+@click.option("--out-dir", "-o", type=click.Path(), default=None,
               help="The location for the new FlaskBB plugin.")
-def new_plugin(plugin_name, template, out_dir):
+@click.option("--force", "-f", is_flag=True, default=False,
+              help="Overwrite the contents of output directory if it exists")
+def new_plugin(template, out_dir, force):
     """Creates a new plugin based on the cookiecutter plugin
     template. Defaults to this template:
     https://github.com/sh4nks/cookiecutter-flaskbb-plugin.
     It will either accept a valid path on the filesystem
     or a URL to a Git repository which contains the cookiecutter template.
     """
-    from cookiecutter.main import cookiecutter  # noqa
-    cookiecutter(template, output_dir=out_dir)
-    click.secho("[+] Created new plugin {} in {}".format(plugin_name, out_dir),
+    cookiecutter = get_cookiecutter()
+
+    if out_dir is None:
+        out_dir = click.prompt("Saving plugin in",
+                               default=os.path.abspath("."))
+
+    r = cookiecutter(template, output_dir=out_dir, overwrite_if_exists=force)
+    click.secho("[+] Created new plugin in {}".format(r),
                 fg="green", bold=True)

+ 16 - 9
flaskbb/cli/themes.py

@@ -17,7 +17,7 @@ from flask import current_app
 from flask_themes2 import get_themes_list, get_theme
 
 from flaskbb.cli.main import flaskbb
-from flaskbb.cli.utils import check_cookiecutter, validate_theme
+from flaskbb.cli.utils import get_cookiecutter, validate_theme
 from flaskbb.utils.settings import flaskbb_config
 
 
@@ -48,23 +48,30 @@ def list_themes():
 
 
 @themes.command("new")
-@click.argument("theme_identifier", callback=check_cookiecutter)
 @click.option("--template", "-t", type=click.STRING,
               default="https://github.com/sh4nks/cookiecutter-flaskbb-theme",
               help="Path to a cookiecutter template or to a valid git repo.")
-def new_theme(theme_identifier, template):
+@click.option("--out-dir", "-o", type=click.Path(), default=None,
+              help="The location for the new FlaskBB theme.")
+@click.option("--force", "-f", is_flag=True, default=False,
+              help="Overwrite the contents of output directory if it exists")
+def new_theme(template, out_dir, force):
     """Creates a new theme based on the cookiecutter theme
     template. Defaults to this template:
     https://github.com/sh4nks/cookiecutter-flaskbb-theme.
     It will either accept a valid path on the filesystem
     or a URL to a Git repository which contains the cookiecutter template.
     """
-    from cookiecutter.main import cookiecutter
-    out_dir = os.path.join(current_app.root_path, "themes")
-    click.secho("[+] Creating new theme {}".format(theme_identifier),
-                fg="cyan")
-    cookiecutter(template, output_dir=out_dir)
-    click.secho("[+] Done. Created in {}".format(out_dir),
+    cookiecutter = get_cookiecutter()
+
+    if out_dir is None:
+        out_dir = click.prompt(
+            "Saving theme in",
+            default=os.path.join(current_app.root_path, "themes")
+        )
+
+    r = cookiecutter(template, output_dir=out_dir, overwrite_if_exists=force)
+    click.secho("[+] Created new theme in {}".format(r),
                 fg="green", bold=True)
 
 

+ 11 - 12
flaskbb/cli/utils.py

@@ -22,13 +22,6 @@ from flaskbb import __version__
 from flaskbb.utils.populate import create_user, update_user
 
 
-cookiecutter_available = False
-try:
-    from cookiecutter.main import cookiecutter  # noqa
-    cookiecutter_available = True
-except ImportError:
-    pass
-
 _email_regex = r"[^@]+@[^@]+\.[^@]+"
 
 
@@ -89,14 +82,20 @@ def validate_theme(theme):
         raise FlaskBBCLIError("Theme {} not found.".format(theme), fg="red")
 
 
-def check_cookiecutter(ctx, param, value):
+def get_cookiecutter():
+    cookiecutter_available = False
+    try:
+        from cookiecutter.main import cookiecutter  # noqa
+        cookiecutter_available = True
+    except ImportError:
+        pass
+
     if not cookiecutter_available:
         raise FlaskBBCLIError(
-            "Can't create {} because cookiecutter is not installed. "
-            "You can install it with 'pip install cookiecutter'.".
-            format(value), fg="red"
+            "Can't continue because cookiecutter is not installed. "
+            "You can install it with 'pip install cookiecutter'.", fg="red"
         )
-    return value
+    return cookiecutter
 
 
 def get_version(ctx, param, value):