Commit 9a0f2263 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add --[no-]cache and --cache-dir options.

By default the caching is disabled.  When turned on with --cache,
the caching dir will be <output-dir>/.cache/ unless overridden
with --cache-dir option.

Previously the whole output dir got cleaned up, but now it can
contain a .cache dir, which we'll want to preserve.  So I created
OutputDir class to take care of output directory cleanup in a way
that preserves the .cache/ directory inside it.

Refs #446
parent 2bfd8b1f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ module JsDuck
  class BatchParser

    def self.parse(opts)
      cache = Cache.new(Dir.getwd + "/.cache")
      cache = Cache.create(opts)

      Util::Parallel.map(opts.input_files) do |fname|
        Logger.log("Parsing", fname)
+15 −1
Original line number Diff line number Diff line
require 'digest/md5'
require 'fileutils'
require 'jsduck/util/null_object'

module JsDuck

  # Reads/writes parsed files in cache.
  class Cache

    # Factory method to produce a cache object.  When caching is
    # disabled, returns a NullObject which emulates a cache that's
    # always empty.
    def self.create(opts)
      # Check also for cache_dir, which will be nil when output_dir is :stdout
      if opts.cache && opts.cache_dir
        Cache.new(opts.cache_dir)
      else
        Util::NullObject.new(:read => nil, :write => nil)
      end
    end

    def initialize(cache_dir)
      @cache_dir = cache_dir
      FileUtils.mkdir(cache_dir) unless File.exists?(cache_dir)
      FileUtils.mkdir_p(cache_dir) unless File.exists?(cache_dir)
    end

    # Given contents of a source file, reads the already parsed data
+2 −1
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ module JsDuck
    end

    def write_dir(dir, extension)
      FileUtils.mkdir(dir)
      FileUtils.mkdir(dir) unless File.exists?(dir)

      Util::Parallel.each(@relations.classes) do |cls|
        filename = dir + "/" + cls[:name] + extension
        Logger.log("Writing docs", filename)
+2 −1
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ require 'jsduck/exporter/examples'
require 'jsduck/format/batch'
require 'jsduck/class_writer'
require 'jsduck/guide_writer'
require 'jsduck/output_dir'
require 'fileutils'

module JsDuck
@@ -50,7 +51,7 @@ module JsDuck
    # -- util routines --

    def clean_output_dir
      FileUtils.rm_rf(@opts.output_dir)
      OutputDir.clean(@opts)
    end

    def format_classes
+24 −1
Original line number Diff line number Diff line
@@ -48,6 +48,8 @@ module JsDuck

    # Debugging
    attr_accessor :warnings_exit_nonzero
    attr_accessor :cache
    attr_accessor :cache_dir
    attr_accessor :template_dir
    attr_accessor :template_links
    attr_accessor :extjs_path
@@ -132,6 +134,8 @@ module JsDuck

      # Debugging
      @warnings_exit_nonzero = false
      @cache = false
      @cache_dir = nil
      @root_dir = File.dirname(File.dirname(File.dirname(__FILE__)))
      @template_dir = @root_dir + "/template-min"
      @template_links = false
@@ -203,7 +207,12 @@ module JsDuck
          "This option is REQUIRED.  When the directory exists,",
          "it will be overwritten.  Give dash '-' as argument",
          "to write docs to STDOUT (works only with --export).") do |path|
          @output_dir = path == "-" ? :stdout : canonical(path)
          if path == "-"
            @output_dir = :stdout
          else
            @output_dir = canonical(path)
            @cache_dir = @output_dir + "/.cache" unless @cache_dir
          end
        end

        opts.on('--export=TYPE',
@@ -739,6 +748,20 @@ module JsDuck
          Util::Parallel.in_processes = count.to_i
        end

        opts.on('--[no-]cache',
          "Turn parser cache on/off (EXPERIMENTAL).",
          "",
          "Off by default.") do |enabled|
          @cache = enabled
        end

        opts.on('--cache-dir=PATH',
          "Directory where to cache the parsed source.",
          "",
          "Defaults to: <output-dir>/.cache") do |path|
          @cache_dir = path
        end

        opts.on('--pretty-json',
          "Turns on pretty-printing of JSON.",
          "",
Loading