Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Remove the notion of a project type.
Browse files Browse the repository at this point in the history
Rather than a "type" of a project, plugins start to emerge as the "one
true way™" of doing everything. We will have a single project class, and
one would mix in plugins to add the needed functionality. There are some
new methods on the graph to find projects with a given plugin, replacing
the old `all_by_type` and `changed_by_type`: these new methods are
`all_with_plugin` and `changed_with_plugin`, respectively.

This work is in regards to #5.#5.#5.
  • Loading branch information
nathankleyn committed Mar 19, 2015
1 parent 814081a commit 6e4407c
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 144 deletions.
4 changes: 2 additions & 2 deletions lib/shanty/discoverers/rubygem_discoverer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'shanty/discoverer'
require 'shanty/projects/rubygem_project'
require 'shanty/plugins/rubygem_plugin'

module Shanty
# Public: Discoverer for Shantyfiles
Expand All @@ -9,7 +9,7 @@ class RubygemDiscoverer < Discoverer
def discover
Dir[File.join(env.root, '**', '*.gemspec')].map do |path|
create_project_template(File.absolute_path(File.dirname(path))) do |project_template|
project_template.type = RubygemProject
project_template.plugin(RubygemPlugin)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/shanty/discoverers/shantyfile_discoverer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'shanty/discoverer'
require 'shanty/projects/static_project'

module Shanty
# Public: Discoverer for Shantyfiles
Expand Down
1 change: 1 addition & 0 deletions lib/shanty/env.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'logger'
require 'pathname'
require 'yaml'

module Shanty
Expand Down
20 changes: 11 additions & 9 deletions lib/shanty/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'forwardable'
require 'tsort'

require 'shanty/project'

module Shanty
# Public: Represents the link graph of projects in the repository. This class is
# responsible for collecting up all the information the projects we have,
Expand Down Expand Up @@ -46,24 +48,24 @@ def by_name(name)
find { |project| project.name == name }
end

# Public: Returns all projects of the given types.
# Public: Returns all projects that have the given plugin.
#
# *types - One or more types to filter by.
# *plugins - One or more plugins to filter by.
#
# Returns an Array of Project subclasses, one for each project in the
# repository.
def all_of_type(*types)
select { |project| types.include?(project.class) }
def all_with_plugin(*plugins)
reject { |project| (project.plugins & plugins).empty? }
end

# Public: Returns all the changed projects of the given types.
# Public: Returns all changed projects that have the given plugin.
#
# *types - One or more types to filter by.
# *plugins - One or more plugins to filter by.
#
# Returns an Array of Project subclasses, one for each project in the
# repository.
def changed_of_type(*types)
changed.select { |project| types.include?(project.class) }
def changed_with_plugin(*plugins)
changed.reject { |project| (project.plugins & plugins).empty? }
end

# Public: Given a path to a file or directory (normally a path obtained
Expand Down Expand Up @@ -107,7 +109,7 @@ def tsort_each_child(project)

def projects_by_path
@projects_by_path ||= Hash[@project_templates.map do |project_template|
project = project_template.type.new(@env, project_template)
project = Project.new(@env, project_template)
project.setup!
@project_path_trie[project.path] = project
[project.path, project]
Expand Down
14 changes: 14 additions & 0 deletions lib/shanty/plugins/rubygem_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'shanty/plugin'

module Shanty
# Public: Rubygem plugin for buildin gems.
module RubygemPlugin
extend Plugin

subscribe :build, :build_gem

def build_gem
system 'gem build *.gemspec'
end
end
end
14 changes: 6 additions & 8 deletions lib/shanty/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Project
include ActsAsGraphVertex
include CallMeRuby

attr_accessor :name, :path, :options, :parents_by_path, :changed
attr_accessor :name, :path, :options, :parents_by_path, :changed, :plugins
alias_method :changed?, :changed

# Public: Initialise the Project instance.
Expand All @@ -23,13 +23,11 @@ def initialize(env, project_template)
@options = project_template.options
@parents_by_path = project_template.parents
@changed = false
@plugins = @project_template.plugins
end

def setup!
@project_template.plugins.each do |plugin|
plugin.add_to_project(self)
end

@plugins.each { |plugin| plugin.add_to_project(self) }
instance_eval(&@project_template.after_create) unless @project_template.after_create.nil?
end

Expand All @@ -43,16 +41,16 @@ def artifact_paths

# Public: Overriden String conversion method to return a simplified
# representation of this instance that doesn't include the cyclic
# parent/children attributes as defined by the ActsAsGraphNode mixin.
# parent/children attributes as defined by the ActsAsGraphVertex mixin.
#
# Returns a simple String representation of this instance.
def to_s
"Name: #{name}, Type: #{self.class}"
name
end

# Public: Overriden String conversion method to return a more detailed
# representation of this instance that doesn't include the cyclic
# parent/children attributes as defined by the ActsAsGraphNode mixin.
# parent/children attributes as defined by the ActsAsGraphVertex mixin.
#
# Returns more detailed String representation of this instance.
def inspect
Expand Down
4 changes: 1 addition & 3 deletions lib/shanty/project_template.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require 'attr_combined_accessor'
require 'shanty/projects/static_project'

module Shanty
# Public: Allows creation of a project using a discoverer
class ProjectTemplate
attr_combined_accessor :name, :type, :priority, :plugins, :parents, :options
attr_combined_accessor :name, :priority, :plugins, :parents, :options
attr_reader :env, :path

def initialize(env, path)
Expand All @@ -14,7 +13,6 @@ def initialize(env, path)
@path = path

@name = File.basename(path)
@type = StaticProject
@priority = 0
@plugins = []
@parents = []
Expand Down
12 changes: 0 additions & 12 deletions lib/shanty/projects/rubygem_project.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/shanty/projects/static_project.rb

This file was deleted.

9 changes: 9 additions & 0 deletions spec/fixtures/test_unused_plugin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'shanty/plugin'

module Shanty
# Test unused Plugin fixture, used for testing whether looking plugins up by this plugin type, given none of them
# have it included, returns nothing.
module UnusedPlugin
extend Plugin
end
end
29 changes: 16 additions & 13 deletions spec/lib/shanty/graph_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'spec_helper'
require 'tmpdir'
require 'shanty/graph'
require 'shanty/projects/static_project'
require 'shanty/project'

require_fixture 'test_plugin'
require_fixture 'test_unused_plugin'

# Allows all classes to be refereneced without the module name
module Shanty
Expand All @@ -15,7 +18,7 @@ module Shanty
project_templates[:shanty].parent(missing_parent)

expect { subject }.to raise_error("Cannot find project at path #{File.join(root, missing_parent)}, which was "\
'specified as a dependency for Name: shanty, Type: Shanty::StaticProject')
'specified as a dependency for shanty')
end
end

