Last active
December 13, 2015 18:38
-
-
Save ronan-mch/4956934 to your computer and use it in GitHub Desktop.
This program takes files of a given type and bundles them together into a single complete file. It is intended for web developers who need to develop their css and js in separate files for the sake of modularity, but need to reduce the number of HTTP requests to increase page serve time. It can either take a list of files or the name of a direct…
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
# == Synopsis | |
# This application bundles multiple files into a | |
# single file. It is intended for use with js and css | |
# files to allow developers to work on modular files | |
# which can be bundled together to minimise http requests. | |
# == Examples | |
# This command bundles all css files in the relative | |
# directory styles and its subdirectories into one | |
# file complete.css. | |
# bundler .css styles | |
# == Usage | |
# bundler -r [.@extension] [folder] | |
# bundler -l [.@extension] [file] [file] [file] | |
# exit if we don't get any arguments | |
# == Author | |
# Ronan McHugh mchugh[dot]r[at]gmail[dot]com | |
#require 'rdoc/usage' | |
require 'optparse' | |
class Bundler | |
#scan through directory find any files with correct @extension | |
def getFiles(folder) | |
Dir.chdir(folder) | |
targetFiles = [] | |
Dir.entries('.').each { | |
|entryName| | |
#grab any of the correct filetypes | |
if entryName.include? @extension | |
targetFiles << File.absolute_path(entryName) | |
#if it's a directory, call self recursively | |
#TODO - fix 2nd condition to be more precise | |
elsif File.directory?(entryName) && !entryName.include?('.') | |
targetFiles += getFiles(entryName) | |
end | |
} | |
return targetFiles | |
end | |
#combine given files and write as bundle to dest file | |
def bundleFiles(files) | |
Dir.chdir(@startDir) | |
#open each file and read it | |
files.each{ | |
|fileName| | |
File.open(fileName) do |file| | |
#write it to common file | |
file.each_line{|line| @complete.write(line)} | |
end | |
} | |
end | |
#this should actually be using rdoc::usage, but I couldn't | |
#get it working properly on my system - will fix at | |
#some stage | |
#RDoc::usage('usage') if (ARGV.length < 2) | |
def initialize(arguments) | |
if (arguments.length < 2) | |
puts "Insufficient arguments - see documentation for more information."; exit | |
end | |
@arguments = arguments | |
@extension = @arguments[1] | |
@extension = "." + @extension if @extension[0] != "." | |
@method = "recursive" | |
end | |
def run | |
if parsed_options? | |
@startDir = Dir.pwd | |
@complete = File.new("complete"+@extension, "w") #create file | |
#get desired file @extension | |
if @method == "list" | |
run_list | |
else | |
run_recursive | |
end | |
else | |
puts "error parsing options" | |
end | |
end | |
#run through list of files bundling all together | |
def run_list | |
files = @arguments.drop(1) | |
bundleFiles(files) | |
end | |
# run through given folder recursively | |
# bundling all matching files found | |
def run_recursive | |
folder = @arguments[1] #get target folder | |
files = getFiles(folder) #find all files | |
bundleFiles(files) #write complete file | |
end | |
def parsed_options? | |
opts = OptionParser.new | |
opts.on('-v', '--version') { puts 1.0; exit 0 } | |
opts.on('-h', '--help') { puts "help"; exit 0 } | |
opts.on('-l', '--list') { @method = "list" } | |
opts.on('-r', '--recursive') { @method = "recursive" } | |
opts.parse!(@arguments) rescue return false | |
end | |
end | |
bundler = Bundler.new(ARGV) | |
bundler.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment