Class: Gitnesse::Wiki

Inherits:
Object
  • Object
show all
Defined in:
lib/gitnesse/wiki.rb,
lib/gitnesse/wiki/page.rb

Defined Under Namespace

Classes: Page

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, dir, opts = {clone: true}) ⇒ Wiki

Public: Clones/updates a wiki in the provided dir

url - cloneable URL for wiki repository dir - directory to clone git wiki into opts - hash of options:

clone - whether or not to clone/update local copy of remote wiki

Returns a Gitnesse::Wiki object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gitnesse/wiki.rb', line 17

def initialize(url, dir, opts={clone: true})
  @url = url
  @dir = dir
  @opts = opts
  @config = Gitnesse::Config.instance

  clone_or_update unless !opts[:clone]

  @repo ||= Git.init(dir)

  @pages = @repo.status.each_with_object([]) do |s, a|
    if s.path =~ /\.feature\.md$/
      a << Gitnesse::Wiki::Page.new("#{dir}/#{s.path}")
    end
  end

  @pages
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



7
8
9
# File 'lib/gitnesse/wiki.rb', line 7

def dir
  @dir
end

#pagesObject (readonly)

Returns the value of attribute pages.



7
8
9
# File 'lib/gitnesse/wiki.rb', line 7

def pages
  @pages
end

#repoObject (readonly)

Returns the value of attribute repo.



7
8
9
# File 'lib/gitnesse/wiki.rb', line 7

def repo
  @repo
end

Instance Method Details

#add_page(filename, content) ⇒ Object

Public: Adds or updates wiki page

filename - filename for wiki page content - content for page

Returns a Wiki::Page



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gitnesse/wiki.rb', line 86

def add_page(filename, content)
  full_filename = "#{@dir}/#{filename}"

  if @pages.detect { |f| f.wiki_path == full_filename }
    page = @pages.find { |f| f.wiki_path == full_filename }
  else
    page = Gitnesse::Wiki::Page.new(full_filename)
    @pages << page
  end

  page.write(content)
  @repo.add(filename)
  page
end

#commitObject

Public: Commits staged wiki changes

Returns nothing



62
63
64
65
66
67
68
69
70
71
# File 'lib/gitnesse/wiki.rb', line 62

def commit
  begin
    @repo.commit("Update features with Gitnesse")
  rescue Git::GitExecuteError => e
    unless e.message =~ /nothing to commit/
      puts "  A Git error occured. The message it passed to us was:"
      abort e.message
    end
  end
end

#pushObject

Public: Pushes new commits to remote wiki

Returns nothing



76
77
78
# File 'lib/gitnesse/wiki.rb', line 76

def push
  @repo.push
end

#remove_featuresObject

Public: Removes pages previously placed by Gitnesse. This includes the feature listing pages.

Returns nothing.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitnesse/wiki.rb', line 40

def remove_features
  @repo.status.each do |file|
    if file.path =~ /^features(\.md|\ >)/
      begin
        @repo.remove(file.path)
      rescue Git::GitExecuteError => e
        # Git spat something on git rm [file]. Likely the file doesn't
        # exist, or was previously removed and hasn't been committed yet.
        # It's likely fine. If not, we'll abort and show the end user the
        # error Git sent to us.
        unless e.message =~ /did not match any files/
          puts "  A Git error occured. The message it passed to us was:"
          abort e.message
        end
      end
    end
  end
end