Module: Ronin::Core::CLI::Generator
- Includes:
- CommandKit::Colors
- Defined in:
- lib/ronin/core/cli/generator.rb,
lib/ronin/core/cli/generator/options/author.rb,
lib/ronin/core/cli/generator/options/summary.rb,
lib/ronin/core/cli/generator/options/reference.rb,
lib/ronin/core/cli/generator/options/description.rb
Overview
Adds generator methods to a command.
Example
class Gen < Command
include Core::Generator
template_dir File.join(ROOT,'data','templates')
argument :path, desc: 'The path of the script to genereate'
def run(path)
erb 'script.rb.erb', path
end
end
Defined Under Namespace
Modules: ClassMethods, Options
Instance Attribute Summary collapse
-
#template_dir ⇒ String
readonly
The directory to read files from.
Class Method Summary collapse
-
.included(command) ⇒ Object
private
Adds ClassMethods to the class.
Instance Method Summary collapse
-
#chmod(mode, path) ⇒ Object
Changes the permissions of a file or directory.
-
#cp(source, dest) ⇒ Object
Copies a file in.
-
#cp_r(source, dest) ⇒ Object
Copies a directory in.
-
#erb(source, dest = nil) ⇒ Object
Renders a file using an
.erb
template in the #template_dir. -
#initialize(**kwargs) ⇒ Object
Initializes the command.
-
#mkdir(path) ⇒ Object
Creates an empty directory.
-
#print_action(command, source = nil, dest) ⇒ Object
Prints an generator action to STDOUT.
-
#sh(command, *arguments) ⇒ Boolean?
Runs a command.
-
#touch(path) ⇒ Object
Creates an empty file.
Instance Attribute Details
#template_dir ⇒ String (readonly)
The directory to read files from.
93 94 95 |
# File 'lib/ronin/core/cli/generator.rb', line 93 def template_dir @template_dir end |
Class Method Details
.included(command) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds ClassMethods to the class.
58 59 60 |
# File 'lib/ronin/core/cli/generator.rb', line 58 def self.included(command) command.extend ClassMethods end |
Instance Method Details
#chmod(mode, path) ⇒ Object
Changes the permissions of a file or directory.
164 165 166 167 |
# File 'lib/ronin/core/cli/generator.rb', line 164 def chmod(mode,path) print_action "chmod", path FileUtils.chmod(mode,path) end |
#cp(source, dest) ⇒ Object
Copies a file in.
178 179 180 181 182 |
# File 'lib/ronin/core/cli/generator.rb', line 178 def cp(source,dest) print_action 'cp', source, dest FileUtils.cp(File.join(@template_dir,source),dest) end |
#cp_r(source, dest) ⇒ Object
Copies a directory in.
193 194 195 196 197 |
# File 'lib/ronin/core/cli/generator.rb', line 193 def cp_r(source,dest) print_action "cp -r", source, dest FileUtils.cp_r(File.join(@template_dir,source),dest) end |
#erb(source, dest = nil) ⇒ Object
Renders a file using an .erb
template in the #template_dir.
211 212 213 214 215 216 217 218 219 |
# File 'lib/ronin/core/cli/generator.rb', line 211 def erb(source,dest=nil) if dest print_action 'erb', source, dest end source_path = File.join(@template_dir,source) return super(source_path,dest) end |
#initialize(**kwargs) ⇒ Object
Initializes the command.
104 105 106 107 108 109 110 |
# File 'lib/ronin/core/cli/generator.rb', line 104 def initialize(**kwargs) super(**kwargs) unless (@template_dir = self.class.template_dir) raise(NotImplementedError,"#{self.class} did not define template_dir") end end |
#mkdir(path) ⇒ Object
Creates an empty directory.
139 140 141 142 |
# File 'lib/ronin/core/cli/generator.rb', line 139 def mkdir(path) print_action 'mkdir', path FileUtils.mkdir_p(path) end |
#print_action(command, source = nil, dest) ⇒ Object
Prints an generator action to STDOUT.
124 125 126 127 128 129 130 131 |
# File 'lib/ronin/core/cli/generator.rb', line 124 def print_action(command,source=nil,dest) line = String.new line << "\t" << colors.bold(colors.green(command)) line << "\t" << colors.green(source) if source line << "\t" << colors.green(dest) if dest puts(line) end |
#sh(command, *arguments) ⇒ Boolean?
Runs a command.
233 234 235 236 237 |
# File 'lib/ronin/core/cli/generator.rb', line 233 def sh(command,*arguments) print_action "run", [command, *arguments].join(' ') system(command,*arguments) end |
#touch(path) ⇒ Object
Creates an empty file.
150 151 152 153 |
# File 'lib/ronin/core/cli/generator.rb', line 150 def touch(path) print_action 'touch', path FileUtils.touch(path) end |