Rakefile 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # ----- Utility Functions -----
  2. def scope(path)
  3. File.join(File.dirname(__FILE__), path)
  4. end
  5. # ----- Default: Testing ------
  6. task :default => :test
  7. require 'rake/testtask'
  8. Rake::TestTask.new do |t|
  9. t.libs << 'test'
  10. test_files = FileList[scope('test/**/*_test.rb')]
  11. test_files.exclude(scope('test/rails/*'))
  12. test_files.exclude(scope('test/plugins/*'))
  13. t.test_files = test_files
  14. t.verbose = true
  15. end
  16. # ----- Packaging -----
  17. # Don't use Rake::GemPackageTast because we want prerequisites to run
  18. # before we load the gemspec.
  19. desc "Build all the packages."
  20. task :package => [:revision_file, :submodules, :permissions] do
  21. version = get_version
  22. File.open(scope('VERSION'), 'w') {|f| f.puts(version)}
  23. load scope('sass.gemspec')
  24. Gem::Builder.new(SASS_GEMSPEC).build
  25. sh %{git checkout VERSION}
  26. pkg = "#{SASS_GEMSPEC.name}-#{SASS_GEMSPEC.version}"
  27. mkdir_p "pkg"
  28. verbose(true) {mv "#{pkg}.gem", "pkg/#{pkg}.gem"}
  29. sh %{rm -f pkg/#{pkg}.tar.gz}
  30. verbose(false) {SASS_GEMSPEC.files.each {|f| sh %{tar rf pkg/#{pkg}.tar #{f}}}}
  31. sh %{gzip pkg/#{pkg}.tar}
  32. end
  33. task :permissions do
  34. sh %{chmod -R a+rx bin}
  35. sh %{chmod -R a+r .}
  36. require 'shellwords'
  37. Dir.glob('test/**/*_test.rb') do |file|
  38. next if file =~ %r{^test/haml/spec/}
  39. sh %{chmod a+rx #{file}}
  40. end
  41. end
  42. task :revision_file do
  43. require scope('lib/sass')
  44. release = Rake.application.top_level_tasks.include?('release') || File.exist?(scope('EDGE_GEM_VERSION'))
  45. if Sass.version[:rev] && !release
  46. File.open(scope('REVISION'), 'w') { |f| f.puts Sass.version[:rev] }
  47. elsif release
  48. File.open(scope('REVISION'), 'w') { |f| f.puts "(release)" }
  49. else
  50. File.open(scope('REVISION'), 'w') { |f| f.puts "(unknown)" }
  51. end
  52. end
  53. # We also need to get rid of this file after packaging.
  54. at_exit { File.delete(scope('REVISION')) rescue nil }
  55. desc "Install Sass as a gem. Use SUDO=1 to install with sudo."
  56. task :install => [:package] do
  57. gem = RUBY_PLATFORM =~ /java/ ? 'jgem' : 'gem'
  58. sh %{#{'sudo ' if ENV["SUDO"]}#{gem} install --no-ri pkg/sass-#{get_version}}
  59. end
  60. desc "Release a new Sass package to Rubyforge."
  61. task :release => [:check_release, :package] do
  62. name = File.read(scope("VERSION_NAME")).strip
  63. version = File.read(scope("VERSION")).strip
  64. sh %{rubyforge add_release sass sass "#{name} (v#{version})" pkg/sass-#{version}.gem}
  65. sh %{rubyforge add_file sass sass "#{name} (v#{version})" pkg/sass-#{version}.tar.gz}
  66. sh %{gem push pkg/sass-#{version}.gem}
  67. end
  68. # Ensures that the VERSION file has been updated for a new release.
  69. task :check_release do
  70. version = File.read(scope("VERSION")).strip
  71. raise "There have been changes since current version (#{version})" if changed_since?(version)
  72. raise "VERSION_NAME must not be 'Bleeding Edge'" if File.read(scope("VERSION_NAME")) == "Bleeding Edge"
  73. end
  74. # Reads a password from the command line.
  75. #
  76. # @param name [String] The prompt to use to read the password
  77. def read_password(prompt)
  78. require 'readline'
  79. system "stty -echo"
  80. Readline.readline("#{prompt}: ").strip
  81. ensure
  82. system "stty echo"
  83. puts
  84. end
  85. # Returns whether or not the repository, or specific files,
  86. # has/have changed since a given revision.
  87. #
  88. # @param rev [String] The revision to check against
  89. # @param files [Array<String>] The files to check.
  90. # If this is empty, checks the entire repository
  91. def changed_since?(rev, *files)
  92. IO.popen("git diff --exit-code #{rev} #{files.join(' ')}") {}
  93. return !$?.success?
  94. end
  95. task :submodules do
  96. if File.exist?(File.dirname(__FILE__) + "/.git")
  97. sh %{git submodule sync}
  98. sh %{git submodule update --init}
  99. elsif !File.exist?(File.dirname(__FILE__) + "/vendor/listen/lib")
  100. warn <<WARN
  101. WARNING: vendor/listen doesn't exist, and this isn't a git repository so
  102. I can't get it automatically!
  103. WARN
  104. end
  105. end
  106. task :release_edge do
  107. ensure_git_cleanup do
  108. puts "#{'=' * 50} Running rake release_edge"
  109. sh %{git checkout master}
  110. sh %{git reset --hard origin/master}
  111. sh %{rake package}
  112. version = get_version
  113. sh %{rubyforge add_release sass sass "Bleeding Edge (v#{version})" pkg/sass-#{version}.gem}
  114. sh %{gem push pkg/sass-#{version}.gem}
  115. end
  116. end
  117. # Get the version string. If this is being installed from Git,
  118. # this includes the proper prerelease version.
  119. def get_version
  120. written_version = File.read(scope('VERSION').strip)
  121. return written_version unless File.exist?(scope('.git'))
  122. # Get the current master branch version
  123. version = written_version.split('.')
  124. version.map! {|n| n =~ /^[0-9]+$/ ? n.to_i : n}
  125. return written_version unless version.size == 5 && version[3] == "alpha" # prerelease
  126. return written_version if (commit_count = `git log --pretty=oneline HEAD ^stable | wc -l`).empty?
  127. version[4] = commit_count.strip
  128. version.join('.')
  129. end
  130. task :watch_for_update do
  131. sh %{ruby extra/update_watch.rb}
  132. end
  133. # ----- Documentation -----
  134. task :rdoc do
  135. puts '=' * 100, <<END, '=' * 100
  136. Sass uses the YARD documentation system (http://github.com/lsegal/yard).
  137. Install the yard gem and then run "rake doc".
  138. END
  139. end
  140. begin
  141. require 'yard'
  142. namespace :doc do
  143. task :sass do
  144. require scope('lib/sass')
  145. Dir[scope("yard/default/**/*.sass")].each do |sass|
  146. File.open(sass.gsub(/sass$/, 'css'), 'w') do |f|
  147. f.write(Sass::Engine.new(File.read(sass)).render)
  148. end
  149. end
  150. end
  151. desc "List all undocumented methods and classes."
  152. task :undocumented do
  153. opts = ENV["YARD_OPTS"] || ""
  154. ENV["YARD_OPTS"] = opts.dup + <<OPTS
  155. --list --query "
  156. object.docstring.blank? &&
  157. !(object.type == :method && object.is_alias?)"
  158. OPTS
  159. Rake::Task['yard'].execute
  160. end
  161. end
  162. YARD::Rake::YardocTask.new do |t|
  163. t.files = FileList.new(scope('lib/**/*.rb')) do |list|
  164. list.exclude('lib/sass/plugin/merb.rb')
  165. list.exclude('lib/sass/plugin/rails.rb')
  166. list.exclude('lib/sass/less.rb')
  167. end.to_a
  168. t.options << '--incremental' if Rake.application.top_level_tasks.include?('redoc')
  169. t.options += FileList.new(scope('yard/*.rb')).to_a.map {|f| ['-e', f]}.flatten
  170. files = FileList.new(scope('doc-src/*')).to_a.sort_by {|s| s.size} + %w[MIT-LICENSE VERSION]
  171. t.options << '--files' << files.join(',')
  172. t.options << '--template-path' << scope('yard')
  173. t.options << '--title' << ENV["YARD_TITLE"] if ENV["YARD_TITLE"]
  174. t.before = lambda do
  175. if ENV["YARD_OPTS"]
  176. require 'shellwords'
  177. t.options.concat(Shellwords.shellwords(ENV["YARD_OPTS"]))
  178. end
  179. end
  180. end
  181. Rake::Task['yard'].prerequisites.insert(0, 'doc:sass')
  182. Rake::Task['yard'].instance_variable_set('@comment', nil)
  183. desc "Generate Documentation"
  184. task :doc => :yard
  185. task :redoc => :yard
  186. rescue LoadError
  187. desc "Generate Documentation"
  188. task :doc => :rdoc
  189. task :yard => :rdoc
  190. end
  191. task :pages do
  192. ensure_git_cleanup do
  193. puts "#{'=' * 50} Running rake pages"
  194. sh %{git checkout sass-pages}
  195. sh %{git reset --hard origin/sass-pages}
  196. Dir.chdir("/var/www/sass-pages") do
  197. sh %{git fetch origin}
  198. sh %{git checkout stable}
  199. sh %{git reset --hard origin/stable}
  200. sh %{git checkout sass-pages}
  201. sh %{git reset --hard origin/sass-pages}
  202. sh %{rake build --trace}
  203. sh %{mkdir -p tmp}
  204. sh %{touch tmp/restart.txt}
  205. end
  206. end
  207. end
  208. # ----- Coverage -----
  209. begin
  210. require 'rcov/rcovtask'
  211. Rcov::RcovTask.new do |t|
  212. t.test_files = FileList[scope('test/**/*_test.rb')]
  213. t.rcov_opts << '-x' << '"^\/"'
  214. if ENV['NON_NATIVE']
  215. t.rcov_opts << "--no-rcovrt"
  216. end
  217. t.verbose = true
  218. end
  219. rescue LoadError; end
  220. # ----- Profiling -----
  221. begin
  222. require 'ruby-prof'
  223. desc <<END
  224. Run a profile of sass.
  225. TIMES=n sets the number of runs. Defaults to 1000.
  226. FILE=str sets the file to profile. Defaults to 'complex'.
  227. OUTPUT=str sets the ruby-prof output format.
  228. Can be Flat, CallInfo, or Graph. Defaults to Flat. Defaults to Flat.
  229. END
  230. task :profile do
  231. times = (ENV['TIMES'] || '1000').to_i
  232. file = ENV['FILE']
  233. require 'lib/sass'
  234. file = File.read(scope("test/sass/templates/#{file || 'complex'}.sass"))
  235. result = RubyProf.profile { times.times { Sass::Engine.new(file).render } }
  236. RubyProf.const_get("#{(ENV['OUTPUT'] || 'Flat').capitalize}Printer").new(result).print
  237. end
  238. rescue LoadError; end
  239. # ----- Handling Updates -----
  240. def email_on_error
  241. yield
  242. rescue Exception => e
  243. IO.popen("sendmail nex342@gmail.com", "w") do |sm|
  244. sm << "From: nex3@nex-3.com\n" <<
  245. "To: nex342@gmail.com\n" <<
  246. "Subject: Exception when running rake #{Rake.application.top_level_tasks.join(', ')}\n" <<
  247. e.message << "\n\n" <<
  248. e.backtrace.join("\n")
  249. end
  250. ensure
  251. raise e if e
  252. end
  253. def ensure_git_cleanup
  254. email_on_error {yield}
  255. ensure
  256. sh %{git reset --hard HEAD}
  257. sh %{git clean -xdf}
  258. sh %{git checkout master}
  259. end
  260. task :handle_update do
  261. email_on_error do
  262. unless ENV["REF"] =~ %r{^refs/heads/(master|stable|sass-pages)$}
  263. puts "#{'=' * 20} Ignoring rake handle_update REF=#{ENV["REF"].inspect}"
  264. next
  265. end
  266. branch = $1
  267. puts
  268. puts
  269. puts '=' * 150
  270. puts "Running rake handle_update REF=#{ENV["REF"].inspect}"
  271. sh %{git fetch origin}
  272. sh %{git checkout stable}
  273. sh %{git reset --hard origin/stable}
  274. sh %{git checkout master}
  275. sh %{git reset --hard origin/master}
  276. case branch
  277. when "master"
  278. sh %{rake release_edge --trace}
  279. when "stable", "sass-pages"
  280. sh %{rake pages --trace}
  281. end
  282. puts 'Done running handle_update'
  283. puts '=' * 150
  284. end
  285. end