9
9
'''
10
10
"new" plugin for cocos command line tool
11
11
'''
12
+ from __future__ import print_function
13
+ from __future__ import unicode_literals
12
14
13
15
__docformat__ = 'restructuredtext'
14
16
15
17
# python
16
18
import os
17
19
import sys
18
- import getopt
19
- import ConfigParser
20
20
import json
21
21
import shutil
22
22
import cocos
23
- from MultiLanguage import MultiLanguage
24
23
import cocos_project
25
24
import re
26
25
import utils
27
26
from collections import OrderedDict
28
-
27
+ from MultiLanguage import MultiLanguage
29
28
30
29
#
31
30
# Plugins should be a sublass of CCJSPlugin
@@ -74,9 +73,28 @@ def init(self, args):
74
73
self ._mac_bundleid = args .mac_bundleid
75
74
self ._ios_bundleid = args .ios_bundleid
76
75
77
- self ._templates = Templates (args .language , self ._templates_paths , args .template )
78
- if self ._templates .none_active ():
79
- self ._templates .select_one ()
76
+ self ._tpdir = ''
77
+ # old way to choose a template from args:
78
+ # --language + --template (???)
79
+ # new way to choose a template from args:
80
+ # --template-name
81
+ if args .template_name :
82
+ # New Way
83
+ dic = Templates .list (self .get_templates_paths ())
84
+ template_key = args .template_name
85
+ if template_key in dic :
86
+ self ._tpdir = dic [template_key ]["path" ]
87
+ else :
88
+ raise cocos .CCPluginError (
89
+ "Template name '%s' not found. Available templates: %s" %
90
+ (template_key , dic .keys ()),
91
+ cocos .CCPluginError .ERROR_PATH_NOT_FOUND )
92
+ else :
93
+ # Old way
94
+ self ._templates = Templates (args .language , self ._templates_paths , args .template )
95
+ if self ._templates .none_active ():
96
+ self ._templates .select_one ()
97
+ self ._tpdir = self ._templates .template_path ()
80
98
81
99
# parse arguments
82
100
def parse_args (self , argv ):
@@ -85,18 +103,13 @@ def parse_args(self, argv):
85
103
from argparse import ArgumentParser
86
104
# set the parser to parse input params
87
105
# the correspond variable name of "-x, --xxx" is parser.xxx
88
- name = CCPluginNew .plugin_name ()
89
- category = CCPluginNew .plugin_category ()
90
106
parser = ArgumentParser (prog = "cocos %s" % self .__class__ .plugin_name (),
91
107
description = self .__class__ .brief_description ())
108
+
92
109
parser .add_argument (
93
110
"name" , metavar = "PROJECT_NAME" , nargs = '?' , help = MultiLanguage .get_string ('NEW_ARG_NAME' ))
94
111
parser .add_argument (
95
112
"-p" , "--package" , metavar = "PACKAGE_NAME" , help = MultiLanguage .get_string ('NEW_ARG_PACKAGE' ))
96
- parser .add_argument ("-l" , "--language" ,
97
- required = True ,
98
- choices = ["cpp" , "lua" , "js" ],
99
- help = MultiLanguage .get_string ('NEW_ARG_LANG' ))
100
113
parser .add_argument ("-d" , "--directory" , metavar = "DIRECTORY" ,
101
114
help = MultiLanguage .get_string ('NEW_ARG_DIR' ))
102
115
parser .add_argument ("-t" , "--template" , metavar = "TEMPLATE_NAME" ,
@@ -109,16 +122,29 @@ def parse_args(self, argv):
109
122
help = MultiLanguage .get_string ('NEW_ARG_ENGINE_PATH' ))
110
123
parser .add_argument ("--portrait" , action = "store_true" , dest = "portrait" ,
111
124
help = MultiLanguage .get_string ('NEW_ARG_PORTRAIT' ))
112
-
113
125
group = parser .add_argument_group (MultiLanguage .get_string ('NEW_ARG_GROUP_SCRIPT' ))
114
126
group .add_argument (
115
127
"--no-native" , action = "store_true" , dest = "no_native" ,
116
128
help = MultiLanguage .get_string ('NEW_ARG_NO_NATIVE' ))
117
129
130
+ # -l | --list-templates
131
+ group = parser .add_mutually_exclusive_group (required = True )
132
+ group .add_argument ("-l" , "--language" ,
133
+ choices = ["cpp" , "lua" , "js" ],
134
+ help = MultiLanguage .get_string ('NEW_ARG_LANG' ))
135
+ group .add_argument ("--list-templates" , action = "store_true" ,
136
+ help = 'List available templates. To be used with --template option.' )
137
+ group .add_argument ("-k" , "--template-name" ,
138
+ help = 'Name of the template to be used to create the game. To list available names, use --list-templates.' )
139
+
118
140
# parse the params
119
141
args = parser .parse_args (argv )
120
142
121
- if args .name is None :
143
+ if args .list_templates :
144
+ print (json .dumps (Templates .list (self .get_templates_paths ())))
145
+ sys .exit (0 )
146
+
147
+ if args .name is None and args .language is not None :
122
148
args .name = CCPluginNew .DEFAULT_PROJ_NAME [args .language ]
123
149
124
150
if not args .package :
@@ -190,10 +216,8 @@ def _create_from_cmd(self):
190
216
message = MultiLanguage .get_string ('NEW_ERROR_FOLDER_EXISTED_FMT' , self ._projdir )
191
217
raise cocos .CCPluginError (message , cocos .CCPluginError .ERROR_PATH_NOT_FOUND )
192
218
193
- tp_dir = self ._templates .template_path ()
194
-
195
219
creator = TPCreator (self ._lang , self ._cocosroot , self ._projname , self ._projdir ,
196
- self ._tpname , tp_dir , self ._package , self ._mac_bundleid , self ._ios_bundleid )
220
+ self ._tpname , self . _tpdir , self ._package , self ._mac_bundleid , self ._ios_bundleid )
197
221
# do the default creating step
198
222
creator .do_default_step ()
199
223
@@ -267,6 +291,34 @@ def replace_string(filepath, src_string, dst_string):
267
291
268
292
class Templates (object ):
269
293
294
+ @staticmethod
295
+ def list (paths ):
296
+ dirs = []
297
+
298
+ # enumerate directories and append then into 'dirs'
299
+ for template_dir in paths :
300
+ try :
301
+ dirs += [os .path .join (template_dir , name ) for name in os .listdir (template_dir ) if os .path .isdir (
302
+ os .path .join (template_dir , name ))]
303
+ except Exception :
304
+ continue
305
+
306
+ # if dir contains the template_metadata.json file, then it is a valid template
307
+ valid_templates = {}
308
+ for d in dirs :
309
+ try :
310
+ f = open (os .path .join (d , 'template_metadata' , 'config.json' ))
311
+ # python dictionary
312
+ dictionary = json .load (f )
313
+ # append current path
314
+ dictionary ['path' ] = d
315
+ # use 'key' as key
316
+ name = dictionary ['key' ]
317
+ valid_templates [name ] = dictionary
318
+ except Exception :
319
+ continue
320
+ return valid_templates
321
+
270
322
def __init__ (self , lang , templates_paths , current ):
271
323
self ._lang = lang
272
324
self ._templates_paths = templates_paths
@@ -353,7 +405,12 @@ def __init__(self, lang, cocos_root, project_name, project_dir, tp_name, tp_dir,
353
405
self .tp_dir = tp_dir
354
406
self .tp_json = 'cocos-project-template.json'
355
407
356
- tp_json_path = os .path .join (tp_dir , self .tp_json )
408
+ # search in 'template_metadata' first
409
+ tp_json_path = os .path .join (tp_dir , 'template_metadata' , self .tp_json )
410
+ if not os .path .exists (tp_json_path ):
411
+ # if not, search in the old place
412
+ tp_json_path = os .path .join (tp_dir , self .tp_json )
413
+
357
414
if not os .path .exists (tp_json_path ):
358
415
message = MultiLanguage .get_string ('NEW_WARNING_FILE_NOT_FOUND_FMT' , tp_json_path )
359
416
raise cocos .CCPluginError (message , cocos .CCPluginError .ERROR_PATH_NOT_FOUND )
0 commit comments