|
#!/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 |