Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Commands refactoring using Executor superclass #183

Open
wants to merge 1 commit into from

2 participants

@kristianmandrup

All commands should inherit from this Executor class. Each command class should have a #run method.
The ouput is set to $stdout when calling super. The method #print will print out to console and #system_exec is in charge of executing system commands. This all makes for a much cleaner and DRY design and makes it easy to add new Commands.

class Jeweler
  module Commands
    class Executor
      attr_accessor :output

      def initialize
        self.output = $stdout
      end      

      def run
        raise "Must be implemented by subclass"
      end
      
      protected

      def print msg
        output.puts msg
      end

      def system_exec command
        print "Executing #{command.inspect}:"
        Kernel::system command
      end
    end
  end
end
@technicalpickles

To be fair, new commands aren't added that often so it's not a huge pain point. It does make it more obvious what commands do though.

This is really the Command pattern, so I would suggest renaming Executor to Command though, or maybe Base, or BaseCommand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 23, 2011
  1. @kristianmandrup
This page is out of date. Refresh to see the latest.
View
59 Gemfile.lock
@@ -1,59 +0,0 @@
-GEM
- remote: http://rubygems.org/
- remote: http://gems.github.com/
- specs:
- activesupport (2.3.8)
- bluecloth (2.0.7)
- builder (2.1.2)
- columnize (0.3.1)
- cucumber (0.8.5)
- builder (~> 2.1.2)
- diff-lcs (~> 1.1.2)
- gherkin (~> 2.1.4)
- json_pure (~> 1.4.3)
- term-ansicolor (~> 1.0.4)
- diff-lcs (1.1.2)
- gherkin (2.1.5)
- trollop (~> 1.16.2)
- git (1.2.5)
- json_pure (1.4.6)
- linecache (0.43)
- mhennemeyer-output_catcher (1.0.1)
- mocha (0.9.8)
- rake
- rake (0.8.7)
- rcov (0.9.8)
- redgreen (1.2.2)
- rr (0.10.11)
- ruby-debug (0.10.3)
- columnize (>= 0.1)
- ruby-debug-base (~> 0.10.3.0)
- ruby-debug-base (0.10.3)
- linecache (>= 0.3)
- shoulda (2.11.3)
- term-ansicolor (1.0.5)
- test-construct (1.2.0)
- timecop (0.3.5)
- trollop (1.16.2)
- yard (0.6.1)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- activesupport (~> 2.3.5)
- bluecloth
- bundler (~> 1.0.0)
- cucumber
- git (>= 1.2.5)
- mhennemeyer-output_catcher
- mocha
- rake
- rcov
- redgreen
- rr
- ruby-debug
- shoulda
- test-construct
- timecop
- yard (~> 0.6.0)
View
1  lib/jeweler/commands.rb
@@ -1,5 +1,6 @@
class Jeweler
module Commands
+ autoload :BuildGem, 'jeweler/commands/executor'
autoload :BuildGem, 'jeweler/commands/build_gem'
autoload :InstallGem, 'jeweler/commands/install_gem'
autoload :CheckDependencies, 'jeweler/commands/check_dependencies'
View
5 lib/jeweler/commands/build_gem.rb
@@ -1,9 +1,10 @@
class Jeweler
module Commands
- class BuildGem
+ class BuildGem < Executor
attr_accessor :base_dir, :gemspec_helper, :file_utils, :version_helper
def initialize
+ super
self.file_utils = FileUtils
end
@@ -24,11 +25,9 @@ def run
def self.build_for(jeweler)
command = new
-
command.base_dir = jeweler.base_dir
command.gemspec_helper = jeweler.gemspec_helper
command.version_helper = jeweler.version_helper
-
command
end
end
View
14 lib/jeweler/commands/check_dependencies.rb
@@ -1,12 +1,16 @@
class Jeweler
module Commands
- class CheckDependencies
+ class CheckDependencies < Executor
class MissingDependenciesError < RuntimeError
attr_accessor :dependencies, :type
end
attr_accessor :gemspec, :type
+ def initialize
+ super
+ end
+
def run
missing_dependencies = dependencies.select do |dependency|
begin
@@ -18,11 +22,11 @@ def run
end
if missing_dependencies.empty?
- puts "#{type || 'All'} dependencies seem to be installed."
+ print "#{type || 'All'} dependencies seem to be installed."
else
- puts "Missing some dependencies. Install them with the following commands:"
+ print "Missing some dependencies. Install them with the following commands:"
missing_dependencies.each do |dependency|
- puts %Q{\tgem install #{dependency.name} --version "#{dependency.requirement.to_s}"}
+ print %Q{\tgem install #{dependency.name} --version "#{dependency.requirement.to_s}"}
end
abort "Run the specified gem commands before trying to run this again: #{$0} #{ARGV.join(' ')}"
@@ -40,9 +44,7 @@ def dependencies
def self.build_for(jeweler)
command = new
-
command.gemspec = jeweler.gemspec
-
command
end
end
View
26 lib/jeweler/commands/executor.rb
@@ -0,0 +1,26 @@
+class Jeweler
+ module Commands
+ class Executor
+ attr_accessor :output
+
+ def initialize
+ self.output = $stdout
+ end
+
+ def run
+ raise "Must be implemented by subclass"
+ end
+
+ protected
+
+ def print msg
+ output.puts msg
+ end
+
+ def system_exec command
+ print "Executing #{command.inspect}:"
+ Kernel::system command
+ end
+ end
+ end
+end
View
11 lib/jeweler/commands/install_gem.rb
@@ -1,17 +1,14 @@
class Jeweler
module Commands
- class InstallGem
- attr_accessor :gemspec_helper, :output
+ class InstallGem < Executor
+ attr_accessor :gemspec_helper
def initialize
- self.output = $stdout
+ super
end
def run
- command = "#{gem_command} install #{gemspec_helper.gem_path}"
- output.puts "Executing #{command.inspect}:"
-
- sh command # TODO where does sh actually come from!? - rake, apparently
+ system_exec "#{gem_command} install #{gemspec_helper.gem_path}"
end
def gem_command
View
8 lib/jeweler/commands/release_gemspec.rb
@@ -15,7 +15,7 @@ def initialize(attributes = {})
def run
unless clean_staging_area?
- system "git status"
+ system_exec "git status"
raise "Unclean staging area! Be sure to commit or .gitignore everything first. See `git status` above."
end
@@ -24,7 +24,7 @@ def run
regenerate_gemspec!
commit_gemspec! if gemspec_changed?
- output.puts "Pushing master to origin"
+ print "Pushing master to origin"
repo.push
end
@@ -36,7 +36,7 @@ def clean_staging_area?
def commit_gemspec!
gemspec_gitpath = working_subdir.join(gemspec_helper.path)
repo.add(gemspec_gitpath.to_s)
- output.puts "Committing #{gemspec_gitpath}"
+ print "Committing #{gemspec_gitpath}"
repo.commit "Regenerate gemspec for version #{version}"
end
@@ -47,7 +47,7 @@ def regenerate_gemspec!
def gemspec_changed?
# OMGHAX. ruby-git status always ends up being 'M', so let's do it a crazy way
- system "git status -s #{working_subdir.join(gemspec_helper.path)} | grep #{working_subdir.join(gemspec_helper.path)} > /dev/null 2>/dev/null"
+ system_exec "git status -s #{working_subdir.join(gemspec_helper.path)} | grep #{working_subdir.join(gemspec_helper.path)} > /dev/null 2>/dev/null"
end
def gemspec_helper
View
4 lib/jeweler/commands/release_to_git.rb
@@ -4,7 +4,7 @@ class ReleaseToGit
attr_accessor :gemspec, :version, :repo, :output, :gemspec_helper, :base_dir
def initialize(attributes = {})
- self.output = $stdout
+ super
attributes.each_pair do |key, value|
send("#{key}=", value)
@@ -13,7 +13,7 @@ def initialize(attributes = {})
def run
unless clean_staging_area?
- system "git status"
+ system_exec "git status"
raise "Unclean staging area! Be sure to commit or .gitignore everything first. See `git status` above."
end
View
8 lib/jeweler/commands/release_to_rubygems.rb
@@ -1,16 +1,14 @@
class Jeweler
module Commands
- class ReleaseToRubygems
+ class ReleaseToRubygems < Executor
attr_accessor :gemspec, :version, :output, :gemspec_helper
def initialize
- self.output = $stdout
+ super
end
def run
- command = "gem push #{@gemspec_helper.gem_path}"
- output.puts "Executing #{command.inspect}:"
- sh command
+ system_exec "gem push #{@gemspec_helper.gem_path}"
end
def self.build_for(jeweler)
View
10 lib/jeweler/commands/validate_gemspec.rb
@@ -1,18 +1,18 @@
class Jeweler
module Commands
- class ValidateGemspec
- attr_accessor :gemspec_helper, :output
+ class ValidateGemspec < Executor
+ attr_accessor :gemspec_helper
def initialize
- self.output = $stdout
+ super
end
def run
begin
gemspec_helper.parse
- output.puts "#{gemspec_helper.path} is valid."
+ print "#{gemspec_helper.path} is valid."
rescue Exception => e
- output.puts "#{gemspec_helper.path} is invalid. See the backtrace for more details."
+ print "#{gemspec_helper.path} is invalid. See the backtrace for more details."
raise
end
end
View
8 lib/jeweler/commands/write_gemspec.rb
@@ -1,10 +1,10 @@
class Jeweler
module Commands
- class WriteGemspec
- attr_accessor :base_dir, :gemspec, :version, :output, :gemspec_helper, :version_helper
+ class WriteGemspec < Executor
+ attr_accessor :base_dir, :gemspec, :version, :gemspec_helper, :version_helper
def initialize
- self.output = $stdout
+ super
end
def run
@@ -15,7 +15,7 @@ def run
gemspec_helper.write
- output.puts "Generated: #{gemspec_helper.path}"
+ print "Generated: #{gemspec_helper.path}"
end
def gemspec_helper
Something went wrong with that request. Please try again.