-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathatom-python-run.js
More file actions
233 lines (211 loc) · 8.78 KB
/
atom-python-run.js
File metadata and controls
233 lines (211 loc) · 8.78 KB
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
'use strict';
module.exports = {
activate() {
const CompositeDisposable = require('atom').CompositeDisposable;
const path = require('path');
const terminal = require('./terminal');
this.subscriptions = new CompositeDisposable();
let disposables = [
atom.commands.add('atom-text-editor', 'Python run: run-f5', saveAndRun.bind(this, 'f5')),
atom.commands.add('atom-text-editor', 'Python run: run-f6', saveAndRun.bind(this, 'f6')),
];
function saveAndRun(key) {
try {
console.log(`${key} pressed`);
// save() returns a promise
let save = atom.workspace.getActiveTextEditor().save();
if (save) {
// Atom expects that promise to be resolved or rejected
return save.then(() => {
run({
'command': atom.config.get(`atom-python-run.${key}Command`),
'pause': atom.config.get(`atom-python-run.${key}Pause`),
});
});
} else { // editor.save() will be sync and returns null on atom v1.18 and older
run({
'command': atom.config.get(`atom-python-run.${key}Command`),
'pause': atom.config.get(`atom-python-run.${key}Pause`),
});
}
} catch(error) {
// the promise failed and the exception was caught
console.log(`atom-python-run: saveAndRun: ${error}`);
atom.notifications.addError(`atom-python-run: \`${error}\`.`);
// nothing to do, so we return undefined here
return;
}
}
function run(config) {
let shell, spawn, tty;
let editor = atom.workspace.getActiveTextEditor();
let info = path.parse(editor.buffer.file.path);
let args = config.command.split(' ');
// load configuration settings in to 'config' object
config = Object.assign(config, {
'terminal': atom.config.get('atom-python-run.terminal'),
'pipeFile': atom.config.get('atom-python-run.pipeFile'),
'pipePath': atom.config.get('atom-python-run.pipePath'),
'extensions': atom.config.get('atom-python-run.extensionFilter'),
'consoleLog': atom.config.get('atom-python-run.consoleLog'),
'f5envVariables': atom.config.get('atom-python-run.f5envVariables') // Environment Variables
});
let tmpEnv = {};
var tmpArr = []
// Extracting the env Variables from array format
for (var i = 0; i < config.f5envVariables.length; i++) {
if(config.f5envVariables[i].includes(":")){
tmpArr = config.f5envVariables[i].split(":")
if(tmpArr.length==2){
tmpEnv[tmpArr[0]] = tmpArr[1];
}
}
}
// console.log(tmpEnv);
function format(string, object) {
return string.replace(/{.*?}/g, (element) =>
object[element.substring(1, element.length - 1)]
);
}
args.forEach((item, index, arr) => {
arr[index] = format(item, {
'file': editor.buffer.file.path,
'dir': info.dir,
'name': info.name,
'ext': info.ext,
});
});
if (2 > args.length) {
atom.notifications.addError(`atom-python-run: \`FnKeyError\`: Invalid argument length for \`Fn-Key Command\`: requires \`repl [options] {file}\` formatting. Got \`${config.command}\` instead.`);
return;
}
if (config.extensions.length && !config.extensions.includes(info.ext)) {
atom.notifications.addError(`atom-python-run: \`ExtensionsError\`: \`extensions\` option has been enabled, but only \`${config.extensions}\` are allowed. Cowardly refusing to continue...`);
return;
}
if (config.pipeFile && ('' === config.pipePath || null == config.pipePath)) {
atom.notifications.addError(`atom-python-run: \`PipeError\`: \`pipe\` option has been enabled, but has no \`file\` to write to. Cowardly refusing to continue...`);
return;
}
if ('win32' === process.platform) {
shell = new terminal.Shell(__dirname + '\\..\\cp\\main.py');
} else {
shell = new terminal.Shell(__dirname + '/../cp/main.py');
}
spawn = new terminal.SpawnWrapper(shell.object);
// set up a user defined 'shell'
if (2 <= config.terminal.length) {
// requires a 'shell' name and at least one 'option'
if (config.terminal[0] != null && config.terminal[1] != null) {
spawn.object.shell = config.terminal[0];
// more than one option may be required depending on the system
spawn.object.option = [];
for (let option of config.terminal) {
if (option === config.terminal[0]) {
continue;
}
spawn.object.option.push(option);
}
}
}
tty = spawn.tty({
'pause': config.pause,
'pipeFile': config.pipeFile,
'pipePath': config.pipePath,
'log': config.consoleLog,
'args': args,
'options': {
cwd: info.dir,
detached: true,
env: tmpEnv // Passing Environment variables to the shell instance
}
});
try {
tty.on('error', function (error) {
atom.notifications.addError(`atom-python-run: \`TerminalError\`: There was a problem opening \`${spawn.object.shell}\`. If your computer doesn't have \`${spawn.object.shell}\` installed, you can set your own terminal on the package's Settings.`);
});
tty.unref();
} catch(e) {
atom.notifications.addError(`atom-python-run: \`ExceptionError\`: \`terminal\` refused to spawn a \`tty\` instance: \`${e}\``);
return;
}
}
this.subscriptions.add(...disposables);
},
deactivate() {
this.subscriptions.dispose();
},
config: {
consoleLog: {
title: 'Console Log',
description: 'Output log to `atom`s built-in console (`Slow`)',
type: 'boolean',
default: true
},
extensionFilter: {
title: 'Extension filter',
description: 'Leave it empty it accepts all files, or you can try `.py`',
type: 'array',
items: {
type: 'string',
},
default: []
},
f5Command: {
title: 'F5 Command',
description: '`{file}` = `{dir}/{name}{ext}`',
type: 'string',
default: 'python {file}'
},
/////// Env Variables option
f5envVariables: {
title: 'Environment Variables',
description: 'format [key:pair, key:pair ...]',
type: 'array',
default: [],
items:{
type: "string"
}
},
f6Command: {
title: 'F6 Command',
description: 'Same as above',
type: 'string',
default: 'python {file}'
},
f5Pause: {
title: 'Pause (F5)',
description: 'Show elapsed time and pause `cp`',
type: 'boolean',
default: true
},
f6Pause: {
title: 'Pause (F6)',
description: 'Show elapsed time and pause `cp`',
type: 'boolean',
default: true
},
pipeFile: {
title: 'Pipe to File',
description: 'Pipe `cp`s output to a file',
type: 'boolean',
default: false
},
pipePath: {
title: 'Pipe to file',
description: 'The `destination` or `file` to write to',
type: 'string',
default: ''
},
terminal: {
title: 'Terminal',
description: 'Leave it empty to use default `shell`, `option`; or you can try `terminator`, `-x`',
type: 'array',
items: {
type: 'string',
},
default: []
}
},
subscriptions: null
};