require 'open-uri' require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'restfulx') if !defined?(RestfulX) class EmergentConfigGenerator < Rails::Generator::NamedBase include RestfulX::Configuration attr_reader :project_name, :flex_project_name, :base_package, :base_folder, :command_controller_name, :component_names, :application_tag, :use_air, # Added :model_names, :event_names def initialize(runtime_args, runtime_options = {}) super @project_name, @flex_project_name, @command_controller_name, @base_package, @base_folder = extract_names # if we are updating main file only we probably want to maintain the type of project it is if options[:main_only] project_file_name = APP_ROOT + '/.project' if File.exist?(project_file_name) puts "Cannot combine -m (--main-app) and -a (--air) flags at the same time for an existing application.\n" << 'If you want to convert to AIR, remove -m flag.' if options[:air_config] @use_air = true if File.read(project_file_name) =~/com.adobe.flexbuilder.apollo.apollobuilder/m else puts "Flex Builder project file doesn't exist. You should run 'rx_config' with -a (--air) option " << "or no arguments first to generate primary project structure." exit 0; end else @use_air = options[:air_config] end if @use_air @application_tag = 'WindowedApplication' else @application_tag = 'Application' end @component_names = [] if File.exists?("app/flex/#{base_folder}/components/generated") @component_names = list_mxml_files("app/flex/#{base_folder}/components/generated") end end def manifest record do |m| if !options[:main_only] m.file 'core/restfulx_tasks.rake', 'lib/tasks/restfulx_tasks.rake' m.file 'core/emergent_tasks.rake', 'lib/tasks/emergent_tasks.rake' m.file 'core/flex.properties', '.flexProperties' m.file 'core/restfulx.yml', 'config/restfulx.yml' if @use_air m.template 'core/actionscriptair.properties', '.actionScriptProperties' m.template 'core/projectair.properties', '.project' else m.template 'core/actionscript.properties', '.actionScriptProperties' m.template 'core/project.properties', '.project' end m.directory 'html-template/history' %w(index.template.html AC_OETags.js playerProductInstall.swf).each do |file| m.file "core/html-template/#{file}", "html-template/#{file}" end %w(history.css history.js historyFrame.html).each do |file| m.file "core/html-template/history/#{file}", "html-template/history/#{file}" end # DIRECTORY # src directory %w(extras assets).each do |dir| m.directory "app/flex/#{dir}" end %w(models views controllers).each do |dir| m.directory "app/flex/#{base_folder}/#{dir}" end # models directory %w(domain constant factory presentation).each do |dir| m.directory "app/flex/#{base_folder}/models/#{dir}" end # views directory %w(factory main navigation pages home aboutUs contactUs gallery login account profile document).each do |dir| m.directory "app/flex/#{base_folder}/views/#{dir}" end # controllers directory %w(command event manager map).each do |dir| m.directory "app/flex/#{base_folder}/controllers/#{dir}" end # assets directory %w(icons fonts styles images text html videos audio pdfs).each do |dir| m.directory "app/flex/assets/#{dir}" end m.directory "config" # RAILS FILES # library and general config files m.template 'lib/authenticated_system.rb', File.join("lib", "authenticated_system.rb") m.template 'lib/authenticated_test_helper.rb', File.join("lib", "authenticated_test_helper.rb") m.template 'lib/flash_session_hack.rb', File.join("config/initializers", "flash_session_hack.rb") m.template 'lib/site_keys.rb', File.join("config/initializers", "site_keys.rb") m.template 'lib/app_controller.rb.erb', File.join("app/controllers", "application.rb") m.template 'config/Capfile', File.join("", "Capfile") m.template 'config/deploy.rb', File.join("config", "deploy.rb") # Models %w(account account_mailer account_observer address asset asset_assignment category content content_assignment image post section site tag user).each do |file| m.template "app/models/#{file}.rb", "app/models/#{file}.rb" end # Account Views m.directory "app/views/account_mailer" %w(activation signup_notification).each do |file| m.file "app/views/account_mailer/#{file}.erb", File.join("app/views/account_mailer/#{file}.erb") end # Controllers %w(accounts_controller addresses_controller users_controller sites_controller sessions_controller contents_controller content_assignments_controller).each do |file| m.template "app/controllers/#{file}.rb", "app/controllers/#{file}.rb" end # Helpers m.file 'app/helpers/accounts_helper.rb', File.join("app/helpers", "accounts_helper.rb") # m.template 'controls/accounts_helper.rb.zip', File.join("app/helpers", # "accounts_helper.rb") # Routing file! This is where everything happens! m.template 'config/routes.rb', File.join("config", "routes.rb") # FLEX FILES # Factories # Maps %w(ResourceFactory DocumentFactory DocumentViewFactory).each do |file| m.template "app/flex/application/models/factory/#{file}.as", "app/flex/#{base_folder}/models/factory/#{file}.as" end # Maps %w(ManagerPresentationModelMap PresentationModelViewMap PresenterViewMap ObjectMap DocumentsMap DocumentMap).each do |file| m.template "app/flex/application/controllers/map/#{file}.mxml", "app/flex/#{base_folder}/controllers/map/#{file}.mxml" end # Managers %w(AboutUsManager AccountManager AuthenticationManager ContentManager HomeManager MainManager PagesManager SiteManager ResourceManager DocumentManager DocumentsManager UndoManager).each do |file| m.template "app/flex/application/controllers/manager/#{file}.as", "app/flex/#{base_folder}/controllers/manager/#{file}.as" end # Commands %w(ICommand Undoable UpdateDocumentCommand).each do |file| m.template "app/flex/application/controllers/command/#{file}.as", "app/flex/#{base_folder}/controllers/command/#{file}.as" end # Events %w(ResourceEvent NavigationEvent DocumentEvent UndoEvent).each do |file| m.template "app/flex/application/controllers/event/#{file}.as", "app/flex/#{base_folder}/controllers/event/#{file}.as" end # Domain Models %w(Account Address Asset AssetAssignment Content ContentAssignment Session User Document RichDocument PlainDocument Snapshot).each do |file| m.template "app/flex/application/models/domain/#{file}.as", "app/flex/#{base_folder}/models/domain/#{file}.as" end # Constants %w(Resource ResourceAction ResourceResult DocumentType).each do |file| m.template "app/flex/application/models/constant/#{file}.as", "app/flex/#{base_folder}/models/constant/#{file}.as" end # Presentation Models %w(AboutUsModel AccountModel AuthenticationModel ContactUsModel DocumentModel ContentModel DocumentsModel HomeModel MainModel PagesModel ProfileModel ResourceModel NavigationBarLeftModel NavigationBarRightModel NavigationBarTopModel NavigationBarBottomModel).each do |file| m.template "app/flex/application/models/presentation/#{file}.as", "app/flex/#{base_folder}/models/presentation/#{file}.as" end # Views %w(about_us account contact_us gallery home login main pages profile).each do |file| m.template "app/flex/application/views/#{file}/#{file}.mxml", "app/flex/#{base_folder}/views/#{file.camelcase.dcfirst}/#{file.camelcase}View.mxml" end # Presenters %w(home main pages).each do |file| # FIX! m.template "app/flex/application/views/#{file}/#{file}_presenter.as", "app/flex/#{base_folder}/views/#{file}/#{file.camelcase}Presenter.as" end # Troparchies %w(home main pages).each do |file| # FIX! m.template "app/flex/application/views/#{file}/#{file}_troparchy.mxml", "app/flex/#{base_folder}/views/#{file}/#{file.camelcase}Troparchy.mxml" end # Document View Extras :) %w(documents plain_document rich_document document).each do |file| m.template "app/flex/application/views/document/#{file}.mxml", "app/flex/#{base_folder}/views/document/#{file.camelcase}View.mxml" end # Navigation Extras :) %w(navigation_bars navigation_bar_left navigation_bar_right navigation_bar_top navigation_bar_bottom).each do |file| m.template "app/flex/application/views/navigation/#{file}_view.mxml", "app/flex/#{base_folder}/views/navigation/#{file.camelcase}View.mxml" m.template "app/flex/application/views/navigation/#{file}_presenter.as", "app/flex/#{base_folder}/views/navigation/#{file.camelcase}Presenter.as" end m.template "app/flex/application/views/navigation/navigation_bars_troparchy.mxml", "app/flex/#{base_folder}/views/navigation/NavigationBarsTroparchy.mxml" # LIBRARIES AND EXTRAS # Add sample html file to aboutme %w(lorem.html).each do |file| m.file "app/flex/assets/html/#{file}", "app/flex/assets/html/#{file}" end # Add all of the SWC Libraries to the Project :) %w(as3crypto-1.3patched.swc as3ds-1.04.swc as3httpclientlib-1.0.swc as3corelib-0.92.1.swc flare.swc fluint.swc mate_0_8_6_enhanced.swc pv3d.swc tweenMax.swc flexlib.swc containerEx.swc restfulx.swc flex2BrowserManager.swc urlkitFlex3.swc openflux.swc analytics.swc).each do |file| m.file "lib/#{file}", "lib/#{file}" end # Add all of the Theme Libraries to the Style directory %w(itunes7Bg.jpg jukebox.css jukeboxGfx.swf obsidian.css obsidianBg.jpg obsidianGfx.swf).each do |file| m.file "app/flex/assets/styles/#{file}", "app/flex/assets/styles/#{file}" end # Add all of the Images used in the Papervision Skybox %w(hot_nebula_0.jpg hot_nebula_90.jpg hot_nebula_180.jpg hot_nebula_270.jpg hot_nebula_bottom.jpg hot_nebula_top.jpg).each do |file| m.file "app/flex/assets/images/#{file}", "app/flex/assets/images/#{file}" end # Add some alex grey images for showing off layouts m.directory "app/flex/assets/images/alexgrey" %w(Picture3.png Picture4.png Picture5.png Picture6.png Picture7.png Picture8.png Picture9.png Picture10.png Picture11.png Picture12.png Picture13.png Picture14.png).each do |file| m.file "app/flex/assets/images/alexgrey/#{file}", "app/flex/assets/images/alexgrey/#{file}" end # Add all of the Fonts to the assets/font directory %w(MyriadPro-Bold.otf MyriadPro-Regular.otf).each do |file| m.file "app/flex/assets/fonts/#{file}", "app/flex/assets/fonts/#{file}" end m.dependency 'rx_controller', @args # Add all of our Migrations m.directory "db/migrate" unless options[:skip_migration] %w(assets asset_assignments addresses accounts sessions contents content_assignments users).each_with_index do |file, i| m.template "db/migrate/create_#{file}.rb", "db/migrate/2009021800470#{i}_create_#{file}.rb" end end end # MAIN APP CONFIG # Main app and Config m.template 'core/project-textmate.erb', "#{project_name.underscore}.tmproj" m.template 'core/mainapp.mxml', File.join('app', 'flex', "#{project_name}.mxml") m.template 'core/mainapp-config.xml', File.join('app', 'flex', "#{project_name}-config.xml") m.template 'core/mainair-app.xml', File.join('app', 'flex', "#{project_name}-app.xml") if @use_air end end protected def add_options!(opt) opt.separator '' opt.separator 'Options:' opt.on("-m", "--main-only", "Only generate the main Flex/AIR application file.", "Default: false") { |v| options[:main_only] = v } opt.on("-a", "--air", "Configure AIR project instead of Flex. Flex is default.", "Default: false") { |v| options[:air_config] = v } opt.on("--skip-framework", "Don't fetch the latest framework binary. You'll have to link/build the framework yourself.", "Default: false") { |v| options[:skip_framework] = v } opt.on("--skip-migration", "Don't generate a migration file for this model") { |v| options[:skip_migration] = v } end # These three methods, model_names, list_as_files, and event_names, are used to # create the Events.as class properly def model_names @model_names = [] if File.exists?("app/flex/#{base_folder}/models/domain") @model_names = list_as_files("app/flex/#{base_folder}/models/domain") end end def list_as_files(dir_name) Dir.entries(dir_name).grep(/\.as$/).map { |name| name.sub(/\.as$/, "") } end def event_names @event_names = [] if File.exists?("app/flex/#{base_folder}/models") @event_names = list_as_files_for_events("app/flex/#{base_folder}/models") end end def list_as_files_for_events(dir_name) Dir.entries(dir_name).grep(/\.as$/).map { |name| name.sub(/\.as$/, "") } end def banner "Usage: #{$0} #{spec.name}" end end