Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add per project settings.
  • Loading branch information
vaanwd committed Feb 25, 2013
1 parent 40c2719 commit cdefa90
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 9 deletions.
31 changes: 30 additions & 1 deletion README.md
Expand Up @@ -34,11 +34,40 @@ Go to your Sublime Text 2 `Packages` directory and clone the repository using th
* Unzip the files and rename the folder to `Drush`
* Copy the folder to your Sublime Text 2 `Packages` directory

## Usage

Sublime Drush is a simple plugin that allows execution of drush commands from Sublime Text Editor.

To use, you must enable input console with `alt` + `shift` + `d` (Win / Linux) or `shift` + `super` + `d` (Mac).

From the Sublime menu `Preferences`->`Package Settings`->`Drush`->`Settings - User` you can set the path to drush utility and other arguments.

For example you can set answer `"yes"` to all questions required by drush adding:

"drush_args": "--yes",

[© 2012 Vaan Web Design](http://www.vaanwebdesign.ro)

You can also set the arguments used by drush for each Sublime project accesed from Sublime menu `Project`->`Edit Project`.

An example for **PROJECT.sublime-project**:

{
"folders":
[
{
"path": "/E/UwAmp/www/drupal7"
}
],
"settings":
{
"Drush":
{
"drush_args": "--uri=http://d7.com"
}
}
}

**NOTE:** You should avoid introducing identical parameters in **Drush.sublime-settings** file and **PROJECT.sublime-project**
to avoid incorrect operations.

[© 2012 Vaan Web Design](http://www.vaanwebdesign.ro)
91 changes: 83 additions & 8 deletions drush.py
@@ -1,6 +1,7 @@
import platform
import sublime
import sublime_plugin
import re
import os

os.environ["TERM"] = "dumb"
Expand All @@ -11,7 +12,6 @@ class DrushCommand(sublime_plugin.TextCommand):
path = ''
folders = []
DEFAULT_DRUSH_EXECUTABLE = 'drush.bat' if PLATFORM_IS_WINDOWS else 'drush'
DEFAULT_DRUSH_ARGS = ''
SETTINGS = sublime.load_settings("Drush.sublime-settings")
view_panel = None
last_cmd = ''
Expand Down Expand Up @@ -42,14 +42,32 @@ def on_change(self, text):

def _runDrush(self, text):
try:
drush = str(self._get_drush_executable().strip())
drush_args = str(self._get_drush_sup_args().strip())
root = ''
uri = ''
command_splitted = ''
import shlex
if drush_args != '':
command_splitted = [drush] + [drush_args] + shlex.split(str(text)) + [str('--root=' + self.path)]
drush = str(self._get_drush_executable().strip())
drush_args = str(self._get_drush_sup_args().strip()).replace('\\', '/')
drush_project_args = str(self._get_drush_sup_project_args().strip()).replace('\\', '/')

if "--root=" in drush_args:
root = ''
elif "--root=" in drush_project_args:
root = ''
elif "--root=" in text:
root = ''
else:
command_splitted = [drush] + shlex.split(str(text)) + [str('--root=' + self.path)]
root = str('--root=' + self.path).replace('\\', '/')

if "--uri=" in drush_args:
uri = ''
elif "--uri=" in drush_project_args:
uri = ''
elif "--uri=" in text:
uri = ''

import shlex
command_splitted = shlex.split(str(drush + ' ' + drush_args + ' ' + drush_project_args + ' ' + text.replace('\\', '/') + ' ') + root + uri)

sublime.active_window().run_command('exec', {'cmd': command_splitted, "working_dir": self.path})
except OSError as e:
error_message = 'Drush error: '
Expand All @@ -63,5 +81,62 @@ def _runDrush(self, text):
def _get_drush_executable(self):
return self.SETTINGS.get('drush_executable') or self.DEFAULT_DRUSH_EXECUTABLE

def getDirectories(self):
return sublime.active_window().folders()

# Credit to titoBouzout - https://github.com/titoBouzout/SideBarEnhancements
def getProjectFile(self):
if not self.getDirectories():
return None
import json
data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', 'Settings', 'Session.sublime_session')), 'r').read()
data = data.replace('\t', ' ')
data = json.loads(data, strict=False)
projects = data['workspaces']['recent_workspaces']

if os.path.lexists(os.path.join(sublime.packages_path(), '..', 'Settings', 'Auto Save Session.sublime_session')):
data = file(os.path.normpath(os.path.join(sublime.packages_path(), '..', 'Settings', 'Auto Save Session.sublime_session')), 'r').read()
data = data.replace('\t', ' ')
data = json.loads(data, strict=False)
if 'workspaces' in data and 'recent_workspaces' in data['workspaces'] and data['workspaces']['recent_workspaces']:
projects += data['workspaces']['recent_workspaces']
projects = list(set(projects))
for project_file in projects:
project_file = re.sub(r'^/([^/])/', '\\1:/', project_file);
project_json = json.loads(file(project_file, 'r').read(), strict=False)
if 'folders' in project_json:
folders = project_json['folders']
found_all = True
for directory in self.getDirectories():
found = False
for folder in folders:
folder_path = re.sub(r'^/([^/])/', '\\1:/', folder['path']);
if folder_path == directory.replace('\\', '/'):
found = True
break;
if found == False:
found_all = False
break;
if found_all:
return project_file
return None

def hasOpenedProject(self):
return self.getProjectFile() != None

def getProjectJson(self):
if not self.hasOpenedProject():
return None
import json
return json.loads(file(self.getProjectFile(), 'r').read(), strict=False)

def _get_drush_sup_args(self):
return self.SETTINGS.get('drush_args') or self.DEFAULT_DRUSH_ARGS
return self.SETTINGS.get('drush_args')

def _get_drush_sup_project_args(self):
p_drush_args = self.getProjectJson()
if 'settings' in p_drush_args:
if 'Drush' in p_drush_args['settings']:
if 'drush_args' in p_drush_args['settings']['Drush']:
return p_drush_args['settings']['Drush']['drush_args']
return ''

0 comments on commit cdefa90

Please sign in to comment.