lib/lurker/cli.rb in lurker-0.6.6 vs lib/lurker/cli.rb in lurker-0.6.7

- old
+ new

@@ -1,17 +1,15 @@ require 'thor' require 'execjs' require 'digest/sha1' -require 'lurker/service' +require 'lurker' +require 'active_support/inflector' module Lurker # A Thor::Error to be thrown when an lurker directory is not found class NotFound < Thor::Error; end - # A Thor::Error to be thrown when an lurker output destination is unavailable - class NotADirectory < Thor::Error; end - # A Thor definition for an lurker to HTML conversion operation class Cli < Thor include Thor::Actions attr_accessor :origin_path, :content @@ -26,12 +24,12 @@ desc "convert [LURKER_PATH]", "Convert lurker to HTML" method_option :rails, :type => :boolean, :desc => "Includes Rails environment" method_option :exclude, :aliases => "-e", :desc => "Select endpoints by given regexp, if NOT matching prefix" method_option :select, :aliases => "-s", :desc => "Select endpoints by given regexp, matching prefix" - method_option :output, :aliases => "-o", :desc => "Output path" - method_option :url_base_path, :aliases => "-u", :desc => "URL base path" + method_option :output, :aliases => "-o", :desc => "Output path", :default => "public" + method_option :url_base_path, :aliases => "-u", :desc => "URL base path", :default => Lurker::DEFAULT_URL_BASE method_option :format, :aliases => "-f", :desc => "Format in html or pdf, defaults to html", :default => "html" method_option :templates, :aliases => "-t", :desc => "Template overrides path" method_option :content, :aliases => "-c", :desc => "Content to be rendered into html-docs main page" def convert(lurker_path=Lurker::DEFAULT_SERVICE_PATH) say_status nil, "Converting lurker to #{options[:format]}" @@ -39,12 +37,11 @@ self.content = get_content(options[:content]) self.origin_path = File.expand_path(lurker_path) raise Lurker::NotFound.new(origin_path) unless has_valid_origin? say_status :using, lurker_path - self.destination_root = output_path - raise Lurker::NotADirectory.new(output_path) unless has_valid_destination? + FileUtils.mkdir_p(output_path) say_status :inside, output_path if options[:rails] require "#{Dir.pwd}/config/environment" end @@ -58,40 +55,40 @@ no_tasks do def convert_to_pdf Lurker.safe_require('pdfkit') css = File.expand_path('application.css', self.class.precompiled_static_root) - in_root do + inside(output_path) do service_presenters.each do |service_presenter| html = "<html><body>" service_presenter.endpoints.each do |endpoint_prefix_group| endpoint_prefix_group.each do |endpoint_presenter| html << endpoint_presenter.to_html(layout: false) end end html << "</body></html>" kit = PDFKit.new(html, :page_size => 'Letter') kit.stylesheets << css - create_file("#{service_presenter.name}.pdf", kit.to_pdf, force: true) + url_name = ActiveSupport::Inflector.parameterize(service_presenter.name, '_') + create_file("#{url_name}.pdf", kit.to_pdf, force: true) end end end def convert_to_html - in_root do + inside(output_path) do # js, css, fonts static = [] Dir["#{self.class.precompiled_static_root}/*"].each do |fname| if match = fname.match(/application\.(js|css)$/) sha1 = Digest::SHA1.hexdigest(open(fname).read) html_options.merge! match[1] => sha1 static << (new_name = "application-#{sha1}.#{match[1]}") - to = destination.join(new_name).to_s - FileUtils.cp_r fname, to - spawn "cat #{to} | gzip -9 > #{to}.gz" + FileUtils.cp_r fname, new_name + spawn "cat #{new_name} | gzip -9 > #{new_name}.gz" else - FileUtils.cp_r fname, destination.to_s + FileUtils.cp_r fname, Pathname.new(fname).basename.to_s end end service_presenters.each do |service_presenter| create_file("index.html", service_presenter.to_html, force: true) @@ -115,18 +112,23 @@ end end end def output_path - @output_path ||= - if options[:output] - File.expand_path(options[:output]) - else - File.expand_path("../#{options[:format]}", origin_path) - end + "#{output_prefix}/#{url_base_path}" end + def output_prefix + if explicit = options[:output] + explicit.sub(/\/?#{url_base_path}\/?$/, '') + elsif File.exists? "public" + "public" + else + raise "Please, run it from `Rails.root` or pass `-o` option" + end + end + def template_path @template_path ||= if options[:templates] File.expand_path(options[:templates]) else @@ -136,14 +138,10 @@ def has_valid_origin? origin.directory? end - def has_valid_destination? - !destination.exist? || destination.directory? - end - def service_presenters @service_presenters ||= services.map do |service| Lurker::ServicePresenter.new(service, html_options, &filtering_block) end end @@ -168,22 +166,26 @@ end def html_options @html_options ||= { :static_html => true, - :url_base_path => options[:url_base_path].presence || "/#{Lurker::DEFAULT_URL_BASE}", + :url_base_path => url_base_path.prepend('/'), :template_directory => template_path, - :html_directory => destination_root, + :html_directory => output_path, :content => self.content, :footer => (`git rev-parse --short HEAD`.to_s.strip rescue ""), :lurker => gem_info } end end private + def url_base_path + options[:url_base_path].presence.try(:strip).try(:sub, /^\/+/, '') || Lurker::DEFAULT_URL_BASE + end + def gem_info spec = if Bundler.respond_to? :locked_gems Bundler.locked_gems.specs.select { |s| s.name == 'lurker' } .first # 1.6 else Bundler.definition.sources.detect { |s| s.specs.map(&:name).include?('lurker') } # 1.3 @@ -214,12 +216,8 @@ @services ||= [Lurker::Service.new(origin_path)] end def origin Pathname.new(origin_path) - end - - def destination - Pathname.new(destination_root) end end end