class Danger::DangerAndroidVersionChange

Asserts the versionName or versionCode strings have been changed in your Android project. This is done by checking the git diff of your project's build.gradle file. The plugin will either call Danger's fail method or do nothing if the versionName or versionCode strings were changed.

@example Assert the versionName string changed for your Android project

 # Calls Danger `fail` if the versionName not updated or nothing if it has changed.
android_version_change.assert_version_name_changed()

 # Calls Danger `fail` if the versionName not updated or nothing if it has changed.
android_version_change.assert_version_code_changed()

@tags android, ci, continuous integration, android version, danger, dangerfile, danger plugin

Constants

DEFAULT_BUILD_GRADLE_FILE_PATH

Public Class Methods

instance_name() click to toggle source

The instance name used in the Dangerfile @return [String]

# File lib/android_version_change/plugin.rb, line 19
def self.instance_name
  "android_version_change"
end
new(dangerfile) click to toggle source
Calls superclass method
# File lib/android_version_change/plugin.rb, line 25
def initialize(dangerfile)
  super(dangerfile)
end

Public Instance Methods

assert_version_changed(build_gradle_file_path, name_or_code) click to toggle source

(Internal use) Takes path to build.gradle as well as “versionName” or “versionCode” string to check for change. @return [void]

# File lib/android_version_change/plugin.rb, line 46
def assert_version_changed(build_gradle_file_path, name_or_code)
  unless File.file?(build_gradle_file_path)
    fail "build.gradle file at path " + build_gradle_file_path + " does not exist."
    return # rubocop:disable UnreachableCode
  end

  unless git.diff_for_file(build_gradle_file_path) # No diff found for build.gradle file.
    fail "You did not edit your build.gradle file at all. Therefore, you did not change the " + name_or_code + "."
    return # rubocop:disable UnreachableCode
  end

  git_diff_string = git.diff_for_file(build_gradle_file_path).patch
  assert_version_changed_diff(git_diff_string, name_or_code)
end
assert_version_changed_diff(git_diff_string, name_or_code) click to toggle source

(Internal use) Pass the git diff string here to see if the version string has been updated. @return [void]

# File lib/android_version_change/plugin.rb, line 31
def assert_version_changed_diff(git_diff_string, name_or_code)
  git_diff_lines = git_diff_string.lines
  git_diff_string.each_line.each_with_index do |line, index|
    next unless line.include? name_or_code
    # we need to check the current line and the next line of the string to determine if it's a git diff change.
    if git_diff_lines.length >= (index + 2) && git_diff_lines[index][0] == "-" && git_diff_lines[index + 1][0] == "+"
      return # rubocop:disable NonLocalExitFromIterator
    end
  end

  fail "You did not change the Android " + name_or_code + "."
end
assert_version_code_changed(override_build_gradle_file_path = DEFAULT_BUILD_GRADLE_FILE_PATH) click to toggle source

Assert the versionCode has been changed in your build.gradle file.

@example Assert the versionCode has been changed in your Android project.

 # Calls Danger `fail` if the versionName not updated or nothing if it has changed.
android_version_change.assert_version_code_changed()

# If your build.gradle file is not located in the default location: `app/build.gradle`
android_version_change.assert_version_code_changed("specialDirectory/build.gradle")

@param [String] override_build_gradle_file_path

(optional) Path to build.gradle file for Android project that versionCode is located in.

@return [void]

# File lib/android_version_change/plugin.rb, line 89
def assert_version_code_changed(override_build_gradle_file_path = DEFAULT_BUILD_GRADLE_FILE_PATH)
  assert_version_changed(override_build_gradle_file_path, "versionCode")
end
assert_version_name_changed(override_build_gradle_file_path = DEFAULT_BUILD_GRADLE_FILE_PATH) click to toggle source

Assert the versionName has been changed in your build.gradle file.

@example Assert the versionName has been changed in your Android project.

 # Calls Danger `fail` if the versionName not updated or nothing if it has changed.
android_version_change.assert_version_name_changed()

# If your build.gradle file is not located in the default location: `app/build.gradle`
android_version_change.assert_version_name_changed("specialDirectory/build.gradle")

@param [String] override_build_gradle_file_path

(optional) Path to build.gradle file for Android project that versionName is located in.

@return [void]

# File lib/android_version_change/plugin.rb, line 73
def assert_version_name_changed(override_build_gradle_file_path = DEFAULT_BUILD_GRADLE_FILE_PATH)
  assert_version_changed(override_build_gradle_file_path, "versionName")
end