Commit 0fc22515 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Invalidate cache generated by different Ruby/JSDuck version.

Marshal dump format can change between Ruby versions and although
the Marshal has its own version numbers, I get an encoding error
when reading the Ruby 1.8 Marshal dump with Ruby 1.9.

The parsed source code data structure can also change between
versions of JSDuck.

To eliminate these problems, a .cache/manifest.txt file is created
with the contents like:

    Ruby: 1.9.3, JSDuck: 5.2.0

The file is then checked to match the current version of Ruby and
JSDuck.  If the versions don't match, the whole cache is invalidated
(all files removed from it).

Refs #446
parent 4347e94a
Loading
Loading
Loading
Loading
+30 −4
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ module JsDuck
    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)
        Cache.new(opts)
      else
        Util::NullObject.new(
          :read => nil,
@@ -30,10 +30,17 @@ module JsDuck
    # But it will always be available after the #write call.
    attr_reader :previous_entry

    def initialize(cache_dir)
      @cache_dir = cache_dir
    def initialize(opts)
      @jsduck_version = opts.version
      @cache_dir = opts.cache_dir
      @manifest_file = @cache_dir + "/manifest.txt"
      @previous_entry = nil
      FileUtils.mkdir_p(cache_dir) unless File.exists?(cache_dir)

      FileUtils.mkdir_p(@cache_dir) unless File.exists?(@cache_dir)

      # Invalidate the whole cache when it was generated with a
      # different Ruby and/or JSDuck version.
      invalidate_all! unless valid_manifest?
    end

    # Given the name and contents of a source file, reads the already
@@ -78,6 +85,25 @@ module JsDuck
      Digest::MD5.hexdigest(string)
    end

    def valid_manifest?
      manifest = File.exists?(@manifest_file) ? Util::IO.read(@manifest_file) : ""
      return manifest == current_manifest
    end

    def invalidate_all!
      FileUtils.rm_rf(@cache_dir)
      FileUtils.mkdir(@cache_dir)
      save_manifest
    end

    def save_manifest
      File.open(@manifest_file, "w") {|f| f.write(current_manifest) }
    end

    def current_manifest
      "Ruby: #{RUBY_VERSION}, JSDuck: #{@jsduck_version}\n"
    end

  end

end
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ module JsDuck
    attr_accessor :ignore_global
    attr_accessor :external_classes
    attr_accessor :ext4_events
    attr_accessor :version

    # Customizing output
    attr_accessor :title