Class: FubuDocs

Inherits:
Object
  • Object
show all
Defined in:
lib/fubudocs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ FubuDocs

Returns a new instance of FubuDocs.



34
35
36
37
38
39
40
41
# File 'lib/fubudocs.rb', line 34

def initialize(options)
  # :host, :include, :nested, :dump

  @branch = options.fetch(:branch, 'gh-pages')
  @repository = options[:repository]
  @prefix = options.fetch(:prefix, 'docs')
  @export_dir = options.fetch(:dir, 'fubudocs-export')
  @options = options
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



32
33
34
# File 'lib/fubudocs.rb', line 32

def branch
  @branch
end

#export_dirObject

Returns the value of attribute export_dir.



32
33
34
# File 'lib/fubudocs.rb', line 32

def export_dir
  @export_dir
end

#prefixObject

Returns the value of attribute prefix.



32
33
34
# File 'lib/fubudocs.rb', line 32

def prefix
  @prefix
end

Instance Method Details

#cleanObject



64
65
66
67
# File 'lib/fubudocs.rb', line 64

def clean
  cleanDirectory @export_dir
  Dir.delete @export_dir unless !Dir.exists?(@export_dir)
end

#commit_newObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fubudocs.rb', line 114

def commit_new
  # commit and push the generated docs

  Dir.chdir @export_dir

  if !File.exists?('.nojekyll')
    writeNoJekyll
  end

  sh "git add ."
  sh 'git commit -a -m "Doc generation version ' + @options[:version] + '"' do |ok, res|
    if ok
      sh "git push origin #{@branch}"
      puts "Documentation generation and push to #{@repository}/#{@branch} is successful"
    else
      puts "commit failed, might be because there are no differences in the content"
    end
  end

  Dir.chdir '..'
end

#create_branchObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fubudocs.rb', line 69

def create_branch
  sh "ripple gitignore #{@export_dir}"

  sh "git clone #{@repository} #{@export_dir}"

  Dir.chdir @export_dir

  sh "git checkout --orphan #{@branch}"
  sh "git rm -rf ."

  writeNoJekyll

  sh "git add ."
  sh 'git commit -a -m "initial clean slate"'
  sh 'git push origin #{@branch}'

  Dir.chdir '..'
end

#dump_taskObject



156
157
158
159
160
161
162
163
# File 'lib/fubudocs.rb', line 156

def dump_task
  dumpTask = Rake::Task.define_task "#{@prefix}:dump" do
    clean
    @options[:dump] = true
    export
  end
  dumpTask.add_description "Export the generated documentation to #{@export_dir} for local usage"
end

#exportObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fubudocs.rb', line 43

def export
  cmd = "fubudocs export #{@export_dir}"
  if (@options[:host] != nil)
    cmd += " --host #{@options[:host]}"
  end

  if (@options[:include] != nil)
    cmd += " -i #{@options[:include]}"
  end

  if (@options[:nested] == true)
    cmd += " -m GhPagesChildFolder"
  elsif (@options[:dump] == true)
    cmd += " -m HtmlDump"
  else
    cmd += ' -m GhPagesRooted'
  end

  sh cmd
end

#export_tasksObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fubudocs.rb', line 135

def export_tasks
  initTask = Rake::Task.define_task "#{@prefix}:init_branch" do
    clean
    create_branch
  end

  initTask.add_description "Initializes the #{@branch} branch in git repository #{@repository}"

  exportTaskName = "#{@prefix}:export"
  exportTask = Rake::Task.define_task exportTaskName do
    clean
    fetch_existing
    export
    commit_new
  end
  exportTask.add_description "Export the generated documentation to #{@repository}/#{@branch}"
  #exportTask.enhance [:compile]


  return exportTaskName
end

#fetch_existingObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fubudocs.rb', line 94

def fetch_existing
  Dir.mkdir @export_dir

  # fetch the gh-pages branch from the server

  Dir.chdir @export_dir
  sh 'git init'
  sh "git remote add -t #{@branch} -f origin #{@repository}"
  sh "git checkout #{@branch}" 

  # clean the existing content

  sleep 0.5 # let the file system try to relax its locks

  content_files = FileList['*.*'].exclude('.nojekyll').exclude('CNAME')
  content_files.each do |f|
    FileUtils.rm_r f
  end

  # do the actual export

  Dir.chdir '..'
end

#writeNoJekyllObject



88
89
90
91
92
# File 'lib/fubudocs.rb', line 88

def writeNoJekyll
  output = File.new( ".nojekyll", "w+" )
  output << "Just a marker"
  output.close
end