This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathjavac.py
329 lines (262 loc) · 10.4 KB
/
javac.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import sublime
import os, json
import shutil
import glob
try:
from . import javacbase
except ValueError:
import javacbase
sget = javacbase.sget
project_config_filename = "settings.sublime-javac"
class JavacCompileProjectCommand(javacbase.CommandBase):
def load_config(self, project_config_path):
def clear_path(path, ):
if path[0] in ['.', '/']: return path
return os.path.join(rel_dir, path)
settings_obj = open(project_config_path)
try:
settings = json.load(settings_obj)
except ValueError:
self.write("\nUnable to load project settings file. Check your 'settings.sublime-javac' for errors.\n")
return False
settings_obj.close()
self.project_dir = rel_dir = os.path.dirname(project_config_path)
self.base_libs = settings.get('libs', [])
self.encoding = settings.get('encoding', 'utf-8')
self.libs = [clear_path(path) for path in self.base_libs]
self.libs.append('.')
self.resources = [clear_path(path) for path in settings.get('resources', [])]
self.src = clear_path(settings.get('sources_dir', 'src'))
if not os.path.isdir(self.src): os.makedirs(self.src)
self.project_name = settings.get('project_name', 'project')
self.output_dir = build_path = clear_path(settings.get('output_dir', 'output'))
self.build_classes_path = os.path.join(build_path,'classes')
#delete old files
filelist = glob.glob(os.path.join(self.build_classes_path, "*.class"))
for f in filelist: os.remove(f)
if not os.path.isdir(self.build_classes_path): os.makedirs(self.build_classes_path)
self.build_dist_folder = os.path.join(build_path,'dist')
if not os.path.isdir(self.build_dist_folder): os.makedirs(self.build_dist_folder)
#delete old files
filelist = glob.glob(os.path.join(self.build_dist_folder, "*.jar"))
for f in filelist: os.remove(f)
self.output_jar_file = '.'.join([self.project_name, 'jar'])
self.entry_point = settings.get('entry_point', 'Namespace.EntryPointClass')
self.entry_file = settings.get('entry_file', 'Namespace/EntryPointClass.java')
return True
def generate_base_config(self, target_dir):
target_path = os.path.join(target_dir, project_config_filename)
_file = open(target_path, 'w')
_file.write("""{
"project_name" : "HelloWorld",
"output_dir" : "output",
"sources_dir" : "src",
"resources" : [],
"libs" : [],
"encoding" : "utf-8",
"entry_file" : "Test/HelloWorld.java",
"entry_point" : "Test.HelloWorld"
}""" )
_file.close()
if hasattr(self, '_output'):
self.output().close()
self.view.window().open_file(target_path)
def init(self):
""" Looking for 'project_config_filename' file in project folders, and load
config from it. If it non exists, it will display menu where you can choose
where it should be generated.
"""
self.view.run_command('save')
window = self.view.window()
dirs = window.folders()
files = [os.path.join(_dir, project_config_filename) for _dir in dirs ]
files = [_file for _file in files if os.path.exists(_file)]
if len(files) > 1:
self.write("Found more than one '%s' file. Can not continue." % project_config_filename)
return False
if len(files) == 0:
def show_folders(result):
if result == 1 or result == -1: return
if len(dirs) > 1:
def choose(result):
if result == -1: return
self.generate_base_config(dirs[result])
_list = [[os.path.basename(_dir), _dir] for _dir in dirs]
window.show_quick_panel(_list, choose)
else:
self.generate_base_config(dirs[0])
options = [
['Generate new configuration.', 'Generate new java project file configuration.'],
['Cancel', 'Abandon compilation.']
]
window.show_quick_panel(options, show_folders)
return False
if hasattr(self, '_output'):
self.output().clear()
return self.load_config(files[0])
def compile_project_order(self):
self.write("\n------------Compiling project------------")
self.write("")
javac = [
sget('javac_path', 'javac'),
'-d', self.build_classes_path,
'-encoding', self.encoding
]
libs = self.libs
if len(self.libs) > 0:
javac.extend(['-cp', '%s' % os.pathsep.join(libs)])
javac.append( self.entry_file )
return (javac, self.src)
def copy_resources(self):
files_to_copy = []
for pathname in self.resources:
files_to_copy.extend(glob.glob(pathname))
op = os.path
for path in files_to_copy:
filename = op.basename(path)
targetdir = op.join(self.build_classes_path, op.relpath(op.dirname(path), self.src))
targetpath = op.join(targetdir, filename)
if not op.isfile(targetpath):
if not op.isdir(targetdir): os.makedirs(targetdir)
shutil.copy(path, targetpath)
def _run(self, edit):
self.view.run_command('save')
if self.init():
self.copy_resources()
orders = (self.compile_project_order, )
self.call_new_thread_chain(orders)
class JavacCompileAndRunProjectCommand(JavacCompileProjectCommand):
def run_classes_order(self):
java = [
sget('java_path', 'java'),
]
libs = self.libs
if len(self.libs) > 0:
java.extend(['-cp', '%s' % os.pathsep.join(libs)])
java.append( self.entry_point )
cwd = self.build_classes_path
self.write("\n------------Running application------------")
self.write("")
return java, cwd, True
def _run(self, edit):
self.view.run_command('save')
if self.init():
self.copy_resources()
self.output().clear()
orders = (
self.compile_project_order,
self.run_classes_order
)
self.call_new_thread_chain(orders)
class JavacClearProjectCommand(JavacCompileProjectCommand):
def _clearing(self, result):
if result == 0 and self.init():
self.view.run_command('save')
self.write("\n------------Clearing project------------")
self.write("")
if os.path.isdir(self.output_dir):
shutil.rmtree(self.output_dir)
if sget('javac_autohide', True):
self.output().close()
def _run(self, edit):
options = [
['Confirmation', 'All files in my "output" directory will be deleted.'],
['Cancel', 'Abandon project clearing.']
]
self.view.window().show_quick_panel(options, self._clearing)
class JavacCompileFileCommand(javacbase.CommandBase):
def init(self):
self.file_name = self.view.file_name()
self.file_dir = os.path.dirname(self.file_name)
def compile(self):
javac = [sget('javac_path', 'javac'), self.file_name]
cwd = self.file_dir
self.write("\n------------Compiling file------------")
self.write("")
return javac, cwd
def _run(self, edit):
self.view.run_command('save')
self.init()
self.output().clear()
self.call_new_thread_chain((self.compile,))
class JavacCompileAndRunFileCommand(JavacCompileFileCommand):
def java_run(self):
base_name = os.path.splitext(os.path.basename(self.file_name))[0]
java = [
sget('java_path', 'java'),
base_name
]
cwd = self.file_dir
return java, cwd
def _run(self, edit):
self.init()
orders = (self.compile, self.java_run)
self.call_new_thread_chain(orders)
class JavacGenerateJarCommand(JavacCompileProjectCommand):
def pack_jar_order(self):
self.write("\n------------Packing jar------------")
self.write("")
output_path = os.path.join(self.build_dist_folder, self.output_jar_file)
jar = [
sget('jar_path', 'jar'),
'cmfev',
'Manifest',
output_path,
self.entry_point,
'.'
]
cwd = self.build_classes_path
return (jar, cwd)
def prepare_manifest(self):
libs = self.base_libs
text = 'Class-Path: %s' % ' '.join(libs) + '\n'
path = os.path.join(self.build_classes_path, 'Manifest')
_file = open(path, 'w')
_file.write(text)
_file.close()
def copy_libs(self):
op = os.path
libs = self.libs[:]
libs.remove('.')
for path in libs:
target_file = op.join(self.build_dist_folder, op.relpath(path, self.project_dir))
target_dir = op.dirname(target_file)
if not op.isfile(target_file):
if not op.isdir(target_dir): os.makedirs(target_dir)
shutil.copy(path, target_file)
def _run(self, edit):
self.view.run_command('save')
if self.init():
self.output().clear()
self.copy_resources()
self.copy_libs()
self.prepare_manifest()
orders = (
self.compile_project_order,
self.pack_jar_order
)
self.call_new_thread_chain(orders)
class JavacGenerateAndRunJarCommand(JavacGenerateJarCommand):
def run_jar_order(self):
java = [
sget('java_path', 'java'),
'-jar',
self.output_jar_file
]
cwd = self.build_dist_folder
self.write("\n------------Running application------------")
self.write("")
return java, cwd, True
def _run(self, edit):
self.view.run_command('save')
if self.init():
self.output().clear()
self.copy_resources()
self.copy_libs()
self.prepare_manifest()
orders = (
self.compile_project_order,
self.pack_jar_order,
self.run_jar_order
)
self.call_new_thread_chain(orders)