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

Commit

Permalink
Merge pull request #38 from anvil-src/finetuning
Browse files Browse the repository at this point in the history
Help finetuning
  • Loading branch information
Fran Casas committed Feb 4, 2015
2 parents 6f92ed8 + b7dc4c0 commit c23d823
Show file tree
Hide file tree
Showing 31 changed files with 121 additions and 83 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: ruby
rvm:
- 2.1.1
- 2.1.0
- 2.0.0
- 1.9.3
- 2.1
- 2.0
- 1.9
gemfile: Gemfile.ci
addons:
code_climate:
Expand Down
3 changes: 1 addition & 2 deletions anvil-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ SUMMARY

# Development dependencies
spec.add_development_dependency 'bundler', '~> 1.5'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'rspec', '~> 3.2'
spec.add_development_dependency 'rspec-its'
spec.add_development_dependency 'fakefs', '~> 0.5'
spec.add_development_dependency 'rubocop', '~> 0.18'
end
49 changes: 30 additions & 19 deletions lib/anvil/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
module Anvil
# Anvil command line interface
class Cli
HELP = <<-HELP
HELP_HEADER = <<-HELP_HEADER
Anvil is a tool for making your life easier.
Available tasks:
HELP
HELP_HEADER

# Runs a task or prints its help if it needs arguments
#
Expand All @@ -19,11 +19,10 @@ class Cli
def run(argv)
load_tasks

if argv.empty?
print_help
else
build_task(argv).run
end
return build_task(argv).run unless argv.empty?

print_help_header
print_help_body
end

def load_tasks
Expand All @@ -41,30 +40,42 @@ def build_task(argv)
klazz.new(*klazz.parse_options!(arguments))
rescue NameError
task_not_found(task_name)
exit(FALSE)
exit false
rescue ArgumentError
help(task_name)
exit(FALSE)
bad_arguments(task_name)
exit false
end

def task_not_found(task_name)
printf("Task '#{task_name}' not found\n\n")
print_help
def task_not_found(task_name = nil)
Anvil.logger.info "Task '#{task_name}' not found"
Anvil.logger.info('Maybe you mean one of the following') if task_name
Anvil.logger.info("\n")
print_help_body task_name
end

def help(task_name)
printf("Wrong number of arguments.\n\n")
def bad_arguments(task_name)
Anvil.logger.info("Wrong number of arguments.\n")
HelpTask.run(task_name)
end

def print_help
printf('%s', HELP)
def print_help_body(task_name = nil)
task_list(task_name).each { |task| print_task_line(task) }
end

def task_list(task_name)
tasks = Anvil::ExtensionsManager.tasks_by_name
tasks.each { |task| print_task_line(task) }

