lib/docurium.rb in docurium-0.0.1 vs lib/docurium.rb in docurium-0.0.2

- old
+ new

@@ -1,12 +1,14 @@ require 'json' require 'tempfile' require 'version_sorter' +require 'rocco' +require 'docurium/layout' require 'pp' class Docurium - Version = VERSION = '0.0.1' + Version = VERSION = '0.0.2' attr_accessor :branch, :output_dir, :data def initialize(config_file) raise "You need to specify a config file" if !config_file @@ -18,11 +20,11 @@ def clear_data(version = 'HEAD') @data = {:files => [], :functions => {}, :globals => {}, :types => {}, :prefix => ''} @data[:prefix] = option_version(version, 'input', '') end - def option_version(version, option, default) + 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 @@ -32,28 +34,69 @@ opt = default if !opt opt end def generate_docs - puts "generating docs" + out "* generating docs" outdir = mkdir_temp copy_site(outdir) versions = get_versions versions << 'HEAD' versions.each do |version| - puts "generating docs for version #{version}" + out " - processing version #{version}" workdir = mkdir_temp Dir.chdir(workdir) do clear_data(version) checkout(version, workdir) - puts "parsing headers" parse_headers tally_sigs(version) - File.open(File.join(outdir, "#{version}.json"), 'w+') do |f| - f.write(@data.to_json) + end + + tf = File.expand_path(File.join(File.dirname(__FILE__), 'docurium', 'layout.mustache')) + if ex = option_version(version, 'examples') + workdir = mkdir_temp + Dir.chdir(workdir) do + with_git_env(workdir) do + `git rev-parse #{version}:#{ex} 2>&1` # check that it exists + if $?.exitstatus == 0 + out " - processing examples for #{version}" + `git read-tree #{version}:#{ex}` + `git checkout-index -a` + + files = [] + Dir.glob("**/*") do |file| + next if !File.file?(file) + files << file + end + files.each do |file| + out " # #{file}" + rocco = Rocco.new(file, files, {:language => 'c'}) + rocco_layout = Rocco::Layout.new(rocco, tf) + rocco_layout.version = version + rf = rocco_layout.render + rf_path = File.basename(file).split('.')[0..-2].join('.') + '.html' + rel_path = "ex/#{version}/#{rf_path}" + rf_path = File.join(outdir, rel_path) + FileUtils.mkdir_p(File.dirname(rf_path)) + File.open(rf_path, 'w+') do |f| + @data[:examples] ||= [] + @data[:examples] << [file, rel_path] + f.write(rf) + end + end + end + end end + + if version == 'HEAD' + show_warnings + end end + + File.open(File.join(outdir, "#{version}.json"), 'w+') do |f| + f.write(@data.to_json) + end end Dir.chdir(outdir) do project = { :versions => versions.reverse, @@ -64,21 +107,63 @@ File.open("project.json", 'w+') do |f| f.write(project.to_json) end end - if @options['branch'] - write_branch + if br = @options['branch'] + out "* writing to branch #{br}" + ref = "refs/heads/#{br}" + with_git_env(outdir) do + psha = `git rev-parse #{ref}`.chomp + `git add -A` + tsha = `git write-tree`.chomp + puts "\twrote tree #{tsha}" + if(psha == ref) + csha = `echo 'generated docs' | git commit-tree #{tsha}`.chomp + else + csha = `echo 'generated docs' | git commit-tree #{tsha} -p #{psha}`.chomp + end + puts "\twrote commit #{csha}" + `git update-ref -m 'generated docs' #{ref} #{csha}` + puts "\tupdated #{br}" + end else final_dir = File.join(@project_dir, @options['output'] || 'docs') + out "* output html in #{final_dir}" FileUtils.mkdir_p(final_dir) Dir.chdir(final_dir) do FileUtils.cp_r(File.join(outdir, '.'), '.') end end end + def show_warnings + out '* checking your api' + + # check for unmatched paramaters + unmatched = [] + @data[:functions].each do |f, fdata| + unmatched << f if fdata[:comments] =~ /@param/ + end + if unmatched.size > 0 + out ' - unmatched params in' + unmatched.sort.each { |p| out ("\t" + p) } + end + + # check for changed signatures + sigchanges = [] + @sigs.each do |fun, data| + if data[:changes]['HEAD'] + sigchanges << fun + end + end + if sigchanges.size > 0 + out ' - signature changes in' + sigchanges.sort.each { |p| out ("\t" + p) } + end + end + def get_versions VersionSorter.sort(git('tag').split("\n")) end def parse_headers @@ -114,15 +199,21 @@ end out.strip end def checkout(version, workdir) + with_git_env(workdir) do + `git read-tree #{version}:#{@data[:prefix]}` + `git checkout-index -a` + end + end + + def with_git_env(workdir) ENV['GIT_INDEX_FILE'] = mkfile_temp ENV['GIT_WORK_TREE'] = workdir ENV['GIT_DIR'] = File.join(@project_dir, '.git') - `git read-tree #{version}:#{@data[:prefix]}` - `git checkout-index -a` + yield ENV.delete('GIT_INDEX_FILE') ENV.delete('GIT_WORK_TREE') ENV.delete('GIT_DIR') end @@ -396,15 +487,10 @@ # TODO: rolled this back, want to strip the first few spaces, not everything def strip_block(block) block.strip end - def write_branch - puts "Writing to branch #{@branch}" - puts "Done!" - end - def mkdir_temp tf = Tempfile.new('docurium') tpath = tf.path tf.unlink FileUtils.mkdir_p(tpath) @@ -412,24 +498,26 @@ end def mkfile_temp tf = Tempfile.new('docurium-index') tpath = tf.path - tf.close + tf.unlink tpath end - def copy_site(outdir) - puts "Copying site files to temp path (#{outdir})" here = File.expand_path(File.dirname(__FILE__)) FileUtils.mkdir_p(outdir) Dir.chdir(outdir) do FileUtils.cp_r(File.join(here, '..', 'site', '.'), '.') end end def write_dir - puts "Writing to directory #{output_dir}" - puts "Done!" + out "Writing to directory #{output_dir}" + out "Done!" + end + + def out(text) + puts text end end