1require "rake"
 
2
 
3module CodeQuality
 
4  class CLI
 
5    def self.start(argv = ARGV)
 
6      Application.new.run
 
7    end
 
8
 
 9    # doc: http://www.rubydoc.info/gems/rake/Rake/Application
 
10    class Application < Rake::Application
 
11      def initialize
 
12        super
 
13        @name = "code_quality"
 
14      end
 
 
16      def run
 
17        Rake.application = self
 
18        @rakefiles = []
 
19        add_import File.join(lib_dir, "tasks", "code_quality.rake")
 
20        standard_exception_handling do
 
21          init name
 
22          load_rakefile
 
23          top_level
 
24        end
 
25      end
 
 
27      def in_namespace(name)
 
28        if name == @name # remove root namespace
 
29          yield
 
30        else
 
31          super
 
32        end
 
33      end
 
 
35      # allow option "--help"
 
36      def handle_options
 
37        options.rakelib = ["rakelib"]
 
38        options.trace_output = $stderr
 
 
40        OptionParser.new do |opts|
 
41          opts.separator "Run code_quality for a ruby/rails project, e.g.:"
 
42          opts.separator "    code_quality lowest_score=90 rubocop_max_offenses=100 metrics=stats,rails_best_practices,roodi rails_best_practices_max_offenses=10 roodi_max_offenses=10"
 
43          opts.separator ""
 
44          opts.separator "Show available tasks:"
 
45          opts.separator "    code_quality -T"
 
46          opts.separator ""
 
47          opts.separator "Invoke an audit task:"
 
48          opts.separator "    code_quality AUDIT_TASK"
 
49          opts.separator ""
 
50          opts.separator "Invoke all security audit tasks:"
 
51          opts.separator "    code_quality security_audit"
 
52          opts.separator ""
 
53          opts.separator "Invoke all quality audit tasks:"
 
54          opts.separator "    code_quality quality_audit"
 
55          opts.separator ""
 
56          opts.separator "Advanced options:"
 
 
58          opts.on_tail("-h", "--help", "-H", "Display this help message.") do
 
59            puts opts
 
60            exit
 
61          end
 
 
63          standard_rake_options.each { |args| opts.on(*args) }
 
64          opts.environment("RAKEOPT")
 
65        end.parse!
 
66      end
 
 
68      # overwrite options
 
69      def sort_options(options)
 
70        super.push(__version)
 
71      end
 
 
73      # allow option "--version"
 
74      def __version
 
75        ["--version", "-V",
 
76         "Display the program version.",
 
77         lambda do |_value|
 
78           puts "CodeQuality #{CodeQuality::VERSION}"
 
79           exit
 
80         end]
 
81      end
 
 
83      # allows running `code_quality` without a Rakefile
 
84      def find_rakefile_location
 
85        if (location = super).nil?
 
86          [rakefile_path, Dir.pwd]
 
87        else
 
88          location
 
89        end
 
90      end
 
 
92      def lib_dir
 
93        File.expand_path("../../../lib", __FILE__)
 
94      end
 
 
96      def rakefile_path
 
97        File.join(lib_dir, "code_quality.rb")
 
98      end
 
 99    end
 
100  end
 
101end