Skip to content

Commit 202b6a3

Browse files
authored
Improve support for Windows (#1451)
1 parent ce003f5 commit 202b6a3

File tree

8 files changed

+140
-104
lines changed

8 files changed

+140
-104
lines changed

lib/grammar-utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ export default {
5050
}
5151
},
5252

53+
// Public: Returns cmd or bash, depending on the current OS
54+
command: os.platform() === 'win32' ? 'cmd' : 'bash',
55+
56+
// Public: Format args for cmd or bash, depending on the current OS
57+
formatArgs(command) {
58+
if (os.platform() === 'win32') {
59+
return [`/c ${command.replace(/['"]/g, '')}`];
60+
}
61+
return ['-c', command];
62+
},
63+
5364
/* eslint-disable global-require */
5465
// Public: Get the Java helper object
5566
//

lib/grammars/c.coffee

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
path = require 'path'
2-
{OperatingSystem} = GrammarUtils = require '../grammar-utils'
2+
{OperatingSystem, command} = GrammarUtils = require '../grammar-utils'
33

44
os = OperatingSystem.platform()
55
windows = OperatingSystem.isWindows()
66

7+
options = '-Wall -include stdio.h'
8+
9+
args = ({filepath}) ->
10+
args = switch os
11+
when 'darwin'
12+
"xcrun clang #{options} -fcolor-diagnostics '#{filepath}' -o /tmp/c.out && /tmp/c.out"
13+
when 'linux'
14+
"cc #{options} '#{filepath}' -o /tmp/c.out && /tmp/c.out"
15+
return ['-c', args]
16+
717
exports.C =
818
'File Based':
919
command: 'bash'
1020
args: ({filepath}) ->
1121
args = switch os
1222
when 'darwin'
13-
"xcrun clang -fcolor-diagnostics -Wall -include stdio.h '#{filepath}' -o /tmp/c.out && /tmp/c.out"
23+
"xcrun clang #{options} -fcolor-diagnostics '#{filepath}' -o /tmp/c.out && /tmp/c.out"
1424
when 'linux'
15-
"cc -Wall -include stdio.h '#{filepath}' -o /tmp/c.out && /tmp/c.out"
25+
"cc #{options} '#{filepath}' -o /tmp/c.out && /tmp/c.out"
1626
return ['-c', args]
1727

1828
'Selection Based':
@@ -22,30 +32,30 @@ exports.C =
2232
tmpFile = GrammarUtils.createTempFileWithCode(code, '.c')
2333
args = switch os
2434
when 'darwin'
25-
"xcrun clang -fcolor-diagnostics -Wall -include stdio.h #{tmpFile} -o /tmp/c.out && /tmp/c.out"
35+
"xcrun clang #{options} -fcolor-diagnostics #{tmpFile} -o /tmp/c.out && /tmp/c.out"
2636
when 'linux'
27-
"cc -Wall -include stdio.h #{tmpFile} -o /tmp/c.out && /tmp/c.out"
37+
"cc #{options} #{tmpFile} -o /tmp/c.out && /tmp/c.out"
2838
return ['-c', args]
2939

3040
exports['C#'] =
31-
'Selection Based':
32-
command: if windows then 'cmd' else 'bash'
41+
'Selection Based': {
42+
command
3343
args: (context) ->
3444
code = context.getCode()
3545
tmpFile = GrammarUtils.createTempFileWithCode(code, '.cs')
36-
progname = tmpFile.replace /\.cs$/, ''
46+
exe = tmpFile.replace /\.cs$/, '.exe'
3747
if windows
38-
return ["/c csc /out:#{progname}.exe #{tmpFile} && #{progname}.exe"]
39-
else ['-c', "csc /out:#{progname}.exe #{tmpFile} && mono #{progname}.exe"]
40-
41-
'File Based':
42-
command: if windows then 'cmd' else 'bash'
48+
return ["/c csc /out:#{exe} #{tmpFile} && #{exe}"]
49+
else ['-c', "csc /out:#{exe} #{tmpFile} && mono #{exe}"]
50+
}
51+
'File Based': {
52+
command
4353
args: ({filepath, filename}) ->
44-
progname = filename.replace /\.cs$/, ''
54+
exe = filename.replace /\.cs$/, '.exe'
4555
if windows
46-
return ["/c csc #{filepath} && #{progname}.exe"]
47-
else ['-c', "csc '#{filepath}' && mono #{progname}.exe"]
48-
56+
return ["/c csc #{filepath} && #{exe}"]
57+
else ['-c', "csc '#{filepath}' && mono #{exe}"]
58+
}
4959
exports['C# Script File'] =
5060

