diff --git a/README.md b/README.md index 9579dac..cc11102 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Or install it yourself as: Create a `gitnesse.rb` file somewhere in your project, and add something like the following to it: - Gitnesse.config do - repository_url "git@github.com:hybridgroup/gitnesse-demo.wiki" + Gitnesse.configure do |config| + config.repository_url = "git@github.com:hybridgroup/gitnesse-demo.wiki" end ## rake tasks diff --git a/bin/gitnesse b/bin/gitnesse index cd14000..c696244 100755 --- a/bin/gitnesse +++ b/bin/gitnesse @@ -11,7 +11,7 @@ end def print_help puts "Gitnesse commands:" - puts " run: pull remote features from git-based wiki to local, and run Cucumber" + puts " run: pull remote features from git-based wiki to local, and run Cucumber" puts " push: push local features to git-based wiki" puts " pull: pull remote features from git-based wiki to local" puts " info: print current Gitnesse configuration" @@ -25,7 +25,7 @@ when "pull" when "run" Gitnesse.run when "info" - puts Gitnesse.config_to_hash.to_yaml + puts Gitnesse.configuration.to_yaml when "help" print_help else diff --git a/lib/gitnesse.rb b/lib/gitnesse.rb index 81bd53b..5f03d14 100644 --- a/lib/gitnesse.rb +++ b/lib/gitnesse.rb @@ -2,77 +2,21 @@ require 'gollum' require 'fileutils' require 'tmpdir' +require 'gitnesse/configuration' require 'gitnesse/railtie' if defined?(Rails) # core module module Gitnesse - - extend self - - # Public: Set url of the git-based wiki repo containing features. - # - # repository_url - A String containing your repo's url. - # - # Example: - # - # Gitnesse.config do - # repository_url "git@github.com:luishurtado/gitnesse-wiki.wiki" - # end - # - def self.repository_url(repository_url = false) - if repository_url == false - unless defined?(@@repository_url) - puts "---" - puts "No repository_url has been defined for Gitnesse." - puts "Add a gitnesse.rb file to your project and use it to configure Gitnesse." - puts "For more details, check out gitnesse.com" - auts "---" - exit 1 - end - return @@repository_url - else - @@repository_url = repository_url - end + class << self + attr_accessor :configuration end - # Public: Set branch of the git-based wiki repo containing features. - # - # branch - A String containing which branch of your repo to use. - # - # Example: - # - # Gitnesse.config do - # branch "master" - # end - # - def self.branch(branch = false) - if branch == false - @@branch ||= "master" - else - @@branch = branch - end - end + self.configuration ||= Configuration.new - # Public: Set local directory used to sync with git-wiki stored feature stories. - # - # target_directory - A String containing which directory to use. - # - # Example: - # - # Gitnesse.config do - # target_directory "features" - # end - # - def self.target_directory(target_directory = false) - if target_directory == false - @@target_directory ||= File.join(Dir.pwd, 'features') - else - @@target_directory = target_directory - end - end + extend self - def self.config(&block) - instance_eval &block + def self.configure + yield(configuration) end # -- all methods after this are module functions -- @@ -81,7 +25,7 @@ def self.config(&block) def run if pull puts "Now going to run cucumber..." - exec("cucumber #{Gitnesse.target_directory}/*.feature") + exec("cucumber #{Gitnesse.configuration.target_directory}/*.feature") end end @@ -91,10 +35,10 @@ def pull ensure_cucumber_available ensure_repository - puts "Pulling features into #{Gitnesse.target_directory} from #{Gitnesse.repository_url}..." + puts "Pulling features into #{Gitnesse.configuration.target_directory} from #{Gitnesse.configuration.repository_url}..." Dir.mktmpdir do |tmp_dir| if clone_feature_repo(tmp_dir) - FileUtils.mkdir(Gitnesse.target_directory) unless File.exists?(Gitnesse.target_directory) + FileUtils.mkdir(Gitnesse.configuration.target_directory) unless File.exists?(Gitnesse.configuration.target_directory) wiki_pages = Gollum::Wiki.new(tmp_dir).pages wiki_pages.each do |wiki_page| @@ -114,7 +58,7 @@ def push ensure_repository commit_info - puts "Pushing features from #{Gitnesse.target_directory} to #{Gitnesse.repository_url}..." + puts "Pushing features from #{Gitnesse.configuration.target_directory} to #{Gitnesse.configuration.repository_url}..." Dir.mktmpdir do |tmp_dir| if clone_feature_repo(tmp_dir) load_feature_files_into_wiki(tmp_dir) @@ -130,7 +74,7 @@ def push def load_feature_files_into_wiki(tmp_dir) wiki = Gollum::Wiki.new(tmp_dir) - feature_files = Dir.glob("#{Gitnesse.target_directory}/*.feature") + feature_files = Dir.glob("#{Gitnesse.configuration.target_directory}/*.feature") feature_files.each do |feature_file| feature_name = File.basename(feature_file, ".feature") @@ -199,7 +143,7 @@ def extract_features(data) end def clone_feature_repo(dir) - output = `git clone #{Gitnesse.repository_url} #{dir} 2>&1` + output = `git clone #{Gitnesse.configuration.repository_url} #{dir} 2>&1` puts output $?.success? end @@ -244,7 +188,7 @@ def gather_features(page_features) end def write_feature_file(page_name, page_features) - File.open("#{Gitnesse.target_directory}/#{page_name}.feature","w") {|f| f.write(gather_features(page_features)) } + File.open("#{Gitnesse.configuration.target_directory}/#{page_name}.feature","w") {|f| f.write(gather_features(page_features)) } end def ensure_git_available @@ -256,7 +200,7 @@ def ensure_cucumber_available end def ensure_repository - raise "You must select a repository_url to run Gitnesse." if Gitnesse.repository_url.nil? + raise "You must select a repository_url to run Gitnesse." if Gitnesse.configuration.repository_url.nil? end def load_config @@ -271,7 +215,7 @@ def load_config false else file_content = File.read(file_name) - file_content.match("Gitnesse.config") + file_content.match("Gitnesse.configure") end end @@ -284,19 +228,4 @@ def load_config raise "Several config files found: #{files_with_config.join(", ")}" end end - - def config_to_hash - { "repository_url" => Gitnesse.repository_url, - "branch" => Gitnesse.branch, - "target_directory" => Gitnesse.target_directory } - end - - def method_missing(sym, *args, &block) - unless ["to_str", "to_ary"].contains?(sym) - raise "Invalid variable name for Gitnesse configuration. - Allowed variables are repository_url, branch, and target_directory." - else - super - end - end end diff --git a/lib/gitnesse/configuration.rb b/lib/gitnesse/configuration.rb new file mode 100644 index 0000000..5750cfa --- /dev/null +++ b/lib/gitnesse/configuration.rb @@ -0,0 +1,21 @@ +module Gitnesse + class Configuration + attr_accessor :repository_url + attr_accessor :branch + attr_accessor :target_directory + + def initialize + @branch = 'master' + @target_directory = File.join(Dir.pwd, 'features') + end + + # Public: Returns the current Gitnesse configuration as a Hash + # + # Returns a hash containing the current Gitnesse configuration + def to_hash + { 'repository_url' => @repository_url, + 'branch' => @branch, + 'target_directory' => @target_directory } + end + end +end diff --git a/lib/gitnesse/railtie.rb b/lib/gitnesse/railtie.rb index 8209f60..c3be507 100644 --- a/lib/gitnesse/railtie.rb +++ b/lib/gitnesse/railtie.rb @@ -3,10 +3,10 @@ module Gitnesse class Railtie < Rails::Railtie - railtie_name :gitnesse + railtie_name :gitnesse rake_tasks do load File.dirname(__FILE__) + '/tasks.rake' end end -end \ No newline at end of file +end diff --git a/lib/gitnesse/tasks.rake b/lib/gitnesse/tasks.rake index 5f50dc8..01ad1c0 100644 --- a/lib/gitnesse/tasks.rake +++ b/lib/gitnesse/tasks.rake @@ -23,6 +23,6 @@ namespace :gitnesse do desc "Dump the current config info to the console." task :info => :environment do Gitnesse.load_config - puts Gitnesse.config_to_hash.to_yaml + puts Gitnesse.configuration.to_yaml end -end \ No newline at end of file +end diff --git a/test/lib/gitnesse/check_dependencies_test.rb b/test/lib/gitnesse/check_dependencies_test.rb index caa3d8c..d9875d1 100644 --- a/test/lib/gitnesse/check_dependencies_test.rb +++ b/test/lib/gitnesse/check_dependencies_test.rb @@ -5,12 +5,12 @@ let(:method) { lambda { Gitnesse.ensure_repository } } describe "when repository was defined" do - before { Gitnesse.repository_url("git://github.com/hybridgroup/gitnesse-demo.wiki") } + before { Gitnesse.configuration.repository_url = "git://github.com/hybridgroup/gitnesse-demo.wiki" } it { method.call.must_be_nil } end describe "when repository was not defined" do - before { Gitnesse.repository_url(nil) } + before { Gitnesse.configuration.repository_url = nil } it { method.must_raise(RuntimeError) } end end diff --git a/test/lib/gitnesse/config_to_hash_test.rb b/test/lib/gitnesse/configuration_to_hash_test.rb similarity index 53% rename from test/lib/gitnesse/config_to_hash_test.rb rename to test/lib/gitnesse/configuration_to_hash_test.rb index 7aa9f9c..30af936 100644 --- a/test/lib/gitnesse/config_to_hash_test.rb +++ b/test/lib/gitnesse/configuration_to_hash_test.rb @@ -1,13 +1,14 @@ require_relative '../../test_helper' -describe Gitnesse do - describe ".config_to_hash" do - let(:method) { lambda { Gitnesse.config_to_hash } } +describe Gitnesse::Configuration do + describe ".to_hash" do + let(:method) { lambda { Gitnesse.configuration.to_hash } } + before do - Gitnesse.config do - repository_url "git://github.com/hybridgroup/gitnesse-demo.wiki.git" - branch "wiki" - target_directory "feature_files" + Gitnesse.configure do |config| + config.repository_url = "git://github.com/hybridgroup/gitnesse-demo.wiki.git" + config.branch = "wiki" + config.target_directory = "feature_files" end end diff --git a/test/lib/gitnesse/custom_branch_test.rb b/test/lib/gitnesse/custom_branch_test.rb index 4ea78c6..48abea8 100644 --- a/test/lib/gitnesse/custom_branch_test.rb +++ b/test/lib/gitnesse/custom_branch_test.rb @@ -1,25 +1,24 @@ require_relative '../../test_helper' -describe Gitnesse do +describe Gitnesse::Configuration do describe "#branch" do describe "defaults to 'master'" do - before { Gitnesse.branch nil } - it { Gitnesse.branch.must_equal "master" } + it { Gitnesse.configuration.branch.must_equal "master" } end describe "when changed" do - before { Gitnesse.branch "wiki" } - it { Gitnesse.branch.must_equal "wiki" } + before { Gitnesse.configuration.branch = "wiki" } + it { Gitnesse.configuration.branch.must_equal "wiki" } end - describe "when changed through #config" do + describe "when changed through #configure" do before do - Gitnesse.config do - branch "wiki" + Gitnesse.configure do |config| + config.branch = "wiki" end end - it { Gitnesse.branch.must_equal "wiki" } + it { Gitnesse.configuration.branch.must_equal "wiki" } end end end diff --git a/test/lib/gitnesse/load_feature_files_into_wiki_test.rb b/test/lib/gitnesse/load_feature_files_into_wiki_test.rb index 7262cef..901c3eb 100644 --- a/test/lib/gitnesse/load_feature_files_into_wiki_test.rb +++ b/test/lib/gitnesse/load_feature_files_into_wiki_test.rb @@ -26,7 +26,7 @@ file.write(feature) end - Dir.expects(:glob).with("#{Gitnesse.target_directory}/*.feature").returns(:feature_file_dir) + Dir.expects(:glob).with("#{Gitnesse.configuration.target_directory}/*.feature").returns(:feature_file_dir) wiki.expects(:page).with("testing").returns(wiki_page) Gitnesse.expects(:update_wiki_page).with(wiki_page, "testing", "blarg") end diff --git a/test/lib/gitnesse/write_feature_file_test.rb b/test/lib/gitnesse/write_feature_file_test.rb index ea3cfd0..08aebdf 100644 --- a/test/lib/gitnesse/write_feature_file_test.rb +++ b/test/lib/gitnesse/write_feature_file_test.rb @@ -6,7 +6,7 @@ let(:file) { StringIO.new } before do - File.expects(:open).with("#{Gitnesse.target_directory}/test.feature", "w").yields(file) + File.expects(:open).with("#{Gitnesse.configuration.target_directory}/test.feature", "w").yields(file) Gitnesse.expects(:gather_features).with({ "test-feature" => "feature content" }).returns("feature content") end