aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/cask/lib/hbc/cli/style.rb
blob: 261bed50b9bda8efae4c14f1414972018c1dffdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Hbc
  class CLI
    class Style < AbstractCommand
      def self.help
        "checks Cask style using RuboCop"
      end

      option "--fix", :fix, false

      def run
        install_rubocop
        cache_env = { "XDG_CACHE_HOME" => "#{HOMEBREW_CACHE}/style" }
        hide_warnings = debug? ? [] : [ENV["HOMEBREW_RUBY_PATH"], "-W0", "-S"]
        system(cache_env, *hide_warnings, "rubocop", *rubocop_args, "--", *cask_paths)
        raise CaskError, "style check failed" unless $CHILD_STATUS.success?
      end

      def install_rubocop
        capture_stderr do
          begin
            Homebrew.install_gem_setup_path! "rubocop-cask", HOMEBREW_RUBOCOP_CASK_VERSION, "rubocop"
          rescue SystemExit
            raise CaskError, Tty.strip_ansi($stderr.string).chomp.sub(/\AError: /, "")
          end
        end
      end

      def cask_paths
        @cask_paths ||= if args.empty?
          Hbc.all_tapped_cask_dirs
        elsif args.any? { |file| File.exist?(file) }
          args
        else
          casks.map(&:sourcefile_path)
        end
      end

      def rubocop_args
        fix? ? autocorrect_args : normal_args
      end

      def default_args
        [
          "--require", "rubocop-cask",
          "--force-default-config",
          "--format", "simple"
        ]
      end

      def normal_args
        default_args + ["--parallel"]
      end

      def autocorrect_args
        default_args + ["--auto-correct"]
      end
    end
  end
end