Class: Beaver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeaver

Initialize functon should not be used by build scripts



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/beaver.rb', line 11

def initialize
  # Contains all commands
  # { CommandName: Symbol => command: Command }
  @commands = Hash.new
  # Name of the main command
  @mainCommand = nil
  @cache_loc = "./.beaver"
  unless Dir.exist? @cache_loc
    Dir.mkdir @cache_loc
  end
  @term_256_color = `echo $TERM`.include? "256color"
  @opts = []
end

Instance Attribute Details

#cache_locObject

The location where beaver stores its info about files, etc



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

def cache_loc
  @cache_loc
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

#term_256_colorObject

whether the current terminal supports 256 color support



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

def term_256_color
  @term_256_color
end

Instance Method Details

#__appendCommand(cmd) ⇒ Object

Append a command to the global beaver object

  • cmd: Command



51
52
53
54
55
56
57
# File 'lib/beaver.rb', line 51

def __appendCommand(cmd)
  if @commands.size == 0
    @mainCommand = cmd.name
  end

  @commands[cmd.name] = cmd
end

#call(cmd) ⇒ Object

Call a command



60
61
62
63
64
65
66
67
68
# File 'lib/beaver.rb', line 60

def call(cmd)
  _cmd = @commands[cmd.to_sym]
  if _cmd.nil?
    STDERR.puts "No command called #{cmd} found"
    exit 1
  end

  _cmd.call
end

#cleanObject

Clean cache



105
106
107
108
# File 'lib/beaver.rb', line 105

def clean
  FileUtils.rm_r @cache_loc
  reset_cache
end

#endObject

Put this at the end of a file



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/beaver.rb', line 87

def end
  $cache = CacheManager.new # load cache file
  
  command = ARGV[0] || @mainCommand
  if command == "--" # passing arguments to be processed by the builld file -> pass "--" as the command to specify the default
    command = @mainCommand
  end
  self.call command

  $cache.save # save cache file
end

#file_cache_fileObject



25
26
27
28
29
30
31
# File 'lib/beaver.rb', line 25

def file_cache_file
  file_loc = File.join(@cache_loc, "files.info")
  unless File.exist? file_loc
    FileUtils.touch file_loc
  end
  return file_loc
end

#has(opt) ⇒ Object

Check if an option is present



40
41
42
# File 'lib/beaver.rb', line 40

def has(opt)
  @opts.include? opt
end

#list_commandsObject

Returns all available commands as an array



100
101
102
# File 'lib/beaver.rb', line 100

def list_commands
  return @commands.map { |k, v| k }
end

#must_run(cmd) ⇒ Object

Run this command when it is called, no matter if its dependencies did not change



71
72
73
74
75
76
77
78
79
80
# File 'lib/beaver.rb', line 71

def must_run cmd
  _cmd = @commands[cmd.to_sym]
  if _cmd.nil?
    STDERR.puts "\001b[31mNON-FATAL ERROR\001b[0m: Command #{cmd} does not exist, so `must_run` has not effect"
    exit 1
  end
  _cmd.overwrite_should_run = true

  _cmd.call
end

#rm(opt) ⇒ Object

Remove an option



45
46
47
# File 'lib/beaver.rb', line 45

def rm(opt)
  @opts.delete opt
end

#set(opt) ⇒ Object

Set an option :e = exit on non-zero exit code of ‘sh` execution



35
36
37
# File 'lib/beaver.rb', line 35

def set(opt)
  @opts << opt.to_sym
end

#set_main(cmd) ⇒ Object



82
83
84
# File 'lib/beaver.rb', line 82

def set_main(cmd)
  @mainCommand = cmd.to_sym
end