Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 255 lines (211 sloc) 7.897 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
require 'rake'
require 'rake/packagetask'
require 'yaml'

SCRIPTY2_ROOT = File.expand_path(File.dirname(__FILE__))
SCRIPTY2_SRC_DIR = File.join(SCRIPTY2_ROOT, 'src')

SCRIPTY2_DIST_DIR = File.join(SCRIPTY2_ROOT, 'dist')
SCRIPTY2_DEBUG_DIR = File.join(SCRIPTY2_DIST_DIR, 'debug')
SCRIPTY2_RELEASE_DIR = File.join(SCRIPTY2_DIST_DIR, 'release')

SCRIPTY2_DOC_DIR = File.join(SCRIPTY2_ROOT, 'doc')
SCRIPTY2_PKG_DIR = File.join(SCRIPTY2_ROOT, 'pkg')
SCRIPTY2_VERSION = YAML.load(IO.read(File.join(SCRIPTY2_SRC_DIR, 'constants.yml')))['SCRIPTY2_VERSION']

SCRIPTY2_TEST_DIR = File.join(SCRIPTY2_ROOT, 'test')
SCRIPTY2_TEST_UNIT_DIR = File.join(SCRIPTY2_TEST_DIR, 'unit')
SCRIPTY2_TMP_DIR = File.join(SCRIPTY2_TEST_UNIT_DIR, 'tmp')

TEMPLATES_ROOT = File.join(SCRIPTY2_ROOT, "templates")
TEMPLATES_DIRECTORY = File.join(TEMPLATES_ROOT, "html")


$:.unshift File.join(SCRIPTY2_ROOT, 'vendor', 'sprockets', 'lib')

def sprocketize(path, source, destination=nil)
  destination ||= [*source].first
  begin
    require "sprockets"
  rescue LoadError => e
    puts "\nYou'll need Sprockets to build script.aculo.us 2. Just run:\n\n"
    puts " $ git submodule init"
    puts " $ git submodule update"
    puts "\nand you should be all set.\n\n"
  end
  
  puts "Sprocketizing (#{[*source].join(', ')})..."
  secretary = Sprockets::Secretary.new(
    :root => File.join(SCRIPTY2_ROOT, path),
    :load_path => [SCRIPTY2_SRC_DIR],
    :source_files => [*source]
  )
  
  secretary.concatenation.save_to(File.join(SCRIPTY2_DIST_DIR, destination))
end

task :default => [:clean, :dist, :unified, :doc, :package, :clean_package_source]

desc "Clean the distribution directory."
task :clean do
  rm_rf SCRIPTY2_DIST_DIR
  mkdir SCRIPTY2_DIST_DIR
  mkdir SCRIPTY2_DEBUG_DIR
  mkdir SCRIPTY2_RELEASE_DIR
end

def dist_from_sources(sources)
  sprocketize("src", sources, "s2.js")
  cp File.join(SCRIPTY2_ROOT,'lib','prototype.js'), File.join(SCRIPTY2_DIST_DIR,'prototype.js')
end

desc "Builds the distribution."
task :dist => ['dist:default']
namespace :dist do
  task :default do
    dist_from_sources(["s2.js"])
  end
  
  desc "Builds the distribution, including experimental UI controls."
  task :experimental do
    dist_from_sources(["s2.js", "experimental.js"])
  end
end

def minify(src, target)
  puts "Minifying #{src}..."
  `java -jar vendor/yuicompressor/yuicompressor-2.4.2.jar #{src} -o #{target}`
  cp target, File.join(SCRIPTY2_DEBUG_DIR,'temp.js')
  msize = File.size(File.join(SCRIPTY2_DEBUG_DIR,'temp.js'))
  `gzip -9 #{File.join(SCRIPTY2_DEBUG_DIR,'temp.js')}`
  
  osize = File.size(src)
  dsize = File.size(File.join(SCRIPTY2_DEBUG_DIR,'temp.js.gz'))
  rm_rf File.join(SCRIPTY2_DEBUG_DIR,'temp.js.gz')
  
  puts "Original version: %.1fk" % (osize/1024.0)
  puts "Minified: %.1fk" % (msize/1024.0)
  puts "Minified and gzipped: %.1fk, compression factor %.1f" % [dsize/1024.0, osize/dsize.to_f]
end

desc "Generates a minified version of the distribution."
task :min do
  minify File.join(SCRIPTY2_DIST_DIR,'s2.js'), File.join(SCRIPTY2_RELEASE_DIR,'s2.min.js')
  minify File.join(SCRIPTY2_ROOT,'lib','prototype.js'), File.join(SCRIPTY2_RELEASE_DIR,'prototype.min.js')
end

def unify_distribution
  unified = IO.read(File.join(SCRIPTY2_DIST_DIR,'prototype.js')) + IO.read(File.join(SCRIPTY2_DIST_DIR,'s2.js'))
  File.open(File.join(SCRIPTY2_RELEASE_DIR,'prototype.s2.js'), 'w') do |file|
    file.write unified
  end
  minify File.join(SCRIPTY2_RELEASE_DIR,'prototype.s2.js'), File.join(SCRIPTY2_DIST_DIR,'prototype.s2.min.js')
end

