Class: TextmateFcsh

Inherits:
Object
  • Object
show all
Defined in:
lib/textmate_fcsh.rb

Constant Summary collapse

CONFIG_FILE =
'.textmate_fcsh'
TEXTMATE_BUNDLE_LOCATION =
"git://github.com/japetheape/textmate_fcsh_bundle.git"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TextmateFcsh

Returns a new instance of TextmateFcsh.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/textmate_fcsh.rb', line 18

def initialize(options = {})
  @options = options
  @server = WebsocketServer.new
  open_browser_first_time
  @growl = Growl.new "127.0.0.1", "ruby-growl", ["ruby-growl Notification"]
  
  check_preconditions
  read_config!
  write_to_tempfile("Waiting for run...")

  if options[:standalone]
    puts "Running standalone"
    run
  else
    @fcsh = Fcsh.new
    d = DirWatcher.new(@config[:mxmlc_options][:source_path].split(",").join(' ')) do 
      run
    end
  end
end

Class Method Details

.create_config!Object

Create the config file



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/textmate_fcsh.rb', line 150

def self.create_config!
  if File.exist?(CONFIG_FILE)
    raise "Before recreating, first remove %s" % CONFIG_FILE
  end
  example_config = {:mxmlc_options => {:default_background_color => '#FFFFFF', :default_frame_rate => 24, :default_size => "970 550", :output => "bin/project-debug.swf", :source_path => "src,assets,lib/mvc,lib/editor_core", :verbose_stacktraces => true, :warnings => true}}
  example_config[:main_file] = 'src/editor.mxml'
  
  File.open('.textmate_fcsh', 'w+') do |f|
    f.write(YAML::dump(example_config))
  end
end

.create_textmate_bundle!Object

Clones the textmate bundle



163
164
165
166
167
168
169
# File 'lib/textmate_fcsh.rb', line 163

def self.create_textmate_bundle!
  puts "Installing textmate bundle."
  `mkdir -p ~/Library/Application\\ Support/TextMate/Bundles`
  `cd ~/Library/Application\\ Support/TextMate/Bundles/ && git clone #{TEXTMATE_BUNDLE_LOCATION} "textmate_fcsh.tmbundle"`
  `osascript -e 'tell app "TextMate" to reload bundles'`
  puts "Textmate bundle installed."
end

Instance Method Details

#check_preconditionsObject



138
139
140
# File 'lib/textmate_fcsh.rb', line 138

def check_preconditions
  raise "Configuration file does not exist, first run textmate_fcsh --setup" if !File.exist?(CONFIG_FILE)
end

#get_compile_commandObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/textmate_fcsh.rb', line 94

def get_compile_command
  mxmlc_command = ""
  #" -default-background-color=#FFFFFF -default-frame-rate=24 -default-size 970 550 -output=bin/editor-debug.swf -source-path+=src -source-path+=assets -source-path+=lib/mvc -source-path+=lib/editor_core -verbose-stacktraces=true -warnings=true src/editor.mxml"
  @config[:mxmlc_options].each do |k,v|
    #next if v
    splitted = v.to_s.split(",")
    splitted.each do |vsplitted|
      splitter =  splitted.length > 1 ? '+=' : '='
      if k == :default_size
        splitter = ' '
      end
      if k == :library_path
        splitter = '+='
      end
      mxmlc_command << " -%s%s%s" % [k.to_s.gsub(/\_/, '-'),splitter,vsplitted]
    end
  end
  mxmlc_command << " %s" % @config[:main_file]
  mxmlc_command
end

#open_browserObject

Open the report in the browser



59
60
61
# File 'lib/textmate_fcsh.rb', line 59

def open_browser
  `open #{@report_file.path}`
end

#open_browser_first_timeObject



51
52
53
54
55
56
# File 'lib/textmate_fcsh.rb', line 51

def open_browser_first_time
  if !@options[:standalone]
    f = File.join(File.dirname(__FILE__), '..','templates', 'standard.html')
    `open #{f}`
  end
end

#read_config!Object

Read configurion file, stored in .textmate_fcsh



144
145
146
# File 'lib/textmate_fcsh.rb', line 144

def read_config!
  @config = YAML.load_file(CONFIG_FILE)
end

#runObject

Compile the mxmlc, extract errors, create a report.



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
# File 'lib/textmate_fcsh.rb', line 64

def run
  if @options[:standalone]
    puts get_compile_command
    output_watcher = OutputWatcher.new("mxmlc " + get_compile_command)
    output_watcher.stdout.each_line do |line|
      puts " -->\t" + line
    end
    
    output = output_watcher.stderr
    errors = MxmlcOutputReader.new(output)
    
    @report = HtmlMxmlcErrorFormatter.new(errors)
    write_report!
    open_browser
    
  else
    output = run_mxmlc
    errors = @fcsh.errors
    
    message = "Complete: %d errors, %d warnings" % [errors.errors.size, errors.warnings.size]
    puts message
    error_array = errors.messages.map {|x| {"filename" => x.filename, "line" => x.line, "level" => x.level, "message" => x.message, "content" => x.content, "column" => x.column } }
    @server.send  JSON.generate(error_array)
    @growl.notify "ruby-growl Notification", "Textmate FCSH", message
  end
  

    
end

#run_bogus_mxmlcObject

Run a bogus mxmlc command, not used anymore.



125
126
127
128
129
130
# File 'lib/textmate_fcsh.rb', line 125

def run_bogus_mxmlc
  out = ''
  mxmlc_output_file = File.join(File.dirname(__FILE__), '..', 'spec', 'fixtures', 'mxmlc_output.txt')
  File.open(mxmlc_output_file).each_line {|l| out << l}
  out
end

#run_mxmlcObject

Runs the mxmlc in the fcsh compiler



116
117
118
119
120
121
122
# File 'lib/textmate_fcsh.rb', line 116

def run_mxmlc
  if @target_id.nil?
    @target_id = @fcsh.mxmlc(get_compile_command)
  else
    @fcsh.compile(@target_id)
  end
end

#write_report!Object



133
134
135
# File 'lib/textmate_fcsh.rb', line 133

def write_report!
  write_to_tempfile(@report.out)
end

#write_to_tempfile(txt) ⇒ Object

Write report to file



45
46
47
48
49
# File 'lib/textmate_fcsh.rb', line 45

def write_to_tempfile(txt)
  @report_file = File.new('/tmp/mxmlc_error_report.html', 'w+')
  @report_file.write(txt)
  @report_file.close
end