Sha256: 5cebea72ecdeda66899cd3f4e08783d183e6a49de0e02a08889ac4bd28145992

Contents?: true

Size: 1.79 KB

Versions: 5

Compression:

Stored size: 1.79 KB

Contents

module CC
  module CLI
    class Init < Command
      include CC::Analyzer

      AUTO_EXCLUDE_PATHS = %w[config db features node_modules script spec test vendor].freeze

      def run
        if filesystem.exist?(CODECLIMATE_YAML)
          say "Config file .codeclimate.yml already present.\nTry running 'validate-config' to check configuration."
        else
          create_codeclimate_yaml
          say "Config file .codeclimate.yml successfully generated.\nEdit and then try running 'validate-config' to check configuration."
        end
      end

      private

      def create_codeclimate_yaml
        config = { "engines" => {} }
        eligible_engines.each do |engine_name, engine_config|
          config["engines"][engine_name] = {
            "enabled" => true
          }
          config["ratings"] ||= {}
          config["ratings"]["paths"] ||= []

          config["ratings"]["paths"] |= engine_config["default_ratings_paths"]
        end

        config["exclude_paths"] = exclude_paths(AUTO_EXCLUDE_PATHS)

        filesystem.write_path(CODECLIMATE_YAML, config.to_yaml)
      end

      def exclude_paths(paths)
        expanded_paths = []
        paths.each do |dir|
          if filesystem.exist?(dir)
            expanded_paths << "#{dir}/**/*"
          end
        end
        expanded_paths
      end

      def engine_eligible?(engine)
        !engine["community"] &&
          engine["enable_regexps"].present? &&
          filesystem.any? do |path|
            engine["enable_regexps"].any? { |re| Regexp.new(re).match(path) }
          end
      end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
codeclimate-0.2.12 lib/cc/cli/init.rb
codeclimate-0.2.11 lib/cc/cli/init.rb
codeclimate-0.2.9 lib/cc/cli/init.rb
codeclimate-0.2.7 lib/cc/cli/init.rb
codeclimate-0.2.6 lib/cc/cli/init.rb