class Toys::Templates::Rake

A template that generates tools matching a rakefile.

Constants

DEFAULT_RAKEFILE_PATH

Default path to the Rakefile. @return [String]

Attributes

bundler[W]

Set the bundler state and options for all Rake tools.

Pass `false` to disable bundler. Pass `true` or a hash of options to enable bundler. See the documentation for the [bundler mixin](dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler) for information on the options that can be passed.

@param value [Boolean,Hash] @return [Boolean,Hash]

context_directory[RW]

Custom context directory for this tool.

@param value [String] @return [String]

gem_version[W]

Version requirements for the minitest gem. If set to `nil`, has no version requirement (unless one is specified in the bundle.)

@param value [String,Array<String>,nil] @return [String,Array<String>,nil]

only_described[RW]

Whether to generate tools only for rake tasks with descriptions.

@param value [Boolean] @return [Boolean]

rakefile_path[W]

Path to the Rakefile. If set to `nil`, defaults to {DEFAULT_RAKEFILE_PATH}.

@param value [String,nil] @return [String,nil]

use_flags[RW]

Whether generated tools should use flags instead of positional arguments to pass arguments to rake tasks.

@param value [Boolean] @return [Boolean]

Public Class Methods

find_rakefile(path, context_dir) click to toggle source

@private

# File lib/toys/templates/rake.rb, line 207
def self.find_rakefile(path, context_dir)
  if path == ::File.absolute_path(path)
    return ::File.file?(path) && ::File.readable?(path) ? path : nil
  end
  dir = ::Dir.getwd
  50.times do
    rakefile_path = ::File.expand_path(path, dir)
    return rakefile_path if ::File.file?(rakefile_path) && ::File.readable?(rakefile_path)
    break if dir == context_dir
    next_dir = ::File.dirname(dir)
    break if dir == next_dir
    dir = next_dir
  end
  nil
end
flag_specs(arg) click to toggle source

@private

# File lib/toys/templates/rake.rb, line 195
def self.flag_specs(arg)
  name = arg.to_s.gsub(/\W/, "").downcase
  specs = []
  unless name.empty?
    specs << "--#{name}=VALUE"
    name2 = name.tr("_", "-")
    specs << "--#{name2}=VALUE" unless name2 == name
  end
  specs
end
new(gem_version: nil, rakefile_path: nil, only_described: false, use_flags: false, bundler: false, context_directory: nil) click to toggle source

Create the template settings for the rake template.

@param gem_version [String,Array<String>,nil] Version requirements for

the rake gem. Defaults to nil, indicating no version requirement.

@param rakefile_path [String] Path to the Rakefile. Defaults to

{DEFAULT_RAKEFILE_PATH}.

@param only_described [Boolean] If true, tools are generated only for

rake tasks with descriptions. Default is false.

@param use_flags [Boolean] Generated tools use flags instead of

positional arguments to pass arguments to rake tasks. Default is
false.

@param bundler [Boolean,Hash] If `false` (the default), bundler is not

enabled for Rake tools. If `true` or a Hash of options, bundler is
enabled. See the documentation for the
[bundler mixin](https://dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler)
for information on available options.

@param context_directory [String] A custom context directory to use

when executing this tool.
# File lib/toys/templates/rake.rb, line 37
def initialize(gem_version: nil,
               rakefile_path: nil,
               only_described: false,
               use_flags: false,
               bundler: false,
               context_directory: nil)
  @gem_version = gem_version
  @rakefile_path = rakefile_path
  @only_described = only_described
  @use_flags = use_flags
  @bundler = bundler
  @context_directory = context_directory
end
prepare_rake(rakefile_path, context_dir) click to toggle source

@private

# File lib/toys/templates/rake.rb, line 224
def self.prepare_rake(rakefile_path, context_dir)
  ::Rake::TaskManager.record_task_metadata = true
  rake = ::Rake::Application.new
  ::Rake.application = rake
  ::Dir.chdir(context_dir) do
    ::Rake.load_rakefile(rakefile_path)
  end
  rake
end

Public Instance Methods

bundler_settings() click to toggle source

@private

# File lib/toys/templates/rake.rb, line 141
def bundler_settings
  if @bundler && !@bundler.is_a?(::Hash)
    {}
  else
    @bundler
  end
end
gem_version() click to toggle source

@private

# File lib/toys/templates/rake.rb, line 131
def gem_version
  Array(@gem_version)
end
rakefile_path() click to toggle source

@private

# File lib/toys/templates/rake.rb, line 136
def rakefile_path
  @rakefile_path || DEFAULT_RAKEFILE_PATH
end
use_bundler(**opts) click to toggle source

Use bundler for all Rake tools.

See the documentation for the [bundler mixin](dazuma.github.io/toys/gems/toys-core/latest/Toys/StandardMixins/Bundler) for information on the options that can be passed.

@param opts [keywords] Options for bundler @return [self]

# File lib/toys/templates/rake.rb, line 118
def use_bundler(**opts)
  @bundler = opts
  self
end