Class: DotModule::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/dotmodule.rb

Overview

A collection of modules, with an optional YAML configuration file

Constant Summary collapse

CONFIG_FILE_NAME =
'dotmodule.collection'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = Dir.pwd) ⇒ Collection

Returns a new instance of Collection.

Raises:

  • (ArgumentError)


41
42
43
44
45
# 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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



39
40
41
# File 'lib/dotmodule.rb', line 39

def root
  @root
end

Instance Method Details

#core_modulesObject



63
64
65
# File 'lib/dotmodule.rb', line 63

def core_modules
  @config[:core_modules]
end

#create_shared_directories(target_root) ⇒ Object



71
72
73
74
75
76
77
78
79
# 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_targetObject



67
68
69
# File 'lib/dotmodule.rb', line 67

def default_target
  Pathname.new(Dir.home)
end

#install_all(target = default_target) ⇒ Object



102
103
104
# File 'lib/dotmodule.rb', line 102

def install_all(target=default_target)
  install_modules(modules)
end

#install_module(name, target = default_target) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# 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_configObject

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



50
51
52
53
# 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

#modulesObject



55
56
57
# File 'lib/dotmodule.rb', line 55

def modules
  @root.children.select(&:directory?).map(&:basename).map(&:to_s).reject {|d| d.start_with?('.')}
end

#shared_directoriesObject



59
60
61
# File 'lib/dotmodule.rb', line 59

def shared_directories
  @config[:shared_directories]
end

#to_sObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dotmodule.rb', line 106

def to_s
  # <<~HEREDOC would be nicer, but not worth sacrificing compatibility with rubies <2.3 for
  "\n Collection root:    \#{@root}\n Default target:     \#{default_target}\n\n Shared target subdirectories:\n   \#{shared_directories.join(', ')}\n\n Modules:\n   \#{modules.join(', ')}\n\n Core modules:\n   \#{core_modules.join(', ')}\n\n  HEREDOC\nend\n"