PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/support/compile.rb

https://bitbucket.org/byronsanchez/hackbytes.com
Ruby | 189 lines | 128 code | 32 blank | 29 comment | 12 complexity | ba79f47a7136e4e38c4403d1dca38614 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-3.0
  1. #!/usr/bin/env ruby
  2. #
  3. # Local compilation script.
  4. # Reads and execute the db schema changescripts in assets/database
  5. def build_db()
  6. puts "------------------------------"
  7. puts "Generating the site databases."
  8. puts "------------------------------"
  9. Dir.mkdir(@config['database_output']) unless File.exists?(@config['database_output'])
  10. @config['database_scripts'].each do |path, db|
  11. puts "Building #{db}..."
  12. db_dir = "#{@config['database']}/#{path}"
  13. db_path = "#{@config['database_output']}/#{db}"
  14. # Only build the database if it does not already exist
  15. # The path database can always be rebuilt
  16. system "rm -f #{db_path}" if path == "path"
  17. if !File.exist?(db_path)
  18. files = Dir.glob(File.join(db_dir, '/sc*'))
  19. files.sort.each { |x|
  20. system "sqlite3 #{db_path} < #{x}"
  21. }
  22. puts "#{db} built successfully!".green
  23. else
  24. puts "#{db} already exists".yellow
  25. end
  26. end
  27. end
  28. # Precompiles any assets necessary that will not be created on the server.
  29. def precompile_site()
  30. puts "-----------------------"
  31. puts "Compiling Coffee files."
  32. puts "-----------------------"
  33. coffeeDir = "coffee/"
  34. jsDir = "js/"
  35. Dir.foreach(coffeeDir) do |coffeeFile|
  36. unless coffeeFile == '.' || coffeeFile == '..'
  37. js = CoffeeScript.compile File.read("#{coffeeDir}#{coffeeFile}")
  38. open "#{jsDir}#{coffeeFile.gsub('.coffee', '.js')}", 'w' do |file|
  39. file.puts js
  40. end
  41. end
  42. end
  43. puts "Coffee files compiled".green
  44. puts "-------------------"
  45. puts "Compiling JS files."
  46. puts "-------------------"
  47. File.open("js/hackbytes.min.js", "w") do |file|
  48. # spin.js
  49. file.write Uglifier.compile(File.read("vendor/assets/bower_components/spin.js/spin.js"))
  50. # jquery.spin.js
  51. file.write Uglifier.compile(File.read("vendor/assets/bower_components/spin.js/jquery.spin.js"))
  52. # jquery.easing.js
  53. file.write Uglifier.compile(File.read("vendor/assets/bower_components/jquery-easing-original/jquery.easing.1.3.js"))
  54. # jquery.mixitup.js
  55. file.write Uglifier.compile(File.read("vendor/assets/bower_components/mixitup2/src/jquery.mixitup.js"))
  56. # jquery.jcarousel.js
  57. file.write Uglifier.compile(File.read("vendor/assets/bower_components/jcarousel/dist/jquery.jcarousel.js"))
  58. # jquery.pikachoose.js
  59. file.write Uglifier.compile(File.read("vendor/assets/bower_components/pikachoose-bower/lib/jquery.pikachoose.js"))
  60. # jquery.fancybox.js
  61. file.write Uglifier.compile(File.read("vendor/assets/bower_components/fancybox/source/jquery.fancybox.js"))
  62. # application.js
  63. file.write Uglifier.compile(File.read("js/application.js"))
  64. # bs-comments.js
  65. file.write Uglifier.compile(File.read("js/bs-comments.js"))
  66. end
  67. system "cp vendor/assets/bower_components/jquery/dist/jquery.min.js js/jquery.min.js"
  68. system "cp vendor/assets/bower_components/html5shiv/dist/html5shiv.min.js js/html5shiv.min.js"
  69. system "cp -r vendor/assets/bower_components/pikachoose-bower/styles css/pikachoose"
  70. system "cp -r vendor/assets/bower_components/fancybox/source css/fancybox"
  71. puts "JS files compiled".green
  72. end
  73. # Compiles the entire site. Removes pages from compiled source if they have
  74. # been specified.
  75. def compile_site()
  76. puts "--------------------"
  77. puts "Compiling CSS files."
  78. puts "--------------------"
  79. output = system "bundle exec compass compile #{@config['source']}"
  80. if output.nil? || output == 0
  81. puts "CSS compilation failed. The compass compile command failed to run.".red
  82. else
  83. puts "CSS files compiled".green
  84. end
  85. puts "-------------------------"
  86. puts "Compiling entire website."
  87. puts "-------------------------"
  88. # TODO: If beneficial, use the API to run the build with the environment we
  89. # setup so far.
  90. output = system "bundle exec jekyll build"
  91. if output.nil? || output == 0
  92. puts "Website failed to compile. The jekyll compilation command failed to run.".red
  93. else
  94. puts "Website compiled.".green
  95. end
  96. # Remove all files listed in the no_deploy array.
  97. puts "-------------------------------"
  98. puts "Removing files from deployment."
  99. puts "-------------------------------"
  100. @config["no_deploy"].each { |dir| system "rm -rf #{@config['destination']}/#{dir}"
  101. puts "#{@config['destination']}/#{dir} removed from deployment." }
  102. puts "Extra files successfully removed".green
  103. puts "-------------------------"
  104. puts "Packaging necessary gems."
  105. puts "-------------------------"
  106. system "cp #{@config['source']}/vendor/server/Gemfile #{@config['destination']}/assets/"
  107. system "cp #{@config['source']}/vendor/server/Gemfile.lock #{@config['destination']}/assets/"
  108. end
  109. # Moves the assets gem files to the _site/ directory, as these are needed as
  110. # part of the app.
  111. def test_gems_for_site()
  112. puts "Installing gems to directory #{@config['destination']}/assets/"
  113. isDir = false
  114. # Only run the deployment install if the directory change was successful.
  115. Dir.chdir("#{@config['destination']}/assets/") do
  116. isDir = true
  117. Bundler.with_clean_env do
  118. system "bundle exec gem pristine --all"
  119. end
  120. puts "Required gems packaged successfully".green
  121. end
  122. if !isDir
  123. puts "Failed to package gems in #{@config['destination']}/assets/. Check permissions or if the directory exists.".red
  124. abort
  125. end
  126. end
  127. # Moves resources files to where they need to be in the _site/ directory. The
  128. # resources directory will not exist in the compiled version of the site.
  129. def package_resources()
  130. puts "--------------------"
  131. puts "Packaging resources."
  132. puts "--------------------"
  133. system "cp -r #{@config['source']}/resources/img/ #{@config['destination']}"
  134. system "cp -r #{@config['source']}/resources/favicons/* #{@config['destination']}"
  135. puts "Required resources packaged successfully".green
  136. end
  137. # Sets the permissions for the entire site directory which will be preserved
  138. # during the rsync.
  139. def chmod_site()
  140. puts "-----------------------------------"
  141. puts "Setting site directory permissions."
  142. puts "-----------------------------------"
  143. isDir = false
  144. # Only run the chmod if the directory change was successful.
  145. Dir.chdir("#{@config['destination']}") do
  146. isDir = true
  147. puts "Modifying directory permissions..."
  148. system "find . -type d -exec chmod u=rwx,g=rx,o= '{}' \\;"
  149. puts "Modifying file permissions..."
  150. system "find . -type f -exec chmod u=rw,g=r,o= '{}' \\;"
  151. puts "Permissions successfully set".green
  152. end
  153. if !isDir
  154. puts "Failed to modify permissions in #{@config['destination']}. Check your user permissions or if the directory exists.".red
  155. abort
  156. end
  157. end