Class: Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file_deps, fn) ⇒ Command

Returns a new instance of Command.



8
9
10
11
12
13
14
15
# File 'lib/command.rb', line 8

def initialize(name, file_deps, fn)
  @name = name
  # When one of these files changes, the command should rerun
  # Type: FileDep, or nil
  @file_deps = file_deps
  @fn = fn
  @overwrite_should_run = false
end

Instance Attribute Details

#fnObject

Returns the value of attribute fn.



5
6
7
# File 'lib/command.rb', line 5

def fn
  @fn
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/command.rb', line 4

def name
  @name
end

#overwrite_should_runObject

Returns the value of attribute overwrite_should_run.



6
7
8
# File 'lib/command.rb', line 6

def overwrite_should_run
  @overwrite_should_run
end

Instance Method Details

#callObject

Execute the command if needed (dependency files changed)



18
19
20
21
22
# File 'lib/command.rb', line 18

def call
  if self.should_run?
    self.call_now()
  end
end

#call_nowObject

Force call the command, even if none of the files changed



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/command.rb', line 25

def call_now
  $file = nil
  $files = nil

  if @file_deps.nil?
    @fn.call()
    return
  end
  
  if @file_deps.type == :each
    @file_deps.files.each do |file_obj|
      $file = file_obj
      @fn.call()
    end
  else
    $files = @file_deps.files
    @fn.call()
  end
end

#should_run?Boolean

Returns wheter the command should run, meaning if any of the depency files changed

Returns:

  • (Boolean)


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
73
74
75
76
77
78
79
80
# File 'lib/command.rb', line 47

def should_run?
  return true if @overwrite_should_run
  
  if changed? "__BEAVER__CONFIG__", $PROGRAM_NAME
    # Ruby script itself changed
    # TODO: does not account for dependencies of the script (probably uncommon though)

    # Clear cache, because the config failed
    reset_cache

    unless @file_deps.nil?
      @file_deps.each_file do |file|
        $cache.files.add(@name, file)
      end
    end
    
    return true
  end
  
  if @file_deps.nil?
    return true
  end

  changed = false
  @file_deps.each_file do |file|
    if !changed && (changed? @name, file)
      changed = true
    end
    
    $cache.files.add(@name, file)
  end

  return changed
end