Skip to content

Instantly share code, notes, and snippets.

@rgrove
Forked from tivac/rakefile.rb
Created September 3, 2010 19:11
Show Gist options
  • Save rgrove/564388 to your computer and use it in GitHub Desktop.
Save rgrove/564388 to your computer and use it in GitHub Desktop.
require 'yui-utils'
task :default, [:file, :replace, :filter, :jsdir] do |t, args|
args.with_defaults(
:filter => nil,
:jsdir => nil,
:replace => nil
)
modules = YUIUtils.build_module_list(args.filter, args.jsdir)
YUIUtils.output(modules, args.file, args.replace)
end
class YUIUtils
@@module_regex = /YUI\.add\((['"][\w\-]+['"]).*\}, ['"].+['"], \{\s*(.+)\s*\}\);?/mi
def self.build_module_list(filter = "**/*.js", jsdir = "Public/js")
out = []
FileList[filter].each do |file|
match = File.read(file).scan(@@module_regex)
if match[0] && match[0][0] && match[0][1] #filter out files that didn't match
#prettier names
yuimodule = match[0][0]
config = match[0][1]
out.push(%(\t#{yuimodule} : {\n\t\t"path" : "#{file.gsub(jsdir, "")}",\n\t\t#{config}\n\t}))
end
end
"{\n#{out.join(",\n")}\n}"
end
def self.output(modules, filename, replace = false)
File.open(filename, "r+") do |file|
contents = (replace) ? file.read.gsub(replace, modules) : modules
file.puts(contents)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment