Class: Lolcommits::Configuration

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

Constant Summary collapse

LOLCOMMITS_BASE =
ENV['LOLCOMMITS_DIR'] || File.join(Dir.home, '.lolcommits')
LOLCOMMITS_ROOT =
File.join(File.dirname(__FILE__), '../..')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_manager, test_mode: false) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
# File 'lib/lolcommits/configuration.rb', line 11

def initialize(plugin_manager, test_mode: false)
  @plugin_manager = plugin_manager
  @loldir         = Configuration.loldir_for('test') if test_mode
end

Instance Attribute Details

#loldirObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lolcommits/configuration.rb', line 28

def loldir
  return @loldir if @loldir

  basename ||= if VCSInfo.repo_root?
                 VCSInfo.local_name
               else
                 File.basename(Dir.getwd)
               end
  basename.sub!(/^\./, 'dot')
  basename.sub!(/ /, '-')
  @loldir = Configuration.loldir_for(basename)
end

#plugin_managerObject

Returns the value of attribute plugin_manager.



8
9
10
# File 'lib/lolcommits/configuration.rb', line 8

def plugin_manager
  @plugin_manager
end

Class Method Details

.loldir_for(basename) ⇒ Object

class methods



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lolcommits/configuration.rb', line 120

def self.loldir_for(basename)
  loldir = File.join(LOLCOMMITS_BASE, basename)

  if File.directory? loldir
    begin
      # ensure 755 permissions for loldir
      File.chmod(0o755, loldir)
    rescue Errno::EPERM
      # abort if permissions cannot be met
      puts "FATAL: directory '#{loldir}' should be present and writeable by user '#{ENV.fetch('USER', nil)}'"
      puts 'Try changing the directory permissions to 755'
      exit 1
    end
  else
    FileUtils.mkdir_p loldir
  end
  loldir
end

Instance Method Details

#ask_for_plugin_nameObject



69
70
71
72
73
# File 'lib/lolcommits/configuration.rb', line 69

def ask_for_plugin_name
  list_plugins
  print 'Name of plugin to configure: '
  gets.strip
end

#capture_path(ext = 'jpg') ⇒ Object



45
46
47
# File 'lib/lolcommits/configuration.rb', line 45

def capture_path(ext = 'jpg')
  File.join loldir, "raw_capture.#{ext}"
end

#configuration_fileObject



24
25
26
# File 'lib/lolcommits/configuration.rb', line 24

def configuration_file
  "#{loldir}/config.yml"
end

#do_configure!(plugin_name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lolcommits/configuration.rb', line 75

def do_configure!(plugin_name)
  $stdout.sync = true
  plugin = find_plugin(plugin_name.to_s.strip)
  return unless plugin

  puts "Configuring plugin: #{plugin.name}\n"
  plugin_config = plugin.plugin_klass.new(config: yaml[plugin_name]).configure_options! || {}

  unless plugin_config[:enabled]
    puts "Disabling plugin: #{plugin.name} - answer with 'true' to enable & configure"
  end
rescue Interrupt
  # e.g. user Ctrl+c or aborted by plugin configure_options!
  puts "\n"
  if plugin
    puts "Configuration aborted: #{plugin.name} has been disabled"
    plugin_config ||= {}
    plugin_config[:enabled] = false
  end
ensure
  if plugin
    # clean away legacy enabled key, later we can remove this
    plugin_config.delete('enabled')
    # save plugin config (if we have anything in the hash)
    save(plugin.name, plugin_config) unless plugin_config.empty?

    # print config if plugin was enabled
    if plugin_config[:enabled]
      puts "\nSuccessfully configured plugin: #{plugin.name} - at path '#{configuration_file}'"
      puts plugin_config.to_yaml
    end
  end
end

#find_plugin(name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/lolcommits/configuration.rb', line 58

def find_plugin(name)
  plugin_name = name.empty? ? ask_for_plugin_name : name
  plugin = plugin_manager.find_by_name(plugin_name)

  return plugin if plugin

  puts "Unable to find plugin: '#{plugin_name}'"
  list_plugins unless name.empty?
  nil
end

#list_pluginsObject



49
50
51
52
53
54
55
56
# File 'lib/lolcommits/configuration.rb', line 49

def list_plugins
  puts "Installed plugins: (* enabled)\n"

  plugin_manager.plugins.each do |gem_plugin|
    plugin = gem_plugin.plugin_klass.new(config: yaml[gem_plugin.name])
    puts " [#{plugin.enabled? ? '*' : '-'}] #{gem_plugin.name}"
  end
end

#save(plugin_name, plugin_config) ⇒ Object



109
110
111
112
# File 'lib/lolcommits/configuration.rb', line 109

def save(plugin_name, plugin_config)
  config_file_contents = yaml.merge(plugin_name => plugin_config).to_yaml
  File.write(configuration_file, config_file_contents)
end

#sha_path(sha, ext) ⇒ Object



41
42
43
# File 'lib/lolcommits/configuration.rb', line 41

def sha_path(sha, ext)
  File.join loldir, "#{sha}.#{ext}"
end

#to_sObject



114
115
116
# File 'lib/lolcommits/configuration.rb', line 114

def to_s
  yaml.to_yaml.to_s
end

#yamlObject



16
17
18
19
20
21
22
# File 'lib/lolcommits/configuration.rb', line 16

def yaml
  @yaml ||= if File.exist?(configuration_file)
              YAML.safe_load(File.open(configuration_file), permitted_classes: [Symbol]) || Hash.new({})
            else
              Hash.new({})
            end
end