#!/usr/bin/env ruby module InlineForms require File.join(File.dirname(__FILE__), "../lib/inline_forms/version.rb") # what is this? Signal.trap("INT") { puts; exit } require 'thor' class Creator < Thor include Thor::Actions def self.source_root File.dirname(__FILE__)+"/.." end desc "create APP", "create an application with inline_forms v#{VERSION}" DATABASE_OPTIONS = %w(sqlite mysql) method_option :database, :aliases => "-d", :banner => DATABASE_OPTIONS.join('|'), :desc => 'specify development database' method_option :example, :type => :boolean, :desc => 'install the example app. uses sqlite as development database' method_option :email, :aliases => "-e", :default => "admin@example.com", :desc => 'specify admin email' method_option :password, :aliases => "-p", :default => "admin999", :desc => 'specify admin password' method_option :runtest, :aliases => "\-\-run-test", :default => false, :desc => 'run tests' method_option :skiprvm, :aliases => "\-\-no-rvm", :type => :boolean, :default => false, :desc => 'install inline_forms without RVM' def create(app_name) def self.skiprvm options[:skiprvm] end def self.runtest options[:runtest] end def self.install_example? options[:example] end def self.database @database ||= options[:database] return @database if DATABASE_OPTIONS.include?(@database) say "No Database specified please choose one database #{DATABASE_OPTIONS.join(' | ')}", :red # if the database is not set ask user which database to use while ! DATABASE_OPTIONS.include?(@database) @database = ask "Database: " return @database if DATABASE_OPTIONS.include?(@database) end end def self.using_sqlite? database == 'sqlite' end def self.email options[:email] end def self.password options[:password] end if install_example? && !using_sqlite? say "--example can only be used with an sqlite development database", :red exit 1 end say "Creating #{app_name} with inline_forms v#{VERSION} and development database #{database}...", :green regex = /\A[0-9a-zA-Z][0-9a-zA-Z_-]+[0-9a-zA-Z]\Z/ if ! regex.match(app_name) say "Error: APP must match #{regex.source}", :red exit 1 end if File.exists?(app_name) say "Error: APP exists", :red exit 1 end require 'rvm' # if RVM is detected and the user has not disabled using rvm via command than use rvm else without if RVM.current && !options[:skiprvm] # Let the user know that he are installing the inline_forms with support of RVM say "Installing inline_forms with RVM", :green # which ruby version is currently activated? ruby_version = (%x[rvm current]).gsub(/@.*/,'') # Create a ruby rvm-file version based on the version detected create_file "#{app_name}/.ruby-version", ruby_version # Creat a ruby-gemset rvm-file based on the version detected create_file "#{app_name}/.ruby-gemset", app_name else # Let the user know that he is installing inline_forms without RVM say "Installing inline_forms without RVM", :green end say "Installing with #{options[:database]}", :green # Creates the app directory empty_directory(app_name) # puts all options in environment to app_template options.each do | k,v | ENV[k] = v.to_s end ENV['using_sqlite'] = using_sqlite?.to_s ENV['database'] = database ENV['install_example'] = install_example?.to_s ENV['ruby_version'] = ruby_version ENV['inline_forms_version'] = VERSION app_template_file = File.join(File.dirname(__FILE__), 'inline_forms_app_template.rb') if ! run("rails _3.2.21_ new #{app_name} -m #{app_template_file} --skip-bundle --skip-gemfile --skip-test-unit") say "Rails could not create the app '#{app_name}', maybe because it is a reserved word...", :red # TODO ROYTJE MAKE ERROR MESSAGE MORE RELEVANT # Rails could not create the app 'MyApp', maybe because it is a reserved word.. exit 1 end end Creator.start end end