PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Rakefile

https://bitbucket.org/kakada/platform-common
Rakefile | 113 lines | 95 code | 18 blank | 0 comment | 12 complexity | 1dc6562e7d0b742e735466e03f1536bc MD5 | raw file
  1. require 'bundler/setup'
  2. require 'smart_asset'
  3. require 'fileutils'
  4. require 'cloudfiles'
  5. require 'digest/md5'
  6. task :default => [:all]
  7. task :all => [:css, :js] do
  8. end
  9. task :js do
  10. puts "compressing js"
  11. SmartAsset.load_config Dir.pwd, 'assets.yml'
  12. SmartAsset.compress 'javascripts'
  13. Dir["#{Dir.pwd}/theme/javascripts/temp/*.js"].each do |filename|
  14. target_filename = filename.sub(/\/temp\/.*_/, '/')
  15. puts "Moving to #{target_filename}"
  16. FileUtils.move filename, filename.sub(/\/temp\/.*_/, '/')
  17. end
  18. puts "Removing temp folder"
  19. Dir.delete "#{Dir.pwd}/theme/javascripts/temp"
  20. abort "Javascript compression failed!" if File.size("#{Dir.pwd}/theme/javascripts/theme.js") == 0
  21. end
  22. task :css do
  23. output_css = "#{Dir.pwd}/theme/stylesheets/theme.css"
  24. File.delete(output_css) if File::exists?(output_css)
  25. puts "compressing css"
  26. puts `compass compile`
  27. abort "CSS compression failed!" unless $?.success?
  28. end
  29. task :deploy => [:build_zip, :upload]
  30. task :sample_index do
  31. puts "building samples/index.htm"
  32. hg_tip = `hg tip`.strip
  33. hg_version = hg_tip.match("^changeset:.*:(.*)$")[1]
  34. file = File.new("samples/index.htm", "w+")
  35. file.puts "<!DOCTYPE HTML><html><body><h1>InSTEDD Platform Common</h1><pre>#{hg_tip}</pre>"
  36. file.puts "<a href=\"https://bitbucket.org/instedd/platform-common/changeset/#{hg_version}\">source on bitbucket</a>"
  37. file.puts "<ul>"
  38. Dir["#{Dir.pwd}/samples/**/*.htm"].sort.each do |filename|
  39. filename.sub!("#{Dir.pwd}/samples/", "")
  40. next if filename == "index.htm"
  41. file.puts "<li><a href=\"#{filename}\">#{filename}</a>"
  42. end
  43. file.puts "</ul></body></html>"
  44. file.close
  45. end
  46. task :build_zip => [:all, :sample_index] do
  47. puts "building theme/theme.zip"
  48. `zip -r theme/theme.zip theme/ -x theme/theme.zip *sass* *.DS_Store *javascripts/source/*`
  49. end
  50. task :upload do
  51. files = Dir['{{samples,theme}/**/*,index.htm}']
  52. files.reject! { |x| !File.file? x }
  53. files.reject! { |x| x.end_with? '/.DS_Store' }
  54. files.reject! { |x| x.start_with? 'theme/sass/' }
  55. files.reject! { |x| x.start_with? 'theme/javascripts/source' }
  56. cf = CloudFiles::Connection.new :username => ENV['CLOUDFILES_USER'], :api_key => ENV['CLOUDFILES_API_KEY']
  57. ['platform-common', 'platform-common-staging'].each do |bucket_name|
  58. puts "Uploading changes to bucket '#{bucket_name}'"
  59. bucket = cf.container bucket_name
  60. $stored_files = bucket.objects_detail
  61. def has_to_update? file
  62. stored = $stored_files[file]
  63. if stored
  64. if File.size(file) != stored[:bytes].to_i || Digest::MD5.file(file).to_s != stored[:hash]
  65. puts "Updating file #{file}"
  66. true
  67. else
  68. false
  69. end
  70. else
  71. puts "Uploading new file #{file}"
  72. true
  73. end
  74. end
  75. files.each do |file|
  76. if has_to_update? file
  77. obj = bucket.create_object file
  78. headers = {}
  79. headers['Access-Control-Allow-Origin'] = '*' if file.start_with? 'theme/fonts/'
  80. obj.write open(file), headers
  81. end
  82. end
  83. ($stored_files.keys - files).each do |missing|
  84. next if File.directory? missing
  85. puts "Deleting file #{missing}"
  86. bucket.delete_object missing
  87. end
  88. begin
  89. puts "Invalidating cache"
  90. bucket.purge_from_cdn
  91. rescue
  92. puts "Failed to invalidate CDN (probably last invalidation was less than an hour ago)"
  93. end
  94. end
  95. end