Class: DotModule::Collection
- Inherits:
-
Object
- Object
- DotModule::Collection
- 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
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #core_modules ⇒ Object
- #create_shared_directories(target_root) ⇒ Object
- #default_target ⇒ Object
-
#initialize(root_path = Dir.pwd) ⇒ Collection
constructor
A new instance of Collection.
- #install_all(target = default_target) ⇒ Object
- #install_module(name, target = default_target) ⇒ Object
- #install_modules(module_names, target = default_target) ⇒ Object
-
#load_config ⇒ Object
Load the optional YAML configuration file Currently, this supports a single array entry listing any folders shared with other applications / the system.
- #modules ⇒ Object
- #shared_directories ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(root_path = Dir.pwd) ⇒ Collection
Returns a new instance of Collection.
41 42 43 44 45 |
# File 'lib/dotmodule.rb', line 41 def initialize(root_path = Dir.pwd) @root = Pathname.new(root_path). raise ArgumentError, "Directory '#{@root}' not found" unless @root.directory? load_config end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
39 40 41 |
# File 'lib/dotmodule.rb', line 39 def root @root end |
Instance Method Details
#core_modules ⇒ Object
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). unless abs_path.directory? puts "Directory '#{abs_path}' not found, creating..." FileUtils.mkdir_p(abs_path) end end end |
#default_target ⇒ Object
67 68 69 |
# File 'lib/dotmodule.rb', line 67 def default_target @root.parent end |
#install_all(target = default_target) ⇒ Object
101 102 103 |
# File 'lib/dotmodule.rb', line 101 def install_all(target=default_target) install_modules(modules) end |
#install_module(name, target = default_target) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/dotmodule.rb', line 81 def install_module(name, target=default_target) create_shared_directories(target) puts "... installing #{name} ..." raise ArgumentError, "Module '#{name}' not found" unless (@root+name).directory? system "stow -d #{@root} -t #{target} #{name}" end |
#install_modules(module_names, target = default_target) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/dotmodule.rb', line 89 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 ⇒ Object
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 => nil, :core_modules => nil } end |
#modules ⇒ Object
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_directories ⇒ Object
59 60 61 |
# File 'lib/dotmodule.rb', line 59 def shared_directories @config[:shared_directories] end |
#to_s ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/dotmodule.rb', line 105 def to_s <<~HEREDOC Collection root: #{@root} Default target: #{default_target} Shared target subdirectories: #{shared_directories.join(', ')} Modules: #{modules.join(', ')} Core modules: #{core_modules.join(', ')} HEREDOC end |