aboutsummaryrefslogtreecommitdiffstats
path: root/Rakefile
blob: d0d8f66a2421b261b0b339dd77156603952266a7 (plain)
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
prefix    = File.dirname( __FILE__ )

# Directory variables
src_dir   = File.join( prefix, 'src' )
build_dir = File.join( prefix, 'build' )
test_dir  = File.join( prefix, 'test' )

# A different destination directory can be set by
# setting DIST_DIR before calling rake
dist_dir  = ENV['DIST_DIR'] || File.join( prefix, 'dist' )

base_files = %w{intro core support data queue attributes event selector traversing manipulation css ajax effects offset dimensions outro}.map { |js| File.join( src_dir, "#{js}.js" ) }

# Sizzle, QUnit and jQuery files/dirs
sizzle_dir = File.join( src_dir, "sizzle" )
sizzle     = File.join( sizzle_dir, "sizzle.js" )
selector   = File.join( src_dir, "selector.js" )

qunit_dir  = File.join( test_dir, "qunit" )
qunit      = File.join( qunit_dir, "qunit", "qunit.js" )

jq         = File.join( dist_dir, "jquery.js" )
jq_min     = File.join( dist_dir, "jquery.min.js" )

# General Variables
date       = `git log -1`[/^Date:\s+(.+)$/, 1]
version    = File.read( File.join( prefix, 'version.txt' ) ).strip

# Build tools
rhino      = "java -jar #{build_dir}/js.jar"
minfier    = "java -jar #{build_dir}/google-compiler-20100917.jar"

# Turn off output other than needed from `sh` and file commands
verbose(false) 

# Tasks
task :default => "all"

desc "Builds jQuery; Tests with JSLint; Minifies jQuery"
task :all => [:jquery, :lint, :min] do
  puts "jQuery build complete."
end

desc "Builds jQuery: jquery.js (Default task)"
task :jquery => [:selector, jq]

desc "Builds a minified version of jQuery: jquery.min.js"
task :min => jq_min


task :init => [sizzle, qunit] do
  sizzle_git = File.join(sizzle_dir, '.git')
  qunit_git  = File.join(qunit_dir,  '.git')
  
  puts "Updating SizzleJS with latest..."
	sh "git --git-dir=#{sizzle_git} pull -q origin master"

  puts "Updating QUnit with latest..."
	sh "git --git-dir=#{qunit_git} pull -q origin master"
end

desc "Removes dist folder, selector.js, and Sizzle/QUnit"
task :clean do
  puts "Removing Distribution directory: #{dist_dir}..." 
  rm_rf dist_dir

  puts "Removing built copy of Sizzle..."
  rm_rf selector

  puts "Removing cloned directories..."
  rm_rf qunit_dir
  rm_rf sizzle_dir
end

desc "Rebuilds selector.js from SizzleJS"
task :selector => [:init, selector]

desc "Tests built jquery.js against JSLint"
task :lint => jq do
  puts "Checking jQuery against JSLint..."
  sh "#{rhino} " + File.join(build_dir, 'jslint-check.js')
end


# File and Directory Dependencies
directory dist_dir

file jq => [dist_dir, base_files].flatten do
  puts "Building jquery.js..."
  
  File.open(jq, 'w') do |f|
    f.write cat(base_files).gsub(/(Date:.)/, "\\1#{date}" ).gsub(/@VERSION/, version)
  end
end

file jq_min => jq do
  puts "Building jquery.min.js..."

  sh "#{minfier} --js #{jq} --warning_level QUIET --js_output_file #{jq_min}"
  
  min = File.read( jq_min )
  
  # Equivilent of "head"
  File.open(jq_min, 'w') do |f|
    f.write File.readlines(jq)[0..14].join()
    f.write min
  end
end

file selector => [sizzle, :init] do 
  puts "Building selector code from Sizzle..."
  
  File.open(selector, 'w') do |f|
    f.write File.read(sizzle).gsub( 
      /^.+EXPOSE$\n/, 
      '\0' + File.read( File.join( src_dir, 'sizzle-jquery.js' ))
    ).gsub(
      /^window.Sizzle.+$\n/, ''
    )
  end
end

file sizzle do
  puts "Retrieving SizzleJS from Github..."
  sh "git clone git://github.com/jeresig/sizzle.git #{sizzle_dir}"
end

file qunit do
  puts "Retrieving QUnit from Github..."
  sh "git clone git://github.com/jquery/qunit.git #{qunit_dir}"
end


def cat( files )
  files.map do |file|
    File.read(file)
  end.join('')
end