Top Level Namespace

Defined Under Namespace

Classes: Beaver, FileInfo, FileInfoRW, OneWayDependency

Instance Method Summary collapse

Instance Method Details

#__beaver_file_changed?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
# File 'lib/file_change.rb', line 56

def __beaver_file_changed?
  if $__beaver_file == nil
    $__beaver_file = file_changed?($0)
  end
  
  return $__beaver_file
end

#_command(name, &func) ⇒ Object

Register a new beaver command



31
32
33
# File 'lib/command.rb', line 31

def _command(name, &func)
  $beaver.__appendCommand(name, func)
end

#_command_mto(name, src: nil, target: nil, &func) ⇒ Object



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

def _command_mto(name, src:nil, target:nil, &func)
  $beaver.__appendCommandCB name do
    should_execute = false
    
    if_any_changed src do |_|
      should_execute = true
    end
    
    if !File.file?(target)
      should_execute = true
    end
    
    if should_execute
      src_files = get_matches(src)
      func.call src_files.join(" "), target
    end
  end
end

#_command_oto(name, src: nil, target_dir: nil, target_ext: nil, &func) ⇒ Object

  • Example:

“‘ruby command_oto :command_name, src: “src/*/.c”, target_dir: “build”, target_ext: “.o” do |source_file, target_file|

end “‘



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
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/command.rb', line 44

def _command_oto(name, src: nil, target_dir: nil, target_ext: nil, &func)
  $beaver.__appendCommandCB name do
    src_files = get_matches(src)
    
    abs_cd = File.dirname(File.expand_path($0))
    
    # Contains all files with (source and target)
    files = []
    src_files.each do |srcfile|
      file_name = File.basename(srcfile, ".*")
      
      # path of the source file relative to the current directory
      path_ext = File.dirname(
        Pathname.new(File.expand_path(srcfile))
          .relative_path_from(abs_cd)
      )
      
      files << OneWayDependency.new(
        srcfile,
        File.join(target_dir, path_ext, "#{file_name}#{target_ext}")
      )
    end
    
    for file in files
      should_execute = false
      if file_changed?(file.src)
        # If source file has changed, then the function should be called
        should_execute = true
      elsif !File.file?(file.target)
        # If the target file does not exist, then the function should be called
        should_execute = true
      elsif __beaver_file_changed?
        # If the beaver config file changed, then the function should be called
        should_execute = true
      end
    
      if should_execute
        func.call file.src, file.target
      end      
    end
  end
end

#command(name, src: nil, target_dir: nil, target_ext: nil, target: nil, &func) ⇒ Object

register a new beaver command

  • Possible combinations

Register a normal command

command :name do end

Register a command that executes if a source file changed, or the target file does not exist (one target file per source file)

command :name, src: "", target_dir: "", target_ext: "", do end

Register a command that executes if a source file changed, or the single target file does not exist (multiple sources into one target file) The callback will receive a files variables, which is a string containing all paths, separated by spaces

command :name, src: "", target: "" do end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/command.rb', line 14

def command(name, src:nil, target_dir:nil, target_ext:nil, target:nil, &func)
  if !src.nil? && !target_dir.nil? && !target_ext.nil?
    _command_oto name, src:src, target_dir:target_dir, target_ext:target_ext do |src, trg|
      func.call src, trg
    end
  elsif !src.nil? && !target.nil?
      _command_mto name, src:src, target:target do |files, target|
        func.call files, target
      end
  else
    _command name do ||
      func.call
    end
  end
end

#file_changed?(file) ⇒ Boolean

check if a single file has changed returns true if it has been changed

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/file_change.rb', line 37

def file_changed?(file)
  cache_file_loc = "#{$beaver.cache_loc}/#{file}.info"
  info_rw = FileInfoRW.new(cache_file_loc, file)
  if !File.file?(cache_file_loc)
      # File does not exist, so create it and call the function
      info_rw.create_cache()
      
      return true
  else
    # File exists, so read it
    if info_rw.file_has_changed?
      return true
    else
      return false
    end
  end
end

#get_matches(files) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/file_matches.rb', line 1

def get_matches(files)
  # All file patterns to process
  patterns_to_process = []
  
  # Check if array or string
  if files.respond_to? :each
    # is an array
    patterns_to_process = file
  else
    patterns_to_process << files
  end
  
  # Contains all files that match the expressions
  matches = nil
  
  patterns_to_process.each do |pattern|
    if pattern.is_a?(Regexp)
      matches = Find.find("./").grep(pattern)
    else
      # is a string
      matches = Dir.glob(pattern)
    end
  end
  
  return matches
end

#if_any_changed(files, &func) ⇒ Object

Executes if any file changed The callback receives a list of changed files



24
25
26
27
28
29
30
31
32
33
# File 'lib/file_change.rb', line 24

def if_any_changed(files, &func)
  changed_files = []
  if_changed files do |file|
      changed_files << file
  end
  
  unless changed_files.empty?
    func.call changed_files
  end
end

#if_changed(files, &func) ⇒ Object

Execute a function if the specified files have changed since last execution Also executes the function if the config file has changed

# Parameters

  • files: either an array of files (or file patterns), or a file pattern as a string or regexp

  • func: the body of the if statement



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/file_change.rb', line 10

def if_changed(files, &func)
  matches = get_matches(files)
  
  # Execute for each match if either no cache exists, or
  # if the cache indicates a older file version
  matches.each do |file|
    if file_changed?(file) || __beaver_file_changed?
      func.call file
    end
  end
end

#if_exists(files, &func) ⇒ Object

Executes a function if a file exists, passing in the existing file



2
3
4
5
6
7
8
# File 'lib/file_exists.rb', line 2

def if_exists(files, &func)
  files.each do |file|
    if File.file?(file)
      func.call file
    end
  end
end

#if_not_exists(files, &func) ⇒ Object

Executes a function if a file does not exists, passing in the non-existing file



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

def if_not_exists(files, &func)
  files.each do |file|
    unless File.file?(file)
      func.call file
    end
  end
end

#sys(cmd) ⇒ Object

Execute system command



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

def sys(cmd)
  puts cmd
  system cmd
end

#unless_exists(files, &func) ⇒ Object

Executes a function if a file exists, passing in the existing file



20
21
22
# File 'lib/file_exists.rb', line 20

def unless_exists(files, &func)
  if_not_exists(files, func)
end

#§(cmd) ⇒ Object

Execute system command



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

def