class DotModule::Collection

A collection of modules, with an optional YAML configuration file

Constants

CONFIG_FILE_NAME

Attributes

root[R]

Public Class Methods

new(root_path = Dir.pwd) click to toggle source
# File lib/dotmodule.rb, line 41
def initialize(root_path = Dir.pwd)
  @root = Pathname.new(root_path).expand_path
  raise ArgumentError, "Directory '#{@root}' not found" unless @root.directory?
  load_config
end

Public Instance Methods

core_modules() click to toggle source
# File lib/dotmodule.rb, line 63
def core_modules
  @config[:core_modules]
end
create_shared_directories(target_root) click to toggle source
# File lib/dotmodule.rb, line 71
def create_shared_directories(target_root)
  shared_directories.each do |dir|
    abs_path = Pathname.new(target_root + dir).expand_path
    unless abs_path.directory?
      puts "Directory '#{abs_path}' not found, creating..."
      FileUtils.mkdir_p(abs_path)
    end
  end
end
default_target() click to toggle source
# File lib/dotmodule.rb, line 67
def default_target
  Pathname.new(Dir.home)
end
install_all(target=default_target) click to toggle source
# File lib/dotmodule.rb, line 102
def install_all(target=default_target)
  install_modules(modules)
end
install_module(name, target=default_target) click to toggle source
# File lib/dotmodule.rb, line 81
def install_module(name, target=default_target)
  create_shared_directories(target)
  puts ".. Module #{name} .."
  raise ArgumentError, "Module '#{name}' not found" unless (@root+name).directory?
  preinstall(name)
  system "stow -d #{@root} -t #{target} #{name}"
  postinstall(name)
end
install_modules(module_names, target=default_target) click to toggle source
# File lib/dotmodule.rb, line 90
def install_modules(module_names, target=default_target)
  puts "Installing #{module_names.size} modules ..." unless modules.size.zero?
  module_names.each do |m|
    begin
      install_module(m)
    rescue ArgumentError
      puts "WARNING: Module '#{m}' not found"
      break unless ask('... (a)bort or (c)ontinue [a]: ') == 'c'
    end
  end
end
load_config() click to toggle source

Load the optional YAML configuration file Currently, this supports a single array entry listing any folders shared with other applications / the system

# File lib/dotmodule.rb, line 50
def load_config
  file = root+CONFIG_FILE_NAME
  @config = file.file? ? YAML.load_file(file) : { :shared_directories => [], :core_modules => [] }
end
modules() click to toggle source
# File lib/dotmodule.rb, line 55
def modules
  @root.children.select(&:directory?).map(&:basename).map(&:to_s).reject {|d| d.start_with?('.')}
end
shared_directories() click to toggle source
# File lib/dotmodule.rb, line 59
def shared_directories
  @config[:shared_directories]
end
to_s() click to toggle source
# File lib/dotmodule.rb, line 106
    def to_s
      # <<~HEREDOC would be nicer, but not worth sacrificing compatibility with rubies <2.3 for
      <<-HEREDOC

 Collection root:    #{@root}
 Default target:     #{default_target}

 Shared target subdirectories:
   #{shared_directories.join(', ')}

 Modules:
   #{modules.join(', ')}

 Core modules:
   #{core_modules.join(', ')}

      HEREDOC
    end

Private Instance Methods

postinstall(module_name) click to toggle source
# File lib/dotmodule.rb, line 131
def postinstall(module_name)
  runhook(module_name, 'post')
end
preinstall(module_name) click to toggle source
# File lib/dotmodule.rb, line 127
def preinstall(module_name)
  runhook(module_name, 'pre')
end
runhook(module_name, hook) click to toggle source
# File lib/dotmodule.rb, line 135
def runhook(module_name, hook)
  hook_file = @root + "#{module_name}-#{hook}"
  return unless hook_file.file?
  puts "... running hook '#{hook}': '#{hook_file}'"
  IO.popen("sh #{hook_file}").each do |line|
    puts ".... #{line}"
  end
end