Skip to content
Snippets Groups Projects
Commit cc0724cf authored by Leaminn Ma's avatar Leaminn Ma Committed by Leaminn Ma
Browse files

Add Kotlin Gradle config file class

Adds the Kotlin Gradle config file class
which contains logic to parse 'build.gradle.kts'
files. Refactors Java Gradle config file class
so it can be inherited by KotlinGradle.
parent f3ad6d6c
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -13,6 +13,7 @@ module Constants
ConfigFiles::GoModules,
ConfigFiles::JavaGradle,
ConfigFiles::JavaMaven,
ConfigFiles::KotlinGradle,
ConfigFiles::RubyGemsLock
].freeze
end
Loading
Loading
Loading
Loading
@@ -7,10 +7,12 @@ module ConfigFiles
class JavaGradle < Base
START_SECTION_REGEX = /^dependencies {/ # Identifies the dependencies section
END_SECTION_REGEX = /^}$/
PREFIX_REGEX = /^(implementation |testImplementation )/ # Appears before each dependency
LONG_FORM_KEYWORD = 'name:' # Only present in the long form dependency definition
PREFIX_REGEX = /^['"]?(implementation|testImplementation)['"]?/ # Appears before each dependency
LONG_FORM_NAME_REGEX = /name:\s*(?<value>[^,\s]*)/
LONG_FORM_VERSION_REGEX = /version:\s*(?<value>[^,\s]*)/
QUOTED_VALUE_REGEX = /(?<quote>["'])(?<value>[^"']+)\k<quote>/ # Matches value between double or single quotes
INLINE_COMMENT_REGEX = %r{\s+//.*$}
STRING_INTERPOLATION_CHAR = '$'
EXCLUDE_KEYWORD = '('
 
def self.file_name_glob
Loading
Loading
@@ -28,7 +30,8 @@ def self.lang_name
# dependencies {
# // Short form: <group>:<name>:<version>
# implementation 'org.codehaus.groovy:groovy:3.+'
# testImplementation "com.google.guava:guava:29.0.1"
# testImplementation "com.google.guava:guava:29.0.1" // Inline comment
# // The quotes on `implementation` may be a legacy format; ported from Repository X-Ray Go repo
# "implementation" 'org.ow2.asm:asm:9.6'
#
# // Long form
Loading
Loading
@@ -41,7 +44,7 @@ def self.lang_name
# runtimeOnly files('libs/a.jar', 'libs/b.jar')
#
# // TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/481317
# // String interpolation is not currently supported; below outputs "$arcgisVersion" as version
# // String interpolation is not currently supported; below outputs nil as version
# implementation "com.esri.arcgisruntime:arcgis-java:$arcgisVersion"
# }
#
Loading
Loading
@@ -51,33 +54,40 @@ def extract_libs
 
content.each_line do |line|
line.strip!
line.gsub!(/'|"/, '')
line.gsub!(INLINE_COMMENT_REGEX, '')
 
if START_SECTION_REGEX.match?(line)
in_deps_section = true
elsif in_deps_section && PREFIX_REGEX.match?(line)
libs << parse_lib(line) unless line.include?(EXCLUDE_KEYWORD)
elsif in_deps_section && END_SECTION_REGEX.match?(line)
break
elsif in_deps_section
libs << parse_lib(line) if self.class::PREFIX_REGEX.match?(line)
break if END_SECTION_REGEX.match?(line)
end
end
 
libs
libs.compact
end
 
def parse_lib(line)
line.gsub!(PREFIX_REGEX, '')
line.gsub!(self.class::PREFIX_REGEX, '')
return if line.include?(EXCLUDE_KEYWORD)
 
if line.include?(LONG_FORM_KEYWORD)
# Parse long form
name = LONG_FORM_NAME_REGEX.match(line).try(:[], 'value')
version = LONG_FORM_VERSION_REGEX.match(line).try(:[], 'value')
long_form_name_match = LONG_FORM_NAME_REGEX.match(line)
if long_form_name_match
# Parse long form (not used in Kotlin Gradle)
name = long_form_name_match[:value].delete('"\'')
version_match = LONG_FORM_VERSION_REGEX.match(line)
version = version_match[:value].delete('"\'') if version_match
else
# Parse short form
_group, name, version = line.split(':')
match = QUOTED_VALUE_REGEX.match(line)
_group, name, version = match[:value].split(':') if match
end
 
Lib.new(name: name, version: version)
name = nil if name&.include?(STRING_INTERPOLATION_CHAR)
version = nil if version&.include?(STRING_INTERPOLATION_CHAR)
Lib.new(name: name, version: version) if name
end
end
end
Loading
Loading
# frozen_string_literal: true
module Ai
module Context
module Dependencies
module ConfigFiles
### Example format:
#
# dependencies {
# // Format <group>:<name>:<version>
# implementation("org.codehaus.groovy:groovy:3.+")
# testImplementation("com.google.guava:guava:29.0.1") // Inline comment
#
# // Project, file, or other dependencies are ignored
# implementation(project(":utils"))
# runtimeOnly(files("libs/a.jar", "libs/b.jar"))
#
# // TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/481317
# // String interpolation is not currently supported; below outputs nil as version
# implementation("com.esri.arcgisruntime:arcgis-java:$arcgisVersion")
# }
#
class KotlinGradle < JavaGradle
PREFIX_REGEX = /^(implementation|testImplementation)\(/ # Appears before each dependency
def self.file_name_glob
'build.gradle.kts'
end
def self.lang_name
'Kotlin'
end
end
end
end
end
end
Loading
Loading
@@ -26,7 +26,7 @@
 
dependencies { // Comment
implementation 'org.codehaus.groovy:groovy:3.+'
testImplementation "com.google.guava:guava:29.0.1"
testImplementation "com.google.guava:guava:29.0.1" // Inline comment
"implementation" 'org.ow2.asm:asm:9.6'
 
implementation group: "org.neo4j", name: "neo4j-jmx", version: "1.3"
Loading
Loading
@@ -55,7 +55,7 @@
'neo4j-jmx (1.3)',
'junit (4.11)',
'ant (1.10.14)',
'arcgis-java ($arcgisVersion)'
'arcgis-java'
]
end
end
Loading
Loading
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ai::Context::Dependencies::ConfigFiles::KotlinGradle, feature_category: :code_suggestions do
it 'returns the expected language value' do
expect(described_class.lang).to eq('kotlin')
end
it_behaves_like 'parsing a valid dependency config file' do
let(:config_file_content) do
<<~CONTENT
buildscript {
repositories {
google()
mavenCentral()
}
}
val arcgisVersion = "4.5.0"
dependencies { // Comment
implementation("org.codehaus.groovy:groovy:3.+")
testImplementation("com.google.guava:guava:29.0.1") // Inline comment
implementation(project(":utils"))
runtimeOnly(files("libs/a.jar", "libs/b.jar"))
// String interpolation is not currently supported
implementation("com.esri.arcgisruntime:arcgis-java:$arcgisVersion")
}
java {
withSourcesJar()
}
CONTENT
end
let(:expected_formatted_lib_names) do
[
'groovy (3.+)',
'guava (29.0.1)',
'arcgis-java'
]
end
end
it_behaves_like 'parsing an invalid dependency config file'
describe '.matches?' do
using RSpec::Parameterized::TableSyntax
where(:path, :matches) do
'build.gradle.kts' | true
'dir/build.gradle.kts' | true
'dir/subdir/build.gradle.kts' | true
'dir/build.gradle' | false
'xbuild.gradle.kts' | false
'Build.gradle.kts' | false
'build_gradle_kts' | false
'build.gradle' | false
end
with_them do
it 'matches the file name glob pattern at various directory levels' do
expect(described_class.matches?(path)).to eq(matches)
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment