Skip to content

Commit 3587532

Browse files
committed
added angular templates for app, service, and factory
1 parent c120a59 commit 3587532

File tree

14 files changed

+182
-9
lines changed

14 files changed

+182
-9
lines changed

flask_xxl/hooks.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
from mrbob import bobexceptions as mbe
22

33
# post question hooks - takes (configurator,question,answer)
4+
def skip_next_if(c,q,a):
5+
if a:
6+
c.questions.pop(c.questions.index(q)+1)
7+
return a
48

9+
def skip_next_if_not(c,q,a):
10+
if not a:
11+
c.questions.pop(c.questions.index(q)+1)
12+
return a
13+
14+
def to_list(c,q,a):
15+
return a.split(',')
16+
17+
def choose_app_template(c):
18+
if c.variables['app.language'].startswith('c'):
19+
c.template_dir = '{}_coffee'.format(c.template_dir)
520

621
# pre question hooks - takes (configurator,question)
22+
def only_if_type(c,q):
23+
print c.variables['service.service_type']
24+
print q.extra.get('check_type')
25+
if c.variables['service.service_type'] != q.extra.get('check_type'):
26+
raise mbe.SkipQuestion
27+
728
def add_apps(c,q):
829
if not c.variables['project.add_apps']:
930
raise mbe.SkipQuestion
@@ -18,7 +39,8 @@ def db_related_skip(c,q):
1839

1940
# pre render hooks - takes nothing - maybe configurator
2041

21-
42+
def get_service_type(c):
43+
c.template_dir = '{}/{}'.format(c.template_dir,c.variables['service.service_type'])
2244

2345
def make_db_uri(c):
2446
if c.variables['project.use_database']:
@@ -29,9 +51,6 @@ def make_db_uri(c):
2951
c.variables['project.db-host'],
3052
c.variables['project.db-port'],
3153
c.variables['project.db-name'])
32-
33-
34-
3554
# post render hooks - takes (configurator)
3655

3756

flask_xxl/mr_bob.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from flask.ext.script import Manager, prompt_bool
22
import os
3-
from mrbob import cli
3+
from mrbob import cli
4+
import mrbob
5+
from jinja2.loaders import FileSystemLoader
46
import sys
57

8+
69
manager = Manager(usage="performs mr.bob template runs")
710

811
EXT_ROOT = os.path.abspath(os.path.dirname(__file__))
912

1013
MRBOB_TEMPLATE_DIR = os.path.join(EXT_ROOT,'templates')
14+
mrbob.rendering.jinja2_env.loader = FileSystemLoader(MRBOB_TEMPLATE_DIR)
1115

1216

1317
def get_all_files(dirname,seen=None):
@@ -64,10 +68,11 @@ def print_template_files(template=None):
6468
]
6569
)
6670
)
71+
_template_dir = lambda name: os.path.join(MRBOB_TEMPLATE_DIR,name)
6772

6873
@manager.option("-t","--testing",action='store_true')
6974
def start_project(testing=False):
70-
template_dir = os.path.join(MRBOB_TEMPLATE_DIR,'project')
75+
template_dir = _template_dir('project')
7176
if testing:
7277
target_dir = './testing'
7378
else:
@@ -82,7 +87,7 @@ def start_project(testing=False):
8287

8388
@manager.option("-t","--testing",action="store_true")
8489
def add_blueprint(testing=False):
85-
template_dir = os.path.join(MRBOB_TEMPLATE_DIR,'blueprint')
90+
template_dir = _template_dir('blueprint')
8691
if testing:
8792
target_dir = './testing'
8893
else:
@@ -95,7 +100,28 @@ def add_blueprint(testing=False):
95100
args = [template_dir,'-O',target_dir,'-v','-w']
96101
cli.main(args)
97102

