Class: AsProject::FCSHS

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip = nil, port = nil) ⇒ FCSHS

Returns a new instance of FCSHS.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fcshs.rb', line 13

def initialize(ip=nil, port=nil)
  @ip = (ip.nil?) ? "127.0.0.1" : ip
  @port = (port.nil?) ? 20569 : port
  parse!
  if(self.kill_process)
    puts '>> Attempting to kill process now!'
    kill(@ip, @port)
  elsif(self.start_process)
    open(@ip, @port)
  end
end

Instance Attribute Details

#kill_processObject

Returns the value of attribute kill_process.



10
11
12
# File 'lib/fcshs.rb', line 10

def kill_process
  @kill_process
end

#start_processObject

Returns the value of attribute start_process.



10
11
12
# File 'lib/fcshs.rb', line 10

def start_process
  @start_process
end

Instance Method Details

#close_fcshObject



164
165
166
167
168
# File 'lib/fcshs.rb', line 164

def close_fcsh
  @instances.each do |fc|
    fc.close
  end
end

#execute(io, dir, command) ⇒ Object



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

def execute(io, dir, command)
  io.puts "#{command} at #{Dir.pwd} and #{dir}"
  fcsh = get_fcsh(io, dir)

  cmd = @compile_commands[command]
  msg = command
  if(!cmd.nil?)
    msg = "compile #{cmd.index}"
  end

  fcsh.stdin.puts(msg)
  stay_open = true
  compile_errors = false

  e = Thread.new {
    while(line = fcsh.stderr.gets)
      io.puts line
      $stderr.puts line
      compile_errors = true
    end
    io.puts 'STD ERROR COMPLETE'
  }
  
  o = Thread.new {
    while(line = fcsh.stdout.gets)
      io.puts line
      $stdout.puts line.size.to_s + " : " + line
#          io.flush
#          $stdout.flush
      if(!preamble.match(line))
        puts 'CLOSING NOW!'
        stay_open = false
      end
    end
  }
  
  while(stay_open)
    sleep(0.75)
  end
  
  # Close the Connection...
  io.puts '0xE'

  # Store the successful compilation
  if(cmd.nil? && !compile_errors)
    cmd = CompileCommand.new(command)
    @compile_commands[command] = cmd
    cmd.index = @compile_commands.size
  end
end

#get_fcsh(session, dir) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/fcshs.rb', line 170

def get_fcsh(session, dir)
  begin
    if(instances[dir])
      return instances[dir]
    else
      dir.chomp!
      if(File.directory?(dir))
        Dir.chdir(dir)
      else
        msg = ">> Error - unable to locate: #{dir}"
        session.puts msg
        raise StandardError.new(msg)
      end

      instances[dir] = FCSHProcess.new
    end
  rescue SystemCallError
    puts ">> Failed to start the actual fcsh application, please make sure it's in your class path by opening a new terminal and typing 'fcsh'"
  end
end

#instancesObject



25
26
27
28
29
30
# File 'lib/fcshs.rb', line 25

def instances
  if(@instances.nil?)
    @instances = Hash.new
  end
  return @instances
end

#kill(ip, port) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/fcshs.rb', line 63

def kill(ip, port)
  begin
    @socket = TCPSocket.open(@ip, @port) do |s|
      s.puts '0'
    end
    puts '>> Successfully killed running fcshs process'
  rescue SystemCallError
    puts ">> Was unable to find the currently running process at #{ip}:#{port}, try running 'ps -a | egrep ruby' and then 'kill -9 [process id]'"
  end
end

#open(ip, port) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fcshs.rb', line 74

def open(ip, port)
  begin
    @compile_commands = Hash.new
    @fcsh = nil
    @socket = TCPServer.new(@ip, @port)
    @socket.setsockopt(Socket::SOL_SOCKET, Socket::TCP_NODELAY, true)
    puts "fcsh server started on #{@ip}:#{@port}"
    puts "enter 'fcshs -k' to kill this process when you're done"
  rescue StandardError => e
    puts 'Error opening socket: ' + e.to_s + "\nTry running fcshs -k to kill the currently-running socket."
    @compile_commands = nil
  end
  start
end

#parse!Object



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

def parse!
  opts = OptionParser.new do |opts|
    opts.banner = <<EOF

Usage: #$0 [options]

Begin or kill the Flex Command Sell Socket (fcshs).

This is a simple ruby socket server that sits on top of the Flex Command Shell terminal tool that makes shell-based MXMLC compilation exponentially more performant.

Running this command with no options will start the service. This is not really necessary, as the FCSHS Rake task will automatically begin this process, you just need to know how to kill it later - when you want to shut down your shell.

Options:
EOF

   opts.on('-k', '--kill', 'Kill the running fcshs process') do
      self.kill_process = true
    end

    opts.on_tail('-h', '--help', 'Display this help and exit') do
      puts opts
      exit
    end
  end
  if(ARGV.size > 0)
    opts.parse!(ARGV)
  else
    self.start_process = true
  end
end

#preambleObject



156
157
158
159
160
161
162
# File 'lib/fcshs.rb', line 156

def preamble
  str <<EOF
Adobe Flex Compiler SHell (fcsh)
Version 2.0.1 build 155542
Copyright (c) 2004-2006 Adobe Systems, Inc. All rights reserved.
EOF
end

#startObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fcshs.rb', line 89

def start
  while
    session = @socket.accept
    dir = session.gets
    if(dir.match(/^0/))
      puts '>> Kill all fcsh instances and close the socket now!'
      close_fcsh
      exit
    end
    command = session.gets
    execute(session, dir, command)
    session.close
    sleep 0.75
  end
end