Class: Docurium

Inherits:
Object
  • Object
show all
Defined in:
lib/docurium/cli.rb,
lib/docurium/cparser.rb,
lib/docurium/version.rb,
lib/docurium/docparser.rb,
lib/docurium.rb

Defined Under Namespace

Classes: CLI, CParser, DocParser, Warning

Constant Summary collapse

Version =
VERSION = '0.7.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file, cli_options = {}, repo = nil) ⇒ Docurium

Returns a new instance of Docurium.



24
25
26
27
28
29
30
31
# File 'lib/docurium.rb', line 24

def initialize(config_file, cli_options = {}, repo = nil)
  raise "You need to specify a config file" if !config_file
  raise "You need to specify a valid config file" if !valid_config(config_file)
  @sigs = {}
  @head_data = nil
  @repo = repo || Rugged::Repository.discover(config_file)
  @cli_options = cli_options
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



22
23
24
# File 'lib/docurium.rb', line 22

def branch
  @branch
end

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/docurium.rb', line 22

def data
  @data
end

#head_dataObject

Returns the value of attribute head_data.



22
23
24
# File 'lib/docurium.rb', line 22

def head_data
  @head_data
end

#output_dirObject

Returns the value of attribute output_dir.



22
23
24
# File 'lib/docurium.rb', line 22

def output_dir
  @output_dir
end

Instance Method Details

#check_warnings(options) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
# File 'lib/docurium.rb', line 337

def check_warnings(options)
  versions = []
  versions << get_versions.pop
  versions << 'HEAD'

  process_project(versions)

  collect_warnings(head_data).each do |warning|
    puts "#{warning.file}:#{warning.line}:#{warning.column}: #{warning.message}"
  end
end

#collect_warnings(data) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/docurium.rb', line 300

def collect_warnings(data)
  warnings = []
  input_dir = File.join(@project_dir, option_version("HEAD", 'input'))

  # check for unmatched paramaters
  data[:functions].each do |f, fdata|
    warnings << Warning::UnmatchedParameter.new(f, type: fdata, input_dir: input_dir) if fdata[:comments] =~ /@param/
  end

  # check for changed signatures
  sigchanges = []
  @sigs.each do |fun, sig_data|
    warnings << Warning::SignatureChanged.new(fun) if sig_data[:changes]['HEAD']
  end

  # check for undocumented things
  types = [:functions, :callbacks, :globals, :types]
  types.each do |type_id|
    under_type = type_id.tap {|t| break t.to_s[0..-2].to_sym }
    data[type_id].each do |ident, type|
      under_type = type[:type] if type_id == :types

      warnings << Warning::MissingDocumentation.new(under_type, ident, type: type, input_dir: input_dir) if type[:description].empty?

      case type[:type]
      when :struct
        if type[:fields]
          type[:fields].each do |field|
            warnings << Warning::MissingDocumentation.new(:field, "#{ident}.#{field[:name]}", type: type, input_dir: input_dir) if field[:comments].empty?
          end
        end
      end
    end
  end
  warnings
end

#force_utf8(data) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/docurium.rb', line 228

def force_utf8(data)
  # Walk the data to force strings encoding to UTF-8.
  if data.instance_of? Hash
    data.each do |key, value|
      if [:comment, :comments, :description].include?(key)
        data[key] = value.force_encoding('UTF-8') unless value.nil?
      else
        force_utf8(value)
      end
    end
  elsif data.respond_to?(:each)
    data.each { |x| force_utf8(x) }
  end
end

#format_examples!(data, version) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/docurium.rb', line 52

