Skip to content

Commit 9513425

Browse files
authored
Merge pull request #101 from TTWShell/feature/init-dev-cmd
Feature/init dev cmd
2 parents 14ec85d + 84d222c commit 9513425

File tree

11 files changed

+149
-18
lines changed

11 files changed

+149
-18
lines changed

hobbit/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def format_options(self, ctx, formatter):
2929
formatter.write_dl(opts)
3030

3131

32-
class CLI(click.Group, HobbitCommand):
32+
class HobbitGroup(click.Group, HobbitCommand):
3333

3434
def list_commands(self, ctx):
3535
return sorted(self.cmds.keys())
@@ -47,8 +47,10 @@ def get_command(self, ctx, cmd_name):
4747

4848
@property
4949
def cmds(self):
50-
from . import bootstrap
51-
return {func.name: func for func in bootstrap.CMDS}
50+
from .bootstrap import cmd_list
51+
from .devtools import dev
52+
cmd_list.append(dev)
53+
return {func.name: func for func in cmd_list}
5254

5355
def format_options(self, ctx, formatter):
5456
HobbitCommand.format_options(self, ctx, formatter)
@@ -90,7 +92,7 @@ def format_commands(self, ctx, formatter):
9092

9193

9294
@click.group(
93-
cls=CLI,
95+
cls=HobbitGroup,
9496
epilog='More details: https://hobbit-core.readthedocs.io/zh/latest/',
9597
context_settings=CONTEXT_SETTINGS)
9698
@click.version_option()

hobbit/bootstrap.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55

66
import click
77

8-
from .handlers.bootstrap import echo, render_project, gen_metadata_by_name, \
8+
from .handlers import echo
9+
from .handlers.bootstrap import render_project, gen_metadata_by_name, \
910
validate_template_path, validate_csv_file, MetaModel, create_models_csv
10-
from . import HobbitCommand, main as cli
11+
from . import HobbitCommand, HobbitGroup, CONTEXT_SETTINGS
1112

1213
templates = ['shire', 'rivendell']
1314

1415

16+
@click.group(cls=HobbitGroup, context_settings=CONTEXT_SETTINGS)
17+
def cli():
18+
pass
19+
20+
1521
def common_options(func):
1622
func = click.option(
1723
'-f', '--force', default=False, is_flag=True,
@@ -114,4 +120,4 @@ def create(ctx, name):
114120
create_models_csv(name)
115121

116122

117-
CMDS = [new, gen, create]
123+
cmd_list = [new, gen, create]

hobbit/devtools.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import click
2+
3+
from . import HobbitGroup, CONTEXT_SETTINGS
4+
from .handlers.devtools import dev_init
5+
6+
7+
class BootstrapGroup(HobbitGroup):
8+
9+
@property
10+
def cmds(self):
11+
return {func.name: func for func in [init]}
12+
13+
14+
@click.group(cls=BootstrapGroup, context_settings=CONTEXT_SETTINGS)
15+
def dev():
16+
"""Dev tools, more: hobbit dev --help.
17+
"""
18+
pass
19+
20+
21+
@dev.command()
22+
@click.option('-a', '--all', 'all_', default=False, is_flag=True,
23+
help='Run all.')
24+
@click.option('--hooks', default=False, is_flag=True, help='Install hooks.')
25+
@click.option('--pipenv', default=False, is_flag=True,
26+
help='Create virtualenv by pipenv.')
27+
@click.pass_context
28+
def init(ctx, all_, hooks, pipenv):
29+
"""Init dev env: git hooks, pyenv install etc.
30+
"""
31+
dev_init(all_, hooks, pipenv)

hobbit/handlers/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import click
2+
3+
4+
@click.pass_context
5+
def echo(ctx, msg):
6+
if not ctx.obj['ECHO']:
7+
return
8+
click.echo(msg)

hobbit/handlers/bootstrap.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from jinja2 import Environment, FileSystemLoader, Template
99

1010
from hobbit import inflect_engine
11+
from . import echo
1112

1213
SUFFIX = '.jinja2'
1314

@@ -17,13 +18,6 @@ def regex_replace(s, find, replace):
1718
return re.sub(find, replace, s)
1819

1920

20-
@click.pass_context
21-
def echo(ctx, msg):
22-
if not ctx.obj['ECHO']:
23-
return
24-
click.echo(msg)
25-
26-
2721
@contextmanager
2822
def chdir(dist):
2923
cwd = os.getcwd()

hobbit/handlers/devtools.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import sys
3+
from subprocess import run, PIPE, STDOUT
4+
5+
import click
6+
7+
from hobbit import ROOT_PATH
8+
9+
10+
def dev_init(all_, hooks, pipenv):
11+
run('git init', shell=True)
12+
13+
if all_ or hooks:
14+
HOOKS_PATH = os.path.join(ROOT_PATH, 'static', 'hooks')
15+
run(f'cp -r {HOOKS_PATH}/* .git/hooks', shell=True)
16+
17+
if all_ or pipenv:
18+
sub = run('which pipenv', shell=True, stdout=PIPE, stderr=STDOUT)
19+
if sub.returncode != 0:
20+
click.echo(click.style('cmd pipenv not exist.', fg='red'))
21+
sys.exit(sub.returncode)
22+
pipenv_path = sub.stdout.strip().decode('utf8')
23+
24+
pipenv_cmds = [
25+
f'{pipenv_path} install --dev pytest pytest-cov pytest-env flake8',
26+
]
27+
if 'requirements.txt' in os.listdir():
28+
pipenv_cmds.insert(
29+
0, f'{pipenv_path} install -r requirements.txt --pre')
30+
31+
cmd = ' && '.join(pipenv_cmds)
32+
click.echo(click.style(cmd, fg='green'))
33+
# force pipenv to ignore that environment and create its own instead
34+
env = os.environ.copy()
35+
env.update({'PIPENV_IGNORE_VIRTUALENVS': '1'})
36+
run(cmd, shell=True, env=env)

hobbit/static/hooks/commit-msg

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message.
4+
# Called by "git commit" with one argument, the name of the file
5+
# that has the commit message. The hook should exit with non-zero
6+
# status after issuing an appropriate message if it wants to stop the
7+
# commit. The hook is allowed to edit the commit message file.
8+
#
9+
# To enable this hook, rename this file to "commit-msg".
10+
11+
# Uncomment the below to add a Signed-off-by line to the message.
12+
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
13+
# hook is more suited to it.
14+
#
15+
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
16+
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
17+
18+
# This example catches duplicate Signed-off-by lines.
19+
20+
CURRENT_BRANCH=`git branch | grep \* | cut -d ' ' -f2`
21+
JIRA_ID_REGEX='[A-Za-z0-9]+-[0-9]+'
22+
JIRA_ID=`echo $CURRENT_BRANCH | grep -oE '${JIRA_ID_REGEX}$'`
23+
if [[ -z $JIRA_ID ]]; then
24+
echo "Warning: 'jira id' not found in current branch name, example: feature/test-SKYF-458"
25+
fi
26+
27+
COMMENT=`cat $1 | head -n 1`
28+
if [[ -z $COMMENT ]]; then
29+
echo "Error: comment can't be empty"
30+
exit 1
31+
fi
32+
33+
hasHead=`echo $COMMENT | grep -oE '^\(${JIRA_ID_REGEX}\)'`
34+
if [[ -z $hasHead && ! -z $JIRA_ID ]]; then
35+
COMMENT_TAIL=`tail -n +2 $1`
36+
COMMENT="($JIRA_ID) $COMMENT\n${COMMENT_TAIL}"
37+
printf "$COMMENT" > "$1"
38+
fi
39+
40+
isOK=`echo $COMMENT | grep -oE '^\(${JIRA_ID_REGEX}\) .+'`
41+
if [[ -z $isOK ]]; then
42+
echo "Error: comment should be in format: (jira-id) comment msg. Example: (SKYF-458) finished xx"
43+
exit 1
44+
fi

hobbit/static/hooks/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flake8 . --max-line-length=120

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def gen_data(data_root='static'):
1111
"""just for collect static files.
1212
"""
13-
return [fpath for fpath in Path(
13+
return [fpath.as_posix() for fpath in Path(
1414
PurePath(src_path) / data_root).glob(f'**/*{SUFFIX}')]
1515

1616

tests/test_hobbit.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ def test_new_rivendell_tpl_and_gen_cmd(self, runner, gen_cmd, csv_file):
9292
assert os.getcwd() == self.wkdir
9393

9494
# new project use rivendell template
95-
cmd = [
96-
'--echo', 'new', '-n', 'haha', '-p', '1024',
97-
'-t', 'rivendell']
95+
cmd = ['--echo', 'new', '-n', 'haha', '-p', '1024', '-t', 'rivendell']
9896
result = runner.invoke(hobbit, cmd, obj={})
9997
# start + files + mkdir + tail
10098
assert result.exit_code == 0
@@ -109,3 +107,13 @@ def test_new_rivendell_tpl_and_gen_cmd(self, runner, gen_cmd, csv_file):
109107
# pytest
110108
assert subprocess.call(
111109
['pytest', '--no-cov'], stdout=subprocess.DEVNULL) == 0
110+
111+
@chdir(wkdir)
112+
def test_dev_init_cmd(self, runner):
113+
# new project use rivendell template
114+
cmd = ['--echo', 'new', '-n', 'haha', '-p', '1024', '-t', 'rivendell']
115+
result = runner.invoke(hobbit, cmd, obj={})
116+
assert result.exit_code == 0
117+
118+
result = runner.invoke(hobbit, ['dev', 'init', '--all'], obj={})
119+
assert result.exit_code == 0, result.output

0 commit comments

Comments
 (0)