#!/usr/bin/env ruby require 'optparse' require 'ftools' options = {} OptionParser.new do |opts| opts.banner = "Usage: #{File.basename($0)} [path]" opts.on("-h", "--help", "Displays this help info") do puts opts exit 0 end opts.on('--grails', "Include the groovy/grails specific rake tasks.") { |val| options[:grails] = val } #opts.on('--csharp', "Include the groovy/grails specific rake tasks.") { |val| options[:grails] = val } begin opts.parse!(ARGV) rescue OptionParser::ParseError => e warn e.message puts opts exit 1 end end if ARGV.empty? abort "Please specify the directory to migratify, e.g. `#{File.basename($0)} .'" elsif !File.exists?(ARGV.first) abort "`#{ARGV.first}' does not exist." elsif !File.directory?(ARGV.first) abort "`#{ARGV.first}' is not a directory." elsif ARGV.length > 1 abort "Too many arguments; please specify only the directory to migratifu." end def unindent(string) indentation = string[/\A\s*/] string.strip.gsub(/^#{indentation}/, "") end dirs = [ 'db','db/migrate', 'db/config', 'rake' ] files = { "Rakefile" => unindent(<<-FILE), require 'rake' Dir[ 'rake/**/*.rake' ].each { |rake| load rake } task :default do warn "\n[warn] You must specify a rake task to run\n" end FILE } gem_which_loc = "" gem_name = "migratification" IO.popen( 'gem which migratification' ) { |f| gem_which_loc = f.gets } #gem_which_loc = `gem which migratification` puts "\njason" puts "\n\nlocation: #{gem_which_loc}" gem_which_loc.gsub!( /\n/, '' ) gem_which_loc.gsub!( /\/lib\/#{gem_name}.rb/, '' ) if gem_which_loc =~ /\/lib\/#{gem_name}.rb/ gem_which_loc.gsub!( /#{gem_name}.rb/, '' ) if gem_which_loc =~ /#{gem_name}.rb/ puts "\nCreating directories..." # Create the directories dirs.each do |dir| if File.exists?(dir) warn "[skip] `#{dir}' already exists" elsif File.exists?(dir.downcase) warn "[skip] `#{dir.downcase}' exists, which could conflict with `#{file}'" else puts "[create] directory `#{dir}'" FileUtils.mkdir_p( dir ) end end puts "\nCreating Rakefile..." # Create the files base = ARGV.shift files.each do |file, content| file = File.join(base, file) if File.exists?(file) warn "[skip] `#{file}' already exists" elsif File.exists?(file.downcase) warn "[skip] `#{file.downcase}' exists, which could conflict with `#{file}'" elsif !File.exists?(File.dirname(file)) warn "[skip] directory `#{File.dirname(file)}' does not exist" else puts "[add] writing `#{file}'" File.open(file, "w") { |f| f.write(content) } end end puts "\nCopying resources..." # Copy the other files Dir["#{gem_which_loc}/resources/rake/all/**/*.rake"].each do |file| file_path = File.join( base, "rake", File.basename( file ) ) if File.exists?(file_path) warn "[skip] `#{file_path}' already exists" elsif File.exists?(file_path.downcase) warn "[skip] `#{file_path.downcase}' exists, which could conflict with `#{file}'" elsif !File.exists?(File.dirname(file_path)) warn "[skip] directory `#{File.dirname(file_path)}' does not exist" else puts "[add] writing `#{file_path}'" File.copy( file, file_path, false ) end end if options[:grails] == true file_name = "grails.rake" file_path = File.join( base, "rake", file_name ) puts "[add] writing `#{file_path}'" FileUtils.copy( File.join( gem_which_loc, "resources", "rake", "optional", file_name ), file_path ) end puts "\n[done] migratified!\n\n"