Skip to content

Commit

Permalink
Add the variable support: $/$/$
Browse files Browse the repository at this point in the history
  • Loading branch information
liuhewei committed Nov 13, 2015
1 parent e68c970 commit 653a6a7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Run any customized application from command palette. Just Ctrl/Command+Shift+P,
```

## Usage
Firstly, add applications through: "Tools" -> "Run Apps" -> "Add Application".
Firstly, add applications through: "Tools" -> "Run Apps" -> "Add Application".

Each application follows:
```
Expand All @@ -30,17 +30,18 @@ Each application follows:
"command": "runapp", // cannot be changed
"args":{
// application full path on Win/Linux, or only name on MAC
"app": "",
"app": "",
// argument list
"args": [""],
// variables can be use: $DIR$, $FILE$, $PROJ$
"args": [""],
// define what should follow the command:
// "dir" - file directory
// "file" - file name
// "proj" - project directory
// "none" - nothing
"type": ""
// "dir" - file directory, same as $DIR$
// "file" - file name, same as $FILE$
// "proj" - project directory, same as $PROJ$
// "none" - nothing: if args use variables, "type" must be "none"
"type": ""
}
}
```
Expand All @@ -54,8 +55,8 @@ Take "Git bash on windows" as an example, the original command is:
"caption": "Run: Git",
"command": "runapp",
"args":{
"app": "C:\\Windows\\system32\\wscript",
"args": ["D:\\Tools\\Git\\Git Bash.vbs"],
"app": "C:\\Windows\\system32\\wscript",
"args": ["D:\\Tools\\Git\\Git Bash.vbs"],
"type": "proj"
}
}
Expand Down
71 changes: 55 additions & 16 deletions runapp.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,65 @@
import os
# import sys
import sublime
import sublime_plugin

class RunappCommand(sublime_plugin.WindowCommand):
def run(self, app = "", args = [], type = ""):
filename = self.window.active_view().file_name()
# get the string of $FILE$
file_s = self.window.active_view().file_name()

# get the string of $DIR$
dir_s = os.path.split(file_s)[0]

# get the string of $PROJ$
data = sublime.active_window().project_data()
if data != None:
for folder in data['folders']:
proj_s = folder['path']
break

# handle the 'type'
if type == "file":
target = filename
target = '"'+file_s+'"'
elif type == "dir":
target = os.path.split(filename)[0]
target = '"'+dir_s+'"'
elif type == "proj":
data = sublime.active_window().project_data()
if data != None:
for folder in data['folders']:
target = folder['path']
break
if proj_s != None:
target = '"'+proj_s+'"'
else:
sublime.error_message('It\'s not a project yet. Please go to "Project->Save Project as..." firstly.')
return

# handle the embedded $var$
elif type == "none":
target = ""
for i in range(0,len(args)):
arg = args[i]
arg = arg.replace('$FILE$', '"'+file_s+'"')
arg = arg.replace('$DIR$', '"'+dir_s+'"')
if proj_s != None:
arg = arg.replace('$PROJ$', '"'+proj_s+'"')
args[i] = arg

else:
sublime.error_message('"type" must be one of "file", "dir", "proj", and "none".')

if target is None:
return

import subprocess
# invoke the application
# import subprocess
try:
# join to one string for os.popen
# ? subprocess.Popen can't work with msys_git 2.5.3
exec_s = ' '.join([app] + args + [target])
# print(exec_s)

if sublime.platform() == 'osx':
subprocess.Popen(['open', '-a', app] + args + [target])
# subprocess.Popen(['open', '-a', app] + args + [target])
os.popen('open -a ' + exec_s)
else:
subprocess.Popen([app] + args + [target])
# subprocess.Popen([app] + args + [target])
os.popen(exec_s)
except:
sublime.error_message('Unable to open current file with "' + app + '", check the Console.')

Expand All @@ -49,11 +76,23 @@ def run(self):
"caption": "Run: Git",
"command": "runapp",
"args":{
"app": "C:\\\\Windows\\\\system32\\\\wscript", // application name
"args": ["D:\\\\Tools\\\\Git\\\\Git Bash.vbs"], // application arguments
"type": "dir" // "dir", "proj", "file", "none"
"app": "D:\\\\Tools\\\\Git\\\\git-bash.exe",
"args": ["--cd=$DIR$"],
"type": "none"
}
}
},
{
"caption": "Run: Chrome",
"command": "runapp",
"args":{
"app": "C:\\\\Users\\\\lhw\\\\AppData\\\\Local\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe",
"args": [],
"type": "file"
}
},
]"""
open(cmdFile, 'w+', encoding='utf8', newline='').write(str(content))
sublime.active_window().open_file(cmdFile)
Expand Down

0 comments on commit 653a6a7

Please sign in to comment.