Skip to content

Instantly share code, notes, and snippets.

@robin850
Created April 6, 2013 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin850/5325645 to your computer and use it in GitHub Desktop.
Save robin850/5325645 to your computer and use it in GitHub Desktop.

Bamboo

Bamboo is a little Ruby script to reorganize Dojo Toolkit assets. It's designed to reorganize correctly assets for a Ruby on Rails structure for the dojo-rails gem since Rails needs to seperate stylesheets, javascripts and images. The script is pretty simple but needs some dependencies. To install them, just run

$ gem install path rubyzip progressbar

Then just clone the gist (the Ruby code) and execute the script. You need to specify an archive or a folder to reorganize :

$ wget http://download.dojotoolkit.org/release-1.8.3/dojo-release-1.8.3.zip

Then execute the script :

$ bamboo -z dojo-release-1.8.3.zip

You can also specify a folder instead of an archive

$ bamboo -f dojo-release

The code is a little bit crappy because very simple but feel free to improve this gist to help us improve dojo-rails. The code is licensed under the MIT license. See dojo-rails's repo's README.

#!/usr/bin/env ruby
require 'optparse'
require 'ostruct'
require 'path'
require "progressbar"
require 'zip/zip'
Config = OpenStruct.new
# Option parse for the shell script
OptionParser.new do |opts|
opts.banner = "Usage: bamboo [-f folder] [-z zip]"
opts.on("-d FOLDER", "The folder where the full dojo is located") do |f|
Config.folder = f
end
opts.on("-z ARCHIVE", "The archive containing the full dojo (zip)") do |a|
Config.archive = a
end
end.parse!
# Uncompress the archive if one is specified and raise an
# exception in case we don't have specified any folder or
# any archive
if Config.archive
Zip::ZipFile.open(Config.archive) do |zip_file|
Config.zip_file = zip_file
Config.source = Config.archive
Config.source.gsub!(".zip", "")
ProgressBar.new("Extracting", zip_file.entries.length) do |pbar|
zip_file.each do |f|
f_path = File.join(Config.source, f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
pbar.inc
end
end
end
Config.source = Path(Config.source).expand
elsif Config.folder
Config.source = Config.folder
else
raise ArgumentError, "You haven't specified any source"
end
# The script itself:
# * We create a Proc containing some treatements
# * We yield the proc in Config.source
# * Then we relocate each file
# * Done!
#
# The proc check each file extensions and then push them into
# a specific Array (one for each type of file). Finnaly, we
# move all files to their final location
module Kernel
def rejected?(file)
rejected = ["license", "readme"]
ext = [".uncompressed.js", ".html"]
name = file.basename.to_s.downcase
rejected.delete_if do |e|
name.start_with?(e)
end
ext.delete_if do |e|
name.end_with?(e)
end
(rejected + ext).length != 4
end
def image?(file)
["png", "jpg", "jpeg", "gif"].include?(file.ext)
end
def stylesheet?(file)
["less", "css"].include?(file.ext)
end
end
Config.images = []
Config.stylesheets = []
process = proc {|e, pbar|
if !e.directory?
if image?(e)
Config.images << e
elsif stylesheet?(e)
Config.stylesheets << e
elsif rejected?(e)
File.delete(e.expand.to_s)
end
else
e.children.each {|e| process.call(e, pbar)}
end
pbar.inc
}
ProgressBar.new("Sorting", Config.zip_file.entries.length) do |pbar|
source_children = Config.source.children
Config.source = (source_children.length == 1) ? source_children[0] : source_children
Config.source.children.each {|e| process.call(e, pbar) }
end
# Relocate
[:images, :stylesheets].each do |e|
Path(Config.source / e).mkdir
ProgressBar.new(e.to_s.capitalize, Config.zip_file.entries.length) do |pbar|
Config.send(:"#{e}").each do |i|
location = Path(Config.source.expand.to_s + "/#{e}" + i.expand.to_s.gsub(Config.source.to_s, ""))
location.expand.parent.mkdir_p
i.mv(location)
pbar.inc
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment