Class: HotReload

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHotReload



16
17
18
19
# File 'lib/hotreload.rb', line 16

def initialize
  @projectInjected = Hash.new
  @pending = []
end

Class Method Details

.test(path) ⇒ Object



12
13
14
# File 'lib/hotreload.rb', line 12

def self.test(path)
  puts "success, path is #{path}"
end

Instance Method Details

#changeFiles(changeFiles) ⇒ Object



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

def changeFiles(changeFiles)
  now = Time.now.to_i
  changeFiles.each do |file|
    unless @pending.include?(file)
      time = @lastInjected[file]
      if time == nil || now > time
        @lastInjected[file] = now
        @pending.push(file)
      end
    end
  end

  @pending.each do |file|
    inject(file)
  end
  @pending.clear

end

#inject(source) ⇒ Object



82
83
84
85
86
# File 'lib/hotreload.rb', line 82

def inject(source)
  Thread.new do
    @server.sendCommand(Command::InjectionInject, source)
  end
end

#listenCommandObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/hotreload.rb', line 117

def listenCommand
  Thread.new do
    while (command = readInt) != Command::InjectionEOF
      case command
      when Command::InjectionComplete
        puts "Injection Complete"
      when Command::InjectionPause
      when Command::InjectionSign
        dylib = readMessage
        signer = Signer.new
        ret = signer.codeSignDylib(dylib)
        @server.sendCommand(Command::InjectionSigned, ret ? '1' : '0')
      when Command::InjectionError
        puts "Injection error: #{readMessage}"
      else
        puts "InjectionServer: Unexpected case #{command}"
        exit
      end
    end
    puts "command end"
  end
end

#readIntObject



74
75
76
# File 'lib/hotreload.rb', line 74

def readInt
  @server.readInt
end

#readMessageObject



78
79
80
# File 'lib/hotreload.rb', line 78

def readMessage
  @server.readMessage
end

#setProject(projectFile) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/hotreload.rb', line 107

def setProject(projectFile)
  @server.sendCommand(Command::InjectionProject, projectFile)
  # /Users/shantj/Documents/workspace/third/injection
  @watcher = FileWatcher.new(@projectPath)
  # @watcher = FileWatcher.new('/Users/shantj/Documents/workspace/third/injection')  TODO
  @watcher.startWatcher  do |file|
    changeFiles(file)
  end
end

#startServer(projectpath) ⇒ Object



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

def startServer(projectpath)
  @projectPath = File.expand_path("..", projectpath)
  @server = Server.new
  @server.run

  # get current dir
  @server.sendMessage(@projectPath)

  # tell client the project being watched
  projectfile = "#{@projectPath}/*.xcworkspace"
  files_sorted_by_time = Dir[projectfile].sort_by{ |f| File.mtime(f) }
  projectfile = files_sorted_by_time[0]
  if projectfile == nil
    projectfile = "#{@projectPath}/*.xcodeproj"
    files_sorted_by_time = Dir[projectfile].sort_by{ |f| File.mtime(f) }
    projectfile = files_sorted_by_time[0]
  end

  if readMessage != Config::HOTRELOAD_KEY
    puts "not the validate client"
    return
  end

  # client spcific data for building
  frameworkPath = readMessage
  arch = readMessage

  @lastInjected = @projectInjected[projectfile]
  if @lastInjected == nil
    @lastInjected = Hash.new
    @projectInjected[projectfile] = @lastInjected
  end

  executable = readMessage
  if executable != nil
    executableBuild = File.mtime(executable).tv_sec
    @lastInjected.each do |key, value|
      if File.extname(key) != ".storyboard" && File.extname(key) != "xib" && File.mtime(key).tv_sec > executable
        inject(key)
      end
    end
  else
    return
  end

  listenCommand

  setProject(projectfile)

  @watcher = nil

end