namespace :flex do
APPS_DIR = '.'
COMPONENTS_DIR = 'components'
PUB_DIR = '../pub/flash'

#Find components and build them out
components = []
Dir.foreach(‘flex/components‘) do |filename|
if filename.match /.mxml$/
component_name = filename.sub(/.mxml$/, ‘‘)
component_files = [File.join(COMPONENTS_DIR, filename)].concat(Dir[File.join(COMPONENTS_DIR, component_name,‘**‘)])
components << file(File.join(COMPONENTS_DIR, 'build', component_name + '.swc') => component_files) do
puts `#{File.join(ENV[‘FLEX_SDK‘], ‘compc‘)} -source-path #{APPS_DIR} -output #{COMPONENTS_DIR}/build/#{component_name}.swc -include-classes components.#{component_name}`
end
end
end

task :components => components

#Find applications and build them into the public/flash directory
app_tasks = []
Dir.foreach(APPS_DIR) do |dirname|
if COMPONENTS_DIR != dirname && !dirname.match(/^./) && File.directory?(dirname)
app_files = Dir[File.join(APPS_DIR, dirname,‘**.mxml‘)].concat Dir[File.join(APPS_DIR, dirname,‘**.as‘)]
app_tasks << file( File.join(PUB_DIR, dirname + '.swf') => app_files.concat(components)) do
puts `#{File.join(ENV[‘FLEX_SDK‘], ‘mxmlc‘)} -library-path+=#{File.join(COMPONENTS_DIR, 'build')} -output #{File.join(PUB_DIR, dirname + '.swf')} #{File.join(APPS_DIR, dirname, 'Application.mxml')}`
end
end
end

task :build_all => app_tasks

end