Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: af7057cb2a1e
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: f2324db6bf9b
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Mar 23, 2014

  1. Fix detection of uglify-js / gzip

    marcandre authored and elia committed Mar 23, 2014
    Copy the full SHA
    23d3d95 View commit details
  2. Copy the full SHA
    f2324db View commit details
Showing with 54 additions and 23 deletions.
  1. +54 −23 lib/opal/util.rb
77 changes: 54 additions & 23 deletions lib/opal/util.rb
Original file line number Diff line number Diff line change
@@ -2,36 +2,67 @@ module Opal
module Util
extend self

def null
if (/mswin|mingw/ =~ RUBY_PLATFORM).nil?
'/dev/null'
else
'nul'
end
end

# Used for uglifying source to minify
def uglify(str)
IO.popen('uglifyjs 2> ' + null, 'r+') do |i|
i.puts str
i.close_write
return i.read
end
rescue Errno::ENOENT, Errno::EPIPE, Errno::EINVAL
$stderr.puts '"uglifyjs" command not found (install with: "npm install -g uglify-js")'
nil
uglifyjs = DigestSourceCommand.new(:uglifyjs, nil, ' (install with: "npm install -g uglify-js")')
uglifyjs.digest(str)
end

# Gzip code to check file size
def gzip(str)
IO.popen('gzip -f 2> ' + null, 'r+') do |i|
i.puts str
i.close_write
return i.read
gzip = DigestSourceCommand.new(:gzip, '-f', ', it is required to produce the .gz version')
gzip.digest(str)
end


class DigestSourceCommand
def initialize(command, options, message)
@command, @options, @message = command, options, message
end
attr_reader :command, :options, :message

def digest(source)
return unless command_installed? command, message
IO.popen("#{command} #{options} #{hide_stderr}", 'r+') do |i|
i.puts source
i.close_write
i.read
end
end


private

def hide_stderr
if (/mswin|mingw/ =~ RUBY_PLATFORM).nil?
'2> /dev/null'
else
'2> nul'
end
end

# Code from http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each { |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable? exe
}
end
nil
end

INSTALLED = {}
def command_installed?(cmd, install_comment)
cmd = cmd.to_s
INSTALLED.fetch(cmd) do
unless INSTALLED[cmd] = which(cmd) != nil
$stderr.puts %Q("#{cmd}" command not found#{install_comment})
end
end
end
rescue Errno::ENOENT, Errno::EPIPE, Errno::EINVAL
$stderr.puts '"gzip" command not found, it is required to produce the .gz version'
nil
end

end
end