-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathscript.js
More file actions
248 lines (219 loc) · 7.63 KB
/
script.js
File metadata and controls
248 lines (219 loc) · 7.63 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"use babel"
import { CompositeDisposable, Disposable } from "atom"
import CodeContextBuilder from "./code-context-builder"
import GrammarUtils from "./grammar-utils"
import Runner from "./runner"
import Runtime from "./runtime"
import ScriptOptions from "./script-options"
import ScriptOptionsView from "./script-options-view"
import ScriptProfileRunView from "./script-profile-run-view"
import ScriptView from "./script-view"
import ViewRuntimeObserver from "./view-runtime-observer"
export const config = {
enableExecTime: {
title: "Output the time it took to execute the script",
type: "boolean",
default: true,
},
escapeConsoleOutput: {
title: "HTML escape console output",
type: "boolean",
default: true,
},
ignoreSelection: {
title: "Ignore selection (file-based runs only)",
type: "boolean",
default: false,
},
scrollWithOutput: {
title: "Scroll with output",
type: "boolean",
default: true,
},
stopOnRerun: {
title: "Stop running process on rerun",
type: "boolean",
default: false,
},
cwdBehavior: {
title: "Default Current Working Directory (CWD) Behavior",
description: "If no Run Options are set, this setting decides how to determine the CWD",
type: "string",
default: "First project directory",
enum: ["First project directory", "Project directory of the script", "Directory of the script"],
},
position: {
title: "Panel position",
description: `Position of the panel with script output.
(Changes to this value will be applied upon reopening the panel.)`,
type: "string",
default: "bottom",
enum: ["top", "bottom", "left", "right"],
},
}
// For some reason, the text of these options does not show in package settings view
// default: 'firstProj'
// enum: [
// {value: 'firstProj', description: 'First project directory (if there is one)'}
// {value: 'scriptProj', description: 'Project directory of the script (if there is one)'}
// {value: 'scriptDir', description: 'Directory of the script'}
// ]
let scriptView = null
let scriptOptionsView = null
let scriptProfileRunView = null
let scriptOptions = null
let scriptProfiles = []
let runtime = null
const subscriptions = new CompositeDisposable()
let terminals = []
export function activate(state) {
scriptView = new ScriptView(state.scriptViewState)
scriptOptions = new ScriptOptions()
scriptOptionsView = new ScriptOptionsView(scriptOptions)
// profiles loading
scriptProfiles = []
if (state.profiles) {
for (const profile of state.profiles) {
const so = ScriptOptions.createFromOptions(profile.name, profile)
scriptProfiles.push(so)
}
}
scriptProfileRunView = new ScriptProfileRunView(scriptProfiles)
const codeContextBuilder = new CodeContextBuilder()
const runner = new Runner(scriptOptions)
const observer = new ViewRuntimeObserver(scriptView)
runtime = new Runtime(runner, codeContextBuilder, [observer], terminals)
subscriptions.add(
atom.commands.add("atom-workspace", {
"core:cancel": () => closeScriptViewAndStopRunner(),
"core:close": () => closeScriptViewAndStopRunner(),
"script:close-view": () => closeScriptViewAndStopRunner(),
"script:copy-run-results": () => scriptView.copyResults(),
"script:kill-process": () => runtime.stop(),
"script:run-by-line-number": () => runtime.execute("Line Number Based"),
"script:run": () => runtime.execute("Selection Based"),
})
)
// profile created
scriptOptionsView.onProfileSave((profileData) => {
// create and fill out profile
const profile = ScriptOptions.createFromOptions(profileData.name, profileData.options)
const codeContext = runtime.codeContextBuilder.buildCodeContext(
atom.workspace.getActiveTextEditor(),
"Selection Based"
)
profile.lang = codeContext.lang
// formatting description
const opts = profile.toObject()
let desc = `Language: ${codeContext.lang}`
if (opts.cmd) {
desc += `, Command: ${opts.cmd}`
}
if (opts.cmdArgs && opts.cmd) {
desc += ` ${opts.cmdArgs.join(" ")}`
}
profile.description = desc
scriptProfiles.push(profile)
scriptOptionsView.hide()
scriptProfileRunView.show()
scriptProfileRunView.setProfiles(scriptProfiles)
})
// profile deleted
scriptProfileRunView.onProfileDelete((profile) => {
const index = scriptProfiles.indexOf(profile)
if (index === -1) {
return
}
if (index !== -1) {
scriptProfiles.splice(index, 1)
}
scriptProfileRunView.setProfiles(scriptProfiles)
})
// profile renamed
scriptProfileRunView.onProfileChange((data) => {
const index = scriptProfiles.indexOf(data.profile)
if (index === -1 || !scriptProfiles[index][data.key]) {
return
}
scriptProfiles[index][data.key] = data.value
scriptProfileRunView.show()
scriptProfileRunView.setProfiles(scriptProfiles)
})
// profile renamed
return scriptProfileRunView.onProfileRun((profile) => {
if (!profile) {
return
}
runtime.execute("Selection Based", null, profile)
})
}
export function deactivate() {
runtime.destroy()
scriptView.removePanel()
scriptOptionsView.close()
scriptProfileRunView.close()
subscriptions.dispose()
terminals.length = 0
GrammarUtils.deleteTempFiles()
}
export function closeScriptViewAndStopRunner() {
runtime.stop()
scriptView.removePanel()
}
// Public
//
// Service method that provides the default runtime that's configurable through Atom editor
// Use this service if you want to directly show the script's output in the Atom editor
//
// **Do not destroy this {Runtime} instance!** By doing so you'll break this plugin!
//
// Also note that the Script package isn't activated until you actually try to use it.
// That's why this service won't be automatically consumed. To be sure you consume it
// you may need to manually activate the package:
//
// atom.packages.loadPackage('script').activateNow() # this code doesn't include error handling!
//
// see https://github.com/s1mplex/Atom-Script-Runtime-Consumer-Sample for a full example
export function provideDefaultRuntime() {
return runtime
}
// Public
//
// Service method that provides a blank runtime. You are free to configure any aspect of it:
// * Add observer (`runtime.addObserver(observer)`) - see {ViewRuntimeObserver} for an example
// * configure script options (`runtime.scriptOptions`)
//
// In contrast to `provideDefaultRuntime` you should dispose this {Runtime} when
// you no longer need it.
//
// Also note that the Script package isn't activated until you actually try to use it.
// That's why this service won't be automatically consumed. To be sure you consume it
// you may need to manually activate the package:
//
// atom.packages.loadPackage('script').activateNow() # this code doesn't include error handling!
//
// see https://github.com/s1mplex/Atom-Script-Runtime-Consumer-Sample for a full example
export function provideBlankRuntime() {
const runner = new Runner(new ScriptOptions())
const codeContextBuilder = new CodeContextBuilder()
return new Runtime(runner, codeContextBuilder, [], terminals)
}
export function consumeTerminal(terminal) {
terminals.push(terminal)
return new Disposable(() => {
terminals = terminals.filter((t) => t !== terminal)
})
}
export function serialize() {
// TODO: True serialization needs to take the options view into account
// and handle deserialization
const serializedProfiles = []
for (const profile of scriptProfiles) {
serializedProfiles.push(profile.toObject())
}
return {
scriptViewState: scriptView.serialize(),
scriptOptionsViewState: scriptOptionsView.serialize(),
profiles: serializedProfiles,
}
}