PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Support/Tools/build_with_mtasc.rb

http://github.com/textmate/actionscript.tmbundle
Ruby | 162 lines | 141 code | 10 blank | 11 comment | 16 complexity | 94b7452606ea86665be4e2a10e193a7c MD5 | raw file
  1. #!/usr/bin/env ruby
  2. #
  3. # Build - compile ActionScript using MTASC
  4. #
  5. # Based on a command by Chris Sessions, Released on 2006-06-02.
  6. # Copyright (c) 2006. MIT License.
  7. # Modified by Ale Mu?oz <http://bomberstudios.com> on 2006-11-24.
  8. # Improvements suggested by Juan Carlos A?orga <http://www.juanzo.com/>, Helmut Granda <http://helmutgranda.com>
  9. require "open3"
  10. require "yaml"
  11. require "erb"
  12. require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes"
  13. require ENV['TM_SUPPORT_PATH'] + "/lib/progress"
  14. require ENV["TM_SUPPORT_PATH"] + "/lib/web_preview"
  15. # Some routes
  16. @file_path = File.dirname(ENV['TM_FILEPATH'])
  17. @file_name = ENV['TM_FILENAME']
  18. @project_path = ENV['TM_PROJECT_DIRECTORY']
  19. def get_build_path
  20. if File.exist?("#{@file_path}/mtasc.yaml")
  21. return @file_path + "/"
  22. end
  23. if File.exist?("#{@project_path}/mtasc.yaml")
  24. return @project_path + "/"
  25. end
  26. return false
  27. end
  28. # Utils
  29. @q = "\""
  30. def warning(text, title)
  31. html_header("Error!","Build With MTASC")
  32. puts "<h1>#{title}</h1>"
  33. puts "<p>#{text}</p>"
  34. html_footer
  35. TextMate.exit_show_html
  36. end
  37. def get_config
  38. @build_path = get_build_path()
  39. if !@build_path
  40. @build_path = @file_path + "/"
  41. @config = YAML.load(File.open(ENV['TM_BUNDLE_SUPPORT'] + "/mtasc.yaml"))
  42. @config['app'] = File.basename(@file_name)
  43. @config['swf'] = File.basename(@file_name,".as")+".swf"
  44. @config['mtasc_path'] = "#{ENV['TM_BUNDLE_SUPPORT']}/bin/mtasc"
  45. else
  46. @config = YAML.load(File.open(@build_path + "mtasc.yaml"))
  47. # If width or height is a percentage. MTASC chokes because it needs a number. Fix it.
  48. if @config['width'] =~ /%/ || @config['height'] =~ /%/
  49. @size_is_percentage = true
  50. @config['width'].gsub!(/%/,'')
  51. @config['height'].gsub!(/%/,'')
  52. end
  53. if @config['mtasc_path'].nil?
  54. @config['mtasc_path'] = "#{ENV['TM_BUNDLE_SUPPORT']}/bin/mtasc"
  55. end
  56. end
  57. return @config
  58. end
  59. def preview
  60. return ERB.new(IO.read("#{ENV['TM_BUNDLE_SUPPORT']}/swf_template.rhtml")).result
  61. end
  62. def mtasc_compile
  63. config = get_config()
  64. # MTASC binary
  65. cmd = @q + @config['mtasc_path'] + @q
  66. # App name
  67. cmd += " " + @q + @config['app'] + @q
  68. # Player version
  69. cmd += " -version " + @config['player'].to_s
  70. # User-provided Classpath
  71. if !@config['classpaths'].nil?
  72. cmd += " -cp \"" + @config['classpaths'].join('" -cp "') + "\" "
  73. end
  74. # Additional parameters from mtasc.yaml
  75. if @config['params']
  76. cmd += " #{@config['params']} "
  77. end
  78. if @config['trace']
  79. case @config['trace']
  80. when "xtrace"
  81. %x(open "$TM_BUNDLE_SUPPORT/bin/XTrace.app")
  82. cmd += " -cp \"#{ENV['TM_BUNDLE_SUPPORT']}/lib/\" -pack com/mab/util -trace com.mab.util.debug.trace "
  83. when "console"
  84. log_file_path = "#{ENV['HOME']}/Library/Preferences/Macromedia/Flash Player/Logs/"
  85. if(!File.exist?(log_file_path))
  86. Dir.mkdir(log_file_path)
  87. File.new("#{log_file_path}/flashlog.txt",'w')
  88. end
  89. %x(open -a Console.app "#{log_file_path}/flashlog.txt")
  90. when "terminal"
  91. log_file_path = "#{ENV['HOME']}/Library/Preferences/Macromedia/Flash Player/Logs/"
  92. if(!File.exist?(log_file_path))
  93. Dir.mkdir(log_file_path)
  94. File.new("#{log_file_path}/flashlog.txt",'w')
  95. end
  96. %x(osascript "$TM_BUNDLE_SUPPORT/Tools/tail.applescript")
  97. when "no"
  98. cmd += " -trace no "
  99. else
  100. cmd += " -trace #{@config['trace']} "
  101. end
  102. end
  103. if !File.exists? @config['swf']
  104. cmd += " -header #{@config['width']}:#{@config['height']}:#{@config['fps']}"
  105. end
  106. cmd += " -swf #{@config['swf']}"
  107. stdin, stdout, stderr = Open3.popen3(cmd)
  108. warnings = []
  109. errors = []
  110. while err = stderr.gets
  111. if err[0, 10] == 'Warning : '
  112. warnings.push(err.chomp)
  113. else
  114. m = /(.+):([0-9]+): characters ([0-9]+)/.match(err)
  115. if m != nil
  116. a = "txmt://open?url=file://#{@project_path}/#{m[1]}&line=#{m[2]}&column=#{m[3].to_i + 1}"
  117. err = "<a href=\"#{a}\">#{err}</a>"
  118. end
  119. errors.push(err.chomp)
  120. end
  121. end
  122. if !errors.empty?
  123. warning "#{errors.uniq.join('</p><p>')} <br/> <pre>#{cmd}</pre>", "Errors:"
  124. end
  125. if !warnings.empty?
  126. warning "#{warnings.uniq.join('</p><p>')} <br/> <pre>#{cmd}</pre>", "Warnings:"
  127. end
  128. if errors.empty? && warnings.empty?
  129. if @config['preview'] == 'textmate'
  130. puts preview()
  131. TextMate.exit_show_html
  132. else
  133. %x(open #{@config['preview']})
  134. end
  135. else
  136. TextMate.exit_show_html
  137. end
  138. end
  139. # compile with MTASC
  140. TextMate.call_with_progress({:title => "MTASC", :message => "Compiling Classes"}) do
  141. mtasc_compile
  142. end