5161
'Selection Based':
@@ -66,35 +76,34 @@ exports['C++'] =
6676
tmpFile = GrammarUtils.createTempFileWithCode(code, '.cpp')
6777
args = switch os
6878
when 'darwin'
69-
"xcrun clang++ -fcolor-diagnostics -std=c++14 -Wall -include stdio.h -include iostream #{tmpFile} -o /tmp/cpp.out && /tmp/cpp.out"
79+
"xcrun clang++ -std=c++14 #{options} -fcolor-diagnostics -include iostream #{tmpFile} -o /tmp/cpp.out && /tmp/cpp.out"
7080
when 'linux'
71-
"g++ -std=c++14 -Wall -include stdio.h -include iostream #{tmpFile} -o /tmp/cpp.out && /tmp/cpp.out"
81+
"g++ #{options} -std=c++14 -include iostream #{tmpFile} -o /tmp/cpp.out && /tmp/cpp.out"
7282
return ['-c', args]
7383

74-
'File Based':
75-
command: 'bash'
84+
'File Based': {
85+
command
7686
args: ({filepath}) ->
7787
args = switch os
7888
when 'darwin'
79-
"xcrun clang++ -fcolor-diagnostics -std=c++14 -Wall -include stdio.h -include iostream '#{filepath}' -o /tmp/cpp.out && /tmp/cpp.out"
89+
"xcrun clang++ -std=c++14 #{options} -fcolor-diagnostics -include iostream '#{filepath}' -o /tmp/cpp.out && /tmp/cpp.out"
8090
when 'linux'
81-
"g++ -std=c++14 -Wall -include stdio.h -include iostream '#{filepath}' -o /tmp/cpp.out && /tmp/cpp.out"
91+
"g++ -std=c++14 #{options} -include iostream '#{filepath}' -o /tmp/cpp.out && /tmp/cpp.out"
8292
when 'win32'
8393
if GrammarUtils.OperatingSystem.release().split('.').slice -1 >= '14399'
8494
filepath = path.posix.join.apply(path.posix, [].concat([filepath.split(path.win32.sep)[0].toLowerCase()], filepath.split(path.win32.sep).slice(1))).replace(':', '')
85-
"g++ -std=c++14 -Wall -include stdio.h -include iostream '/mnt/#{filepath}' -o /tmp/cpp.out && /tmp/cpp.out"
86-
return ['-c', args]
87-
95+
"g++ -std=c++14 #{options} -include iostream /mnt/#{filepath} -o /tmp/cpp.out && /tmp/cpp.out"
96+
return GrammarUtils.formatArgs(args)
97+
}
8898
exports['C++14'] = exports['C++']
8999

90-
exports['Objective-C'] =
91-
if GrammarUtils.OperatingSystem.isDarwin()
100+
if os is 'darwin'
101+
exports['Objective-C'] =
92102
'File Based':
93103
command: 'bash'
94-
args: ({filepath}) -> ['-c', "xcrun clang -fcolor-diagnostics -Wall -include stdio.h -framework Cocoa '#{filepath}' -o /tmp/objc-c.out && /tmp/objc-c.out"]
104+
args: ({filepath}) -> ['-c', "xcrun clang #{options} -fcolor-diagnostics -framework Cocoa '#{filepath}' -o /tmp/objc-c.out && /tmp/objc-c.out"]
95105

