Created
August 21, 2009 18:21
-
-
Save jarib/172274 to your computer and use it in GitHub Desktop.
add/remove cuke tags from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
require "cucumber" | |
require "cucumber/feature_file" | |
require "cucumber/formatter/pretty" | |
class Tagger | |
USAGE = "#{File.basename $0} [-f|--force] [add|remove]:TAG [FILE[:LINE]]+" | |
def self.execute(args) | |
new.execute(args) | |
end | |
def execute(args) | |
abort(USAGE) if args.empty? || args.shift =~ /^(-h|--help)$/ | |
opts = {:dry_run => true, :source => true} | |
# TODO: use optparse? | |
args.each do |arg| | |
case arg | |
when /^(.+?\.feature)(:\d+)?$/ | |
add_feature($1, $2) | |
when /^(add|remove):(.+?)$/ | |
alterations << [$1.to_sym, $2] | |
when /^(-f|--force)$/ | |
opts[:autoformat] = "." | |
opts[:source] = false | |
Term::ANSIColor.coloring = false | |
when /^(-h|--help)$/ | |
abort(USAGE) | |
else | |
abort(USAGE) | |
end | |
end | |
features.accept TagVisitor.new(self, step_mother, $stdout, opts) | |
end | |
def process(feature, element, tag_names) | |
return unless should_alter?(feature, element) | |
alterations.each do |op, tag_name| | |
case op | |
when :add | |
tag_names.push tag_name | |
when :remove | |
tag_names.delete tag_name | |
end | |
end | |
end | |
def should_alter?(feature, element) | |
line = element ? element.line : 0 | |
features_to_change.include? [feature.file, line] | |
end | |
private | |
def add_feature(path, line) | |
ff = Cucumber::FeatureFile.new(path).parse(step_mother, {}) | |
features_to_change << [path, line.to_s[1,1].to_i] | |
features.add_feature ff | |
end | |
def step_mother | |
@step_mother ||= Cucumber::StepMother.new | |
end | |
def features | |
@features ||= Cucumber::Ast::Features.new | |
end | |
def alterations | |
@alterations ||= [] | |
end | |
def features_to_change | |
@features_to_change ||= [] | |
end | |
end | |
class TagVisitor < Cucumber::Formatter::Pretty | |
def initialize(tagger, step_mother, io, options) | |
@tagger = tagger | |
super(step_mother, io, options) | |
end | |
def visit_feature(feature) | |
@current_feature = feature | |
super | |
end | |
def visit_feature_element(element) | |
@current_element = element | |
super | |
@current_element = nil | |
end | |
def visit_tags(tags) | |
@tagger.process(@current_feature, @current_element, tags.instance_variable_get("@tag_names")) | |
super | |
end | |
private | |
def record_tag_occurrences(*args) | |
# override to avoid error if options[:include_tags] is nil | |
end | |
end | |
if __FILE__ == $0 | |
Tagger.execute(ARGV) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
Gem::Specification.new do |s| | |
s.name = %q{cuketagger} | |
s.version = "0.1" | |
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= | |
s.authors = ["Jari", "Bakken"] | |
s.date = %q{2009-08-21} | |
s.default_executable = %q{cuketagger} | |
s.description = %q{batch tagging of cucumber features and scenarios} | |
s.email = %q{[email protected]} | |
s.executables = ["cuketagger"] | |
s.files = ["bin/cuketagger"] | |
s.homepage = %q{http://cukes.info} | |
s.require_paths = ["lib"] | |
s.rubygems_version = %q{1.3.5} | |
s.summary = %q{batch tagging of cucumber features and scenarios} | |
if s.respond_to? :specification_version then | |
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION | |
s.specification_version = 3 | |
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then | |
s.add_runtime_dependency(%q<cucumber>, [">= 0.3.96"]) | |
else | |
s.add_dependency(%q<cucumber>, [">= 0.3.96"]) | |
end | |
else | |
s.add_dependency(%q<cucumber>, [">= 0.3.96"]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require "rake/clean" | |
require "rake/gempackagetask" | |
CLEAN.include %w[pkg] | |
GEM_NAME = "cuketagger" | |
GEM_VERSION = "0.1" | |
spec = Gem::Specification.new do |s| | |
s.name = GEM_NAME | |
s.version = GEM_VERSION | |
s.has_rdoc = false | |
s.summary = "batch tagging of cucumber features and scenarios" | |
s.description = s.summary | |
s.authors = %w[Jari Bakken] | |
s.email = "[email protected]" | |
s.homepage = "http://cukes.info" | |
s.bindir = 'bin' | |
s.executables = Dir['bin/*'].map { |f| File.basename(f) } | |
s.add_runtime_dependency 'cucumber', '>= 0.3.96' | |
end | |
Rake::GemPackageTask.new(spec) do |pkg| | |
pkg.gem_spec = spec | |
end | |
namespace :gem do | |
desc "install the gem locally" | |
task :install => [:package] do | |
sh %{sudo #{Gem.ruby} -S gem install pkg/#{GEM_NAME}-#{GEM_VERSION}} | |
end | |
desc "Create a .gemspec file" | |
task :spec do | |
file = "#{GEM_NAME.downcase}.gemspec" | |
File.unlink file if ::File.exists?(file) | |
File.open(file, "w+") { |f| f << spec.to_ruby } | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment