Sha256: ad505d8aafad100e2af78a0b2fcaa5b93fee54be673635e84884967f3299b71d

Contents?: true

Size: 1.64 KB

Versions: 15

Compression:

Stored size: 1.64 KB

Contents

module CC
  module CLI
    class ConfigGenerator
      CODECLIMATE_YAML = Command::CODECLIMATE_YAML
      AUTO_EXCLUDE_PATHS = %w(config/ db/ features/ node_modules/ script/ spec/ test/ vendor/).freeze

      def self.for(filesystem, engine_registry, upgrade_requested)
        if upgrade_requested && upgrade_needed?(filesystem)
          UpgradeConfigGenerator.new(filesystem, engine_registry)
        else
          ConfigGenerator.new(filesystem, engine_registry)
        end
      end

      def initialize(filesystem, engine_registry)
        @filesystem = filesystem
        @engine_registry = engine_registry
      end

      def eligible_engines
        return @eligible_engines if @eligible_engines

        engines = engine_registry.list
        @eligible_engines = engines.each_with_object({}) do |(name, config), result|
          if engine_eligible?(config)
            result[name] = config
          end
        end
      end

      def exclude_paths
        AUTO_EXCLUDE_PATHS.select { |path| filesystem.exist?(path) }
      end

      def post_generation_verb
        "generated"
      end

      private

      attr_reader :engine_registry, :filesystem

      def self.upgrade_needed?(filesystem)
        if filesystem.exist?(CODECLIMATE_YAML)
          YAML.safe_load(File.read(CODECLIMATE_YAML))["languages"].present?
        end
      end

      def engine_eligible?(engine)
        !engine["community"] && engine["enable_regexps"].present? && files_exist?(engine)
      end

      def files_exist?(engine)
        filesystem.any? do |path|
          engine["enable_regexps"].any? { |re| Regexp.new(re).match(path) }
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
codeclimate-0.11.0 lib/cc/cli/config_generator.rb
codeclimate-0.10.1 lib/cc/cli/config_generator.rb
codeclimate-0.10.0 lib/cc/cli/config_generator.rb
codeclimate-0.9.7 lib/cc/cli/config_generator.rb
codeclimate-0.9.6 lib/cc/cli/config_generator.rb
codeclimate-0.9.5 lib/cc/cli/config_generator.rb
codeclimate-0.9.4 lib/cc/cli/config_generator.rb
codeclimate-0.9.3 lib/cc/cli/config_generator.rb
codeclimate-0.9.2 lib/cc/cli/config_generator.rb
codeclimate-0.9.1 lib/cc/cli/config_generator.rb
codeclimate-0.9.0 lib/cc/cli/config_generator.rb
codeclimate-0.8.1 lib/cc/cli/config_generator.rb
codeclimate-0.8.0 lib/cc/cli/config_generator.rb
codeclimate-0.7.2 lib/cc/cli/config_generator.rb
codeclimate-0.7.0 lib/cc/cli/config_generator.rb