Expand Down Expand Up @@ -57,35 +60,35 @@ module Shanty
end
end

describe('#all_of_type') do
it('returns an empty array when no types are given') do
expect(subject.all_of_type).to be_empty
describe('#all_with_plugin') do
it('returns an empty array when no plugins are given') do
expect(subject.all_with_plugin).to be_empty
end

it('returns an empty array when no projects match the types given') do
expect(subject.all_of_type(Project)).to be_empty
it('returns an empty array when no projects match the plugins given') do
expect(subject.all_with_plugin(UnusedPlugin)).to be_empty
end

it('returns the correct projects when matching types are given') do
expect(subject.all_of_type(StaticProject)).to match_array(subject)
it('returns the correct projects when matching plugins are given') do
expect(subject.all_with_plugin(TestPlugin)).to match_array(subject)
end
end

describe('#changed_of_type') do
describe('#changed_with_plugin') do
before do
subject.first.changed = true
end

it('returns an empty array when no types are given') do
expect(subject.changed_of_type).to be_empty
expect(subject.changed_with_plugin).to be_empty
end

it('returns an empty array when no projects match the types given') do
expect(subject.changed_of_type(Project)).to be_empty
expect(subject.changed_with_plugin(UnusedPlugin)).to be_empty
end

it('returns the correct projects when matching types are given') do
expect(subject.changed_of_type(StaticProject)).to contain_exactly(subject.first)
expect(subject.changed_with_plugin(TestPlugin)).to contain_exactly(subject.first)
end
end

Expand Down
1 change: 1 addition & 0 deletions spec/lib/shanty/plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'spec_helper'
require 'shanty/plugin'

require_fixture 'test_plugin'
require_fixture 'test_project_with_plugin'

Expand Down
22 changes: 22 additions & 0 deletions spec/lib/shanty/plugins/rubygem_plugin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'
require 'shanty/plugins/rubygem_plugin'

# All classes referenced belong to the shanty project
module Shanty
RSpec.describe(RubygemPlugin) do
include_context('basics')
subject { Class.new { include RubygemPlugin }.new }

it('subscribes to the test event') do
expect(RubygemPlugin.callbacks).to include([:build, :build_gem])
end

describe('#build_gem') do
it('calls gem build') do
expect(subject).to receive(:system).with('gem build *.gemspec')

subject.build_gem
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/shanty/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module Shanty

describe('#to_s') do
it('returns a simple string representation of the project') do
expect(subject.to_s).to eql("Name: #{project_template.name}, Type: Shanty::Project")
expect(subject.to_s).to eql(project_template.name)
end
end

Expand Down
6 changes: 0 additions & 6 deletions spec/lib/shanty/project_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ module Shanty
end
end

describe('#type') do
it('defaults the type to StaticProject') do
expect(subject.type).to eql(StaticProject)
end
end

describe('#priority') do
it('defaults the priority to 0') do
expect(subject.priority).to eql(0)
Expand Down
22 changes: 0 additions & 22 deletions spec/lib/shanty/projects/rubygem_project_spec.rb

This file was deleted.

44 changes: 0 additions & 44 deletions spec/lib/shanty/projects/static_project_spec.rb

This file was deleted.

Loading

0 comments on commit 6e4407c

Please sign in to comment.