-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
82 lines (63 loc) · 2.54 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
from os import path
from os import getcwd
PROJECT_FILECONTENT_TEMPLATE = '{"folders":[{"path": "%p"}]}\n'
PROJET_FILENAME_TEMPLATE = "{0}.sublime-project"
class BaseCommand(object):
def __init__(self, name):
self.args = None
self.name = name
def run(self, ctx):
args = ctx["args"]
progname = ctx["progname"]
parser = argparse.ArgumentParser(prog="{0} {1}".format(progname, self.name))
BaseCommand.add_common_args(parser)
parser.add_argument("-p", "--project", type=str, help="the sublime text project file name")
parser.add_argument("--force", action="store_true", help="override the project file if it exists")
self.args = parser.parse_args(args)
@classmethod
def add_common_args(self, parser):
parser.add_argument("-f", "--folder", type=str, help="the project folder")
class InitCommand(BaseCommand):
def __init__(self):
super(InitCommand, self).__init__("init")
def run(self, ctx):
super(InitCommand, self).run(ctx)
projectRootPath = getcwd()
if self.args.folder:
projectRootPath = path.abspath(self.args.folder)
projectFileName = None
if self.args.project:
projectName = self.args.project
projectFileName = PROJET_FILENAME_TEMPLATE.format(self.args.project)
else:
projectName = path.basename(projectRootPath)
projectFileName = PROJET_FILENAME_TEMPLATE.format(projectName)
projectFile = None
try:
if not self.args.force and path.exists(projectFileName):
print "{0} already exists, use --force to override the project file.".format(projectFileName)
return
projectFile = open(projectFileName, "w")
content = PROJECT_FILECONTENT_TEMPLATE.replace("%p", projectRootPath)
projectFile.write(content)
print "{0} project has been created.".format(projectName)
except Exception, e:
print "Failed to create the project due to {0}.".format(e)
finally:
if projectFile :
projectFile.close()
registry = {'init' : InitCommand()}