def format_examples!(data, version)
  examples = []
  if ex = option_version(version, 'examples')
    if subtree = find_subtree(version, ex) # check that it exists
      index = Rugged::Index.new
      index.read_tree(subtree)

      files = []
      index.each do |entry|
        next unless entry[:path].match(/\.c$/)
        files << entry[:path]
      end

      files.each do |file|
        # highlight, roccoize and link
        rocco = Rocco.new(file, files, {:language => 'c'}) do
          ientry = index[file]
          blob = @repo.lookup(ientry[:oid])
          blob.content
        end

        extlen = -(File.extname(file).length + 1)
        rf_path = file[0..extlen] + '.html'
        rel_path = "ex/#{version}/#{rf_path}"

        rocco_layout = Rocco::Layout.new(rocco, @tf)
        # find out how deep our file is so we can use the right
        # number of ../ in the path
        depth = rel_path.count('/') - 1
        if depth == 0
          rocco_layout[:dirsup] = "./"
        else
          rocco_layout[:dirsup] = "../"*depth
        end

        rocco_layout.version = version
        rf = rocco_layout.render


        # look for function names in the examples and link
        id_num = 0
        data[:functions].each do |f, fdata|
          rf.gsub!(/#{f}([^\w])/) do |fmatch|
            extra = $1
            id_num += 1
            name = f + '-' + id_num.to_s
            # save data for cross-link
            data[:functions][f][:examples] ||= {}
            data[:functions][f][:examples][file] ||= []
            data[:functions][f][:examples][file] << rel_path + '#' + name
            "<a name=\"#{name}\" class=\"fnlink\" href=\"../../##{version}/group/#{fdata[:group]}/#{f}\">#{f}</a>#{extra}"
          end
        end

        # write example to the repo
        sha = @repo.write(rf, :blob)
        examples << [rel_path, sha]

        data[:examples] ||= []
        data[:examples] << [file, rel_path]
      end
    end
  end

  examples
end

#generate_doc_for(version) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/docurium.rb', line 119

def generate_doc_for(version)
  index = Rugged::Index.new
  read_subtree(index, version, option_version(version, 'input', ''))

  data = parse_headers(index, version)
  examples = format_examples!(data, version)
  [data, examples]
end

#generate_docsObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/docurium.rb', line 149

def generate_docs
  output_index = Rugged::Index.new
  write_site(output_index)
  @tf = File.expand_path(File.join(File.dirname(__FILE__), 'docurium', 'layout.mustache'))
  versions = get_versions
  versions << 'HEAD'
  # If the user specified versions, validate them and overwrite
  if !(vers = (@cli_options[:for] || [])).empty?
    vers.each do |v|
      next if versions.include?(v)
      puts "Unknown version #{v}"
      exit(false)
    end
    versions = vers
  end

  if (@repo.config['user.name'].nil? || @repo.config['user.email'].nil?)
    puts "ERROR: 'user.name' or 'user.email' is not configured. Docurium will not be able to commit the documentation"
    exit(false)
  end

  process_project(versions) do |i, version, result|
    data, examples = result
    sha = @repo.write(data.to_json, :blob)

    print "Generating documentation [#{i}/#{versions.count}]\r"

    unless dry_run?
      output_index.add(:path => "#{version}.json", :oid => sha, :mode => 0100644)
      examples.each do |path, id|
        output_index.add(:path => path, :oid => id, :mode => 0100644)
      end
    end
  end

  if head_data
    puts ''
    show_warnings(head_data)
  end

  return if dry_run?

  # We tally the signatures in the order they finished, which is
  # arbitrary due to the concurrency, so we need to sort them once
  # they've finished.
  sort_sigs!

  project = {
    :versions => versions.reverse,
    :github   => @options['github'],
    :name     => @options['name'],
    :signatures => @sigs,
  }
  sha = @repo.write(project.to_json, :blob)
  output_index.add(:path => "project.json", :oid => sha, :mode => 0100644)

  css = File.read(File.expand_path(File.join(File.dirname(__FILE__), 'docurium', 'css.css')))
  sha = @repo.write(css, :blob)
  output_index.add(:path => "ex/css.css", :oid => sha, :mode => 0100644)

  br = @options['branch']
  out "* writing to branch #{br}"
  refname = "refs/heads/#{br}"
  tsha = output_index.write_tree(@repo)
  puts "\twrote tree   #{tsha}"
  ref = @repo.references[refname]
  user = { :name => @repo.config['user.name'], :email => @repo.config['user.email'], :time => Time.now }
  options = {}
  options[:tree] = tsha
  options[:author] = user
  options[:committer] = user
  options[:message] = 'generated docs'
  options[:parents] = ref ? [ref.target] : []
  options[:update_ref] = refname
  csha = Rugged::Commit.create(@repo, options)
  puts "\twrote commit #{csha}"
  puts "\tupdated #{br}"
end

#get_versionsObject



362
363
364
365
366
367
# File 'lib/docurium.rb', line 362

def get_versions
  releases = @repo.tags
             .map { |tag| tag.name.gsub(%r(^refs/tags/), '') }
             .delete_if { |tagname| tagname.match(%r(-rc\d*$)) }
  VersionSorter.sort(releases)
end

#init_data(version = 'HEAD') ⇒ Object



33
34
35
36
37
# File 'lib/docurium.rb', line 33

def init_data(version = 'HEAD')
  data = {:files => [], :functions => {}, :callbacks => {}, :globals => {}, :types => {}, :prefix => ''}
  data[:prefix] = option_version(version, 'input', '')
  data
end

#option_version(version, option, default = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/docurium.rb', line 39

def option_version(version, option, default = nil)
  if @options['legacy']
    if valhash = @options['legacy'][option]
      valhash.each do |value, versions|
        return value if versions.include?(version)
      end
    end
  end
  opt = @options[option]
  opt = default if !opt
  opt
end

#parse_headers(index, version) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/docurium.rb', line 369

def parse_headers(index, version)
  headers = index.map { |e| e[:path] }.grep(/\.h$/)

  files = headers.map do |file|
    [file, @repo.lookup(index[file][:oid]).content]
  end

  data = init_data(version)
  DocParser.with_files(files, :prefix => version) do |parser|
    headers.each do |header|
      records = parser.parse_file(header, debug: interesting?(:file, header))
      update_globals!(data, records)
    end
  end

  data[:groups] = group_functions!(data)
  data[:types] = data[:types].sort # make it an assoc array
  find_type_usage!(data)

  data
end

#process_project(versions) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/docurium.rb', line 128

def process_project(versions)
  nversions = versions.count
  Parallel.each_with_index(versions, finish: -> (version, index, result) do
    data, examples = result
    # There's still some work we need to do serially
    tally_sigs!(version, data)
    force_utf8(data)

    puts "Adding documentation for #{version} [#{index}/#{nversions}]"

    # Store it so we can show it at the end
    @head_data = data if version == 'HEAD'

    yield index, version, result if block_given?

  end) do |version, index|
    puts "Generating documentation for #{version} [#{index}/#{nversions}]"
    generate_doc_for(version)
  end
end

#show_warnings(data) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/docurium.rb', line 349

def show_warnings(data)
  out '* checking your api'

  collect_warnings(data).group_by {|w| w.warning }.each do |klass, klass_warnings|
    klass_warnings.group_by {|w| w.type }.each do |type, type_warnings|
      out "  - " + type_warnings[0].message
      type_warnings.sort_by {|w| w.identifier }.each do |warning|
        out "\t" + warning.identifier
      end
    end
  end
end