Skip to content

Commit

Permalink
Gradle: Handle property declarations in namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
greysteil committed Mar 17, 2019
1 parent 3bf7845 commit 094d5c3
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 4 deletions.
52 changes: 48 additions & 4 deletions gradle/lib/dependabot/gradle/file_parser/property_value_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ module Dependabot
module Gradle
class FileParser
class PropertyValueFinder
PROPERTY_DECLARATION_REGEX =
SINGLE_PROPERTY_DECLARATION_REGEX =
/(?:^|\s+|ext.)(?<name>[^\s=]+)\s*=\s*['"](?<value>[^\s]+)['"]/.
freeze

MULTI_PROPERTY_DECLARATION_REGEX =
/(?:^|\s+|ext.)(?<namespace>[^\s=]+)\s*=\s*\[(?<values>[^\]]+)\]/m.
freeze

NAMESPACED_DECLARATION_REGEX =
/(?:^|\s+)(?<name>[^\s:]+)\s*:\s*['"](?<value>[^\s]+)['"]\s*/.freeze

def initialize(dependency_files:)
@dependency_files = dependency_files
end
Expand Down Expand Up @@ -57,18 +64,55 @@ def properties(buildfile)
return @properties[buildfile.name] if @properties[buildfile.name]

@properties[buildfile.name] = {}
prepared_content(buildfile).scan(PROPERTY_DECLARATION_REGEX) do

@properties[buildfile.name].
merge!(fetch_single_property_declarations(buildfile))

@properties[buildfile.name].
merge!(fetch_multi_property_declarations(buildfile))

@properties[buildfile.name]
end

def fetch_single_property_declarations(buildfile)
properties = {}

prepared_content(buildfile).scan(SINGLE_PROPERTY_DECLARATION_REGEX) do
declaration_string = Regexp.last_match.to_s.strip
captures = Regexp.last_match.named_captures
name = captures.fetch("name").sub(/^ext\./, "")
@properties[buildfile.name][name] = {
properties[name] = {
value: captures.fetch("value"),
declaration_string: declaration_string,
file: buildfile.name
}
end

@properties[buildfile.name]
properties
end

def fetch_multi_property_declarations(buildfile)
properties = {}

prepared_content(buildfile).scan(MULTI_PROPERTY_DECLARATION_REGEX) do
captures = Regexp.last_match.named_captures
namespace = captures.fetch("namespace").sub(/^ext\./, "")

captures.fetch("values").scan(NAMESPACED_DECLARATION_REGEX) do
declaration_string = Regexp.last_match.to_s.strip
sub_captures = Regexp.last_match.named_captures
name = sub_captures.fetch("name")
full_name = [namespace, name].join(".")

properties[full_name] = {
value: sub_captures.fetch("value"),
declaration_string: declaration_string,
file: buildfile.name
}
end
end

properties
end

def prepared_content(buildfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
let(:property_name) { "commentedVersion" }
it { is_expected.to be_nil }
end

context "and the property is declared within a namespace" do
let(:buildfile_fixture_name) { "properties_namespaced.gradle" }
let(:property_name) { "versions.okhttp" }

its([:value]) { is_expected.to eq("3.12.1") }
its([:declaration_string]) do
is_expected.to eq("okhttp : '3.12.1'")
end
end
end
end
end
Expand Down
89 changes: 89 additions & 0 deletions gradle/spec/fixtures/buildfiles/properties_namespaced.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
ext.versions = [
agp : '3.3.1',
compileSdk : 28,
targetSdk : 28,
minSdk : 21,

shot : '2.0.0',
mavenPublish : '0.8.0',
dokka : '0.9.17',

dagger : '2.16',
kotlin : '1.3.11',
coroutines : '1.1.1',
rxJava : '2.1.9',
rxBinding : '2.0.0',
okhttp : '3.12.1',

retrofit : '2.4.0',
retrofitCoroutines : '0.9.2',
moshi : '1.6.0',
viewModel : '1.1.1',
timber : '4.7.1',

support : '27.1.1',
supportConstraintLayout: '1.0.2',

junit : '4.12',
junit5 : '1.3.2',
spek2 : '2.0.1',
kotlinMockito : '1.5.0',
assertJ : '3.8.0',
espresso : '3.0.2',
testRunner : '1.0.2',
screengrab : '1.2.0',
deviceAnimationRule : '0.0.2',
]

ext.gradlePlugins = [
androidGradle : "com.android.tools.build:gradle:$versions.agp",
mavenPublish : "com.vanniktech:gradle-maven-publish-plugin:$versions.mavenPublish",
dokka : "org.jetbrains.dokka:dokka-gradle-plugin:$versions.dokka",
shot : "com.karumi:shot:$versions.shot",
kotlin : "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin",
]

ext.libraries = [
dagger : "com.google.dagger:dagger:$versions.dagger",
daggerCompiler : "com.google.dagger:dagger-compiler:$versions.dagger",
kotlinStdlib : "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin",
kotlinReflect : "org.jetbrains.kotlin:kotlin-reflect:$versions.kotlin",
coroutinesCore : "org.jetbrains.kotlinx:kotlinx-coroutines-core:$versions.coroutines",
coroutinesAndroid : "org.jetbrains.kotlinx:kotlinx-coroutines-android:$versions.coroutines",
moshi : "com.squareup.moshi:moshi:$versions.moshi",
moshiKotlin : "com.squareup.moshi:moshi-kotlin:$versions.moshi",
moshiCodeGen : "com.squareup.moshi:moshi-kotlin-codegen:$versions.moshi",
timber : "com.jakewharton.timber:timber:$versions.timber",

rxJava : "io.reactivex.rxjava2:rxjava:$versions.rxJava",
rxBinding : "com.jakewharton.rxbinding2:rxbinding-kotlin:$versions.rxBinding",
retrofit : "com.squareup.retrofit2:retrofit:$versions.retrofit",
retrofitMoshi : "com.squareup.retrofit2:converter-moshi:$versions.retrofit",
retrofitCoroutines : "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$versions.retrofitCoroutines",
okhttp : "com.squareup.okhttp3:okhttp:$versions.okhttp"
]

ext.supportLibraries = [
annotations : "com.android.support:support-annotations:$versions.support",
appCompat : "com.android.support:appcompat-v7:$versions.support",
recyclerView : "com.android.support:recyclerview-v7:$versions.support",
constraintLayout: "com.android.support.constraint:constraint-layout:$versions.supportConstraintLayout",
design : "com.android.support:design:$versions.support",
viewModel : "android.arch.lifecycle:extensions:$versions.viewModel"
]

ext.testLibraries = [
junit : "junit:junit:$versions.junit",
junit5Engine : "org.junit.platform:junit-platform-engine:$versions.junit5",
spek2Dsl : "org.spekframework.spek2:spek-dsl-jvm:$versions.spek2",
spek2Junit5Runner : "org.spekframework.spek2:spek-runner-junit5:$versions.spek2",
mockWebServer : "com.squareup.okhttp3:mockwebserver:$versions.okhttp",
okhttpTls : "com.squareup.okhttp3:okhttp-tls:$versions.okhttp",
archCoreTest : "android.arch.core:core-testing:$versions.viewModel",
testRunner : "com.android.support.test:runner:$versions.testRunner",
espressoCore : "com.android.support.test.espresso:espresso-core:$versions.espresso",
espressoContrib : "com.android.support.test.espresso:espresso-contrib:$versions.espresso",
testRules : "com.android.support.test:rules:$versions.testRunner",
screengrab : "tools.fastlane:screengrab:$versions.screengrab",
deviceAnimationRule : "com.github.VictorAlbertos:DeviceAnimationTestRule:$versions.deviceAnimationRule",
]

0 comments on commit 094d5c3

Please sign in to comment.