class ActiveScripts::Packages::Base

Constants

COMMANDS

INFO: ActiveScripts::Packages::Base contains code that is

shared between all package files.
OPTIONS

Attributes

command[RW]
options[RW]

Public Class Methods

execute(command, options={}) click to toggle source
# File lib/active_scripts/packages/base.rb, line 18
def self.execute(command, options={})
  new(command, options).execute
end
new(command, options={}) click to toggle source
# File lib/active_scripts/packages/base.rb, line 13
def initialize(command, options={})
  @command = command
  @options = options
end

Public Instance Methods

execute() click to toggle source
# File lib/active_scripts/packages/base.rb, line 22
def execute
  assert_valid_command!
  assert_valid_options!
  assert_valid_user!
  send(@command)
end

Private Instance Methods

addendum_path!(file) click to toggle source
# File lib/active_scripts/packages/base.rb, line 55
def addendum_path!(file)
  output = File.dirname(__FILE__)
  output = output.shift("/packages")
  "#{output}/addendums/#{file}"
end
assert_valid_command!() click to toggle source
# File lib/active_scripts/packages/base.rb, line 31
def assert_valid_command!
  unless COMMANDS.include?(@command)
    raise ArgumentError,
      "Unknown package command: #{@command.inspect}. Valid package commands are: #{COMMANDS.map(&:inspect).join(', ')}"
  end
end
assert_valid_options!() click to toggle source
# File lib/active_scripts/packages/base.rb, line 38
def assert_valid_options!
  @options.each_key do |option|
    unless OPTIONS.include?(option)
      raise ArgumentError,
        "Unknown package option: #{option.inspect}. Valid package options are: #{OPTIONS.map(&:inspect).join(', ')}"
    end
  end
end
execute_command!(command) click to toggle source
# File lib/active_scripts/packages/base.rb, line 61
def execute_command!(command)
  say("   Executing: '#{command}'") if option_dry_run? || option_verbose?
  option_dry_run? ? (output = error = "") : (output, error, status = Open3.capture3("#{command}"))
  return(error.blank? ? output : error)
end
notify_package_exists!() click to toggle source
# File lib/active_scripts/packages/base.rb, line 90
def notify_package_exists!
  say_warning("   [!] PackageError")
  say_warning("     - The package is already installed.")
end
notify_package_missing!() click to toggle source
# File lib/active_scripts/packages/base.rb, line 95
def notify_package_missing!
  say_error("   [!] PackageError")
  say_error("     - The package is not installed.")
end
notify_package_unavailable!() click to toggle source
# File lib/active_scripts/packages/base.rb, line 100
def notify_package_unavailable!
  say_warning("   [!] PackageError")
  say_warning("     - The package is not available for your operating system.")
end
option_dry_run?() click to toggle source
# File lib/active_scripts/packages/base.rb, line 47
def option_dry_run?
  @options.fetch(:dry_run, false)
end
option_verbose?() click to toggle source
# File lib/active_scripts/packages/base.rb, line 51
def option_verbose?
  @options.fetch(:verbose, false)
end
package_installed?(command: "brew list", excludes: nil, includes: nil) click to toggle source
# File lib/active_scripts/packages/base.rb, line 67
def package_installed?(command: "brew list", excludes: nil, includes: nil)
  begin
    output, error, status = Open3.capture3("#{command}")
    output = output.squish
    if includes.nil? && excludes.nil?
      output.present?
    else
      includes.nil? ? [excludes].flatten.any? { |str| output.exclude?(str) } : [includes].flatten.any? { |str| output.include?(str) }
    end
  rescue
    return(false)
  end
end
package_output?(output, excludes: nil, includes: nil) click to toggle source
# File lib/active_scripts/packages/base.rb, line 81
def package_output?(output, excludes: nil, includes: nil)
  output = output.squish
  if includes.nil? && excludes.nil?
    output.blank?
  else
    includes.nil? ? [excludes].flatten.any? { |str| output.exclude?(str) } : [includes].flatten.any? { |str| output.include?(str) }
  end
end