Created
August 23, 2010 15:08
-
-
Save bootleq/545665 to your computer and use it in GitHub Desktop.
[.vimrc] function to use closure-compiler @see http://bootleq.blogspot.com/2010/08/vim-funcion-closure-compiler-js.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command! -bang -nargs=* JsCompress call JsCompress(<bang>0, <f-args>) | |
" @param boolean save 0: save to temp file and return compressed content. | |
" 1: save with alternative filename, return the new name. | |
" @param boolean interact 1 to prompt before starting compression. | |
" @param string options extra options for running the compiler. | |
fun! JsCompress(save, ...) | |
let jar = '/scripts/google-compiler-20100616.jar' | |
let defaults = ' --compilation_level=SIMPLE_OPTIMIZATIONS' | |
\ . ' --warning_level=QUIET' | |
let input = expand('%') | |
let interact = a:0 > 0 ? a:1 : 0 | |
let options = a:0 > 1 ? a:2 : '' | |
if &modified | |
echomsg 'No write since last change, write before compression? (y/n) ' | |
let ans = getchar() | |
if nr2char(ans) == 'y' | w | |
elseif nr2char(ans) != 'n' | redraw! | echomsg 'Compression aborted.' | return | |
endif | |
endif | |
if ! executable(jar) | |
echohl WarningMsg | echoerr "Can't execute java jar." | echohl None | |
return | |
endif | |
if ! a:save | |
let output = tempname() | |
elseif match(input, '-debug\.js$') > 0 | |
let output = substitute(input, '-debug\.js$', '\.js', '') | |
else | |
let output = substitute(input, '\.js$', '\.min\.js', '') | |
endif | |
if &filetype == 'javascript' | |
if executable('cygpath') | |
let cmd = 'java -jar `cygpath -wp ' . jar . '`' . defaults | |
\ . ' ' . options | |
\ . ' --js=`cygpath -wp ' . input . '`' | |
\ . ' --js_output_file=`cygpath -wp ' . output . '`' | |
else | |
let cmd = 'java -jar ' . jar . defaults . ' ' .options . ' --js=' . input . ' --js_output_file=' . output | |
endif | |
let cmd .= " 2>&1 \\\\| sed '/^$/d'" | |
if interact | |
echomsg 'Compress ' . input . ' to ' . output . '? (y/n) ' | |
let yes = getchar() | |
if nr2char(yes) == 'y' | |
return DoJsCompress(cmd, output, a:save) | |
else | |
redraw! | |
echomsg 'Compression aborted.' | |
return | |
endif | |
else | |
return DoJsCompress(cmd, output, a:save) | |
endif | |
else | |
echohl WarningMsg | echomsg "Unsupported filetype." | echohl None | |
endif | |
endf | |
fun! DoJsCompress(cmd, file, save) | |
let makeprg_orig = &makeprg | |
exec "set makeprg=" . escape(a:cmd, ' \"') | |
silent make | |
let &makeprg = makeprg_orig | |
if a:save | |
let ret = a:file | |
else | |
let ret = join(readfile(a:file), "\n") | |
call delete(a:file) | |
endif | |
if len(getqflist()) == 0 | |
redraw! | |
echomsg "Compression completed." (a:save ? '' : '(result written to temporarily file.)') | |
return ret | |
else | |
return 0 | |
endif | |
endf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment