mintdigital / hemlock

Framework for building multi-user, real-time web applications

hemlock / Rakefile
100644 392 lines (311 sloc) 12.78 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
require 'yaml'
require 'ftools'
require 'fileutils'
 
task :default => 'hemlock:test:default'
namespace :hemlock do
  
  namespace :download do
    desc 'Download all dependencies'
    task :all => ['hemlock:download:xiff']
    
    desc 'Download XIFF'
    task :xiff do
      puts 'Downloading XIFF...'
      
      hemlock_path = File.dirname(__FILE__)
      download_path = File.join(hemlock_path, 'tmp')
      local_source_path = File.join(hemlock_path, 'vendor')
      remote_source_uri = 'http://download.igniterealtime.org/xiff/xiff_3_0_0-beta1.zip'
      source_archive_filename = remote_source_uri.split('/').last
      source_path_parts = %w(org jivesoftware xiff)
      
      # Check for existing local source
      if File.directory?(File.join(local_source_path, 'xiff'))
        raise 'You already downloaded XIFF!: ' +
              File.join(local_source_path, *source_path_parts) and return
      end
      
      # Download file
      Dir.mkdir(download_path) unless File.directory?(download_path)
      Dir.chdir(download_path)
      `curl -O #{remote_source_uri}`
      
      # TODO: Handle network errors
      
      # Unarchive file
      `unzip #{source_archive_filename}`
      File.delete(source_archive_filename)
      
      # Move to local_source_path
      File.makedirs(local_source_path) unless File.directory?(local_source_path)
      Dir.chdir(local_source_path)
      File.move File.join(download_path, 'xiff'), File.join(local_source_path)
      
      # Clean up
      FileUtils.rm_rf File.join(download_path, 'xiff')
      Dir.rmdir(download_path) if (Dir.entries(download_path) - ['.', '..']).empty?
    end
  end
  
  namespace :build do
    desc 'Build DrawingDemo app'
    task :drawingDemo, [:debug] do |t, args|
      # Usage:
      # - `rake hemlock:build:drawingDemo`
      # - `rake hemlock:build:drawingDemo[true] (debug mode)
      
      # Enabling debug mode here enables Flash Player to report code line
      # numbers in error messages. To get debug output in your Hemlock app via
      # DebugWidget, use the `debug` flag in environment.as instead.
      
      args.with_defaults(:debug => false)
      
      puts "Building DrawingDemo#{' (debug mode)' if args.debug}..."
      
      input_file = 'com/mintdigital/drawingDemo/containers/DrawingDemoContainer.as'
      output_file = 'com/mintdigital/drawingDemo/containers/DrawingDemoContainer.swf'
      mxmlc_options = [
        '-compiler.source-path=.',
        '-compiler.fonts.managers=flash.fonts.AFEFontManager',
        '-compiler.include-libraries=../bin/HemlockCore.swc',
        "-output=#{output_file}"
      ]
      mxmlc_options << '-compiler.debug=true' if args.debug
      
      `cd src && #{ENV['FLEX_SDK_HOME']}/bin/mxmlc #{mxmlc_options.join(' ')} #{input_file}`
      puts "Built #{output_file}"
    end
  end
  
  task :default => 'hemlock:start:all'
  namespace :start do
    desc 'Start all server components'
    task :all => ['hemlock:start:policyd']
 
    desc 'Start policy file daemon'
    task :policyd do |t, args|
      exec './script/flashpolicyd.pl --file=public/crossdomain.xml --port=8040 &'
    end
    
    # More starters here...
  end
  
  desc 'Deploy to staging or production'
  task :deploy, [:environment] do |t, args|
    args.with_defaults(:environment => 'staging')
    
    if !%w(staging production).include?(args.environment) then return end
      
    # TODO: If any step fails, show error message and exit
    
    source_dir = 'src'
    config_dir = "#{source_dir}/config"
    deploy_config = YAML.load_file(config_dir + '/deploy.yml')[args.environment]
    
    # Prepare environment
    # - Requires environment.as-staging or environment.as-production
    `mv #{config_dir}/environment.as #{config_dir}/environment.as-orig`
    `cp #{config_dir}/environment.as-#{args.environment} #{config_dir}/environment.as`
    
    # Build with proper -compiler.debug flag
    Rake::Task['hemlock:build:drawingDemo'].invoke(deploy_config['source_app'], deploy_config['debug'])
 
    # scp to server
    puts 'Transferring to server...'
    `scp #{source_dir}/#{deploy_config['source_app']}.swf #{deploy_config['username']}@#{deploy_config['server']}:#{deploy_config['public_dir']}`
 
    # Reset environment
    `mv #{config_dir}/environment.as-orig #{config_dir}/environment.as`
  end
  
  namespace :test do
  
    desc 'Run tests'
    task :default => ["setup_test_dir", "generate_tests", "generate_package_suites", "generate_suite", "compile_tests", "run_tests"]
    task :with_debugger => ["setup_test_dir", "generate_tests", "generate_package_suites", "generate_suite", "compile_tests", "run_with_debugger"]
    
    desc "Create test dirs"
    task :setup_test_dir do
      unless File.exists?(File.dirname(__FILE__) + "/src/generated_tests")
        Dir.mkdir(File.dirname(__FILE__) + "/src/generated_tests")
        Dir.mkdir(File.dirname(__FILE__) + "/src/generated_tests/com")
        Dir.mkdir(File.dirname(__FILE__) + "/src/generated_tests/com/mintdigital")
      end
    
      Dir[File.dirname(__FILE__) + '/src/tests/com/mintdigital/**'].each do |d|
        unless File.exists?(d.gsub(/tests/, "generated_tests"))
          Dir.mkdir(d.gsub(/tests/, "generated_tests"))
        end
      end
    end
  
    desc "Generate test files"
    task :generate_tests do
      tests = Dir[File.dirname(__FILE__) + "/src/tests/com/mintdigital/**/*Test.as"].each do |file_path|
        file_path = File.expand_path(file_path)
        
        package = File.basename(File.dirname(file_path))
        emit_dir_path = File.dirname(file_path).gsub(/tests/,"generated_tests")
        Dir.mkdir(emit_dir_path) unless File.exists?(emit_dir_path)
        
        emit_file_path = emit_dir_path + "/" + File.basename(file_path, ".as") + "Emit.as"
        
        File.open(emit_file_path, "w") do |file|
          file << <<-eot
