Skip to content

Commit 91b0b47

Browse files
committed
Close atom-community#152 - Add respect shebang
1 parent b9ba9b9 commit 91b0b47

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

examples/version_info.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python2.7
2+
import sys
3+
4+
print(sys.version_info)

lib/code-context.coffee

+18
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,21 @@ class CodeContext
4040
newlineCount = Number(@lineNumber)
4141
newlines = Array(newlineCount).join("\n")
4242
"#{newlines}#{code}"
43+
44+
shebangCommand: ->
45+
code = @textSource?.getText()
46+
return unless code
47+
48+
lines = code.split("\n")
49+
firstLine = lines[0]
50+
return unless firstLine.match(/^#!/)
51+
firstLine = firstLine.replace(/^#! /, '#!')
52+
paths = firstLine.split(' ')
53+
parts = paths[0]?.split('/')
54+
55+
if parts? and parts.length > 1
56+
script = parts[parts.length-1]
57+
else
58+
script = parts.first.sub('#!', '')
59+
60+
script = if script == 'env' then paths[paths.length-1] else script

lib/script-view.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class ScriptView extends View
158158
try
159159
if not @runOptions.cmd? or @runOptions.cmd is ''
160160
# Precondition: lang? and lang of grammarMap
161-
commandContext.command = grammarMap[codeContext.lang][codeContext.argType].command
161+
commandContext.command = codeContext.shebangCommand() or grammarMap[codeContext.lang][codeContext.argType].command
162162
else
163163
commandContext.command = @runOptions.cmd
164164

spec/code-context-spec.coffee

+26
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,29 @@ describe 'CodeContext', ->
4646
code = @codeContext.getCode()
4747
expect(typeof code).toEqual('string')
4848
expect(code).toMatch("print 'hello world!'")
49+
50+
describe 'shebangCommand', ->
51+
it 'returns undefined when no code is available', ->
52+
expect(@codeContext.shebangCommand()).toBe(undefined)
53+
54+
it 'returns undefined when no shebang is found', ->
55+
@codeContext.textSource = @dummyTextSource
56+
expect(@codeContext.shebangCommand()).toBe(undefined)
57+
58+
it 'returns the command name when a shebang is found', ->
59+
@codeContext.textSource = @dummyTextSource
60+
@codeContext.textSource.getText = ->
61+
"#!/bin/bash\necho 'hello from bash!'"
62+
expect(@codeContext.shebangCommand()).toMatch('bash')
63+
64+
@codeContext.textSource.getText = ->
65+
"#!/usr/bin/env ruby\nputs 'hello from ruby!'"
66+
expect(@codeContext.shebangCommand()).toMatch('ruby')
67+
68+
@codeContext.textSource.getText = ->
69+
"#!/usr/bin/python2.7\nprint 'hello from python!'"
70+
expect(@codeContext.shebangCommand()).toMatch('python2.7')
71+
72+
@codeContext.textSource.getText = ->
73+
"#!/usr/bin/python3\nprint 'hello from python3!'"
74+
expect(@codeContext.shebangCommand()).toMatch('python3')

0 commit comments

Comments
 (0)