<commit_before>#!/usr/bin/env ruby -wKU
# encoding: utf-8

module FlexMate

  class Compiler

    # Run mxmlc to compile a swf adapting to the current environment as
    # necessary.
    #
    def build

      FlexMate::SDK.add_flex_bin_to_path
      TextMate.require_cmd('mxmlc')

      cmd = MxmlcCommand.new
      cmd.file_specs = get_file_specs
      cmd.o = get_flex_output(cmd.file_specs)

      require ENV['TM_SUPPORT_PATH'] + '/lib/web_preview'

      puts html_head( :window_title => "ActionScript 3 Build Command",
                      :page_title => "Build (mxmlc)",
                      :sub_title => cmd.file_specs_name )

      puts "<h2>Building...</h2>"
      puts "<p><pre>-file-specs=#{cmd.file_specs}"
      puts "-o=#{cmd.o}</pre></p>"

      exhaust = get_exhaust

      TextMate::Process.run(cmd.line) do |str|
        STDOUT << exhaust.line(str)
      end

      exhaust.complete

      html_footer

    end

    # Create the object responsible for parsing the compiler output.
    #
    def get_exhaust
      require 'fm/mxmlc_exhaust'
      MxmlcExhaust.new
    end

    private

    # Inspects the available environmental variables and gathers the settings
    # necessary for the compiler to run.
    #
    def get_file_specs

      proj_dir = ENV['TM_PROJECT_DIRECTORY']
      file_specs = ENV['TM_FLEX_FILE_SPECS']

      if proj_dir && file_specs
        file_specs = proj_dir + '/' + file_specs
        return file_specs if File.exist?(file_specs)
      end

      if proj_dir
        file_specs = guess_file_specs(proj_dir)
        return file_specs unless file_specs.nil?
      end

      file_specs = ENV['TM_FILEPATH']

    end

    # When TM_FLEX_OUTPUT and TM_PROJECT_DIRECTORY are defined use them to
    # build flex output. Otherwise derive the output from the file specs.
    #
    def get_flex_output(fs)

      flex_output = ENV['TM_FLEX_OUTPUT']
      proj_dir = ENV['TM_PROJECT_DIRECTORY']

      if flex_output && proj_dir
        return proj_dir + '/' + flex_output
      end

      #TODO: Link to usual src dirs and improve sub with a regexp that
      #matches src backwards from the end of line.
      if File.exist?( proj_dir + '/bin' )
        fs.sub!('src','bin')
      end
      
      fs.sub(/\.(mxml|as)/, ".swf")

    end

    # Where we have Project Directory but no TM_FLEX_FILE_SPECS set take a look
    # inside the src/ dir and see if we can work out which file should be
    # compiled.
    #<commit_msg>• Correct error in previous revision where flex file specs was accidentally being translated.<commit_after>#!/usr/bin/env ruby -wKU
# encoding: utf-8

module FlexMate

  class Compiler

    # Run mxmlc to compile a swf adapting to the current environment as
    # necessary.
    #
    def build

      FlexMate::SDK.add_flex_bin_to_path
      TextMate.require_cmd('mxmlc')

      cmd = MxmlcCommand.new
      cmd.file_specs = get_file_specs
      cmd.o = get_flex_output(cmd.file_specs)

      require ENV['TM_SUPPORT_PATH'] + '/lib/web_preview'

      puts html_head( :window_title => "ActionScript 3 Build Command",
                      :page_title => "Build (mxmlc)",
                      :sub_title => cmd.file_specs_name )

      puts "<h2>Building...</h2>"
      puts "<p><pre>-file-specs=#{cmd.file_specs}"
      puts "-o=#{cmd.o}</pre></p>"

      exhaust = get_exhaust

      TextMate::Process.run(cmd.line) do |str|
        STDOUT << exhaust.line(str)
      end

      exhaust.complete

      html_footer

    end

    # Create the object responsible for parsing the compiler output.
    #
    def get_exhaust
      require 'fm/mxmlc_exhaust'
      MxmlcExhaust.new
    end

    private

    # Inspects the available environmental variables and gathers the settings
    # necessary for the compiler to run.
    #
    def get_file_specs

      proj_dir = ENV['TM_PROJECT_DIRECTORY']
      file_specs = ENV['TM_FLEX_FILE_SPECS']

      if proj_dir && file_specs
        file_specs = proj_dir + '/' + file_specs
        return file_specs if File.exist?(file_specs)
      end

      if proj_dir
        file_specs = guess_file_specs(proj_dir)
        return file_specs unless file_specs.nil?
      end

      file_specs = ENV['TM_FILEPATH']

    end

    # When TM_FLEX_OUTPUT and TM_PROJECT_DIRECTORY are defined use them to
    # build flex output. Otherwise derive the output from the file specs.
    #
    def get_flex_output(fs)

      flex_output = ENV['TM_FLEX_OUTPUT']
      proj_dir = ENV['TM_PROJECT_DIRECTORY']

      if flex_output && proj_dir
        return proj_dir + '/' + flex_output
      end

      fx_out = fs.sub(/\.(mxml|as)/, ".swf")
      
      #TODO: Link to usual src dirs and improve sub with a regexp that
      #matches src backwards from the end of line.
      if File.exist?( proj_dir + '/bin' )
        fx_out.sub!('src','bin')
      end
      
      fx_out

    end

    # Where we have Project Directory but no TM_FLEX_FILE_SPECS set take a look
    # inside the src/ dir and see if we can work out which file should be
    # compiled.
    #
{'fraction_non_alphanumeric': 0.08846229962398575, 'fraction_numerical': 0.001583217890362161, 'mean_word_length': 2.3492379058979456, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1848248', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1642, 'n_words': 445}