| 1 |
require 'rubygems/package' |
| 2 |
|
| 3 |
# ----- Utility Functions ----- |
| 4 |
|
| 5 |
def scope(path) |
| 6 |
File.join(File.dirname(__FILE__), path) |
| 7 |
end |
| 8 |
|
| 9 |
# ----- Default: Testing ------ |
| 10 |
|
| 11 |
task :default => :test |
| 12 |
|
| 13 |
require 'rake/testtask' |
| 14 |
|
| 15 |
LINE_SIZE = 80 |
| 16 |
DECORATION_CHAR = '#' |
| 17 |
|
| 18 |
def print_header(string) |
| 19 |
length = string.length |
| 20 |
puts DECORATION_CHAR * LINE_SIZE |
| 21 |
puts string.center(length + 2, ' ').center(LINE_SIZE, DECORATION_CHAR) |
| 22 |
puts DECORATION_CHAR * LINE_SIZE |
| 23 |
end |
| 24 |
|
| 25 |
desc "Run all tests" |
| 26 |
task :test do |
| 27 |
test_cases = [ |
| 28 |
{ |
| 29 |
'env' => {'MATHN' => 'true'}, |
| 30 |
'tasks' => ['test:ruby', 'test:spec'] |
| 31 |
}, |
| 32 |
{ |
| 33 |
'env' => {'MATHN' => 'false'}, |
| 34 |
'tasks' => ['test:ruby'] |
| 35 |
} |
| 36 |
] |
| 37 |
|
| 38 |
test_cases.each do |test_case| |
| 39 |
env = test_case['env'] |
| 40 |
tasks = test_case['tasks'] |
| 41 |
|
| 42 |
env.each do |key, value| |
| 43 |
ENV[key] = value |
| 44 |
end |
| 45 |
tasks.each do |task| |
| 46 |
print_header("Running task: #{task}, env: #{env}") |
| 47 |
Rake::Task[task].execute |
| 48 |
end |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
namespace :test do |
| 53 |
desc "Run the ruby tests (without sass-spec)" |
| 54 |
Rake::TestTask.new("ruby") do |t| |
| 55 |
t.libs << 'test' |
| 56 |
test_files = FileList[scope('test/**/*_test.rb')] |
| 57 |
test_files.exclude(scope('test/rails/*')) |
| 58 |
test_files.exclude(scope('test/plugins/*')) |
| 59 |
t.test_files = test_files |
| 60 |
t.warning = true |
| 61 |
t.verbose = true |
| 62 |
end |
| 63 |
|
| 64 |
desc "Run sass-spec tests against the local code." |
| 65 |
task :spec do |
| 66 |
old_load_path = $:.dup |
| 67 |
begin |
| 68 |
$:.unshift(File.join(File.dirname(__FILE__), "lib")) |
| 69 |
begin |
| 70 |
require 'sass_spec' |
| 71 |
rescue LoadError |
| 72 |
puts "You probably forgot to run: bundle exec rake" |
| 73 |
raise |
| 74 |
end |
| 75 |
SassSpec::Runner.new( |
| 76 |
language_version: get_version[/^\d+\.\d+/], |
| 77 |
spec_directory: SassSpec::SPEC_DIR, |
| 78 |
engine_adapter: SassEngineAdapter.new, |
| 79 |
generate: false, |
| 80 |
tap: false, |
| 81 |
skip: false, |
| 82 |
verbose: false, |
| 83 |
filter: "", |
| 84 |
limit: -1, |
| 85 |
unexpected_pass: false, |
| 86 |
nuke: false, |
| 87 |
).run || exit(1) |
| 88 |
ensure |
| 89 |
$:.replace(old_load_path) |
| 90 |
end |
| 91 |
end |
| 92 |
end |
| 93 |
|
| 94 |
# ----- Code Style Enforcement ----- |
| 95 |
|
| 96 |
def ruby_version_at_least?(version_string) |
| 97 |
ruby_version = Gem::Version.new(RUBY_VERSION.dup) |
| 98 |
version = Gem::Version.new(version_string) |
| 99 |
ruby_version >= version |
| 100 |
end |
| 101 |
|
| 102 |
# ----- Packaging ----- |
| 103 |
|
| 104 |
# Don't use Rake::GemPackageTast because we want prerequisites to run |
| 105 |
# before we load the gemspec. |
| 106 |
desc "Build all the packages." |
| 107 |
task :package => [:revision_file, :date_file, :permissions] do |
| 108 |
version = get_version |
| 109 |
File.open(scope('VERSION'), 'w') {|f| f.puts(version)} |
| 110 |
load scope('sass.gemspec') |
| 111 |
Gem::Package.build(SASS_GEMSPEC) |
| 112 |
sh %{git checkout VERSION} |
| 113 |
|
| 114 |
pkg = "#{SASS_GEMSPEC.name}-#{SASS_GEMSPEC.version}" |
| 115 |
mkdir_p "pkg" |
| 116 |
verbose(true) {mv "#{pkg}.gem", "pkg/#{pkg}.gem"} |
| 117 |
|
| 118 |
sh %{rm -f pkg/#{pkg}.tar.gz} |
| 119 |
verbose(false) {SASS_GEMSPEC.files.each {|f| sh %{tar rf pkg/#{pkg}.tar #{f}}}} |
| 120 |
sh %{gzip pkg/#{pkg}.tar} |
| 121 |
end |
| 122 |
|
| 123 |
task :permissions do |
| 124 |
sh %{chmod -R a+rx bin} |
| 125 |
sh %{chmod -R a+r .} |
| 126 |
require 'shellwords' |
| 127 |
Dir.glob('test/**/*_test.rb') do |file| |
| 128 |
next if file =~ %r{^test/haml/spec/} |
| 129 |
sh %{chmod a+rx #{file}} |
| 130 |
end |
| 131 |
end |
| 132 |
|
| 133 |
task :revision_file do |
| 134 |
require scope('lib/sass') |
| 135 |
|
| 136 |
release = Rake.application.top_level_tasks.include?('release') || File.exist?(scope('EDGE_GEM_VERSION')) |
| 137 |
if Sass.version[:rev] && !release |
| 138 |
File.open(scope('REVISION'), 'w') { |f| f.puts Sass.version[:rev] } |
| 139 |
elsif release |
| 140 |
File.open(scope('REVISION'), 'w') { |f| f.puts "(release)" } |
| 141 |
else |
| 142 |
File.open(scope('REVISION'), 'w') { |f| f.puts "(unknown)" } |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
task :date_file do |
| 147 |
File.open(scope('VERSION_DATE'), 'w') do |f| |
| 148 |
f.puts Time.now.utc.strftime('%d %B %Y %T %Z') |
| 149 |
end |
| 150 |
end |
| 151 |
|
| 152 |
# We also need to get rid of this file after packaging. |
| 153 |
at_exit do |
| 154 |
File.delete(scope('REVISION')) rescue nil |
| 155 |
File.delete(scope('VERSION_DATE')) rescue nil |
| 156 |
end |
| 157 |
|
| 158 |
desc "Install Sass as a gem. Use SUDO=1 to install with sudo." |
| 159 |
task :install => [:package] do |
| 160 |
gem = RUBY_PLATFORM =~ /java/ ? 'jgem' : 'gem' |
| 161 |
sh %{#{'sudo ' if ENV["SUDO"]}#{gem} install --no-ri pkg/sass-#{get_version}} |
| 162 |
end |
| 163 |
|
| 164 |
desc "Release a new Sass package to RubyGems.org." |
| 165 |
task :release => [:check_release, :package] do |
| 166 |
version = File.read(scope("VERSION")).strip |
| 167 |
sh %{gem push pkg/sass-#{version}.gem} |
| 168 |
end |
| 169 |
|
| 170 |
# Ensures that the VERSION file has been updated for a new release. |
| 171 |
task :check_release do |
| 172 |
version = File.read(scope("VERSION")).strip |
| 173 |
raise "There have been changes since current version (#{version})" if changed_since?(version) |
| 174 |
raise "VERSION_NAME must not be 'Bleeding Edge'" if File.read(scope("VERSION_NAME")) == "Bleeding Edge" |
| 175 |
end |
| 176 |
|
| 177 |
# Reads a password from the command line. |
| 178 |
# |
| 179 |
# @param name [String] The prompt to use to read the password |
| 180 |
def read_password(prompt) |
| 181 |
require 'readline' |
| 182 |
system "stty -echo" |
| 183 |
Readline.readline("#{prompt}: ").strip |
| 184 |
ensure |
| 185 |
system "stty echo" |
| 186 |
puts |
| 187 |
end |
| 188 |
|
| 189 |
# Returns whether or not the repository, or specific files, |
| 190 |
# has/have changed since a given revision. |
| 191 |
# |
| 192 |
# @param rev [String] The revision to check against |
| 193 |
# @param files [Array<String>] The files to check. |
| 194 |
# If this is empty, checks the entire repository |
| 195 |
def changed_since?(rev, *files) |
| 196 |
IO.popen("git diff --exit-code #{rev} #{files.join(' ')}") {} |
| 197 |
return !$?.success? |
| 198 |
end |
| 199 |
|
| 200 |
# Get the version string. If this is being installed from Git, |
| 201 |
# this includes the proper prerelease version. |
| 202 |
def get_version |
| 203 |
File.read(scope('VERSION').strip) |
| 204 |
end |
| 205 |
|
| 206 |
task :watch_for_update do |
| 207 |
sh %{ruby extra/update_watch.rb} |
| 208 |
end |
| 209 |
|
| 210 |
# ----- Documentation ----- |
| 211 |
|
| 212 |
task :rdoc do |
| 213 |
puts '=' * 100, <<END, '=' * 100 |
| 214 |
Sass uses the YARD documentation system (http://github.com/lsegal/yard). |
| 215 |
Install the yard gem and then run "rake doc". |
| 216 |
END |
| 217 |
end |
| 218 |
|
| 219 |
begin |
| 220 |
require 'yard' |
| 221 |
|
| 222 |
namespace :doc do |
| 223 |
task :sass do |
| 224 |
require scope('lib/sass') |
| 225 |
Dir[scope("yard/default/**/*.sass")].each do |sass| |
| 226 |
File.open(sass.gsub(/sass$/, 'css'), 'w') do |f| |
| 227 |
f.write(Sass::Engine.new(File.read(sass)).render) |
| 228 |
end |
| 229 |
end |
| 230 |
end |
| 231 |
|
| 232 |
desc "List all undocumented methods and classes." |
| 233 |
task :undocumented do |
| 234 |
opts = ENV["YARD_OPTS"] || "" |
| 235 |
ENV["YARD_OPTS"] = opts.dup + <<OPTS |
| 236 |
--list --tag comment --hide-tag comment --query " |
| 237 |
object.docstring.blank? && |
| 238 |
!(object.type == :method && object.is_alias?)" |
| 239 |
OPTS |
| 240 |
Rake::Task['yard'].execute |
| 241 |
end |
| 242 |
|
| 243 |
# Add IDs to the reference to match the old IDs generated by Maruku. This |
| 244 |
# ensures that old anchored links continue to work. |
| 245 |
task :add_ids do |
| 246 |
require 'nokogiri' |
| 247 |
|
| 248 |
doc = Nokogiri::HTML(File.read('doc/file.SASS_REFERENCE.html')) |
| 249 |
|
| 250 |
doc.css("#filecontents").css("h1, h2, h3, h4, h5, h6").each do |h| |
| 251 |
next if h.inner_text.empty? |
| 252 |
h['id'] = |
| 253 |
case h.inner_text |
| 254 |
when "Referencing Parent Selectors: &"; "parent-selector" |
| 255 |
when /^Comments:/; "comments" |
| 256 |
when "Strings"; "sass-script-strings" |
| 257 |
when "Division and /"; "division-and-slash" |
| 258 |
when /^Subtraction,/; "subtraction" |
| 259 |
when "& in SassScript"; "parent-script" |
| 260 |
when "@-Rules and Directives"; "directives" |
| 261 |
when "@extend-Only Selectors"; "placeholders" |
| 262 |
when "@extend-Only Selectors"; "placeholders" |
| 263 |
when "@each"; "each-directive" |
| 264 |
when "Multiple Assignment"; "each-multi-assign" |
| 265 |
when "Mixin Directives"; "mixins" |
| 266 |
when /^Defining a Mixin:/; "defining_a_mixin" |
| 267 |
when /^Including a Mixin:/; "including_a_mixin" |
| 268 |
when "Arguments"; "mixin-arguments" |
| 269 |
when "Passing Content Blocks to a Mixin"; "mixin-content" |
| 270 |
else |
| 271 |
h.inner_text.downcase.gsub(/[^a-z _-]/, '').gsub(' ', '_') |
| 272 |
end |
| 273 |
end |
| 274 |
|
| 275 |
# Give each option an anchor. |
| 276 |
doc.css("#filecontents li p strong code").each do |c| |
| 277 |
c['id'] = c.inner_text.gsub(/:/, '') + '-option' |
| 278 |
end |
| 279 |
|
| 280 |
File.write('doc/file.SASS_REFERENCE.html', doc.to_html) |
| 281 |
end |
| 282 |
end |
| 283 |
|
| 284 |
YARD::Rake::YardocTask.new do |t| |
| 285 |
t.files = FileList.new(scope('lib/**/*.rb')) do |list| |
| 286 |
list.exclude('lib/sass/plugin/merb.rb') |
| 287 |
list.exclude('lib/sass/plugin/rails.rb') |
| 288 |
end.to_a |
| 289 |
t.options << '--incremental' if Rake.application.top_level_tasks.include?('redoc') |
| 290 |
t.options += FileList.new(scope('yard/*.rb')).to_a.map {|f| ['-e', f]}.flatten |
| 291 |
files = FileList.new(scope('doc-src/*')).to_a.sort_by {|s| s.size} + %w[MIT-LICENSE VERSION] |
| 292 |
t.options << '--files' << files.join(',') |
| 293 |
t.options << '--template-path' << scope('yard') |
| 294 |
t.options << '--title' << ENV["YARD_TITLE"] if ENV["YARD_TITLE"] |
| 295 |
|
| 296 |
t.before = lambda do |
| 297 |
if ENV["YARD_OPTS"] |
| 298 |
require 'shellwords' |
| 299 |
t.options.concat(Shellwords.shellwords(ENV["YARD_OPTS"])) |
| 300 |
end |
| 301 |
end |
| 302 |
end |
| 303 |
Rake::Task['yard'].prerequisites.insert(0, 'doc:sass') |
| 304 |
Rake::Task['yard'].instance_variable_set('@comment', nil) |
| 305 |
|
| 306 |
desc "Generate Documentation" |
| 307 |
task :doc => [:yard, 'doc:add_ids'] |
| 308 |
task :redoc => [:yard, 'doc:add_ids'] |
| 309 |
rescue LoadError |
| 310 |
desc "Generate Documentation" |
| 311 |
task :doc => :rdoc |
| 312 |
task :yard => :rdoc |
| 313 |
end |
| 314 |
|
| 315 |
# ----- Coverage ----- |
| 316 |
|
| 317 |
begin |
| 318 |
require 'rcov/rcovtask' |
| 319 |
|
| 320 |
Rcov::RcovTask.new do |t| |
| 321 |
t.test_files = FileList[scope('test/**/*_test.rb')] |
| 322 |
t.rcov_opts << '-x' << '"^\/"' |
| 323 |
if ENV['NON_NATIVE'] |
| 324 |
t.rcov_opts << "--no-rcovrt" |
| 325 |
end |
| 326 |
t.verbose = true |
| 327 |
end |
| 328 |
rescue LoadError; end |
| 329 |
|
| 330 |
# ----- Profiling ----- |
| 331 |
|
| 332 |
begin |
| 333 |
require 'ruby-prof' |
| 334 |
|
| 335 |
desc <<END |
| 336 |
Run a profile of sass. |
| 337 |
TIMES=n sets the number of runs. Defaults to 1000. |
| 338 |
FILE=str sets the file to profile. Defaults to 'complex'. |
| 339 |
OUTPUT=str sets the ruby-prof output format. |
| 340 |
Can be Flat, CallInfo, or Graph. Defaults to Flat. Defaults to Flat. |
| 341 |
END |
| 342 |
task :profile do |
| 343 |
times = (ENV['TIMES'] || '1000').to_i |
| 344 |
file = ENV['FILE'] |
| 345 |
|
| 346 |
require 'lib/sass' |
| 347 |
|
| 348 |
file = File.read(scope("test/sass/templates/#{file || 'complex'}.sass")) |
| 349 |
result = RubyProf.profile { times.times { Sass::Engine.new(file).render } } |
| 350 |
|
| 351 |
RubyProf.const_get("#{(ENV['OUTPUT'] || 'Flat').capitalize}Printer").new(result).print |
| 352 |
end |
| 353 |
rescue LoadError; end |