#!/usr/bin/env ruby require 'rubygems' require 'bundler/setup' require 'fileutils' require 'term/ansicolor' extend Term::ANSIColor require 'rubyapp' def usage <<-USAGE usage: bundle exec rubyapp Available commands include: create_application Create a basic application in the folder . create_page Create a baisc page consisting of .rb, .html, .css. and .js files USAGE end def create_folder(application, path) print "Creating #{path.sub(FileUtils.pwd, '.')} ... " if File.exists?(path) puts "#{bold}#{red}exists#{reset}" else FileUtils.mkdir(path) puts "#{bold}#{green}created#{reset}" end end def copy_file(application, source_path, destination_path) destination_file = File.join(destination_path, File.basename(source_path)) print "Creating #{destination_file.sub(FileUtils.pwd, '.')} ... " if File.exists?(destination_file) puts "#{bold}#{red}exists#{reset}" else FileUtils.cp(source_path, destination_path) system "sed 's/\$APPLICATION_DOWNCODE/#{downcode(application)}/' < '#{destination_file}' | sed 's/\$APPLICATION_UPCODE/#{upcode(application)}/' > '#{destination_file}.out'" FileUtils.rm(destination_file) FileUtils.mv("#{destination_file}.out", destination_file) puts "#{bold}#{green}created#{reset}" end end def copy_folder(application, source_path, destination_path) create_folder application, destination_path Dir.new(source_path).each do |item| if File.directory?(File.join(source_path, item)) copy_folder application, File.join(source_path, item), File.join(destination_path, File.basename(item)) unless item.start_with?('.') else copy_file application, File.join(source_path, item), destination_path unless item.start_with?('.') end end end def downcode(value) value = value.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') value = value.gsub(/([a-z\d])([A-Z])/,'\1_\2') value = value.tr("-", "_") value.downcase end def upcode(value) value end case $*[0].downcase when 'create_application' application = $*[1] folder = File.join(FileUtils.pwd, application) copy_folder application, File.join(File.dirname(__FILE__), %w[.. lib rubyapp template]), folder else puts "Blank or unrecognized command '#{$*[0].downcase}'" puts usage end