98-
103+
@manager.command
104+
def angular_app():
105+
template_dir = _template_dir('angular_app')
106+
target_dir = os.path.join(os.curdir,'static','js')
107+
if not os.path.exists(target_dir):
108+
os.makedirs(target_dir)
109+
list_questions = False
110+
non_interactive = False
111+
args = [template_dir,'-O',target_dir,'-v','-w']
112+
cli.main(args)
113+
114+
@manager.command
115+
def angular_service():
116+
template_dir = _template_dir('angular_service')
117+
target_dir = os.path.join(os.curdir,'static','js')
118+
if not os.path.exists(target_dir):
119+
os.makedirs(target_dir)
120+
list_questions = False
121+
non_interactive = False
122+
args = [template_dir,'-O',target_dir,'-v','-w']
123+
cli.main(args)
124+
99125

100126

101127

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[template]
2+
pre_render = flask.ext.xxl.hooks:choose_app_template
3+
4+
[questions]
5+
app.app_name.question = 'your application name'
6+
app.app_name.required = true
7+
app.app_name.default = 'app'
8+
9+
app.deps.question = 'does your app have dependencies on other apps'
10+
app.deps.post_ask_question = mrbob.hooks.to_boolean
11+
app.deps.post_ask_question = flask_xxl.hooks:skip_next_if_not
12+
13+
app.dep_names.question = 'dependency names please, in a comma seperated list'
14+
app.dep_names.post_ask_question = flask_xxl.hooks:to_list
15+
16+
17+
app.language.question = 'Javascript or CoffeeScript'
18+
19+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var {{{ app.app_var|default('app') }}} = angular.module('{{{ app.app_name }}}',[{{%-
4+
if app.deps
5+
-%}}
6+
{{%-
7+
for dep in app.dep_names
8+
-%}}
9+
'{{{ dep }}}'
10+
{{%-
11+
if app.dep_names|count
12+
> 1 and
13+
app.dep_names.index(dep)
14+
!= app.dep_names|count-1
15+
-%}},
16+
{{%-
17+
endif
18+
-%}}
19+
{{%-
20+
endfor
21+
-%}}
22+
{{%-
23+
endif
24+
-%}}]);
25+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
{{{ app.app_var|default('app') }}} = angular.module '{{{ app.app_name }}}',[
4+
{{%-
5+
if app.deps
6+
-%}}
7+
{{%-
8+
for dep in app.dep_names
9+
-%}}
10+
'{{{ dep }}}'
11+
{{%-
12+
if app.dep_names|count
13+
> 1 and
14+
app.dep_names.index(dep)
15+
!= app.dep_names|count-1
16+
-%}},
17+
{{%-
18+
endif
19+
-%}}
20+
{{%-
21+
endfor
22+
-%}}
23+
{{%-
24+
endif
25+
-%}}]
26+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[template]
2+
pre_render = flask_xxl.hooks:get_service_type
3+
4+
5+
[questions]
6+
service.app_var.question = 'App var name'
7+
service.app_var.default = app
8+
9+
service.service_name.question = 'service name'
10+
11+
service.service_type.question = 'type of service'
12+
service.service_type.post_ask_question = mrbob.hooks:validate_choices
13+
service.service_type.choices = service factory provider value constant
14+
service.service_type.default = factory
15+
16+
service.deps.question = 'does this service have dependencys'
17+
service.deps.post_ask_question = mrbob.hooks:to_boolean
18+
service.deps.post_ask_question = flask_xxl.hooks:skip_next_if_not
19+
20+
service.dep_names.question = 'dependency names, in a comma seperated list'
21+
service.dep_names.post_ask_question = flask_xxl.hooks:to_list
22+
23+
service.service_value.question = 'service value'
24+
service.service_value.pre_ask_question = flask_xxl.hooks:only_if_type
25+
service.service_value.check_type = constant
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{% extends 'includes/base.app.html' %}}
2+
{{%- block service_type -%}}constant{{%- endblock -%}}{{%- block service_value -%}}{{{ service.value|default('') }}}{{%- endblock -%}}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{% extends 'includes/base.app.html' %}}
2+
{{%- block service_type -%}}factory{{%- endblock -%}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
provider
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{{% extends 'includes/base.app.html' %}}
2+
{{%- block service_type -%}}service{{%- endblock -%}}

0 commit comments

Comments
 (0)