Class: Repofetch

Inherits:
Object
  • Object
show all
Defined in:
lib/repofetch.rb,
lib/repofetch/cli.rb,
lib/repofetch/env.rb,
lib/repofetch/stat.rb,
lib/repofetch/util.rb,
lib/repofetch/theme.rb,
lib/repofetch/config.rb,
lib/repofetch/github.rb,
lib/repofetch/gitlab.rb,
lib/repofetch/plugin.rb,
lib/repofetch/version.rb,
lib/repofetch/exceptions.rb,
lib/repofetch/timespan_stat.rb,
lib/repofetch/bitbucketcloud.rb,
lib/repofetch/bitbucketcloud/stats.rb

Overview

Main class for repofetch

Defined Under Namespace

Modules: Util Classes: BitbucketCloud, CLI, Config, Env, Error, Github, Gitlab, NoPluginsError, Plugin, PluginUsageError, Stat, Theme, TimespanStat, TooManyPluginsError

Constant Summary collapse

MAX_ASCII_WIDTH =
40
MAX_ASCII_HEIGHT =
20
DEFAULT_THEME =
Theme.new.freeze
VERSION =
File.read(File.expand_path('VERSION', __dir__)).strip

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



23
24
25
# File 'lib/repofetch.rb', line 23

def config
  @config
end

.pluginsObject (readonly)

Returns the value of attribute plugins.



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

def plugins
  @plugins
end

Class Method Details

.get_plugin(path, args) ⇒ Plugin

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.

Parameters:

  • path (String)

    The path to check.

  • args (Array<String>)

    The arguments passed to the program.

Returns:

  • (Plugin)

    A plugin to use.

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/repofetch.rb', line 70

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

.get_plugin_for_path(path) ⇒ Plugin?

Gets a single plugin that matches the given path.

Parameters:

  • path (String)

    The path to check.

Returns:

  • (Plugin, nil)

    The plugin that matches the path.

Raises:



98
99
100
101
102
103
104
# File 'lib/repofetch.rb', line 98

def self.get_plugin_for_path(path)
  plugins = get_plugins_for_path(path)

  raise TooManyPluginsError if plugins.length > 1

  plugins[0]
end

.get_plugin_for_repo(path) ⇒ Plugin?

Gets a single plugin that matches the given repository.

Parameters:

  • path (String)

    The repository to check.

Returns:

  • (Plugin, nil)

    The plugin that matches the repository.

Raises:



126
127
128
129
130
131
132
# File 'lib/repofetch.rb', line 126

def self.get_plugin_for_repo(path)
  plugins = get_plugins_for_repo(path)

  raise TooManyPluginsError if plugins.length > 1

  plugins[0]
end

.get_plugins_for_path(path) ⇒ Array<Plugin>

Gets the plugins that matches the given path.

Parameters:

  • path (String)

    The path to check.

Returns:

  • (Array<Plugin>)

    The plugins that match the path.



89
90
91
# File 'lib/repofetch.rb', line 89

def self.get_plugins_for_path(path)
  @plugins.filter { |plugin_class| plugin_class.matches_path?(path) }
end

.get_plugins_for_repo(path) ⇒ Array<Plugin>

Gets the plugins that matches the given repository.

Parameters:

  • path (String)

    The repository to check.

Returns:

  • (Array<Plugin>)

    The plugins that match the repository.



111
112
113
114
115
116
117
118
119
# File 'lib/repofetch.rb', line 111

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

.load_configObject

Loads the config, without affecting the file system.



28
29
30
# File 'lib/repofetch.rb', line 28

def self.load_config
  @config = Config.load
end

.load_config!Object

Loads the config, writing a default config if it doesn't exist.



33
34
35
# File 'lib/repofetch.rb', line 33

def self.load_config!
  @config = Config.load!
end

.register_plugin(plugin) ⇒ Object

Registers a plugin.

Parameters:

  • plugin (Plugin)

    The plugin to register



40
41
42
# File 'lib/repofetch.rb', line 40

def self.register_plugin(plugin)
  @plugins << plugin
end

.replace_or_register_plugin(old, new) ⇒ Object

Replaces an existing plugin. If the existing plugin does not exist, then it registers the plugin instead.

Parameters:

  • old (Plugin)

    The plugin to be replaced

  • new (Plugin)

    The new plugin



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

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