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.



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

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.



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

def branch
  @branch
end

#export_dirObject

Returns the value of attribute export_dir.



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

def export_dir
  @export_dir
end

#prefixObject

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

Instance Method Details

#cleanObject



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

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

#commit_newObject



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

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



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

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



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

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



134
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 134

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



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

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



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

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