96-
exports['Objective-C++'] =
97-
if GrammarUtils.OperatingSystem.isDarwin()
98-
'File Based':
99-
command: 'bash'
100-
args: ({filepath}) -> ['-c', "xcrun clang++ -fcolor-diagnostics -Wc++11-extensions -Wall -include stdio.h -include iostream -framework Cocoa '#{filepath}' -o /tmp/objc-cpp.out && /tmp/objc-cpp.out"]
106+
exports['Objective-C++'] =
107+
'File Based':
108+
command: 'bash'
109+
args: ({filepath}) -> ['-c', "xcrun clang++ -Wc++11-extensions #{options} -fcolor-diagnostics -include iostream -framework Cocoa '#{filepath}' -o /tmp/objc-cpp.out && /tmp/objc-cpp.out"]

lib/grammars/coffeescript.coffee

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
path = require 'path'
2-
GrammarUtils = require '../grammar-utils'
2+
{command} = GrammarUtils = require '../grammar-utils'
33

44
bin = path.join __dirname, '../..', 'node_modules', '.bin'
55
coffee = path.join bin, 'coffee'
66
babel = path.join bin, 'babel'
77

8-
args = ({filepath}) -> ['-c', "'#{coffee}' -p '#{filepath}'|'#{babel}' --filename '#{bin}'| node"]
8+
args = ({filepath}) ->
9+
cmd = "'#{coffee}' -p '#{filepath}'|'#{babel}' --filename '#{bin}'| node"
10+
return GrammarUtils.formatArgs(cmd)
911

1012
exports.CoffeeScript =
11-
'Selection Based':
12-
command: 'bash'
13+
'Selection Based': {
14+
command
1315
args: (context) ->
1416
{scopeName} = atom.workspace.getActiveTextEditor()?.getGrammar()
1517
lit = if scopeName?.includes 'lit' then 'lit' else ''
1618
code = context.getCode()
1719
filepath = GrammarUtils.createTempFileWithCode(code, ".#{lit}coffee")
1820
return args({filepath})
19-
20-
'File Based': { command: 'bash', args }
21+
}
22+
'File Based': { command, args }
2123

2224
exports['CoffeeScript (Literate)'] = exports.CoffeeScript
2325

lib/grammars/fortran.coffee

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
exports['Fortran - Fixed Form'] =
2-
'File Based':
3-
command: 'bash'
4-
args: ({filepath}) -> ['-c', "gfortran '#{filepath}' -ffixed-form -o /tmp/f.out && /tmp/f.out"]
1+
path = require 'path'
2+
{command} = GrammarUtils = require '../grammar-utils'
53

4+
exports['Fortran - Fixed Form'] =
5+
'File Based': {
6+
command
7+
args: ({filepath}) ->
8+
cmd = "gfortran '#{filepath}' -ffixed-form -o /tmp/f.out && /tmp/f.out"
9+
return GrammarUtils.formatArgs(cmd)
10+
}
611
exports['Fortran - Free Form'] =
7-
'File Based':
8-
command: 'bash'
9-
args: ({filepath}) -> ['-c', "gfortran '#{filepath}' -ffree-form -o /tmp/f90.out && /tmp/f90.out"]
10-
12+
'File Based': {
13+
command
14+
args: ({filepath}) ->
15+
cmd = "gfortran '#{filepath}' -ffree-form -o /tmp/f90.out && /tmp/f90.out"
16+
return GrammarUtils.formatArgs(cmd)
17+
}
1118
exports['Fortran - Modern'] = exports['Fortran - Free Form']
1219
exports['Fortran - Punchcard'] = exports['Fortran - Fixed Form']

lib/grammars/index.coffee

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# As well as any special setup for arguments.
33

44
path = require 'path'
5-
{OperatingSystem} = GrammarUtils = require '../grammar-utils'
5+
{OperatingSystem, command} = GrammarUtils = require '../grammar-utils'
66

