cloudamatic/mu

View on GitHub
bin/mu-gen-docs

Summary

Maintainability
Test Coverage
#!/usr/local/ruby-current/bin/ruby
# Copyright:: Copyright (c) 2014 eGlobalTech, Inc., all rights reserved
#
# Licensed under the BSD-3 license (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the root of the project or at
#
#     http://egt-labs.com/mu/LICENSE.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if ARGV.size > 0
  puts "#{$0}: Freshen the Mu Master's API and configuration documentation"
  exit 1
end

require 'rubygems'
require 'bundler/setup'
require 'erb'
require 'tempfile'
require 'fileutils'
require File.realpath(File.expand_path(File.dirname(__FILE__)+"/mu-load-config.rb"))
require 'mu'

ENV['PATH'] += ":"+File.dirname(Gem.ruby)

begin
  MU.log "Looking up the system YARD"
  yard = `which yard 2>&1`.chomp

  if yard.include? "no yard in"
    MU.log "No system YARD found. Trying YARD gem"
    yard = File.dirname(Gem.ruby)+'/yard'
  end

rescue
  MU.log "Using the YARD gem"
  yard = File.dirname(Gem.ruby)+'/yard'
end

docdir = Process.uid == 0 ? "/var/www/html/docs" : MU.dataDir+"/docs"
if !Dir.exist?(docdir)
  FileUtils.mkdir_p(docdir, mode: 0755)
end

MU::Config.emitConfigAsRuby
MU::Config.emitSchemaAsRuby
if Process.uid == 0
  MU.log "Generating YARD documentation in #{docdir} (see http://#{$MU_CFG['public_address']}/docs/frames.html)"
else
  MU.log "Generating YARD documentation in #{docdir}"
end

File.umask 0022

Dir.chdir(MU.myRoot) do
  imgs = %w{alpha.png beta.png release.png}
  FileUtils.cp(imgs.map { |f| "extras/"+f }, docdir)
  FileUtils.chmod(0644, imgs.map { |f| docdir+"/"+f })

  readme = File.read("README.md")
  readme += <<EOF

# Supported resources
EOF

  impl_counts = {}
  cloud_is_useful = {}
  cloudlist = MU::Cloud.supportedClouds.sort { |a, b|
    counts = {
      a => 0,
      b => 0
    }
    MU::Cloud.resource_types.each_pair { |type, cfg|
      impl_counts[type] ||= 0
      [a, b].each { |cloud|
        begin
          case MU::Cloud.resourceClass(cloud, type).quality
          when MU::Cloud::RELEASE
            cloud_is_useful[cloud] = true
            counts[cloud] += 4
            impl_counts[type] += 4
          when MU::Cloud::BETA
            cloud_is_useful[cloud] = true
            counts[cloud] += 2
            impl_counts[type] += 2
          when MU::Cloud::ALPHA
            counts[cloud] += 1
            impl_counts[type] += 1
          end
        rescue MU::Cloud::MuCloudResourceNotImplemented
        end
      }
    }
    counts[b] <=> counts[a]
  }

  cloudlist.reject! { |c| !cloud_is_useful[c] }

  readme += "\n\n<table><tr><th></th>"
  cloudlist.each { |cloud|
    readme += "<th>"+cloud+"</th>"
  }
  readme += "</tr>\n"

  icon_style = 'height:2.2em;width:2.2em;padding:0px;'
  MU::Cloud.resource_types.keys.sort { |a, b| impl_counts[b] <=> impl_counts[a] }.each { |type|
    readme += "<tr><td><strong>{MU::Config::BasketofKittens::#{MU::Cloud.resource_types[type][:cfg_plural]} #{type.to_s}}</strong></td>"
    cloudlist.each { |cloud|
      readme += "<td><center>"
      begin
        case MU::Cloud.resourceClass(cloud, type).quality
        when MU::Cloud::RELEASE
          readme += "<img src='release.png' style='#{icon_style}' title='Release Quality' alt='[Release Quality]'>"
        when MU::Cloud::BETA
          readme += "<img src='beta.png' style='#{icon_style}' title='Beta Quality' alt='[Beta Quality]'>"
        when MU::Cloud::ALPHA
          readme += "<img src='alpha.png' style='#{icon_style}' title='Alpha Quality' alt='[Alpha Quality]'>"
        else
          readme += "?"
        end
      rescue MU::Cloud::MuCloudResourceNotImplemented
        readme += "-"
      end
      readme += "</center></td>"
    }
    readme += "</tr>\n"
  }
  readme += "</table>\n\n"

  idx = Tempfile.new('mu-gen-docs-index', MU.myRoot)
  idx.write(readme)
  idx.rewind
  idx.close
  idx_short = idx.path.gsub(/.*?\/([^\/]+)$/, '\1')

  system(%Q{#{yard} doc modules --readme #{idx_short} --markup markdown --output-dir #{docdir}})
  # --exclude seems to just... not work
  FileUtils.rm(docdir+"/file."+idx_short+".html")

  if Process.uid == 0
    system(%Q{chcon -R -h -t httpd_sys_script_exec_t /var/www/html/})
  end
  system(%Q{#{yard} stats --list-undoc modules})
end