lib/restfulx/tasks.rb in restfulx-1.2.1 vs lib/restfulx/tasks.rb in restfulx-1.2.2

- old
+ new

@@ -1,59 +1,89 @@ # Rake tasks for building RestfulX-based Flex and AIR applications require 'rake' require 'ftools' require 'rexml/document' +require 'activesupport' require File.join(File.dirname(__FILE__), 'configuration') -APP_ROOT = RestfulX::Configuration::APP_ROOT +include RestfulX::Configuration namespace :rx do - # Compile the main Flex/AIR application using given executable to the given - # destination folder - def compile_app(executable, destination, opts = '') - app_properties = REXML::Document.new(File.open(File.join(APP_ROOT, ".actionScriptProperties"))) - app_properties.elements.each("*/applications/application") do |elm| - app_path = elm.attributes['path'] - project_path = File.join(APP_ROOT, "app/flex", app_path) - target_project_path = project_path.sub(/.mxml$/, '.swf') - target_project_air_descriptor = project_path.sub(/.mxml$/, '-app.xml') + # Compile the given Flex/AIR application + # The following options are supported: + # :executable => 'mxmlc' + # :application => nil + # :destination => 'public/bin' + # :opts => nil + # :flex_root => 'app/flex' + def compile_application(params = {}) + project_name, flex_project_name, command_controller_name, base_package, base_folder, + flex_root = extract_names() + + executable = params[:executable] || 'mxmlc' + application = params[:application] || get_main_application + destination = params[:destination] || 'public/bin' + opts = params[:opts] || '' + flex_root = params[:flex_root] || flex_root + + compiler = get_executable(executable) + + application = get_main_application unless application + project_path = File.join(APP_ROOT, flex_root, application) + target_project_path = project_path.sub(/.mxml$/, '.swf') + target_project_air_descriptor = project_path.sub(/.mxml$/, '-app.xml') - libs = Dir.glob(File.join(APP_ROOT, 'lib', '*.swc')).map {|lib| lib.gsub(' ', '\ ')} + libs = Dir.glob(File.join(APP_ROOT, 'lib', '*.swc')).map {|lib| lib.gsub(' ', '\ ')} - cmd = "#{executable} #{opts} -library-path+=#{libs.join(',')} " << - "-keep-as3-metadata+=Resource,HasOne,HasMany,BelongsTo,DateTime,Lazy,Ignored #{project_path.gsub(' ', '\ ')}" - puts "Compiling #{project_path}" - if system(cmd) - FileUtils.makedirs File.join(APP_ROOT, destination) - puts "Moving #{target_project_path} to " + File.join(APP_ROOT, destination) - FileUtils.mv target_project_path, File.join(APP_ROOT, destination), :force => true - if File.exist?(target_project_air_descriptor) - descriptor = File.read(target_project_air_descriptor) - descriptor_name = target_project_air_descriptor.split("/").last - app_swf = target_project_path.split("/").last - descriptor.gsub!("[This value will be overwritten by Flex Builder in the output app.xml]", - app_swf) + additional_compiler_args = get_app_properties().elements["actionScriptProperties"].elements["compiler"].attributes["additionalCompilerArguments"] + additional_compiler_args.gsub!("../locale/", "#{APP_ROOT}/app/locale/") + + cmd = "#{executable} #{opts} -library-path+=#{libs.join(',')} " << additional_compiler_args << " #{project_path.gsub(' ', '\ ')}" + puts "Compiling #{project_path}" + if system(cmd) + FileUtils.makedirs File.join(APP_ROOT, destination) + puts "Moving #{target_project_path} to " + File.join(APP_ROOT, destination) + FileUtils.mv target_project_path, File.join(APP_ROOT, destination), :force => true + if File.exist?(target_project_air_descriptor) + descriptor = File.read(target_project_air_descriptor) + descriptor_name = target_project_air_descriptor.split("/").last + app_swf = target_project_path.split("/").last + descriptor.gsub!("[This value will be overwritten by Flex Builder in the output app.xml]", + app_swf) - File.open("#{APP_ROOT}/#{destination}/#{descriptor_name}", "w") do |file| - file.print descriptor - end - puts "Created #{APP_ROOT}/#{destination}/#{descriptor_name} descriptor." + File.open("#{APP_ROOT}/#{destination}/#{descriptor_name}", "w") do |file| + file.print descriptor end - puts 'Done!' - else - puts "The application was not compiled. Check console for errors. " << - "It is possible that '(a)mxmlc' executable was not found or there are compilation errors." + puts "Created #{APP_ROOT}/#{destination}/#{descriptor_name} descriptor." end - end + puts 'Done!' + else + puts "The application was not compiled. Check console for errors. " << + "It is possible that '(a)mxmlc' executable was not found or there are compilation errors." + end end + def run_air_application(params = {}) + application = params[:application] || get_main_application + descriptor = params[:descriptor] || application.sub(/.mxml$/, '-app.xml') + destination = params[:destination] || 'bin-debug' + + puts "Running AIR application with descriptor: #{descriptor}" + if !system("#{get_executable('adl')} #{destination}/#{descriptor}") + puts "Could not run the application with descriptor: #{destination}/#{descriptor}. Check console for errors." + end + end + # Find what the main application is based on .actionScriptProperties file def get_main_application - app_properties = REXML::Document.new(File.open(File.join(APP_ROOT, ".actionScriptProperties"))) - app_properties.root.attributes['mainApplicationPath'].split("/").last + get_app_properties().root.attributes['mainApplicationPath'].split("/").last end + def get_app_properties + REXML::Document.new(File.open(File.join(APP_ROOT, ".actionScriptProperties"))) + end + # Get appropriate executable based on platform def get_executable(executable) if RUBY_PLATFORM =~ /mswin32/ executable << '.exe' end @@ -61,25 +91,21 @@ end namespace :flex do desc "Build project swf file and move it into public/bin folder" task :build do - compile_app(get_executable('mxmlc'), 'public/bin') + compile_application() end end namespace :air do desc "Build project swf file as an AIR application and move it into bin-debug folder" task :build do - compile_app(get_executable('mxmlc'), 'bin-debug', '+configname=air') + compile_application(:destination => 'bin-debug', :opts => '+configname=air') end desc "Run the AIR application (if this project is configured as an AIR project)" task :run do - target = get_main_application.gsub(/.mxml$/, '-app.xml') - puts "Running AIR application with descriptor: #{target}" - if !system("#{get_executable('adl')} bin-debug/#{target}") - puts "Could not run the application with descriptor: #{target}. Check console for errors." - end + run_air_application end end end \ No newline at end of file