class Halite::HelperBase

Base class for helpers like Rake tasks.

@api semipublic @since 1.0.0

Attributes

base[R]

Base folder of the gem. @return [String]

gem_name[R]

Name of the gem to use in these Rake tasks. @return [String]

options[R]

Helper options. @return [Hash<Symbol, Object>]

Public Class Methods

install(*args) click to toggle source

Class method helper to install the tasks.

@param args Arguments to be passed to {#initialize}. @return [void] @example

MyApp::RakeHelper.install(gem_name: 'otherapp')
# File lib/halite/helper_base.rb, line 38
def self.install(*args)
  new(*args).install
end
new(gem_name: nil, base: nil, **options) click to toggle source

@param gem_name [String] Name of the gem to use in these Rake tasks. @param base [String] Base folder of the gem. @options options [Boolean] no_color Forcibly disable using colors in the output.

# File lib/halite/helper_base.rb, line 57
def initialize(gem_name: nil, base: nil, **options)
  @base = base || if defined?(::Rake) && ::Rake.original_dir
    ::Rake.original_dir
  else
    Dir.pwd
  end # rubocop:disable Lint/EndAlignment
  @gem_name = gem_name || find_gem_name(@base)
  @options = options
end

Public Instance Methods

install() click to toggle source

Subclass hoook to provide the actual tasks or other helpers to install.

@return [void] @example

def install
  extend Rake::DSL
  desc 'My awesome task'
  task 'mytask' do
    # ...
  end
end
# File lib/halite/helper_base.rb, line 78
def install
  raise NotImplementedError
end

Private Instance Methods

cookbook() click to toggle source

Cookbook model for the current gem.

@return [Halite::Gem]

# File lib/halite/helper_base.rb, line 125
def cookbook
  require 'halite/gem'
  @cookbook ||= Halite::Gem.new(gemspec)
end
find_gem_name(base) click to toggle source

Search a directory for a .gemspec file to determine the gem name. Returns nil if no gemspec is found.

@param base [String] Folder to search. @return [String, nil]

# File lib/halite/helper_base.rb, line 102
def find_gem_name(base)
  spec = Dir[File.join(base, '*.gemspec')].first
  File.basename(spec, '.gemspec') if spec
end
gemspec() click to toggle source

Gem specification for the current gem.

@return [Gem::Specification]

# File lib/halite/helper_base.rb, line 110
def gemspec
  @gemspec ||= begin
    raise Error.new("Unable to automatically determine gem name from specs in #{base}. Please set the gem name via #{self.class.name}.install_tasks(gem_name: 'name')") unless gem_name
    g = Bundler.load_gemspec(File.join(base, gem_name+'.gemspec'))
    # This is returning the path it would be in if installed normally,
    # override so we get the local path. Also for reasons that are entirely
    # beyond me, #tap makes Gem::Specification flip out so do it old-school.
    g.full_gem_path = base
    g
  end
end
shell() click to toggle source

Return a Thor::Shell object based on output settings.

@return [Thor::Shell::Basic] @example

shell.say('Operation completed', :green)
# File lib/halite/helper_base.rb, line 89
def shell
  @shell ||= if options[:no_color] || !STDOUT.tty?
    Thor::Shell::Basic
  else
    Thor::Base.shell
  end.new
end