require 'rubygems' require 'templater' module Rhoconnect extend Templater::Manifold extend Rhoconnect desc <<-DESC Rhoconnect generator DESC class BaseGenerator < Templater::Generator def class_name name.gsub('-', '_').camel_case end def underscore_name Rhoconnect.under_score(name) end def gem_version VERSION end # Callback method to run ERB on destination file def run_erb(action) template = ERB.new(File.read(action.destination)) File.open(action.destination, "w") { |f| f << template.result(binding) } end def self.install_js_option(klass) klass.option :js, :required => false, :default => false, :desc => "generate JavaScript code", :as => :boolean end def self.install_app_options(klass) klass.first_argument :name, :required => true, :desc => "application name" install_js_option(klass) end def self.install_source_options(klass) klass.first_argument :name, :required => true, :desc => "source name" install_js_option(klass) end alias_method :module_name, :class_name end class AppGenerator < BaseGenerator install_app_options(self) def self.source_root File.join(File.dirname(__FILE__), 'templates', 'application') end desc <<-DESC Generates a new rhoconnect application. Required: name - application name DESC # 'template' does not work under jruby. Using 'file' and after hook instead file :configru, :after => :run_erb do |template| require 'securerandom' rescue nil @secret = SecureRandom.hex(64) rescue '' template.source = 'config.ru' template.destination = "#{name}/config.ru" end template :gitignore do |template| template.source = 'gitignore' template.destination = "#{name}/.gitignore" end template :settings do |template| template.source = 'settings/settings.yml' template.destination = "#{name}/settings/settings.yml" end template :license do |template| template.source = 'settings/license.key' template.destination = "#{name}/settings/license.key" end template :rakefile do |template| template.source = 'Rakefile' template.destination = "#{name}/Rakefile" end # 'template' does not work under jruby. Using 'file' and after hook instead file :gemfile, :after => :run_erb do |file| file.source = 'Gemfile' file.destination = "#{name}/Gemfile" end template :gemfile_app do |template| template.source = 'rcgemfile' template.destination = "#{name}/.rcgemfile" end empty_directory :public do |dir| dir.destination = File.join("#{name}", 'public') end invoke :js_app, :js => true invoke :ruby_app, :js => false def after_run install_gems_note = <<_BUNDLE_INSTALL_ In the future, to ensure that all the dependencies in your rhoconnect application are available execute these commands: cd #{name} && bundle install If you're setting up the application in a production environment run the following: cd #{name} && bundle install --without=test development _BUNDLE_INSTALL_ running_bundler_first_time = <<_RUN_BUNDLER Executing 'bundle install' for the first time in your freshly baked application! bundle install --gemfile=#{destination_root}/#{name}/Gemfile _RUN_BUNDLER puts running_bundler_first_time system("bundle install --gemfile=#{destination_root}/#{name}/Gemfile") puts install_gems_note end end class RubyAppGenerator < AppGenerator install_app_options(self) template :application do |template| template.source = 'controllers/ruby/application_controller.rb' template.destination = "#{name}/controllers/ruby/application_controller.rb" end template :application_spec do |template| template.source = 'spec/application_controller_spec.rb' template.destination = "#{name}/spec/controllers/ruby/application_controller_spec.rb" end template :spec_helper do |template| template.source = 'spec/spec_helper.rb' template.destination = "#{name}/spec/spec_helper.rb" end end class JsAppGenerator < AppGenerator install_app_options(self) file :packagejson, :after => :npm_install do |template| template.source = 'package.json' template.destination = "#{name}/package.json" end template :application do |template| template.source = 'controllers/js/application_controller.js' template.destination = "#{name}/controllers/js/application_controller.js" end def npm_install(action) npm_install_note = <<_NPM_INSTALL_ In the future, to ensure that all the JavaScript dependencies in your rhoconnect application are available execute these commands: cd #{name} && npm install _NPM_INSTALL_ puts npm_install_note FileUtils.chdir(name) system("npm install") FileUtils.chdir('..') end end class SourceGenerator < BaseGenerator install_source_options(self) def self.source_root File.join(File.dirname(__FILE__), 'templates', 'source') end desc <<-DESC Generates a new source adapter (Controller/Model pair). Required: name - source name (i.e. product) DESC invoke :ruby_source, :js => false invoke :js_source, :js => true end class RubySourceGenerator < SourceGenerator install_source_options(self) # 'template' does not work under jruby. Using 'file' and after hook instead file :model, :after => :run_erb do |file| file.source = 'models/ruby/model.rb' file.destination = "models/ruby/#{underscore_name}.rb" settings_file = File.join(@destination_root,'settings','settings.yml') settings = YAML.load_file(settings_file) settings[:sources] ||= {} settings[:sources][class_name] = {:poll_interval => 300} File.open(settings_file, 'w' ) do |f| f.write "#Sources" + {:sources => settings[:sources]}.to_yaml[3..-1] envs = {} [:development,:test,:production].each do |env| envs[env] = settings[env] end f.write envs.to_yaml[3..-1] # write all other settings [:development, :test, :production, :sources].each do |key| settings.delete(key) end f.write settings.to_yaml[3..-1] unless settings.empty? end end file :controller, :after => :run_erb do |file| file.source = 'controllers/ruby/controller.rb' file.destination = "controllers/ruby/#{underscore_name}_controller.rb" end # 'template' does not work under jruby. Using 'file' and after hook instead file :model_spec, :after => :run_erb do |file| file.source = 'models/ruby/model_spec.rb' file.destination = "spec/models/ruby/#{underscore_name}_spec.rb" end # 'template' does not work under jruby. Using 'file' and after hook instead file :controller_spec, :after => :run_erb do |file| file.source = 'controllers/ruby/controller_spec.rb' file.destination = "spec/controllers/ruby/#{underscore_name}_controller_spec.rb" end end class JsSourceGenerator < SourceGenerator install_source_options(self) file :model, :after => :run_erb do |file| file.source = 'models/js/model.js' file.destination = "models/js/#{underscore_name}.js" end file :controller, :after => :run_erb do |file| file.source = 'controllers/js/controller.js' file.destination = "controllers/js/#{underscore_name}_controller.js" end end add :app, AppGenerator add_private :ruby_app, RubyAppGenerator add_private :js_app, JsAppGenerator add :source, SourceGenerator add_private :ruby_source, RubySourceGenerator add_private :js_source, JsSourceGenerator end