Skip to content

Instantly share code, notes, and snippets.

@sr3d
Created March 14, 2012 18:46
Show Gist options
  • Save sr3d/2038610 to your computer and use it in GitHub Desktop.
Save sr3d/2038610 to your computer and use it in GitHub Desktop.
autobuild.rb
# This file is to rebuild the dist env automatically so that you don't have to
# rerun ant deploy all the time
#
# To run, install watchr gem
#
# gem install watchr
#
# Then simply run
#
# watchr autobuild.rb
#
# Now everytime you make change to nikio or the atmosphere.js files, the
# nikio.js file will be built correctly.
require 'rexml/document'
@sources = [
"src/main/jsp/javascripts/wordnik/json2.js",
"src/main/jsp/javascripts/wordnik/wordnik.js",
"src/main/jsp/javascripts/wordnik/logger.js",
"src/main/jsp/javascripts/wordnik/constants.js",
"src/main/jsp/javascripts/wordnik/utils.js",
"src/main/jsp/javascripts/wordnik/class.js",
"src/main/jsp/javascripts/wordnik/base.js",
"src/main/jsp/javascripts/wordnik/service_handler.js",
"src/main/jsp/javascripts/wordnik/atmosphere_service_handler.js",
"src/main/jsp/javascripts/wordnik/ajax_service_handler.js",
"src/main/jsp/javascripts/wordnik/feature_driver.js",
"src/main/jsp/javascripts/wordnik/widget.js",
"src/main/jsp/javascripts/wordnik/recommendation_widget.js",
"src/main/jsp/javascripts/wordnik/glossary_widget.js",
# "src/main/jsp/javascripts/wordnik/less_recommendation_widget.js",
"src/main/jsp/javascripts/wordnik/bootstrap.js",
"src/main/jsp/nikio.js",
"src/main/jsp/javascripts/jquery.atmosphere.js"
]
watch('src/main/jsp/nikio.js') do |file|
puts "Detect changes to #{file} "
build_nikio_js
end
watch('src/main/jsp/(forbes.html|widgets.html)') do |file|
puts "Detect changes to #{file}"
begin
FileUtils.copy file.to_s, "dist/webapps/root"
puts "Done copying to dist"
rescue Exception => e
puts "*** Exception: #{e}"
end
end
watch('src/main/jsp/css/(.*)\.css') do |file|
puts "Detect changes to #{file}"
begin
FileUtils.copy file.to_s, "dist/webapps/root/css"
puts "Done copying to dist"
rescue Exception => e
puts "*** Exception: #{e}"
end
end
watch('src/main/jsp/javascripts/*/(.*)\.js') do |file|
puts "Detect changes to #{file}"
build_nikio_js
end
# watch('src/main/jsp/javascripts/(.*)\.js') do |file|
# puts "Detect changes to #{file}"
# build_nikio_js
# end
watch('src/main/scalate/widgets/(.*)\.ssp') do |file|
puts "Detect changes to #{file}"
begin
FileUtils.copy file.to_s, "dist/webapps/root/WEB-INF/classes"
FileUtils.copy file.to_s, "dist/webapps/root/WEB-INF/widgets"
puts "Copy #{file} to dist/webapps/root/WEB-INF/classes"
rescue Exception => e
puts e
end
end
watch('src/main/jsp/jasmine/*.*') do |file|
puts "Detect changes to src/main/jsp/jasmine"
copy_jasmine_unit_tests
end
watch('build/(.*)\.jar') do |file|
puts "Detect changes to #{file}"
begin
FileUtils.copy file.to_s, "dist/lib"
puts "Copy #{file} to dist/lib"
rescue Exception => e
puts e
end
end
def concatenate_files sources
out = ""
sources.each do |source|
begin
File.open(source, "r") do |f|
out += "// #{source}\n"
out += f.read + "\n"
end
rescue Exception => e
puts e
end
end
out
end
def number_with_delimiter(number, delimiter=",")
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
end
¸
def write_file file, content
File.open(file, "w") { |f| f.write(content) }
end
def build_nikio_js
out = "dist/webapps/root/v1/nikio.js"
write_file out, concatenate_files(@sources)
size = number_with_delimiter(File.stat(out).size)
puts "Done rebuilding nikio.js #{ size } bytes"
copy_jasmine_unit_tests
update_build_xml
end
def copy_to_jasmine
src = "dist/webapps/root/v1/nikio.js"
dest = "src/test/widgets/jasmine/public/javascripts"
FileUtils.copy src, dest
puts "Copied #{src} to #{dest}"
end
def copy_jasmine_unit_tests
src = "src/main/jsp/jasmine"
dest = "dist/webapps/root"
FileUtils.rm_rf "#{dest}/jasmine" # reset the jasmine folder
FileUtils.cp_r src, dest
puts "Copied #{src} to #{dest}"
end
def update_build_xml
begin
build_xml = REXML::Document.new(IO.read("build.xml"))
node = build_xml.elements["//concat[@destfile='dist/webapps/root/v1/nikio.js']"]
node.elements.delete_all("filelist")
@sources.collect do |source|
filelist = node.add_element("filelist", { 'dir' => File.dirname(source), 'files' => File.basename(source) } )
end
# File.open("build1.xml","w") { |f| f.write(build_xml) }
file = File.open("build.xml", "w")
formatter = REXML::Formatters::Default.new(2)
formatter.write(build_xml, file)
file.close
puts "Updated build.xml"
rescue Exception => e
puts "*** Exception: update_build_xml: #{e}"
puts e.backtrace
end
end
# build_nikio_js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment