~james/gengo

gengo/samples-test/samples/Ruby/repofetch.rb -rw-r--r-- 3.5 KiB
50871857 — james chore(import): snapshot james.dad.town/gengo at 8f775550b0ee 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require 'action_view'
require 'git'
require 'repofetch/config'
require 'repofetch/env'
require 'repofetch/exceptions'
require 'repofetch/plugin'
require 'repofetch/stat'
require 'repofetch/timespan_stat'
require 'repofetch/theme'
require 'repofetch/util'
require 'repofetch/version'

# Main class for repofetch
class Repofetch
  MAX_ASCII_WIDTH = 40
  MAX_ASCII_HEIGHT = 20
  DEFAULT_THEME = Theme.new.freeze
  @plugins = []

  class << self
    attr_accessor :config
    attr_reader :plugins
  end

  # Loads the config, without affecting the file system.
  def self.load_config
    @config = Config.load
  end

  # Loads the config, writing a default config if it doesn't exist.
  def self.load_config!
    @config = Config.load!
  end

  # Registers a plugin.
  #
  # @param [Plugin] plugin The plugin to register
  def self.register_plugin(plugin)
    @plugins << plugin
  end

  # Replaces an existing plugin. If the existing plugin does not exist,
  # then it registers the plugin instead.
  #
  # @param [Plugin] old The plugin to be replaced
  # @param [Plugin] new The new plugin
  def self.replace_or_register_plugin(old, new)
    index = @plugins.find_index(old)
    if index.nil?
      register_plugin(new)
    else
      @plugins[index] = new
      @plugins
    end
  end

  # Returns the plugin that should be used.
  # Raises a +Repofetch::NoPluginsError+ if no plugins are found.
  # Raises a +Repofetch::TooManyPluginsError+ if more than one plugin is found.
  #
  # @param [String] path The path to check.
  # @param [Array<String>] args The arguments passed to the program.
  #
  # @raise [NoPluginsError] If no plugins were selected.
  # @raise [TooManyPluginsError] If more than one plugin was selected.
  #
  # @return [Plugin] A plugin to use.
  def self.get_plugin(path, args)
    path_plugin = get_plugin_for_path(path)
    repo_plugin = get_plugin_for_repo(path)

    raise TooManyPluginsError if path_plugin && repo_plugin

    raise NoPluginsError if path_plugin.nil? && repo_plugin.nil?

    return path_plugin.from_path(path, args) unless path_plugin.nil?

    git = Git.open(path)
    repo_plugin.from_git(git, args)
  end

  # Gets the plugins that matches the given path.
  #
  # @param [String] path The path to check.
  #
  # @return [Array<Plugin>] The plugins that match the path.
  def self.get_plugins_for_path(path)
    @plugins.filter { |plugin_class| plugin_class.matches_path?(path) }
  end

  # Gets a single plugin that matches the given path.
  #
  # @param [String] path The path to check.
  #
  # @return [Plugin, nil] The plugin that matches the path.
  def self.get_plugin_for_path(path)
    plugins = get_plugins_for_path(path)

    raise TooManyPluginsError if plugins.length > 1

    plugins[0]
  end

  # Gets the plugins that matches the given repository.
  #
  # @param [String] path The repository to check.
  #
  # @return [Array<Plugin>] The plugins that match the repository.
  def self.get_plugins_for_repo(path)
    begin
      git = Git.open(path)
    rescue ArgumentError
      return []
    end

    @plugins.filter { |plugin_class| plugin_class.matches_repo?(git) }
  end

  # Gets a single plugin that matches the given repository.
  #
  # @param [String] path The repository to check.
  #
  # @return [Plugin, nil] The plugin that matches the repository.
  def self.get_plugin_for_repo(path)
    plugins = get_plugins_for_repo(path)

    raise TooManyPluginsError if plugins.length > 1

    plugins[0]
  end

  def self.clear_plugins
    @plugins = []
  end
  private_class_method :clear_plugins
end