package generated_tests.com.mintdigital.#{package} {
 
import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;
import com.mintdigital.#{package}.*;
import tests.com.mintdigital.mocks.*;
 
public class #{File.basename(emit_file_path,".as")} extends TestCase {
 
#{File.readlines(file_path)}
}
}
eot
        end
      end
    end
  
    desc "Generate package test suites"
    task :generate_package_suites do
      suites = Dir[File.dirname(__FILE__) + "/src/tests/com/mintdigital/**"].each do |dir|
        package = File.basename(dir)
        tests = Dir[dir + "/*Test.as"].collect{|f| File.basename(f)}
        emit_file_path = (dir + "/" + package.capitalize + "TestSuite.as").gsub(/tests/,"generated_tests")
      
      
        File.open(emit_file_path, "w") do |file|
          file << <<-eot
package generated_tests.com.mintdigital.#{package} {
import flexunit.framework.TestSuite;
import generated_tests.com.mintdigital.#{package}.*;
 
public class #{package.capitalize}TestSuite{
public static function suite() : TestSuite{
var testSuite : TestSuite = new TestSuite();
 
eot
          
            tests.each do |test|
              file << "testSuite.addTestSuite( #{test.gsub('.as', '')}Emit );"
            end
          
            file << <<-eot
 
return testSuite;
}
}
}
eot
        end
      
      end
    end
  
    desc "Generate all tests"
    task :generate_suite do
      puts "-"*80
      puts "Generating test suite..."
      emit_file_path = (File.dirname(__FILE__) + "/src/allTests.as")
      packages = Dir[File.dirname(__FILE__) + "/src/generated_tests/com/mintdigital/**"].collect{|d| File.basename(d)}
      suites = packages.collect{|p| p.capitalize + "TestSuite"}
    
      File.open(emit_file_path, "w") do |file|
        file << <<-eot
