class Rigit::Rig

Handles “rigging” (scaffolding) of new projects from template rigs.

Attributes

name[R]

Public Class Methods

create(name) click to toggle source

Create a new empty rig with a basic config file as a starting point.

# File lib/rigit/rig.rb, line 19
def self.create(name)
  target_dir = "#{home}/#{name}"
  template_file = File.expand_path 'template_config.yml', __dir__
  
  FileUtils.mkdir_p "#{target_dir}/base"
  FileUtils.cp template_file, "#{target_dir}/config.yml"
end
home() click to toggle source

Returns the root path for all rigs. By default, it will be +~/.rigs+ unless the RIG_HOME environment variable is set.

# File lib/rigit/rig.rb, line 8
def self.home
  ENV['RIG_HOME'] ||= File.expand_path('.rigs', Dir.home)
end
home=(path) click to toggle source

Sets the RIG_HOME environment variable, and the new root path for rigs.

# File lib/rigit/rig.rb, line 14
def self.home=(path)
  ENV['RIG_HOME'] = path
end
new(name) click to toggle source

Returns a new instance of Rig. The name argument should be the name of an existing (installed) rig.

# File lib/rigit/rig.rb, line 29
def initialize(name)
  @name = name
end

Public Instance Methods

config() click to toggle source

Returns a configatron instance.

# File lib/rigit/rig.rb, line 68
def config
  @config ||= Config.load(config_file)
end
config_file() click to toggle source

Returns the path to the config.yml file of the rig.

# File lib/rigit/rig.rb, line 63
def config_file
  "#{path}/config.yml"
end
exist?() click to toggle source

Returns true if the rig path exists.

# File lib/rigit/rig.rb, line 53
def exist?
  Dir.exist? path
end
has_config?() click to toggle source

Returns true if the rig has a config.yml file.

# File lib/rigit/rig.rb, line 58
def has_config?
  File.exist? config_file
end
info() click to toggle source

Returns metadata about the rig, including all of its config values.

# File lib/rigit/rig.rb, line 73
def info
  {
    name: name,
    path: path,
    config: (has_config? ? File.read(config_file) : '<empty>')
  }
end
path() click to toggle source

Returns the full path to the rig template.

# File lib/rigit/rig.rb, line 48
def path
  "#{Rig.home}/#{name}"
end
scaffold(arguments: {}, target_dir:'.', &block) click to toggle source

Builds the project from the template rig.

# File lib/rigit/rig.rb, line 34
def scaffold(arguments: {}, target_dir:'.', &block)
  scaffold_dir dir: "#{path}/base", arguments: arguments, 
    target_dir: target_dir, &block

  arguments.each do |key, value| 
    additive_dir = "#{path}/#{key}=#{value}"
    if Dir.exist? additive_dir
      scaffold_dir dir: additive_dir, arguments: arguments, 
        target_dir: target_dir, &block
    end
  end
end

Private Instance Methods

binary?(file) click to toggle source
# File lib/rigit/rig.rb, line 115
def binary?(file)
  return false unless config.binaries
  config.binaries.each do |pattern|
    return true if File.fnmatch pattern, file
  end
  false
end
eval_file_content(file, arguments) click to toggle source
# File lib/rigit/rig.rb, line 109
def eval_file_content(file, arguments)
  File.read(file) % arguments
rescue ArgumentError, KeyError => e
  raise TemplateError.new file, e.message
end
get_file_content(file, arguments) click to toggle source
# File lib/rigit/rig.rb, line 101
def get_file_content(file, arguments)
  if binary? file
    File.read file
  else
    eval_file_content file, arguments
  end
end
get_target_filename(file, arguments, target_dir, source_dir) click to toggle source
# File lib/rigit/rig.rb, line 95
def get_target_filename(file, arguments, target_dir, source_dir)
  (file % arguments).sub source_dir, target_dir
rescue KeyError => e
  raise TemplateError.new file, e.message
end
scaffold_dir(dir:, arguments:, target_dir:) { |target_file| ... } click to toggle source
# File lib/rigit/rig.rb, line 83
def scaffold_dir(dir:, arguments:, target_dir:)
  files = Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH) - %w[. ..]
  files.reject! { |file| File.directory? file }

  files.each do |file|
    target_file = get_target_filename file, arguments, target_dir, dir
    next if block_given? and !yield target_file
    content = get_file_content file, arguments
    File.deep_write target_file, content
  end
end