Skip to content

Commit

Permalink
Add 'push' task
Browse files Browse the repository at this point in the history
  • Loading branch information
stewart committed Aug 27, 2013
1 parent 608f285 commit 6cec309
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
55 changes: 55 additions & 0 deletions lib/gitnesse/cli/task/push.rb
@@ -0,0 +1,55 @@
Gitnesse::Cli.task :push do
desc "Pushes local features to remote git-based wiki"

help do
<<-EOS
USAGE: gitnesse push
#{desc}
Pushes the local features files to the remote git-based wiki, creating/updating
wiki pages as necessary.
Examples:
gitnesse push # will push local features to remote wiki
EOS
end

def perform
load_and_check_config
clone_wiki
collect_features
add_features_to_wiki
commit_and_push_changes
end

private
def collect_features
puts " Collecting local features."
Dir.chdir @config.features_dir do
@features = Dir.glob("**/*.feature")
end
end

def add_features_to_wiki
unless @features.any?
abort " No local features found."
end

@features.each do |feature|
@wiki.add_feature_page feature
end
end

def commit_and_push_changes
Dir.chdir @wiki.dir do
puts " Committing updated wiki features."
`git add . &> /dev/null`
`git commit -m "Update features with Gitnesse" &> /dev/null`
puts " Pushing updated features to remote wiki."
`git push origin #{@config.branch} &> /dev/null`
end

puts " Done."
end
end
29 changes: 28 additions & 1 deletion lib/gitnesse/wiki.rb
Expand Up @@ -4,7 +4,7 @@

module Gitnesse
class Wiki
attr_reader :repo, :pages
attr_reader :repo, :pages, :dir

# Public: Clones/updates a wiki in the provided dir
#
Expand All @@ -15,6 +15,8 @@ class Wiki
#
# Returns a Gitnesse::Wiki object
def initialize(repository_url, dir, opts={})
@dir = dir

clone_or_update_repo repository_url, dir, !!opts[:present]

@repo = Grit::Repo.new dir
Expand All @@ -28,6 +30,31 @@ def initialize(repository_url, dir, opts={})
@pages
end

# Public: Adds a new feature page to the Wiki
#
# feature_path - relative path from Gitnesse::Config.instance.features_path
# to feature file
#
# Returns new Gitnesse::Wiki::Page
def add_feature_page(feature_path)
config = Gitnesse::Config.instance
feature = File.read("#{config.features_dir}/#{feature_path}")

name = "features/#{feature_path}.md".gsub('/', ' > ')
filename = "#{@dir}/#{name}"
content = Gitnesse::FeatureTransform.convert(feature)

if File.exists?(filename)
puts " Updating page: '#{name}'"
else
puts " Creating page: '#{name}'"
end

File.open(filename, 'w') do |f|
f.write content
end
end

private
# Private: Clones or Updates the local copy of the remote wiki
#
Expand Down
1 change: 1 addition & 0 deletions spec/lib/cli/task/help_spec.rb
Expand Up @@ -10,6 +10,7 @@ module Gitnesse
The gitnesse tasks are:
info Prints current Gitnesse configuration
pull Pulls features from remote git-based wiki
push Pushes local features to remote git-based wiki
See 'gitnesse help <task>' for more information on a specific task.
EOS
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/cli/task/push_spec.rb
@@ -0,0 +1,24 @@
require 'spec_helper'
require 'gitnesse/cli'

module Gitnesse
describe Cli, type: :cli do
let(:help) do
<<-EOS
USAGE: gitnesse push
Pushes local features to remote git-based wiki
Pushes the local features files to the remote git-based wiki, creating/updating
wiki pages as necessary.
Examples:
gitnesse push # will push local features to remote wiki
EOS
end

it "has help info" do
expect(gitnesse("help push")).to eq help
end
end
end

0 comments on commit 6cec309

Please sign in to comment.