Skip to content

Commit 08ae9a4

Browse files
committed
Adds initial Plugin with native git plugin example
1 parent 88784eb commit 08ae9a4

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

lib/git_commander.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "git_commander/command"
44
require "git_commander/logger"
5+
require "git_commander/plugin"
56
require "git_commander/registry"
67
require "git_commander/system"
78
require "git_commander/version"

lib/git_commander/plugin.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "plugin/executor"
4+
5+
module GitCommander
6+
#
7+
# @abstract Allows for proxying methods to a plugin from within the context of
8+
# a Command's block.
9+
#
10+
# A Plugin provides additional external instances to a Command's @block
11+
# context. Plugins can define their own inline gems, and can define
12+
# additional Commands.
13+
#
14+
# @example Loadable Command file with native `git` plugin
15+
# # This is a unique example of using `plugin` without any options. It
16+
# # uses the `git` plugin provided natively with this gem. Most times you will
17+
# # use a path: "path/to/my/plugin", or url: "https://example.com/myplugin"
18+
# plugin :git
19+
# command :local_branches do |cmd|
20+
# git.branches.local.map(&:name)
21+
# end
22+
#
23+
class Plugin
24+
attr_accessor :executor, :name, :registry
25+
26+
# Creates a Plugin object. +name+ is the name of the plugin.
27+
#
28+
# Options include:
29+
#
30+
# +source_instance+ - an instance of an object to use in the Command's block context
31+
# +registry+ - a Registry instance for where this Plugin will be stored for lookup
32+
def initialize(name, source_instance: nil, registry: nil)
33+
@name = name
34+
@executor = Executor.new(source_instance) if source_instance
35+
@registry = registry || GitCommander::Registry.new
36+
end
37+
end
38+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
require "delegate"
4+
5+
module GitCommander
6+
class Plugin
7+
class Executor < SimpleDelegator
8+
end
9+
end
10+
end

lib/git_commander/plugins/git.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
gemfile do
4+
source "https://rubygems.org"
5+
gem "git"
6+
gem "rspec" # This is a stop-gap to allow testing of these in the context of the gem
7+
end
8+
9+
Git.open Dir.pwd, log: GitCommander.logger
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
describe GitCommander::Plugin do
6+
it "initializes an Executor with the provided block" do
7+
source_instance = Object.new
8+
expect(described_class::Executor).to receive(:new).with(source_instance)
9+
described_class.new :git, source_instance: source_instance
10+
end
11+
end

0 commit comments

Comments
 (0)