-
Notifications
You must be signed in to change notification settings - Fork 2
/
make.js
391 lines (320 loc) · 14.1 KB
/
make.js
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
'use babel';
'use strict';
import path from 'path';
import fs from 'fs';
import tmp from 'tmp';
export function provideBuilder(){
// Var to Keep track of the temporary file created when compiling against
// an untitled file. Reason for this is that the `doMatch` function (which
// returns a list of errors) doesn't have context to the build config.
var _untitledFileDest;
return class OracleBuildConfig {
constructor(cwd){
this.cwd = cwd;
// Assume we have a saved file.
// We need to keep track of this so that we can act in the pre and
// post build phases to restore the configuration if necessary.
this._savedFile = true;
}
getNiceName(){
return 'Oracle Build Config';
}
isEligible(){
this.oracleBuildConfig = path.join(this.cwd, '.atom-build-oracle.json');
return fs.existsSync(this.oracleBuildConfig);
}
// preBuild: Before running the build, check out the active tab to see
// if it is indeed a saved file or not. If it isn't, we want to save it
// to a temporary file and point the build configuration to this new file
preBuild(){
// The only time the temp file will be opened is when there is an
// issue in the compilation. Set an explanatory message that can
// be added to the top of the untitled file contents when creating
// the temporary file.
const tmpFileWarning = [
`-- This file was auto generated on ${new Date()} by`,
`-- the \`build-oracle\` Atom plugin as the plugin requires a saved file to `,
`-- compile and report any compilation errors. `,
`-- `,
`-- This file will be discarded upon exit from Atom; If you want to keep the `,
`-- script you should save the file somewhere more practical.`
].join("\n");
let fileBuffer = atom.workspace.getActivePaneItem();
let isSavedFile = fileBuffer.buffer.file ? true : false;
if (!isSavedFile){
this._savedFile = false;
// this._configInstance.savedFile = false;
// the script being compiled is the last argument passed in to
// the execute build command.
const SQL_SCRIPT_INDEX = this.args.length-1;
let untitledFile = tmp.fileSync({"postfix": ".sql"});
_untitledFileDest = untitledFile.name;
fs.writeFileSync(untitledFile.fd, `${tmpFileWarning}\n\n${fileBuffer.buffer.cachedText}`);
// Set the build configuration to point to the created temporary file.
this.args[SQL_SCRIPT_INDEX] = untitledFile.name;
delete this.cwd;
}
}
// postBuild: After the build has completed, we want to restore the
// build configuration, to it's original state (which by default, compiles
// the currently active file and the current working directory to the
// path of the file).
postBuild(buildOutcome, stdOut, stdErr){
// If we operated on an untitled file, we need to restore
// the configuration
if (!this._savedFile) {
// the script being compiled is the last argument passed in to
// the execute build command.
const SQL_SCRIPT_INDEX = this.args.length-1;
this._savedFile = true;
this.args[SQL_SCRIPT_INDEX] = '{FILE_ACTIVE}';
this.cwd = '{FILE_ACTIVE_PATH}';
_untitledFileDest = undefined;
}
}
settings(){
this.settingsFetched = new Date();
var pkgDir = this.getPackagePath();
let sqlPath = this.getPackageSetting('sqlPath');
let nlsLang = this.getPackageSetting('nlsLang');
let dyldLibraryPath = this.getPackageSetting('dyldLibraryPath');
let enforceUtf8 = isWindows() ? this.getPackageSetting('enforceUtf8') : false;
//OS X (El Capitan) needs this env variable set in order to find
//the libraries to run SQL*Plus, and there is a security measure
//which prevents it being passed into Atom.
if (dyldLibraryPath){
process.env.DYLD_LIBRARY_PATH = dyldLibraryPath;
}
if (!process.env.NLS_LANG && nlsLang){
process.env.NLS_LANG = nlsLang;
}
//encforceUtf8: workaround for Windows is to set an environment variable
//that the JVM will pick up so that UTF-8 encodings will work.
//Only relevant for Windows.
//See: http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding
//https://github.com/tschf/atom-build-oracle/issues/29
if (enforceUtf8){
let encodingString = '-Dfile.encoding=UTF-8 ';
if (process.env.JAVA_OPTS) {
encodingString += process.env.JAVA_OPTS;
}
process.env.JAVA_OPTS = encodingString;
}
var parsedConfig = this.getFileJson(this.oracleBuildConfig);
var buildTargets = [];
for (var i = 0; i < parsedConfig.length; i++){
let currentTarget = parsedConfig[i];
let user = currentTarget.user;
let pass = currentTarget.password;
let host = currentTarget.host;
let port = currentTarget.port ? currentTarget.port.toString() : "1521";
let sid = currentTarget.sid;
let connectString = currentTarget.connectString;
let thisConfig = {};
if (!connectString){
connectString = `${user}/${pass}@//${host}:${port}/${sid}`;
}
thisConfig.name = 'Oracle Build::' + currentTarget.targetName;
let compileScript = path.join(pkgDir, 'lib', 'compileSimple.sql');
thisConfig.exec = sqlPath
thisConfig.args = [
"-L",
connectString,
'@' + compileScript,
'{FILE_ACTIVE}'
];
thisConfig.sh = false;
thisConfig.cwd = "{FILE_ACTIVE_PATH}";
thisConfig.functionMatch = this.doMatch;
thisConfig.preBuild = this.preBuild;
thisConfig.postBuild = this.postBuild;
// custom attributes not a part of the provider
// _savedFile: keep track if we are compiling on a saved file
// or not. This will allow us to act in the pre and post build
// changing the build config.
thisConfig._savedFile = true;
buildTargets.push(thisConfig);
}
return buildTargets;
}
doMatch(buildOutput){
const buildOutputLines = buildOutput.split(/\n/);
const errors = [];
for(let i = 0; i < buildOutputLines.length; i++){
const currentLine = buildOutputLines[i];
const oraErrorMatches = currentLine.match(/((PLS|ORA)-[\d]+:.+$)/g);
const dyldErrorMatches = currentLine.match(/dyld: Library not loaded:/g)
const currentFile = _untitledFileDest ? _untitledFileDest : atom.workspace.getActivePaneItem().buffer.file.path;
if (oraErrorMatches && oraErrorMatches.length > 0){
errors.push({
message: oraErrorMatches[0] + ' (Please review the build output for more detailed information)',
file: currentFile,
type: 'Error',
severity: 'error'
});
} else if (dyldErrorMatches && dyldErrorMatches.length > 0){
errors.push({
message: 'DYLD_LIBRARY_PATH is not set. Try setting in the build-oracle settings.',
file: currentFile,
type: 'Error',
severity: 'error'
});
}
}
return errors;
}
on(evt, evtCallback){
//As as 25.11.2015, `on` only provides the `refresh` evt
if (evt !== "refresh"){
return;
}
this.fsWatcher = fs.watch(this.oracleBuildConfig, () => {
var minTime = 1000;//1 second
//Make sure a reasonable time has passed before calling the
//refresh callback again
if (new Date() - this.settingsFetched > minTime){
//calling `refreshTargets` in `atom-build/lib/build.js`
evtCallback();
}
});
this.sqlPathObserver = atom.config.observe('build-oracle.sqlPath', (val) => {
//first call, assign the current setting value
if (!(this.sqlPath)){
this.sqlPath = val;
} else {
//only if its changed should we `refreshTargets`
if (this.sqlPath !== val){
this.sqlPath = val;
evtCallback();
}
}
});
this.nlsLangObserver = atom.config.observe('build-oracle.nlsLang', (val) => {
//first call, assign the current setting value
if (!(this.nlsLang)){
this.nlsLang = val;
} else {
//only if its changed should we `refreshTargets`
if (this.nlsLang !== val){
this.nlsLang = val;
evtCallback();
}
}
});
this.dyldLibraryPathObserver = atom.config.observe('build-oracle.dyldLibraryPath', (val) => {
//first call, assign the current setting value
if (!(this.dyldLibraryPath)){
this.dyldLibraryPath = val;
} else {
//only if its changed should we `refreshTargets`
if (this.dyldLibraryPath !== val){
this.dyldLibraryPath = val;
evtCallback();
}
}
});
this.enforceUtf8Observer = atom.config.observe('build-oracle.enforceUtf8', (val) => {
//first call assign the current settings val
if (!(this.hasOwnProperty('enforceUtf8'))){
this.enforceUtf8 = val;
} else {
//only if it changes should we `refreshTargets`
if (this.enforceUtf8 !== val) {
this.enforceUtf8 = val;
evtCallback();
}
}
});
}
off(){
//Called before a new refresh, so the existing fileWatch can be switched off
if (this.fsWatcher){
this.fsWatcher.close();
this.fsWatcher = undefined;
}
if (this.sqlPathObserver){
this.sqlPathObserver.dispose();
this.sqlPathObserver = undefined;
}
if (this.nlsLangObserver){
this.nlsLangObserver.dispose();
this.nlsLangObserver = undefined;
}
if (this.dyldLibraryPathObserver) {
this.dyldLibraryPathObserver.dispose();
this.dyldLibraryPathObserver = undefined;
}
if (this.enforceUtf8Observer) {
this.enforceUtf8Observer.dispose();
this.enforceUtf8Observer = undefined;
}
}
//Helper functions
getPluginName(){
return 'build-oracle';
}
getPackagePath(){
var pkgManager = atom.packages;
return pkgManager.resolvePackagePath(this.getPluginName());
}
getPackageSetting(propertyName){
var pluginName = this.getPluginName();
var settingValue = atom.config.get(`${pluginName}.${propertyName}`);
return settingValue;
}
getFileJson(path){
delete require.cache[path];
return require(path);
}
}
};
function isWindows(){
return /^win/.test(process.platform);
}
function isOsx(){
return /^darwin$/.test(process.platform);
}
export function activate(){
//Install package deps, see: https://github.com/steelbrain/package-deps
require('atom-package-deps').install('build-oracle');
};
function config(){
let buildConfig = {};
let settingIndex = 0;
buildConfig.sqlPath = {
title: 'Path to SQL interpreter',
type: 'string',
default: 'sqlplus',
description: 'Specify the path to SQL*Plus (or SQLcl). If its in your path, the basename is acceptable',
order: ++settingIndex
};
buildConfig.nlsLang = {
title: 'NLS_LANG',
type: 'string',
default: '.AL32UTF8',
description: 'Specify the NLS_LANG to ensure the correct encoding is used. Format is: [NLS_LANGUAGE]_[NLS_TERRITORY].[NLS_CHARACTERSET].',
order: ++settingIndex
};
if (isOsx()){
buildConfig.dyldLibraryPath = {
title: 'DYLD_LIBRARY_PATH',
type: 'string',
default: '',
description: 'Specify the DYLD_LIBRARY_PATH so SQL*Plus can run properly',
order: ++settingIndex
}
}
//SQLcl on Windows doesn't seem to support UTF-8 encoding. Need a work around
//in order to enable it.
if (isWindows()){
buildConfig.enforceUtf8 = {
title: 'Enforce UTF-8 encoding',
type: 'boolean',
default: true,
description: 'Enable UTF-8 encoding in the JVM (Used for SQLcl on Windows. Only works with sql.bat)',
order: ++settingIndex
}
}
return buildConfig;
}
module.exports.config = config();