Skip to content

Commit

Permalink
Add general purpose RakeHelper class for opal gems to use
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 24, 2013
1 parent ce96e67 commit e895410
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
47 changes: 7 additions & 40 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ require 'bundler'
Bundler.require
Bundler::GemHelper.install_tasks

require 'opal/rake_helper'
Opal::RakeHelper.install_tasks

namespace :github do
task :upload_assets do
require 'octokit'
Expand All @@ -25,7 +28,7 @@ require 'rack'
require 'webrick'
require 'opal-sprockets'

# mspec/rubyspec use a top level "language_version" to require relative specs.
# rubyspec uses a top level "language_version" to require relative specs.
# We can't do this at runtime, so we hijack the method (and make sure we only
# do this at the top level). We figure out which file we are including, and
# add it to our require list
Expand Down Expand Up @@ -61,8 +64,7 @@ class SpecEnvironment < Opal::Environment
end

def build_min file = 'build/specs.min.js'
require 'uglifier'
build Uglifier.compile(specs), file
build Opal::RakeHelper.uglify(specs), file
end

def build code = specs, file = 'build/specs.js'
Expand Down Expand Up @@ -105,17 +107,6 @@ class RunSpec
def build_specs
SpecEnvironment.new.build
end

# Only if OPAL_UGLIFY is set
def uglify(str)
if ENV['OPAL_UGLIFY']
require 'uglifier'
puts " * uglifying"
Uglifier.compile(str)
else
str
end
end
end

require 'rspec/core/rake_task'
Expand Down Expand Up @@ -187,8 +178,8 @@ task :dist do
puts "* building #{lib}..."

src = env[lib].to_s
min = uglify src
gzp = gzip min
min = Opal::RakeHelper.uglify src
gzp = Opal::RakeHelper.gzip min

File.open("build/#{lib}.js", 'w+') { |f| f << src }
File.open("build/#{lib}.min.js", 'w+') { |f| f << min } if min
Expand All @@ -206,27 +197,3 @@ desc "Rebuild grammar.rb for opal parser"
task :racc do
%x(racc -l lib/opal/grammar.y -o lib/opal/grammar.rb)
end

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

# Gzip code to check file size
def gzip(str)
IO.popen('gzip -f', 'r+') do |i|
i.puts str
i.close_write
return i.read
end
rescue Errno::ENOENT
$stderr.puts '"gzip" command not found, it is required to produce the .gz version'
nil
end
52 changes: 52 additions & 0 deletions lib/opal/rake_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'opal'

module Opal
class RakeHelper
include Rake::DSL if defined? Rake::DSL

class << self
attr_accessor :instance

def install_tasks
self.new.install_tasks
end

def expose(*names)
names.each do |name|
define_singleton_method(name) do |*args|
instance.__send__(name, *args)
end
end
end
end

expose :uglify, :gzip

def initialize
install_tasks

RakeHelper.instance = self
end

def install_tasks
namespace :opal do
end
end

def uglify(str)
IO.popen('uglifyjs', 'r+') do |i|
i.puts str
i.close_write
return i.read
end
end

def gzip(str)
IO.popen('gzip -f', 'r+') do |i|
i.puts str
i.close_write
return i.read
end
end
end
end
1 change: 0 additions & 1 deletion opal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Gem::Specification.new do |s|
s.add_dependency 'source_map'

s.add_development_dependency 'mspec', '1.5.18'
s.add_development_dependency 'uglifier'
s.add_development_dependency 'rake'
s.add_development_dependency 'racc'
s.add_development_dependency 'opal-sprockets', '~> 0.2.1'
Expand Down

0 comments on commit e895410

Please sign in to comment.