desc "Generate a unified minified version of Prototype and scripty2"
task :unified => ['unified:default']
namespace :unified do
  task :default => [:dist, :min] do
    unify_distribution
  end
  
  desc "Generate a unified minified version of Prototype and scripty2, including experimental UI controls."
  task :experimental => ['dist:experimental', :min] do
    unify_distribution
  end
end

def doc_from_sources(sources)
  
  require 'tempfile'
  begin
    require "sprockets"
  rescue LoadError => e
    puts "\nYou'll need Sprockets to build script.aculo.us 2. Just run:\n\n"
    puts " $ git submodule init"
    puts " $ git submodule update"
    puts "\nand you should be all set.\n\n"
  end
  
  Tempfile.open("pdoc") do |temp|
    secretary = Sprockets::Secretary.new(
      :root => File.join(SCRIPTY2_ROOT, "src"),
      :load_path => [SCRIPTY2_SRC_DIR],
      :source_files => sources,
      :strip_comments => false
    )
      
    secretary.concatenation.save_to(temp.path)
    rm_rf SCRIPTY2_DOC_DIR
    mkdir SCRIPTY2_DOC_DIR
    
    #begin
      PDoc::Runner.new(temp.path,
        :output => SCRIPTY2_DOC_DIR,
        :templates => TEMPLATES_DIRECTORY
      ).run
    #rescue
     # puts "\n\nEXCEPTION WHILE RUNNING PDOC, CONTINUING...\n\n"
    #end
  end
  
  cp File.join(SCRIPTY2_ROOT, 'lib', 'prototype.js'), File.join(SCRIPTY2_DOC_DIR, 'javascripts')
  cp File.join(SCRIPTY2_DIST_DIR,'s2.js'), File.join(SCRIPTY2_DOC_DIR,'javascripts')
end

namespace :doc do
  desc "Builds the documentation."
  task :build => [:require] do
    doc_from_sources(["s2.js"])
  end
  
  desc "Builds the documentation, including experimental UI controls."
  task :experimental => [:require] do
    doc_from_sources(["s2.js", "experimental.js"])
  end

  task :require do
    lib = 'vendor/pdoc/lib/pdoc'
    unless File.exists?(lib)
      puts "\nYou'll need PDoc to generate the documentation. Just run:\n\n"
      puts " $ git submodule init"
      puts " $ git submodule update"
      puts "\nand you should be all set.\n\n"
    end
    require lib
  end
  
end

task :doc => ['doc:build']

Rake::PackageTask.new('scripty2', SCRIPTY2_VERSION) do |package|
  package.need_tar_gz = true
  package.need_zip = true
  package.package_dir = SCRIPTY2_PKG_DIR
  package.package_files.include(
    'README.rdoc',
    'MIT-LICENSE',
    'dist/**/*',
    'doc/**/*',
    'src/**/*'
  )
end

task :clean_package_source do
  rm_rf File.join(SCRIPTY2_PKG_DIR, "scripty2-#{SCRIPTY2_VERSION}")
end

task :test => ['test:build', 'test:run']
namespace :test do
  desc 'Runs all the JavaScript unit tests and collects the results'
  task :run => [:require] do
    testcases = ENV['TESTCASES']
    browsers_to_test = ENV['BROWSERS'] && ENV['BROWSERS'].split(',')
    tests_to_run = ENV['TESTS'] && ENV['TESTS'].split(',')
    runner = UnittestJS::WEBrickRunner::Runner.new(:test_dir => SCRIPTY2_TMP_DIR)

    Dir[File.join(SCRIPTY2_TMP_DIR, '*_test.html')].each do |file|
      file = File.basename(file)
      test = file.sub('_test.html', '')
      unless tests_to_run && !tests_to_run.include?(test)
        runner.add_test(file, testcases)
      end
    end
    
    UnittestJS::Browser::SUPPORTED.each do |browser|
      unless browsers_to_test && !browsers_to_test.include?(browser)
        runner.add_browser(browser.to_sym)
      end
    end
    
    trap('INT') { runner.teardown; exit }
    runner.run
  end
  
  task :build => [:clean, :dist] do
    builder = UnittestJS::Builder::SuiteBuilder.new({
      :input_dir => SCRIPTY2_TEST_UNIT_DIR,
      :assets_dir => SCRIPTY2_DIST_DIR
    })
    selected_tests = (ENV['TESTS'] || '').split(',')
    builder.collect(*selected_tests)
    builder.render
    
    # override UnittestJS stuff
    cp File.join(SCRIPTY2_ROOT, 'lib', 'prototype.js'),
      File.join(SCRIPTY2_TMP_DIR, 'lib_assets', 'prototype.js')
  end
  
  task :clean => [:require] do
    UnittestJS::Builder.empty_dir!(SCRIPTY2_TMP_DIR)
  end
  
  task :require do
    lib = 'vendor/unittest_js/lib/unittest_js'
    unless File.exists?(lib)
      puts "\nYou'll need UnittestJS to run the tests. Just run:\n\n"
      puts " $ git submodule init"
      puts " $ git submodule update"
      puts "\nand you should be all set.\n\n"
    end
    require lib
  end
end
Something went wrong with that request. Please try again.