Class: Danger::DangerAndroidLint

Inherits:
Plugin
  • Object
show all
Defined in:
lib/android_lint/plugin.rb

Overview

Lint files of a gradle based Android project. This is done using the Android’s [Lint](developer.android.com/studio/write/lint.html) tool. Results are passed out as tables in markdown.

Examples:

Running AndroidLint with its basic configuration


android_lint.lint

Running AndroidLint with a specific gradle task


android_lint.gradle_task = "lintMyFlavorDebug"
android_lint.lint

Running AndroidLint without running a Gradle task


android_lint.skip_gradle_task = true
android_lint.lint

Running AndroidLint for a specific severity level and up


# options are ["Warning", "Error", "Fatal"]
android_lint.severity = "Error"
android_lint.lint

See Also:

  • loadsmart/danger-android_lint

Constant Summary collapse

SEVERITY_LEVELS =
["Warning", "Error", "Fatal"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filteringObject

Enable filtering Only show messages within changed files.



85
86
87
# File 'lib/android_lint/plugin.rb', line 85

def filtering
  @filtering
end

#filtering_linesObject

Only shows messages for the modified lines.



88
89
90
# File 'lib/android_lint/plugin.rb', line 88

def filtering_lines
  @filtering_lines
end

#gradle_taskString

A getter for ‘gradle_task`, returning “lint” if value is nil.

Returns:

  • (String)


50
51
52
# File 'lib/android_lint/plugin.rb', line 50

def gradle_task
  @gradle_task
end

#report_fileString

A getter for ‘report_file`.

Returns:

  • (String)


38
39
40
# File 'lib/android_lint/plugin.rb', line 38

def report_file
  @report_file
end

#severityString

A getter for ‘severity`, returning “Warning” if value is nil.

Returns:

  • (String)


79
80
81
# File 'lib/android_lint/plugin.rb', line 79

def severity
  @severity || SEVERITY_LEVELS.first
end

#skip_gradle_taskBoolean

A getter for ‘skip_gradle_task`, returning `false` if value is nil.

Returns:

  • (Boolean)


66
67
68
# File 'lib/android_lint/plugin.rb', line 66

def skip_gradle_task
  @skip_gradle_task ||= false
end

Instance Method Details

#lint(inline_mode: false) ⇒ void

This method returns an undefined value.

Calls lint task of your gradle project. It fails if ‘gradlew` cannot be found inside current directory. It fails if `severity` level is not a valid option. It fails if `xmlReport` configuration is not set to `true` in your `build.gradle` file.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/android_lint/plugin.rb', line 96

def lint(inline_mode: false)
  unless skip_gradle_task
    return fail("Could not find `gradlew` inside current directory") unless gradlew_exists?
  end

  unless SEVERITY_LEVELS.include?(severity)
    fail("'#{severity}' is not a valid value for `severity` parameter.")
    return
  end

  unless skip_gradle_task
    system "./gradlew #{gradle_task}"
  end

  unless File.exists?(report_file)
    fail("Lint report not found at `#{report_file}`. "\
      "Have you forgot to add `xmlReport true` to your `build.gradle` file?")
  end

  issues = read_issues_from_report
  filtered_issues = filter_issues_by_severity(issues)

  message = ""

  if inline_mode
    # Report with inline comment
    send_inline_comment(filtered_issues)
  else
    message = message_for_issues(filtered_issues)
    markdown("### AndroidLint found issues\n\n" + message) unless message.to_s.empty?
  end

  message
end