diff --git a/lib/roku_builder/plugins/analyzer.rb b/lib/roku_builder/plugins/analyzer.rb index 1f4414a..0393f01 100644 --- a/lib/roku_builder/plugins/analyzer.rb +++ b/lib/roku_builder/plugins/analyzer.rb @@ -23,7 +23,7 @@ def self.dependencies [Loader] end - def analyze(options:) + def analyze(options:, quiet: false) @options = options @warnings = [] analyzer_config = get_analyzer_config @@ -41,10 +41,10 @@ def analyze(options:) @warnings.concat(line_inspector.run(file_path)) end if file_path.end_with?("__MACOSX") - add_warning(warning: :packageMacosxDirectory, path: file_path, dir: dir) + add_warning(warning: :packageMacosxDirectory, path: file_path) end if file_path.end_with?(".zip", ".md", ".pkg") - add_warning(warning: :packageExtraneousFiles, path: file_path, dir: dir) + add_warning(warning: :packageExtraneousFiles, path: file_path) end has_source_dir = true if file_path.end_with?("source") end @@ -52,6 +52,7 @@ def analyze(options:) add_warning(warning: :packageSourceDirectory, path: "source") end @warnings.concat(raf_inspector.run(analyzer_config[:inspectors])) + print_warnings(dir) unless quiet end @warnings end @@ -66,12 +67,35 @@ def get_analyzer_config JSON.parse(File.open(file).read, {symbolize_names: true}) end - def add_warning(warning:, path:, dir: nil) + def add_warning(warning:, path:) @warnings.push(@inspector_config[warning].deep_dup) - path = path.dup - path.slice!(dir) if dir @warnings.last[:path] = path end + + def print_warnings(dir) + logger = ::Logger.new(STDOUT) + logger.level = @logger.level + logger.formatter = proc {|severity, _datetime, _progname, msg| + "%5s: %s\n\r" % [severity, msg] + } + @warnings.each do |warning| + message = warning[:message] + if warning[:path] + warning[:path].slice!(dir) if dir + warning[:path].slice!(/^\//) + message += ". pkg:/"+warning[:path] + message += ":"+warning[:line].to_s if warning[:line] + end + case(warning[:severity]) + when "error" + logger.error(message) + when "warning" + logger.warn(message) + when "info" + logger.info(message) + end + end + end end RokuBuilder.register_plugin(Analyzer) end diff --git a/test/roku_builder/plugins/test_analyzer.rb b/test/roku_builder/plugins/test_analyzer.rb index 7bd4926..c510d8b 100644 --- a/test/roku_builder/plugins/test_analyzer.rb +++ b/test/roku_builder/plugins/test_analyzer.rb @@ -274,6 +274,15 @@ def test_manifest_file assert_equal 1, warnings.count assert_match(/Manifest.*missing/, warnings[0][:message]) end + def test_logging_error + test_logger_with_file_content(text: "stop", severity: :error) + end + def test_logging_warning + test_logger_with_file_content(text: "\"roCaptionRenderer\"", severity: :warn) + end + def test_logging_info + test_logger_with_file_content(text: "\"roSGScreen\"", severity: :info) + end private @@ -289,20 +298,34 @@ def use_manifest(manifest_file) FileUtils.cp(File.join(@root_dir, manifest_file), File.join(@root_dir, "manifest")) end - def test_file(text:, file: nil) + def test_file(text:, file: nil, quiet: true) file ||= "test.brs" test_file = File.join(@root_dir, "source", file) File.open(test_file, "w") do |file| file.write(text) end - warnings = test + warnings = test(quiet) FileUtils.rm(test_file) if File.exist?(test_file) warnings end - def test + def test_logger_with_file_content(text:, severity:) + logger = Minitest::Mock.new + + logger.expect(:level=, nil, [Integer]) + logger.expect(:formatter=, nil, [Proc]) + logger.expect(severity, nil, [String]) + + ::Logger.stub :new, logger do + warnings = test_file(text: text, quiet: false) + end + + logger.verify + end + + def test(quiet=true) analyzer = Analyzer.new(config: @config) - analyzer.analyze(options: @options) + analyzer.analyze(options: @options, quiet: quiet) end def print_all(warnings)