return tasks unless task_name
tasks.select { |task| task.to_s.underscore =~ /#{task_name}/ }
end

def print_task_line(task)
printf("%-20s %s\n", task.task_name, task.description)
message = format '%-20s %s', task.task_name, task.description
Anvil.logger.info message
end

def print_help_header
Anvil.logger.info HELP_HEADER
end
end
end
2 changes: 1 addition & 1 deletion lib/anvil/extensions_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'rugged'

module Anvil
# Manage loading and finding anvil tasks
# Manage loading and finding anvil tasks and config extensions
class ExtensionsManager
PATTERNS = {
tasks: '/tasks/**/*_task.rb',
Expand Down
20 changes: 20 additions & 0 deletions lib/anvil/options_detector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Anvil
# Detect whether the options parser has parsed any option or not in
# order, for example, to write the required help for them.
class OptionsDetector
attr_accessor :has_options

def on(*_, &_block)
self.has_options = true
end

def arguments(*_, &_block)
end

def detect_options(&block)
instance_eval(&block)

has_options
end
end
end
5 changes: 5 additions & 0 deletions lib/anvil/parser.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: UTF-8

require 'anvil/task'
require 'anvil/options_detector'

module Anvil
# Parser for anvil command line arguments and options
Expand Down Expand Up @@ -39,5 +40,9 @@ def from(name)
task_klass = Anvil::Task.from_name(name)
instance_eval(&task_klass.parser_block) if task_klass.parser_block
end

def detect_options(&block)
OptionsDetector.new.detect_options(&block)
end
end
end
11 changes: 5 additions & 6 deletions lib/anvil/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ def initialize(options = {})
#
# @return [Object, nil] anything the task might return
def run
if run_assures
run_before_callbacks
task_return_value = run_task
run_after_callbacks
return unless run_assures
run_before_callbacks
task_return_value = run_task
run_after_callbacks

task_return_value
end
task_return_value
end

def logger
Expand Down
2 changes: 2 additions & 0 deletions lib/anvil/task/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def configure_parser(parser, &block)
parser.separator description
end

return parser unless parser.detect_options(&block)

parser.separator ''
parser.separator 'Available options: '
parser.instance_eval(&block)
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/gem/build_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Gem::BuildTask < Anvil::Task
description 'Builds a gem for you and can install it on your system.'

parser do
arguments %w[gemspec_file]
arguments %w(gemspec_file)

on('-i', '--[no-]install', 'Install gem') do |i|
options[:install] = i
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/gem/bump_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(term, options = {})
def task
prepare_repo
version = bump(read_version)
write_version version
write_version version.dup.to_s

version
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/gem/release_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def task
version = bump
gem_file = build

push gem_file, version
push gem_file, version.dup.to_s
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/help_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def initialize(task_name, options = {})
def task
return default_message unless task_name.present?
klazz = Anvil::Task.from_name(task_name)
printf(klazz.help)
Anvil.logger.info(klazz.help)
end

def default_message
printf(self.class.help)
Anvil.logger.info(self.class.help)
end
end
7 changes: 2 additions & 5 deletions lib/tasks/projects/add_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ module Projects
class AddTask < Anvil::Task
include Anvil::Task::Repositories
description 'Adds a new project for anvil.'

parser do
arguments %w[name repository]
end
parser { arguments %w(name repository) }

attr_reader :name, :repo

def initialize(name, repo, options = {})
def initialize(name, repo, _options = {})
@name = name
@repo = repo
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/projects/list_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Projects
class ListTask < Anvil::Task
description 'List the projects that anvil can manage.'

def initialize(options = {}); end
def initialize(_options = {}); end

def task
Dir.chdir(Anvil::Config.base_projects_path) { list_projects(projects) }
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/anvil/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
let(:argv) { %w[foo:dummy arg1 arg2 arg3 arg4 arg5 arg6 arg7] }

it 'prints task list and exits' do
expect(subject).to receive(:help).with('foo:dummy')
expect(subject).to receive(:bad_arguments).with('foo:dummy')
expect do
subject.build_task(argv)
end.to raise_error(SystemExit)
Expand All @@ -47,7 +47,7 @@

context 'without a task name' do
let(:argv) { [] }
before { expect(subject).to receive(:print_help) }
before { expect(subject).to receive(:print_help_body) }
it('prints the help') { subject.run argv }
end
end
Expand Down
21 changes: 8 additions & 13 deletions spec/lib/anvil/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
require 'spec_helper'

describe Anvil::Config, fakefs: true do
describe Anvil::Config do
subject { Anvil::Config }
before { Anvil::Config.reset }
before { Anvil::Config.init }

its('github.user') { should be_nil }
its('github.token') { should be_nil }

context 'with a config file', config: true do
its('github.user') { should eq('dummy_user') }
its('github.token') { should eq('dummy_token') }
end
its('github.user') { is_expected.to eq('dummy_user') }
its('github.token') { is_expected.to eq('dummy_token') }

context '.init_base_path' do
before { Anvil::Config.send :init_base_path }
subject { File }

it { should be_directory(Anvil::Config.base_path) }
it { should be_directory(Anvil::Config.base_tasks_path) }
it { should be_exists(Anvil::Config.base_config_path) }
it { should be_exists(Anvil::Config.base_projects_path) }
it { is_expected.to be_directory(Anvil::Config.base_path) }
it { is_expected.to be_directory(Anvil::Config.base_tasks_path) }
it { is_expected.to be_exists(Anvil::Config.base_config_path) }
it { is_expected.to be_exists(Anvil::Config.base_projects_path) }
end
end
2 changes: 1 addition & 1 deletion spec/lib/tasks/gem/build_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
end

it 'returns the gem file path' do
expect(subject.build_gem(gem_file)).to be_eql('/pkg/anvil-2.0.0.gem')
expect(subject.build_gem(gem_file)).to include('/pkg/anvil-2.0.0.gem')
end
end

Expand Down
4 changes: 3 additions & 1 deletion spec/lib/tasks/gem/bump_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

it 'bumps the version and writes it' do
expect(subject).to receive(:prepare_repo).and_return(true)
expect(subject).to receive(:write_version).and_return(true)
allow(subject).to receive(:write_version)
.with(instance_of(String))
.and_return(true)

expect(subject.task).to eq('3.0.0')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/tasks/gem/release_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

it 'pushes the gem to rubygems' do
expect(subject).to receive(:push).with(gem_file, version)
expect(subject).to receive(:push)
end

after { subject.task }
Expand Down
1 change: 1 addition & 0 deletions spec/lib/tasks/projects/add_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe Projects::AddTask do
let(:repo_url) { '[email protected]:account/repo' }
before { Anvil::Config.send :init_base_path }

subject { described_class.new('repo', 'account/repo') }

Expand Down
17 changes: 12 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration

require 'fakefs/spec_helpers'
require 'rspec/its'
require 'anvil'

begin
require 'codeclimate-test-reporter'
CodeClimate::TestReporter.start
rescue LoadError; end
rescue LoadError
puts "Couldn't load the codeclimate test reporter"
end

Dir["#{File.dirname(__FILE__)}/support/fixtures/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/support/dummy_tasks/**/*.rb"].each { |f| require f }
Dir["#{File.dirname(__FILE__)}/support/shared/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
Expand All @@ -26,7 +27,13 @@
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
config.before(:all) do
Anvil.module_eval { @logger = Logger.new(nil) }
config.before(:suite) { Anvil.module_eval { @logger = Logger.new(nil) } }
config.before do
FileUtils.rm_rf ConfigMock.base_path
FileUtils.mkdir_p ConfigMock.base_path
FileUtils.cp ConfigMock.config_file,
ConfigMock.base_path
end

config.after { FileUtils.rm_rf ConfigMock.base_path }
end
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.user 'dummy_user'
github.user 'dummy_user'
github.token 'dummy_token'
12 changes: 0 additions & 12 deletions spec/support/shared/config_context.rb

This file was deleted.

Loading

0 comments on commit c23d823

Please sign in to comment.