Created
May 15, 2012 21:57
-
-
Save claydotio/2705464 to your computer and use it in GitHub Desktop.
JS Rakefile (compiles & compresses multiple .js files into 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake' | |
require 'rubygems' | |
require 'uglifier' | |
require 'fssm' | |
CLI_SRC_PATH = './src/client' # path to source code for client side | |
SVR_SRC_PATH = './src/server' # path to source code for server side | |
SHARED_SRC_PATH = './src/shared' # path to source code that is shared between client & server | |
CLI_OUT_PATH = './build/client/game' # client build path | |
SVR_OUT_PATH = './build/server/server' # server build path | |
# read_manifest: accepts a directory containing a manifest.js | |
# file, and returns an array of the files required in the manifest | |
# e.g. read_manifest('src/client') => ['A.js', 'B.js'] | |
def read_manifest(manifest_path) | |
manifest = IO.read File.join(manifest_path, 'manifest.js') | |
manifest.scan(/\/\/= require '(.*)'/) | |
end | |
# join_filenames: accepts an array of filenames and an optional | |
# base path, and flattens them into a string for cmd line usage | |
# e.g. join_filenames(['A.js', 'B.js'], '../') => "../A.js ../B.js" | |
def join_filenames(filenames, base='./') | |
filenames.collect {|f| File.expand_path(File.join(base, f)) }.join ' ' | |
end | |
desc "Packages client javascript." | |
task :client do | |
# read names of required files in manifest.js | |
cli_file_array = read_manifest(CLI_SRC_PATH) | |
js = cli_file_array.collect { |f| | |
IO.read File.expand_path(File.join(CLI_SRC_PATH, f)) | |
}.join "\n" | |
File.open("#{CLI_OUT_PATH}.js", 'w') { |f| f.write(js) } | |
minjs = Uglifier.new.compile(js) | |
# build and minify the whole thing into CLI_OUT_PATH | |
File.open("#{CLI_OUT_PATH}.min.js", 'w') { |f| f.write(minjs) } | |
puts "#{CLI_OUT_PATH}.js packaged" | |
end | |
# no need to minify server-side stuff | |
desc "Packages server javascript." | |
task :server do | |
svr_file_array = read_manifest(SVR_SRC_PATH) | |
js = svr_file_array.collect { |f| | |
IO.read File.expand_path(File.join(SVR_SRC_PATH, f)) | |
}.join "\n" | |
File.open("#{SVR_OUT_PATH}.js", 'w') { |f| f.write(js) } | |
puts "#{SVR_OUT_PATH}.js packaged" | |
end | |
# watch and wait for changes, then call `rake client` or | |
# `rake server` to compile the changes | |
desc "Waits for changes to files, then recompiles." | |
task :watch do | |
FSSM.monitor do | |
puts "Watching for client changes in " + CLI_SRC_PATH | |
path CLI_SRC_PATH do | |
update do |base, relative| | |
puts "Client file changed, Recompiling..." | |
system "rake client" | |
end | |
end | |
puts "Watching for server changes in " + SVR_SRC_PATH | |
path SVR_SRC_PATH do | |
update do |base, relative| | |
puts "Server file changed, Recompiling..." | |
system "rake server" | |
end | |
end | |
puts "Watching for shared changes in " + SHARED_SRC_PATH | |
path SHARED_SRC_PATH do | |
update do |base, relative| # recompile everything! | |
puts "Server and client dependent file changed, Recompiling..." | |
system "rake server" | |
system "rake client" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment