Skip to content

Commit

Permalink
fixed a bug which affected minification of file on OSX,
Browse files Browse the repository at this point in the history
optimized background operation in ST3,
added debug info to the console
  • Loading branch information
tssajo committed Oct 16, 2013
1 parent daff650 commit a54e31b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
51 changes: 36 additions & 15 deletions Minify.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,46 @@
import sublime, sublime_plugin, re, os, subprocess

# threading is only needed when sublime.set_timeout_async method is not available (in ST2)
HAS_ASYNC_TIMEOUT = bool(hasattr(sublime.__class__, 'set_timeout_async') and callable(getattr(sublime.__class__, 'set_timeout_async')))
if not HAS_ASYNC_TIMEOUT:
import threading
DEBUG = True

if DEBUG:
print('Sublime Platform: ' + str(sublime.platform()))
print('Sublime Version: ' + str(sublime.version()))

# find out this plugin's directory
if int(sublime.version()) >= 3000:
PLUGIN_DIR = os.path.dirname(__file__)
else:
PLUGIN_DIR = os.getcwd()
if DEBUG:
print('PLUGIN_DIR: ' + str(PLUGIN_DIR))

# Run the shell command in a separate thread. This is not needed in ST3 where sublime.set_timeout_async method is available
class RunCmdInOtherThread(threading.Thread):
# on Windows platform run the commands in shell
RUN_CMD_IN_SHELL = sublime.platform() == 'windows'
if DEBUG:
print('RUN_CMD_IN_SHELL: ' + str(RUN_CMD_IN_SHELL))

def __init__(self, cmdToRun):
self.cmdToRun = cmdToRun
self.result = 1
threading.Thread.__init__(self)
# if there is no sublime.set_timeout_async method available then run the commands in a separate thread using the threading module
HAS_ASYNC_TIMEOUT = callable(getattr(sublime, 'set_timeout_async', None))
if DEBUG:
print('HAS_ASYNC_TIMEOUT: ' + str(HAS_ASYNC_TIMEOUT))

def run(self):
self.result = subprocess.call(self.cmdToRun, shell=True)
if not HAS_ASYNC_TIMEOUT:
import threading

class RunCmdInOtherThread(threading.Thread):

def __init__(self, cmdToRun):
self.cmdToRun = cmdToRun
self.result = 1
threading.Thread.__init__(self)

def run(self):
self.result = subprocess.call(self.cmdToRun, shell=RUN_CMD_IN_SHELL)


class MinifyCommand(sublime_plugin.TextCommand):

def is_enabled(self):
# First, is this actually a file on the file system?
filename = self.view.file_name()
return bool(filename and (len(filename) > 0) and not (re.search('\.(?:css|js)$', filename) is None))

Expand Down Expand Up @@ -53,8 +68,11 @@ def doit(self):
cmdToRun = False
if cmdToRun:
print('Minifying file ' + str(inpfile))
if DEBUG:
print('Output file ' + str(outfile))
print('cmdToRun: ' + str(cmdToRun))
if HAS_ASYNC_TIMEOUT:
result = subprocess.call(cmdToRun, shell=True)
result = subprocess.call(cmdToRun, shell=RUN_CMD_IN_SHELL)
if not result:
sublime.active_window().open_file(outfile)
else:
Expand Down Expand Up @@ -93,8 +111,11 @@ def doit(self):
cmd = sublime.load_settings('Minify.sublime-settings').get('uglifyjs_command') or 'uglifyjs';
cmdToRun = [cmd, inpfile, '-b', '-o', outfile]
print('Beautifying file ' + str(inpfile))
if DEBUG:
print('Output file ' + str(outfile))
print('cmdToRun: ' + str(cmdToRun))
if HAS_ASYNC_TIMEOUT:
result = subprocess.call(cmdToRun, shell=True)
result = subprocess.call(cmdToRun, shell=RUN_CMD_IN_SHELL)
if not result:
sublime.active_window().open_file(outfile)
else:
Expand Down
3 changes: 2 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"install": "messages/install.txt",
"1.0.1": "messages/1.0.1.txt",
"1.0.2": "messages/1.0.2.txt",
"1.0.3": "messages/1.0.3.txt"
"1.0.3": "messages/1.0.3.txt",
"1.0.4": "messages/1.0.4.txt"
}
10 changes: 10 additions & 0 deletions messages/1.0.4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Version 1.0.4
-------------

* Fixed an issue which prevented the minification processes to run on the OSX platform
(The issue was caused by the fact that we must run uglifyjs in a system shell on Windows,
however runnning the same minification commands in a system shell simply won't work on OSX)
* Fixed an issue which prevented the sublime.set_timeout_async method to be used when available
(All minification commands still run in the background using python's threading module,
however it is unnecessary when the sublime.set_timeout_async method is provided, e.g: in ST3)
* Added some debug info to be displayed in the console

0 comments on commit a54e31b

Please sign in to comment.