Skip to content

Commit

Permalink
Merge pull request #6 from shwoodard/fix_cli
Browse files Browse the repository at this point in the history
Fix cli
  • Loading branch information
shwoodard committed Feb 27, 2012
2 parents 5903543 + 16b6e6f commit 536e524
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 69 deletions.
45 changes: 5 additions & 40 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,44 +1,9 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
require 'cucumber/rake/task'

desc "Run specs"
RSpec::Core::RakeTask.new do |t|
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
# Put spec opts in a file named .rspec in root
end
RSpec::Core::RakeTask.new :spec
Cucumber::Rake::Task.new

desc "Generate code coverage"
RSpec::Core::RakeTask.new(:coverage) do |t|
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
t.rcov = true
t.rcov_opts = ['--exclude', 'spec']
end


namespace :spec do
desc 'Create spec'
task :create, :path do |t, args|
spec_path = File.join('spec', args[:path].gsub(%r{.rb$}, '_spec.rb'))
klass = File.basename(args[:path], File.extname(args[:path])).split('_').map {|s| "#{s[0,1].upcase}#{s[1..-1]}" }.join
mkdir_p File.dirname(spec_path)

spec_doc = <<-EOS
require 'spec_helper'
describe #{klass} do
end
EOS

File.open(spec_path, 'w+') do |f|
f.puts spec_doc
end

puts spec_doc
end
end

desc "Run cucumber"
task(:cucumber) { sh 'cucumber' }

desc 'Default: run specs.'
task :default => :spec
desc 'Default: run specs and features.'
task :default => [:spec, :cucumber]
2 changes: 1 addition & 1 deletion bin/sprites
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ begin

Sprites::CliApplication.run(sprite_definition_file_path, configuration)
rescue Sprites::CliApplication::DefinitionFileNotFound
abort 'No Sprite Definition File found. Please supply a path or place your definition file in `pwd` or `pwd/config`'
abort 'No Sprite Definition File found. Please supply a path or place your definition file in `pwd/config`'
end
2 changes: 1 addition & 1 deletion features/cli.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Feature: Creating sprites and stylesheets
Given a project folder
And it contains a sprites dsl definition file
And it contains sprite images
When I run the executable "sprites"
When I run the executable "sprites" with flags "-r spec/fixtures/project1"
Then I should get valid sprites
14 changes: 8 additions & 6 deletions features/step_definitions/common_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
Dir['public/images/sprite_images/**/*.png'].should_not be_empty
end