77
os = OperatingSystem.platform()
88
windows = OperatingSystem.isWindows()
@@ -162,6 +162,7 @@ module.exports =
162162
'Selection Based':
163163
command: 'bash'
164164
args: (context) -> ['-c', context.getCode()]
165+
165166
'File Based':
166167
command: 'make'
167168
args: ({filepath}) -> ['-f', filepath]
@@ -172,10 +173,10 @@ module.exports =
172173
args: (context) ->
173174
code = context.getCode()
174175
tmpFile = GrammarUtils.MATLAB.createTempFileWithCode(code)
175-
return ['-nodesktop', '-nosplash', '-r', "try, run('#{tmpFile}');while ~isempty(get(0,'Children')); pause(0.5); end; catch ME; disp(ME.message); exit(1); end; exit(0);"]
176+
return ['-nodesktop', '-nosplash', '-r', "try, run('#{tmpFile}'); while ~isempty(get(0,'Children')); pause(0.5); end; catch ME; disp(ME.message); exit(1); end; exit(0);"]
176177
'File Based':
177178
command: 'matlab'
178-
args: ({filepath}) -> ['-nodesktop', '-nosplash', '-r', "try run('#{filepath}');while ~isempty(get(0,'Children')); pause(0.5); end; catch ME; disp(ME.message); exit(1); end; exit(0);"]
179+
args: ({filepath}) -> ['-nodesktop', '-nosplash', '-r', "try run('#{filepath}'); while ~isempty(get(0,'Children')); pause(0.5); end; catch ME; disp(ME.message); exit(1); end; exit(0);"]
179180

180181
'MIPS Assembler':
181182
'File Based':
@@ -194,13 +195,14 @@ module.exports =
194195
args: ({filepath}) -> [filepath]
195196

196197
Nim:
197-
'File Based':
198-
command: 'bash'
198+
'File Based': {
199+
command
199200
args: ({filepath}) ->
200201
file = GrammarUtils.Nim.findNimProjectFile(filepath)
201-
path = GrammarUtils.Nim.projectDir(filepath)
202-
return ['-c', "cd '#{path}' && nim c --hints:off --parallelBuild:1 -r '#{file}' 2>&1"]
203-
202+
dir = GrammarUtils.Nim.projectDir(filepath)
203+
commands = "cd '#{dir}' && nim c --hints:off --parallelBuild:1 -r '#{file}' 2>&1"
204+
return GrammarUtils.formatArgs(commands)
205+
}
204206
NSIS:
205207
'Selection Based':
206208
command: 'makensis'
@@ -215,10 +217,12 @@ module.exports =
215217
Octave:
216218
'Selection Based':
217219
command: 'octave'
218-
args: (context) -> ['-p', context.filepath.replace(/[^\/]*$/, ''), '--eval', context.getCode()]
220+
args: (context) ->
221+
dir = path.dirname(context.filepath)
222+
return ['-p', path.dirname(context.filepath), '--eval', context.getCode()]
219223
'File Based':
220224
command: 'octave'
221-
args: ({filepath}) -> ['-p', filepath.replace(/[^\/]*$/, ''), filepath]
225+
args: ({filepath}) -> ['-p', path.dirname(filepath), filepath]
222226

223227
Oz:
224228
'Selection Based':
@@ -243,18 +247,20 @@ module.exports =
243247
args: ({filepath}) -> [filepath]
244248

245249
Prolog:
246-
'File Based':
247-
command: 'bash'
248-
args: ({filepath}) -> ['-c', "cd '" + filepath.replace(/[^\/]*$/, '') + "'; swipl -f '#{filepath}' -t main --quiet"]
249-
250+
'File Based': {
251+
command
252+
args: ({filepath}) ->
253+
dir = path.dirname(filepath)
254+
commands = "cd '#{dir}'; swipl -f '#{filepath}' -t main --quiet"
255+
return GrammarUtils.formatArgs(commands)
256+
}
250257
PureScript:
251-
'File Based':
252-
command: if windows then 'cmd' else 'bash'
258+
'File Based': {
259+
command
253260
args: ({filepath}) ->
254-
filepath = filepath.replace(/[^\/]*$/, '')
255-
command = "cd '#{filepath}' && pulp run"
256-
if windows then ["/c #{command}"] else ['-c', command]
257-
261+
dir = path.dirname(filepath)
262+
return GrammarUtils.formatArgs("cd '#{dir}' && pulp run")
263+
}
258264
R:
259265
'Selection Based':
260266
command: 'Rscript'
@@ -285,14 +291,13 @@ module.exports =
285291
args: ({filepath}) -> [filepath]
286292

