Skip to content

Commit 05d076b

Browse files
release: 0.1.1 (#150)
- Add extraction of Kotlin libraries - Add support of home symbol and relative paths for specifying repos path
1 parent 5e8bb0a commit 05d076b

File tree

15 files changed

+200
-21
lines changed

15 files changed

+200
-21
lines changed

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ buildConfig {
3636
buildConfigField 'String', 'PROFILE_URL', 'https://sourcerer.io/'
3737

3838
// App version.
39-
buildConfigField 'int', 'VERSION_CODE', '3'
40-
buildConfigField 'String', 'VERSION', '0.1.0'
39+
buildConfigField 'int', 'VERSION_CODE', '4'
40+
buildConfigField 'String', 'VERSION', '0.1.1'
4141

4242
// Logging.
4343
buildConfigField 'String', 'ENV', project.hasProperty('env') ? env : 'production'

src/main/kotlin/app/Logger.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ object Logger {
9393
}
9494

9595
private fun configLevelValue() : Int {
96-
val a = mapOf("trace" to TRACE, "debug" to DEBUG, "info" to INFO, "warn" to WARN, "error" to ERROR)
96+
val a = mapOf("trace" to TRACE, "debug" to DEBUG, "info" to INFO,
97+
"warn" to WARN, "error" to ERROR)
9798
return a.getValue(BuildConfig.LOG_LEVEL)
9899
}
99100

src/main/kotlin/app/Main.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import app.utils.CommandConfig
1111
import app.utils.CommandAdd
1212
import app.utils.CommandList
1313
import app.utils.CommandRemove
14+
import app.utils.FileHelper.toPath
1415
import app.utils.Options
1516
import app.utils.PasswordHelper
1617
import app.utils.RepoHelper
@@ -75,9 +76,9 @@ class Main(argv: Array<String>) {
7576
}
7677

7778
private fun doAdd(commandAdd: CommandAdd) {
78-
val path = commandAdd.path
79+
val path = commandAdd.path?.toPath()
7980
if (path != null && RepoHelper.isValidRepo(path)) {
80-
val localRepo = LocalRepo(path)
81+
val localRepo = LocalRepo(path.toString())
8182
localRepo.hashAllContributors = commandAdd.hashAll
8283
configurator.addLocalRepoPersistent(localRepo)
8384
configurator.saveToFile()

src/main/kotlin/app/config/FileConfigurator.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class FileConfigurator : Configurator {
212212

213213
try {
214214
loadConfig = Files.newBufferedReader(FileHelper
215-
.getPath(CONFIG_FILE_NAME)).use {
215+
.toPath(CONFIG_FILE_NAME)).use {
216216
mapper.readValue(it, Config::class.java)
217217
}
218218
} catch (e: IOException) {
@@ -241,7 +241,7 @@ class FileConfigurator : Configurator {
241241
*/
242242
override fun saveToFile() {
243243
try {
244-
Files.newBufferedWriter(FileHelper.getPath(CONFIG_FILE_NAME)).use {
244+
Files.newBufferedWriter(FileHelper.toPath(CONFIG_FILE_NAME)).use {
245245
mapper.writeValue(it, persistent)
246246
}
247247
} catch (e: IOException) {

src/main/kotlin/app/extractors/Extractor.kt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Extractor : ExtractorInterface {
2828
in GoExtractor.FILE_EXTS -> GoExtractor()
2929
in ObjectiveCExtractor.FILE_EXTS -> ObjectiveCExtractor()
3030
in SwiftExtractor.FILE_EXTS -> SwiftExtractor()
31+
in KotlinExtractor.FILE_EXTS -> KotlinExtractor()
3132
else -> CommonExtractor()
3233
}
3334
}

src/main/kotlin/app/extractors/JavaExtractor.kt

+2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ class JavaExtractor : ExtractorInterface {
7979
override fun tokenize(line: String): List<String> {
8080
val importRegex = Regex("""^(.*import)\s[^\n]*""")
8181
val commentRegex = Regex("""^([^\n]*//)[^\n]*""")
82+
val packageRegex = Regex("""^(.*package)\s[^\n]*""")
8283
var newLine = importRegex.replace(line, "")
8384
newLine = commentRegex.replace(newLine, "")
85+
newLine = packageRegex.replace(newLine, "")
8486
return super.tokenize(newLine)
8587
}
8688

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2017 Sourcerer Inc. All Rights Reserved.
2+
// Author: Liubov Yaronskaya ([email protected])
3+
4+
package app.extractors
5+
6+
import app.model.CommitStats
7+
import app.model.DiffFile
8+
9+
class KotlinExtractor : ExtractorInterface {
10+
companion object {
11+
val LANGUAGE_NAME = "kotlin"
12+
val FILE_EXTS = listOf("kt")
13+
val LIBRARIES = ExtractorInterface.getLibraries(LANGUAGE_NAME)
14+
val evaluator by lazy {
15+
ExtractorInterface.getLibraryClassifier(LANGUAGE_NAME)
16+
}
17+
}
18+
19+
override fun extract(files: List<DiffFile>): List<CommitStats> {
20+
files.map { file -> file.language = LANGUAGE_NAME }
21+
return super.extract(files)
22+
}
23+
24+
override fun extractImports(fileContent: List<String>): List<String> {
25+
val imports = mutableSetOf<String>()
26+
27+
val regex = Regex("""import\s+(\w+[.\w+]*)""")
28+
fileContent.forEach {
29+
val res = regex.find(it)
30+
if (res != null) {
31+
val importedName = res.groupValues[1]
32+
LIBRARIES.forEach { library ->
33+
if (importedName.startsWith(library)) {
34+
imports.add(library)
35+
}
36+
}
37+
}
38+
}
39+
40+
return imports.toList()
41+
}
42+
43+
override fun tokenize(line: String): List<String> {
44+
val importRegex = Regex("""^(.*import)\s[^\n]*""")
45+
val commentRegex = Regex("""^([^\n]*//)[^\n]*""")
46+
val packageRegex = Regex("""^(.*package)\s[^\n]*""")
47+
var newLine = importRegex.replace(line, "")
48+
newLine = commentRegex.replace(newLine, "")
49+
newLine = packageRegex.replace(newLine, "")
50+
return super.tokenize(newLine)
51+
}
52+
53+
override fun getLineLibraries(line: String,
54+
fileLibraries: List<String>): List<String> {
55+
56+
return super.getLineLibraries(line, fileLibraries, evaluator, LANGUAGE_NAME)
57+
}
58+
}

src/main/kotlin/app/hashers/CodeLongevity.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class CodeLongevity(private val serverRepo: Repo,
151151
catch(e: Exception) { throw Exception("No branch") }
152152

153153
val df = DiffFormatter(DisabledOutputStream.INSTANCE)
154-
val dataPath = FileHelper.getPath(serverRepo.rehash, "longevity")
154+
val dataPath = FileHelper.toPath(serverRepo.rehash, "longevity")
155155

156156
init {
157157
df.setRepository(repo)

src/main/kotlin/app/hashers/RepoHasher.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import app.config.Configurator
99
import app.model.Author
1010
import app.model.LocalRepo
1111
import app.model.Repo
12+
import app.utils.FileHelper.toPath
1213
import app.utils.HashingException
1314
import app.utils.RepoHelper
1415
import org.eclipse.jgit.api.Git
@@ -17,11 +18,11 @@ import java.io.IOException
1718
import kotlin.collections.HashSet
1819

1920
class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
20-
private val configurator: Configurator) {
21+
private val configurator: Configurator) {
2122
var serverRepo: Repo = Repo()
2223

2324
init {
24-
if (!RepoHelper.isValidRepo(localRepo.path)) {
25+
if (!RepoHelper.isValidRepo(localRepo.path.toPath())) {
2526
throw IllegalArgumentException("Invalid repo $localRepo")
2627
}
2728
}

src/main/kotlin/app/ui/AddRepoState.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import app.Logger
77
import app.api.Api
88
import app.config.Configurator
99
import app.model.LocalRepo
10+
import app.utils.FileHelper.toPath
1011
import app.utils.RepoHelper
1112
import app.utils.UiHelper
1213

@@ -28,12 +29,13 @@ class AddRepoState constructor(private val context: Context,
2829
if (configurator.getLocalRepos().isEmpty()) {
2930
Logger.print("Add at least one valid repository.")
3031
} else {
31-
break // User finished to add repos.
32+
break // User finished to add repos.
3233
}
3334
} else {
34-
if (RepoHelper.isValidRepo(pathString)) {
35-
Logger.print("Added git repository at $pathString.")
36-
val localRepo = LocalRepo(pathString)
35+
val path = pathString.toPath()
36+
if (RepoHelper.isValidRepo(path)) {
37+
Logger.print("Added git repository at $path.")
38+
val localRepo = LocalRepo(path.toString())
3739
localRepo.hashAllContributors = UiHelper.confirm("Do you "
3840
+ "want to hash commits of all contributors?",
3941
defaultIsYes = true)

src/main/kotlin/app/utils/FileHelper.kt

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package app.utils
55

6+
import app.Logger
67
import java.io.File
78
import java.net.URLDecoder
89
import java.nio.file.Files
@@ -17,7 +18,7 @@ object FileHelper {
1718
private val jarPath = getJarPath()
1819
private val settingsPath = jarPath.resolve(dirName)
1920

20-
fun getPath(name: String, vararg parts: String): Path {
21+
fun toPath(name: String, vararg parts: String): Path {
2122
val path = settingsPath.resolve(Paths.get("", *parts))
2223
if (Files.notExists(path)) {
2324
Files.createDirectories(path)
@@ -26,11 +27,11 @@ object FileHelper {
2627
}
2728

2829
fun getFile(name: String, vararg parts: String): File {
29-
return getPath(name, *parts).toFile()
30+
return toPath(name, *parts).toFile()
3031
}
3132

3233
fun notExists(name:String, vararg parts: String): Boolean {
33-
return Files.notExists(getPath(name, *parts))
34+
return Files.notExists(toPath(name, *parts))
3435
}
3536

3637
fun getFileExtension(path: String): String {
@@ -47,4 +48,13 @@ object FileHelper {
4748
// Removing jar filename.
4849
return root.resolve(fullPath.subpath(0, fullPath.nameCount - 1))
4950
}
51+
52+
fun String.toPath(): Path {
53+
val substitutePath = if (this.startsWith("~" + File.separator)) {
54+
System.getProperty("user.home") + this.substring(1)
55+
} else { this }
56+
val pathTemp = Paths.get(substitutePath).toAbsolutePath().normalize()
57+
println(pathTemp.toString())
58+
return pathTemp
59+
}
5060
}

src/main/kotlin/app/utils/RepoHelper.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import org.eclipse.jgit.lib.ObjectId
1212
import org.eclipse.jgit.lib.Repository
1313
import java.io.File
1414
import java.nio.file.InvalidPathException
15+
import java.nio.file.Path
1516
import java.nio.file.Paths
1617

1718
/**
1819
* Class for utility functions on repos.
1920
*/
2021
object RepoHelper {
21-
fun isValidRepo(path: String): Boolean {
22+
fun isValidRepo(path: Path): Boolean {
2223
if (!isDirectory(path)) {
2324
return false
2425
}
@@ -27,7 +28,7 @@ object RepoHelper {
2728
var repository: Repository? = null
2829
val commitId: ObjectId?
2930
try {
30-
git = Git.open(File(path))
31+
git = Git.open(path.toFile())
3132
repository = git.repository
3233
commitId = CommitCrawler.getDefaultBranchHead(git)
3334
} catch (e: Exception) {
@@ -44,9 +45,9 @@ object RepoHelper {
4445
return false
4546
}
4647

47-
fun isDirectory(path: String): Boolean {
48+
fun isDirectory(path: Path): Boolean {
4849
return try {
49-
Paths.get(path).toFile().isDirectory
50+
path.toFile().isDirectory
5051
} catch (e: InvalidPathException) {
5152
Logger.error(e, "Invalid path")
5253
false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
com.winterbe.expekt
2+
com.github.vassilibykov.adventkt
3+
me.lazmaid.kraph
4+
com.github.kittinunf.fuel
5+
imgui
6+
org.mapdb
7+
uy.kohesive.kovert
8+
io.reactivex.rxkotlin
9+
com.compass.snail
10+
io.kweb
11+
com.github.andrewoma.kwery
12+
com.github.pgutkowski.kgraphql
13+
io.vertx
14+
org.spekframework.spek2
15+
com.oneeyedmen.konsent
16+
com.github.salomonbrys.kotson
17+
org.jire.kton
18+
io.kotlintest
19+
ktx
20+
org.kotlinprimavera
21+
org.kottpd
22+
com.almasb.fxgl
23+
org.wasabifx.wasabi
24+
com.fboldog.ext4klaxon
25+
net.yested
26+
ua.com.lavi.komock
27+
kotlinx.nosql
28+
spark
29+
com.github.kittinunf.result
30+
com.nivabit.kuery
31+
io.thelandscape.krawler
32+
io.tekniq
33+
com.beust.klaxon
34+
com.github.shyiko.levelkt
35+
io.javalin
36+
kategory
37+
tornadofx
38+
kotliquery
39+
khttp
40+
ovr
41+
com.hexagonkt
42+
com.nhaarman.mockito_kotlin
43+
io.ktor
44+
com.codepoetics.klenses
45+
io.polymorphicpanda.kspec
46+
com.natpryce.hamkrest
47+
org.http4k
48+
com.vaadin
49+
io.mockk
50+
kara
51+
org.funktionale
52+
com.github.fluidsonic.fluid.json
53+
com.squareup.sqldelight
54+
org.amshove.kluent
55+
com.danneu.kog
56+
org.jetbrains.exposed

src/test/kotlin/test/tests/extractors/ExtractorTest.kt

+19
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ class ExtractorTest : Spek({
104104
assertExtractsLineLibraries("grpc",
105105
line, CExtractor())
106106
}
107+
108+
it("kotlin extractor extracts the library") {
109+
val line = "FuelManager.instance.apply {"
110+
assertExtractsLineLibraries("com.github.kittinunf.fuel",
111+
line, KotlinExtractor())
112+
}
107113
}
108114

109115
given("code line doesn't use libraries" ) {
@@ -161,6 +167,11 @@ class ExtractorTest : Spek({
161167
val line = "int main(int argc, char **argv) {"
162168
assertExtractsNoLibraries(line, CExtractor())
163169
}
170+
171+
it("kotlin extractor returns empty list") {
172+
val line = "val password = \"P@\$\\\$vv0|2|)\""
173+
assertExtractsNoLibraries(line, KotlinExtractor())
174+
}
164175
}
165176

166177
given("import name.h") {
@@ -179,6 +190,14 @@ class ExtractorTest : Spek({
179190
}
180191
}
181192

193+
given("line contains import") {
194+
it("kotlin extractor extracts import") {
195+
val line = "import kategory.optics.*"
196+
val lib = "kategory"
197+
assertExtractsImport(lib, line, KotlinExtractor())
198+
}
199+
}
200+
182201
given("import cv2 or cv") {
183202
it("imports opencv") {
184203
val lib = "opencv"

0 commit comments

Comments
 (0)