Class: Aid::Script

Inherits:
Object
  • Object
show all
Includes:
Colorize, Inheritable
Defined in:
lib/aid/script.rb

Constant Summary

Constants included from Colorize

Colorize::COLOR_CODES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inheritable

included

Methods included from Colorize

colorize, included

Constructor Details

#initialize(*argv) ⇒ Script

Returns a new instance of Script.



52
53
54
# File 'lib/aid/script.rb', line 52

def initialize(*argv)
  @argv = *argv
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



50
51
52
# File 'lib/aid/script.rb', line 50

def argv
  @argv
end

Class Method Details

.descriptionObject



38
39
40
# File 'lib/aid/script.rb', line 38

def self.description
  ''
end

.helpObject



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

def self.help
  <<~HELP
    Help has not been implemented for "#{name}". Please implement a
    help method like so:

    class #{self} < Aid::Script
      def self.help
        <<-EOF
        My awesome help message here.
        This will be so useful for people.
        EOF
      end
    end
  HELP
end

.nameObject



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

def self.name
  klass_name = to_s.split('::').last

  klass_name
    .scan(/[A-Z][a-z0-9]*/)
    .map(&:downcase)
    .join('-')
end

.run(*argv) ⇒ Object



56
57
58
# File 'lib/aid/script.rb', line 56

def self.run(*argv)
  new(*argv).run
end

Instance Method Details

#descriptionObject



46
47
48
# File 'lib/aid/script.rb', line 46

def description
  self.class.description
end

#exit_codeObject



64
65
66
# File 'lib/aid/script.rb', line 64

def exit_code
  0
end

#exit_with_help!Object



17
18
19
20
# File 'lib/aid/script.rb', line 17

def exit_with_help!
  puts self.class.help
  exit
end

#helpObject



42
43
44
# File 'lib/aid/script.rb', line 42

def help
  self.class.help
end

#project_rootObject



78
79
80
# File 'lib/aid/script.rb', line 78

def project_root
  Aid.project_root
end

#runObject

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/aid/script.rb', line 60

def run
  raise NotImplementedError
end

#step(name) ⇒ Object



73
74
75
76
# File 'lib/aid/script.rb', line 73

def step(name)
  puts colorize(:info, "\n== #{name} ==")
  yield if block_given?
end

#system!(*args) ⇒ Object



68
69
70
71
# File 'lib/aid/script.rb', line 68

def system!(*args)
  puts colorize(:command, args.join(' '))
  system(*args) || abort(colorize(:error, "\n== Command #{args} failed =="))
end

#within_dir(directory, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/aid/script.rb', line 82

def within_dir(directory, &block)
  old_pwd = Dir.pwd
  directory = File.expand_path(directory)

  Dir.chdir(directory)

  yield
ensure
  Dir.chdir(old_pwd)
end