require "rubycritic/serializer"
require "rubycritic/files_initializer"
require "rubycritic/analysers_runner"
require "rubycritic/smells_status_setter"

module Rubycritic

  class RevisionComparator
    SNAPSHOTS_DIR_NAME = "snapshots"

    def initialize(analysed_files, source_control_system)
      @analysed_files_now = analysed_files
      @source_control_system = source_control_system
    end

    def set_statuses
      SmellsStatusSetter.set(
        analysed_files_before.flat_map(&:smells),
        @analysed_files_now.flat_map(&:smells)
      )
    end

    private

    def analysed_files_before
      serializer = Serializer.new(revision_file)
      if File.file?(revision_file)
        serializer.load
      else
        analysed_files = FilesInitializer.init(["."])
        @source_control_system.travel_to_head do
          AnalysersRunner.new(analysed_files, @source_control_system).run
        end
        serializer.dump(analysed_files)
        analysed_files
      end
    end

    def revision_file
      @revision_file ||= File.join(
        ::Rubycritic.configuration.root,
        SNAPSHOTS_DIR_NAME,
        @source_control_system.head_reference
      )
    end
  end

end

require "rubycritic/serializer"
require "rubycritic/files_initializer"
require "rubycritic/analysers_runner"
require "rubycritic/smells_status_setter"
require "rubycritic/version"

module Rubycritic

  class RevisionComparator
    SNAPSHOTS_DIR_NAME = "snapshots"

    def initialize(analysed_files, source_control_system)
      @analysed_files_now = analysed_files
      @source_control_system = source_control_system
    end

    def set_statuses
      SmellsStatusSetter.set(
        analysed_files_before.flat_map(&:smells),
        @analysed_files_now.flat_map(&:smells)
      )
    end

    private

    def analysed_files_before
      serializer = Serializer.new(revision_file)
      if File.file?(revision_file)
        serializer.load
      else
        analysed_files = FilesInitializer.init(["."])
        @source_control_system.travel_to_head do
          AnalysersRunner.new(analysed_files, @source_control_system).run
        end
        serializer.dump(analysed_files)
        analysed_files
      end
    end

    def revision_file
      @revision_file ||= File.join(
        ::Rubycritic.configuration.root,
        SNAPSHOTS_DIR_NAME,
        VERSION,
        @source_control_system.head_reference
      )
    end
  end

end

Use RubyCritic's version as a cache key in the name of the revision file
Use RubyCritic's version as a cache key in the name of the revision file

Basically without this change, updating to a new version of the gem
would sometimes cause things to blow up since in the Serializer we're
loading old code with `Marshal.load`. This a terrible way to keep
snapshots, we should probably use a DB like SQLite3.
