forked from SublimeText/PackageDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_dev.py
120 lines (91 loc) · 3.24 KB
/
package_dev.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import sublime
import sublime_plugin
import os
PLUGIN_NAME = os.getcwdu().replace(sublime.packages_path(), '')[1:]
from sublime_lib.path import root_at_packages
DEBUG = 1
status = sublime.status_message
error = sublime.error_message
join_path = os.path.join
path_exists = os.path.exists
DEFAULT_DIRS = (
"Snippets",
"Support",
"Docs",
"Macros",
"bin",
"data"
)
# name, default template
DEFAULT_FILES = [
("LICENSE.txt", None),
("README.rst", "data/README.rst"),
(".hgignore", "data/hgignore.txt"),
(".gitignore", "data/gitignore.txt"),
("bin/MakeRelease.ps1", "data/MakeRelease.ps1"),
("bin/CleanUp.ps1", "data/CleanUp.ps1"),
("data/html_template.txt", "data/html_template.txt"),
("data/main.css", "data/main.css"),
("setup.py", "data/setup.py"),
]
for i, (name, path) in enumerate(DEFAULT_FILES):
if path is not None:
DEFAULT_FILES[i] = (name, root_at_packages(PLUGIN_NAME, path))
class NewPackageCommand(sublime_plugin.WindowCommand):
def on_done(self, pkg_name):
pam = PackageManager()
if pam.exists(pkg_name):
error(" NewPackage -- Error\n\n"
" Package '" + pkg_name + "' already exists.\n"
" You cannot overwrite an existing package."
)
return
pam.create_new(pkg_name)
def on_cancel(self):
status('on_cancel')
def on_changed(self):
status('on_changed')
def run(self):
self.window.show_input_panel("New Package Name", '', self.on_done,
None, None)
class DeletePackageCommand(sublime_plugin.WindowCommand):
def run(self):
pam = PackageManager()
pam.browse()
class PackageManager(object):
def is_installed(self, name):
raise NotImplemented
def exists(self, name):
return path_exists(root_at_packages(name))
def browse(self):
# Let user choose.
sublime.active_window().run_command(
"open_dir",
{"dir": sublime.packages_path()}
)
def create_new(self, name):
print "[NewPackage] Creating new package...",
print root_at_packages(name)
if self.dry_run:
msg = "[NewPackage] ** Nothing done. This was a test. **"
print msg
status(msg)
return
# Create top folder, default folders, default files.
map(os.makedirs, [root_at_packages(name, d) for d in DEFAULT_DIRS])
for fname, template in DEFAULT_FILES:
with open(root_at_packages(name, fname), 'w') as fh:
if template:
try:
content = ("".join(open(template, 'r').readlines())
% {"package_name": name})
except:
pass
finally:
content = "".join(open(template, 'r').readlines())
fh.write(content)
msg = "[NewPackage] Created new package '%s'." % name
print msg
status(msg)
def __init__(self, dry_run=False):
self.dry_run = dry_run