forked from jquery-ui-rails/jquery-ui-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
140 lines (127 loc) · 4.79 KB
/
Rakefile
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
require 'json'
require 'bundler/gem_tasks'
DEPENDENCY_HASH = JSON.load(File.read('dependencies.js'))
LANGUAGE_REGEX = /-[-a-zA-Z]+(?=\.js\z)/
def version
JSON.load(File.read('jquery-ui/package.json'))['version']
end
task :submodule do
sh 'git submodule update --init' unless File.exist?('jquery-ui/README.md')
end
def get_js_dependencies(basename)
if basename.match LANGUAGE_REGEX
# Depend on main module for i18n files
[basename.sub(LANGUAGE_REGEX, '')]
else
dependencies = DEPENDENCY_HASH[basename.sub(/\Ajquery\./, '')]
if dependencies.nil?
puts "Warning: No dependencies found for #{basename}"
dependencies = []
end
dependencies = dependencies
.reject { |dep| dep == 'theme' } # 'theme' pseudo-dependency handled by CSS
.map { |dep| "jquery.#{dep}" }
# Make sure we do not package assets with broken dependencies
dependencies.each do |dep|
fail "#{basename}: missing #{dep}" unless File.exist? "jquery-ui/ui/#{dep}"
end
dependencies
end
end
def remove_js_extension(path)
path.chomp(".js")
end
def protect_copyright_notice(source_code)
# YUI does not minify comments starting with "/*!"
# The i18n files start with non-copyright comments, so we require a newline
# to avoid protecting those
source_code.gsub!(/\A\s*\/\*\r?\n/, "/*!\n")
end
desc "Remove the vendor directory"
task :clean do
rm_rf 'vendor'
end
desc "Generate the JavaScript assets"
task :javascripts => :submodule do
target_dir = "vendor/assets/javascripts"
mkdir_p target_dir
Rake.rake_output_message 'Generating javascripts'
Dir.glob("jquery-ui/ui/**/*.js").each do |path|
basename = File.basename(path)
dep_modules = get_js_dependencies(basename).map(&method(:remove_js_extension))
dep_modules << 'jquery' if basename == 'jquery.ui.core.js'
File.open("#{target_dir}/#{basename}", "w") do |out|
dep_modules.each do |mod|
out.write("//= require #{mod}\n")
end
out.write("\n") unless dep_modules.empty?
source_code = File.read(path)
source_code.gsub!('@VERSION', version)
protect_copyright_notice(source_code)
out.write(source_code)
end
end
File.open("#{target_dir}/jquery.effects.all.js", "w") do |out|
Dir.glob("jquery-ui/ui/jquery.effects.*.js").sort.each do |path|
asset_name = remove_js_extension(File.basename(path))
out.write("//= require #{asset_name}\n")
end
end
File.open("#{target_dir}/jquery.ui.all.js", "w") do |out|
Dir.glob("jquery-ui/ui/*.js").sort.each do |path|
asset_name = remove_js_extension(File.basename(path))
out.write("//= require #{asset_name}\n")
end
end
end
desc "Generate the CSS assets"
task :stylesheets => :submodule do
target_dir = "vendor/assets/stylesheets"
mkdir_p target_dir
Rake.rake_output_message 'Generating stylesheets'
Dir.glob("jquery-ui/themes/base/*.css").each do |path|
basename = File.basename(path)
source_code = File.read(path)
source_code.gsub!('@VERSION', version)
protect_copyright_notice(source_code)
extra_dependencies = []
extra_dependencies << 'jquery.ui.core' unless basename =~ /\.(all|base|core)\./
# Is "theme" listed among the dependencies for the matching JS file?
unless basename =~ /\.(all|base|core|theme)\./
dependencies = DEPENDENCY_HASH[basename.sub(/\Ajquery\./, '').sub(/\.css/, '.js')]
if dependencies.nil?
puts "Warning: No matching JavaScript dependencies found for #{basename}"
else
extra_dependencies << 'jquery.ui.theme' if dependencies.include? 'theme'
end
end
extra_dependencies.reverse.each do |dep|
# Add after first comment block
source_code = source_code.sub(/\A((.*?\*\/\n)?)/m, "\\1/*\n *= require #{dep}\n */\n")
end
# Use "require" instead of @import
source_code.gsub!(/^@import (.*)$/) { |s|
m = s.match(/^@import (url\()?"(?<module>[-_.a-zA-Z]+)\.css"\)?;/) \
or fail "Cannot parse import: #{s}"
"/*\n *= require #{m['module']}\n */"
}
# Be cute: collapse multiple require comment blocks into one
source_code.gsub!(/^( \*= require .*)\n \*\/(\n+)\/\*\n(?= \*= require )/, '\1\2')
# Replace hard-coded image URLs with asset path helpers
source_code.gsub!(/url\(images\/([-_.a-zA-Z0-9]+)\)/, 'url(<%= image_path("jquery-ui/\1") %>)')
File.open("#{target_dir}/#{basename}.erb", "w") do |out|
out.write(source_code)
end
end
end
desc "Generate the image assets"
task :images => :submodule do
target_dir = "vendor/assets/images/jquery-ui"
mkdir_p target_dir
Rake.rake_output_message 'Copying images'
FileUtils.cp(Dir.glob("jquery-ui/themes/base/images/*.png"), target_dir)
end
desc "Clean and then generate everything (default)"
task :assets => [:clean, :javascripts, :stylesheets, :images]
task :build => :assets
task :default => :assets