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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#template_dirString (readonly)

The directory to read files from.

Returns:

  • (String)


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.

Parameters:



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.

Parameters:

  • mode (String, Integer)

    The chmod String (ex: "+x") or octal mask.

  • path (String)

    The path to the 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.

Parameters:

  • source (String)

    The file within the #template_dir to copy in.

  • dest (String)

    The destination path to copy the file to.



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.

Parameters:

  • source (String)

    The relative path to the directory within the #template_dir.

  • dest (String)

    The destination path to copy the directory to.



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.

Parameters:

  • source (String)

    The relative path to the file. The .erb template will be derived from the file path by appending the .erb file extension.

  • dest (String, nil) (defaults to: nil)

    The destination path to write the rendered file to. If no destination path is given, the result of the rendered .erb template will be returned.



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.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for initialize.

Raises:

  • (NotImplementedError)

    The class did not set the #template_dir path.



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.

Parameters:

  • path (String)

    The relative path of the directory to be created.



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

Prints an generator action to STDOUT.

Parameters:

  • command (Symbol)

    The command that represents the generator action.

  • source (String) (defaults to: nil)

    The optional source file being copied or rendered.

  • dest (String)

    The file or directory path being created or modified.



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.

Parameters:

  • command (String)

    The command name to execute.

  • arguments (Array<String>)

    Additional arguments for the command.

Returns:

  • (Boolean, nil)

    Indicates whether the command successfully executed or not.



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.

Parameters:

  • path (String)

    The relative path of the empty file to be created.



150
151
152
153
# File 'lib/ronin/core/cli/generator.rb', line 150

def touch(path)
  print_action 'touch', path
  FileUtils.touch(path)
end