When(/^I run the executable "([^"]*)"$/) do |bin|
`#{GEM_ROOT}/bin/#{bin}`
When /^I run the executable "([^"]*)" with flags "([^"]*)"$/ do |bin, flags|
`#{GEM_ROOT}/bin/#{bin} #{flags}`
$?.to_i.should be(0)
end

Expand All @@ -26,11 +26,13 @@
sprite_definition_file_path = options.definition_file_path
configuration = Sprites::Configuration.new_for_command_line_options(options.options)

require sprite_definition_file_path
sprites = Sprites.new
sprites.configure configuration.to_options
sprites.load

tester = Sprites::SpriteGeneratorTester.new(Sprites.application.sprites[:buttons], configuration)
tester = Sprites::SpriteGeneratorTester.new(sprites[:buttons], sprites.configuration)
tester.should be_accurate

FileUtils.rm Sprites::Sprite.sprite_full_path(configuration, Sprites.application.sprites[:buttons])
FileUtils.rm Sprites::Stylesheet.stylesheet_full_path(configuration, Sprites.application.sprites[:buttons].stylesheet)
FileUtils.rm sprites[:buttons].path
FileUtils.rm sprites[:buttons].stylesheet_path
end
4 changes: 4 additions & 0 deletions lib/sprites.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def each(&blk)
@sprites.values.each(&blk)
end

def configure(options)
configuration.configure(options)
end

# The +sprite+ method adds sprites to the sprites collection
#
# === Examples ===
Expand Down
8 changes: 5 additions & 3 deletions lib/sprites/cli/cli_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ class CliApplication
class DefinitionFileNotFound < StandardError; end

def self.run(def_file, configuration)
load def_file
sprite_generator = ChunkyPngGenerator.new(configuration)
sprite_generator.generate(Sprites.application.sprites)
sprites = Sprites.new
sprites.configure(configuration.to_options)
sprites.load
sprite_generator = ChunkyPngGenerator.new(sprites)
sprite_generator.generate
end
end
end
30 changes: 15 additions & 15 deletions lib/sprites/cli/command_line_option_parser.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require 'pathname'
require 'optparse'

class Sprites
class CommandLineOptionParser < OptionParser
PRIMARY_DEF_FILE_LOCATION = 'config/sprites.rb'
SECONDARY_DEF_FILE_LOCATION = 'sprites.rb'

attr_reader :definition_file_path, :options

def initialize(arguments)
@definition_file_path, @arguments = extract_sprite_definition_file_path(arguments.clone)
@arguments, @raw_arguments = arguments.clone, arguments
@definition_file_path = extract_sprite_definition_file_path(@arguments)
@options = Hash.new

super do |opts|
Expand All @@ -17,25 +16,19 @@ def initialize(arguments)
end

def parse
super(@arguments)
super(@raw_arguments)
end

def extract_sprite_definition_file_path(arguments)
sprite_def_file_pathname = if arguments[0] =~ %r{^.+\.rb}
Pathname(arguments.shift)
elsif file_exists?('config/sprites.rb')
Pathname('config/sprites.rb')
elsif File.exists?(path = File.join(Dir.pwd, 'config/sprites.rb'))
Pathname(path)
else
raise
raise CliApplication::DefinitionFileNotFound
end

[sprite_def_file_pathname.realpath.to_s, arguments]
rescue
raise CliApplication::DefinitionFileNotFound
end

def file_exists?(path)
Pathname(path).exist?
sprite_def_file_pathname.realpath.to_s
end

def setup_parser(opts)
Expand All @@ -44,6 +37,13 @@ def setup_parser(opts)
opts.separator ""
opts.separator "Specific options:"

opts.on("-r", "--project_root", <<-EOS
Define the path to the root of the project. DEFAULT: pwd
EOS
) do |path|
@options[:project_root] = path || Dir.pwd
end

opts.on("-s", "--sprites_path", <<-EOS
Define the path to where the sprites should be placed. DEFAULT: 'public/images/sprites'
EOS
Expand Down
20 changes: 18 additions & 2 deletions lib/sprites/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Sprites
class Configuration
FIELDS = %w{definition_file autoload sprite_asset_path sprites_path sprite_stylesheets_path sprite_pieces_path}
PATH_FIELDS = %w{definition_file sprites_path sprite_stylesheets_path sprite_pieces_path}
OTHER_FIELDS = %w{definition_file autoload sprite_asset_path}
FIELDS = PATH_FIELDS + OTHER_FIELDS

DEFAULT_CONFIGURATION = {
'sprites_path' => 'public/images/sprites',
Expand All @@ -25,6 +27,12 @@ def config
self
end

def to_options
options = Hash.new
FIELDS.each {|option| options[option.intern] = send(option) }
options
end

def configured?
FIELDS.any? do |field|
send(field) != DEFAULT_CONFIGURATION[field]
Expand All @@ -33,11 +41,19 @@ def configured?

def self.new_for_command_line_options(options)
config = new

options.each do |k, v|
if FIELDS.include?(k.to_s)
if options.has_key? :project_root
if PATH_FIELDS.include?(k.to_s)
config.send(:"#{path_field}=", File.join(options[:project_root], options[path_field.intern]))
elsif OTHER_FIELDS.include?(k.to_s)
config.send(:"#{k}=", v)
end
elsif FIELDS.include?(k.to_s)
config.send(:"#{k}=", v)
end
end

config
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sprites/test/sprite_generator_tester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_generate(gem_root = GEM_ROOT)
percent_diferences = sprite_pieces_with_selector_data.map do |sp, selector_data|
begin
raise selector_data.inspect if sp.nil?
sprite_piece_path = gem_root.join(sp.source_path)
sprite_piece_path = sp.source_path
sprite_piece_image = Image.read(sprite_piece_path).first

curr_sprite_image = sprite_image.crop(
Expand Down

0 comments on commit 536e524

Please sign in to comment.