package {
import flexunit.framework.TestSuite;
eot
      
        packages.each do |package|
          file << "import generated_tests.com.mintdigital.#{package}.*;
"
        end
 
      
        file << <<-eot
 
public class allTests{
public static function suite() : TestSuite{
var testSuite : TestSuite = new TestSuite();
 
eot
        
          suites.each do |suite|
            file << "testSuite.addTest( #{suite}.suite() );"
          end
        
          file << <<-eot
 
return testSuite;
}
}
}
eot
      end
    end
  
    desc "Compile the test suite"
    task :compile_tests do
      puts 'Compiling test suite...'
      mxmlc_options = [
        '-compiler.include-libraries "bin/FlexUnit.swc" "bin/FlexUnitRunner.swc"',
        '-compiler.debug=true',
        '-compiler.fonts.managers=flash.fonts.AFEFontManager'
      ]
      `#{ENV['FLEX_SDK_HOME']}/bin/mxmlc #{mxmlc_options.join(' ')} src/HemlockTestRunner.mxml`
    end
 
    desc "Open compiled tests"
    task :run_tests do
      puts 'Running test suite...'
      `open src/HemlockTestRunner.swf`
    end
  
    desc "Debug compiled tests"
    task :run_with_debugger do
      puts 'Running test suite...'
      `fdb src/HemlockTestRunner.swf`
    end
  end # namespace :test
  
  namespace :framework do
    desc 'Build HemlockCore.swc'
    task :build => ["hemlock:framework:manifest", "hemlock:framework:compile"]
 
    task :manifest do
      manifest_filename = 'manifestCore.xml'
      
      xml = <<-EOS
<?xml version="1.0"?>
<componentPackage>
EOS
      excludes = ["handlers", "events", "assets", "views"]
        # TODO: Remove 'events' and 'views', since widgets should now use delegate classes instead
      excludeRegexes = [
/\._[^.]*$/, # e.g., _baseSkin.as (intended as an include)
/.*WidgetEvents$/,
/.*WidgetViews$/
      ]
        
      # Recursively include all the files from given namespaces
      xml << ['com/mintdigital/hemlock', 'com/adobe', 'com/dynamicflash', 'com/gsolo', 'com/pixelbreaker'].map do |dir|
         Dir["src/#{dir}/**/*.as"].map{|path| path.gsub("src/","").gsub(".as","").gsub("/",".") }.map do |klass|
           if excludes.include?(klass.gsub(/.*\./,"")) || excludeRegexes.map{ |regex| klass =~ regex }.any?
             ''
           else
  <<-EOS
<component id="#{klass}" class="#{klass}"/>
EOS
  
           end
         end.join
      end.join
        
      xml << <<-EOS
</componentPackage>
EOS
 
      File.open("src/#{manifest_filename}","w+") do |file|
        file << xml
      end
    end
    
    task :compile do
      namespace = 'http://hemlock.mintdigital.com'
      manifest_filename = 'src/manifestCore.xml'
      output = 'bin/HemlockCore.swc'
      
      puts "Preparing #{output}..."
      
      compc_options = [
        "-namespace #{namespace} #{manifest_filename}",
        "-include-namespaces=#{namespace}",
        '-sp=src',
        '-sp=vendor/xiff/src',
        '-managers=flash.fonts.AFEFontManager',
        "-output=#{output}"
      ]
      
      `#{ENV['FLEX_SDK_HOME']}/bin/compc #{compc_options.join(' ')}`
    end
  end # namespace :framework
  
  namespace :loaders do
    desc 'Build HemlockLoaders.swc'
    task :build => ['hemlock:loaders:manifest', 'hemlock:loaders:compile']
    
    task :manifest do
      manifest_filename = 'manifestLoaders.xml'
      xml = <<-EOS
<?xml version="1.0"?>
<componentPackage>
EOS
      excludes = []
      excludeRegexes = []
 
      # Recursively include all the files from given namespaces
      xml << ['com/mintdigital/hemlockLoaders'].map do |dir|
         Dir["src/#{dir}/**/*.as"].map{|path| path.gsub("src/","").gsub(".as","").gsub("/",".") }.map do |klass|
           if excludes.include?(klass.gsub(/.*\./,"")) || excludeRegexes.map{ |regex| klass =~ regex }.any?
             ''
           else
  <<-EOS
<component id="#{klass}" class="#{klass}"/>
EOS
 
           end
         end.join
      end.join
 
      xml << <<-EOS
</componentPackage>
EOS
 
      File.open("src/#{manifest_filename}","w+") do |file|
        file << xml
      end
    end
    
    task :compile do
      namespace = 'http://hemlock-loaders.mintdigital.com'
      manifest_filename = 'src/manifestLoaders.xml'
      output = 'bin/HemlockLoaders.swc'
      
      puts "Preparing #{output}..."
      
      compc_options = [
        "-namespace #{namespace} #{manifest_filename}",
        "-include-namespaces=#{namespace}",
        '-sp=src',
        '-sp=vendor/xiff/src',
        '-managers=flash.fonts.AFEFontManager',
        "-output=#{output}"
      ]
      `#{ENV['FLEX_SDK_HOME']}/bin/compc #{compc_options.join(' ')}`
    end
  end # namespace :loaders
  
end