287293
Rust:
288-
'File Based':
289-
command: if windows then 'cmd' else 'bash'
294+
'File Based': {
295+
command
290296
args: ({filepath, filename}) ->
291-
progname = filename.replace /\.rs$/, ''
292297
if windows
293-
return ["/c rustc #{filepath} && #{progname}.exe"]
298+
return ["/c rustc #{filepath} && #{filename[..-4]}.exe"]
294299
else ['-c', "rustc '#{filepath}' -o /tmp/rs.out && /tmp/rs.out"]
295-
300+
}
296301
Scala:
297302
'Selection Based':
298303
command: 'scala'

lib/grammars/java.coffee

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
GrammarUtils = require '../grammar-utils'
1+
path = require 'path'
2+
{command} = GrammarUtils = require '../grammar-utils'
23

34
windows = GrammarUtils.OperatingSystem.isWindows()
45

6+
args = (filepath, jar) ->
7+
jar = (jar ? path.basename(filepath)).replace /\.kt$/, '.jar'
8+
cmd = "kotlinc '#{filepath}' -include-runtime -d #{jar} && java -jar #{jar}"
9+
return GrammarUtils.formatArgs(cmd)
10+
511
module.exports =
612

713
Java:
814
'File Based':
9-
command: if windows then 'cmd' else 'bash'
15+
command: 'bash'
1016
args: (context) ->
1117
className = GrammarUtils.Java.getClassName context
1218
classPackages = GrammarUtils.Java.getClassPackage context
1319
sourcePath = GrammarUtils.Java.getProjectPath context
1420
if windows
15-
return ["/c javac -Xlint '#{context.filename}' && java #{className}"]
16-
else ['-c', "javac -sourcepath #{sourcePath} -d /tmp '#{context.filepath}' && java -cp /tmp #{classPackages}#{className}"]
21+
return ["/c javac -Xlint #{context.filename} && java #{className}"]
22+
else ['-c', "javac -sourcepath '#{sourcePath}' -d /tmp '#{context.filepath}' && java -cp /tmp #{classPackages}#{className}"]
1723

1824
Kotlin:
19-
'Selection Based':
20-
command: 'bash'
25+
'Selection Based': {
26+
command
2127
args: (context) ->
2228
code = context.getCode()
2329
tmpFile = GrammarUtils.createTempFileWithCode(code, '.kt')
24-
jarName = tmpFile.replace /\.kt$/, '.jar'
25-
return ['-c', "kotlinc #{tmpFile} -include-runtime -d #{jarName} && java -jar #{jarName}"]
26-
27-
'File Based':
28-
command: 'bash'
29-
args: ({filepath, filename}) ->
30-
jarName = filename.replace /\.kt$/, '.jar'
31-
return ['-c', "kotlinc '#{filepath}' -include-runtime -d /tmp/#{jarName} && java -jar /tmp/#{jarName}"]
32-
30+
return args(tmpFile)
31+
}
32+
'File Based': {
33+
command
34+
args: ({filepath, filename}) -> args(filepath, "/tmp/#{filename}")
35+
}
3336
Processing:
3437
'File Based':
35-
command: if windows then 'cmd' else 'bash'
36-
args: ({filepath, filename}) ->
37-
if windows
38-
return ['/c processing-java --sketch=' + filepath.replace("\\#{filename}", '') + ' --run']
39-
else ['-c', 'processing-java --sketch=' + filepath.replace("/#{filename}", '') + ' --run']
38+
command: 'processing-java'
39+
args: ({filepath}) -> ["--sketch='#{path.dirname(filepath)}'", '--run']

0 commit comments

Comments
 (0)