Skip to content

Instantly share code, notes, and snippets.

@tivac
Created September 3, 2010 17:40
Show Gist options
  • Save tivac/564239 to your computer and use it in GitHub Desktop.
Save tivac/564239 to your computer and use it in GitHub Desktop.
require 'yui-utils'
task :yui_modules do
output = YUIUtils.build_module_list()
#YUIUtils.output(modules)
end
class YUIUtils
@@module_regex = /YUI\.add\((['"][\w\-]+['"]).*\}, ['"].+['"], \{\s*(.+)\s*\}\);?/mi
def self.build_module_list(opts = {})
out = []
opts["filter"] = (opts["filter"].kind_of? String) ? opts["filter"] : "**/*.js"
opts["jsdir"] = (opts["jsdir"].kind_of? String) ? opts["filter"] : "Public/js/"
FileList[opts["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(opts["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, "w") do |file|
contents = (replace) ? file.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