Class: Inmake::Runner

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

Overview

Inmake Command Runner

Finds and runs the commands inside all of the files. The same search mode is used for every file.

Instance Method Summary collapse

Constructor Details

#initialize(cfg) ⇒ Runner

Set up a runner with a configuration object defined earlier.



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

def initialize cfg
  @config = cfg
end

Instance Method Details

#applyCommand(cmd, fd) ⇒ Object

Apply variables and then run the found build command



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/inmake/runner.rb', line 44

def applyCommand cmd, fd
  
  unless @config.variablesDisabled
    @config.variables.each do |k, v|
      cmd.gsub! '{{'+k.to_s+'}}', v
    end

    cmd.gsub! '{{f}}', fd.path
    cmd.gsub! '{{bn}}', File.basename(fd.path)
    cmd.gsub! '{{bn1}}', File.basename(fd.path, '.*')
    cmd.gsub! '{{ext}}', File.extname(fd.path)
    cmd.gsub! '{{dn}}', File.dirname(fd.path)
    st = fd.stat
    cmd.gsub! '{{mode}}', st.mode.to_s
    cmd.gsub! '{{mtime}}', st.mtime.to_i.to_s
    cmd.gsub! '{{ctime}}', st.ctime.to_i.to_s
    cmd.gsub! '{{size}}', st.size.to_s
  end
  fd.flock File::LOCK_UN
  system cmd
  puts cmd
end

#dispatch(file) ⇒ Object

Find and run a command in a given file, depending on the search mode



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/inmake/runner.rb', line 28

def dispatch file
  File.open(file) do |fd|
    fd.flock File::LOCK_EX
    if @config.searchMode == :prefix
      runPrefix fd
    elsif @config.searchMode == :suffix
      runSuffix fd
    elsif @config.searchMode == :regex
      runRegex fd
    elsif @config.searchMode == :secondLine
      runSecondLine fd
    end
  end
end

#runObject

Run inmake on each of the files defined in the configuration



17
18
19
20
21
22
23
24
25
# File 'lib/inmake/runner.rb', line 17

def run
  @config.files.each do |x| 
    begin 
      dispatch x 
    rescue CommandNotFound => e
      raise unless @config.ignoreNonmatches
    end
  end
end

#runPrefix(fd) ⇒ Object

Find a given prefix in a file and apply the build command on match

Raises:



68
69
70
71
72
73
74
75
76
77
# File 'lib/inmake/runner.rb', line 68

def runPrefix fd
  rx = /^\s*#{Regexp.escape @config.searchArgument}\s*/
  command = fd.each_line.detect do |line|
    rx.match line
  end
  raise CommandNotFound, "Did not find a prefixed command in file `#{fd.path}`" unless command
  command.gsub!(rx, '')

  applyCommand command, fd
end

#runRegex(fd) ⇒ Object

Find a given regex in a file and apply the build command when matched Raises CommandNotFound if no command was found in a file

Raises:



94
95
96
97
98
99
100
101
102
103
# File 'lib/inmake/runner.rb', line 94

def runRegex fd
  rx = @config.searchArgument
  command = fd.each_line.detect do |line|
    rx.match line
  end
  raise CommandNotFound, "Did not find a regex-ed command in file `#{fd.path}`" unless command
  command.gsub!(rx, '') if @config.stripMatched

  applyCommand command, fd
end

#runSecondLine(fd) ⇒ Object

Run the second line of code. This is the default option.

Raises:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/inmake/runner.rb', line 106

def runSecondLine fd
  isEncodingLine = /coding[:=]\s*([-\w.]+)/
  fd.gets
  secondLine = fd.gets

  raise CommandNotFound, "There wasn't enough data in the file `#{fd.path}` for two lines!" if $_.nil?

  if isEncodingLine =~ secondLine
    secondLine = fd.gets
    raise CommandNotFound, "There wasn't enough data in the file `#{fd.path}` for three lines!" if $_.nil?
  end

  # Strip stuff until the first whitespace

  command = secondLine[(secondLine =~ /\s/)...secondLine.length].strip

  applyCommand command, fd
end

#runSuffix(fd) ⇒ Object

Find a given suffix in a file and apply the build command when matched Raises CommandNotFound if no command was found in a file

Raises:



81
82
83
84
85
86
87
88
89
90
# File 'lib/inmake/runner.rb', line 81

def runSuffix fd
  rx = /#{Regexp.escape @config.searchArgument}\s*$/
  command = fd.each_line.detect do |line|
    rx.match line
  end
  raise CommandNotFound, "Did not find a suffixed command in file `#{fd.path}`" unless command
  command.gsub!(rx, '') if @config.stripMatched

  applyCommand command, fd
end