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.



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

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.



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

def fn
  @fn
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#overwrite_should_runObject

Returns the value of attribute overwrite_should_run.



8
9
10
# File 'lib/command.rb', line 8

def overwrite_should_run
  @overwrite_should_run
end

Instance Method Details

#callObject

Execute the command if needed (dependency files changed)



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

def call
  global_alread_defined = $__BEAVER_SHOULD_RUN_SCOPE
  if self.overwrite_should_run
    $__BEAVER_SHOULD_RUN_SCOPE  = true
  end
  if $__BEAVER_SHOULD_RUN_SCOPE 
    self.overwrite_should_run = true
  end
  
  if self.should_run?
    self.call_now()
  end

  if !global_alread_defined && $__BEAVER_SHOULD_RUN_SCOPE 
    $__BEAVER_SHOULD_RUN_SCOPE  = false
  end
end

#call_nowObject

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/command.rb', line 39

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)


61
62
63
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
93
94
# File 'lib/command.rb', line 61

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