#!/usr/bin/env ruby module InlineForms require File.join(File.dirname(__FILE__), "../lib/inline_forms/version.rb") require 'rvm' if not RVM.current puts "ruby or rvm not found" exit 2 end # what is this? Signal.trap("INT") { puts; exit } require 'thor' class Creator < Thor include Thor::Actions String.class_eval do def strip_heredoc_with_indent(indent=0) new_indent = ( self.empty? ? 0 : ( scan(/^[ \t]*(?=\S)/).min.size - indent ) ) gsub(/^[ \t]{#{new_indent}}/, '') end end 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", :default => DATABASE_OPTIONS.first, :banner => DATABASE_OPTIONS.join('|'), :desc => 'specify development database' method_option :dry, :type => :boolean, :desc => 'dry run. Do not do most things. Only useful for development of inline_forms' method_option :example, :type => :boolean, :desc => 'install the example app. incompatible with --dry and 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' def create(app_name) def self.dry_run? options[:dry] end def self.install_example? options[:example] end def self.database DATABASE_OPTIONS.include?(options[:database]) ? options[:database] : 'sqlite' end def self.using_sqlite? database == 'sqlite' end def self.email options[:email] end def self.password options[:password] end if install_example? && dry_run? say "--example and --dry-run can not be used together", :red exit 1 end if install_example? && !using_sqlite? say "--example can only be used with an sqlite development database", :red exit 1 end say "This is a dry run. I hope you know what you are doing...", :red if dry_run? 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 if dry_run? empty_directory(app_name) else empty_directory(app_name) ruby_version = (%x[rvm current]).gsub(/@.*/,'') create_file "#{app_name}/.ruby-version", ruby_version create_file "#{app_name}/.ruby-gemset", app_name app_template_file = File.join(File.dirname(__FILE__), 'inline_forms_app_template.rb') if ! run("rails _3.2.12_ 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 exit 1 end end end Creator.start end end