Skip to content

Commit

Permalink
release: 0.1.1 (#150)
Browse files Browse the repository at this point in the history
- Add extraction of Kotlin libraries
- Add support of home symbol and relative paths for specifying repos path
  • Loading branch information
anatolystansler authored Dec 15, 2017
1 parent 5e8bb0a commit 05d076b
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ buildConfig {
buildConfigField 'String', 'PROFILE_URL', 'https://sourcerer.io/'

// App version.
buildConfigField 'int', 'VERSION_CODE', '3'
buildConfigField 'String', 'VERSION', '0.1.0'
buildConfigField 'int', 'VERSION_CODE', '4'
buildConfigField 'String', 'VERSION', '0.1.1'

// Logging.
buildConfigField 'String', 'ENV', project.hasProperty('env') ? env : 'production'
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/app/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ object Logger {
}

private fun configLevelValue() : Int {
val a = mapOf("trace" to TRACE, "debug" to DEBUG, "info" to INFO, "warn" to WARN, "error" to ERROR)
val a = mapOf("trace" to TRACE, "debug" to DEBUG, "info" to INFO,
"warn" to WARN, "error" to ERROR)
return a.getValue(BuildConfig.LOG_LEVEL)
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import app.utils.CommandConfig
import app.utils.CommandAdd
import app.utils.CommandList
import app.utils.CommandRemove
import app.utils.FileHelper.toPath
import app.utils.Options
import app.utils.PasswordHelper
import app.utils.RepoHelper
Expand Down Expand Up @@ -75,9 +76,9 @@ class Main(argv: Array<String>) {
}

private fun doAdd(commandAdd: CommandAdd) {
val path = commandAdd.path
val path = commandAdd.path?.toPath()
if (path != null && RepoHelper.isValidRepo(path)) {
val localRepo = LocalRepo(path)
val localRepo = LocalRepo(path.toString())
localRepo.hashAllContributors = commandAdd.hashAll
configurator.addLocalRepoPersistent(localRepo)
configurator.saveToFile()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/app/config/FileConfigurator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class FileConfigurator : Configurator {

try {
loadConfig = Files.newBufferedReader(FileHelper
.getPath(CONFIG_FILE_NAME)).use {
.toPath(CONFIG_FILE_NAME)).use {
mapper.readValue(it, Config::class.java)
}
} catch (e: IOException) {
Expand Down Expand Up @@ -241,7 +241,7 @@ class FileConfigurator : Configurator {
*/
override fun saveToFile() {
try {
Files.newBufferedWriter(FileHelper.getPath(CONFIG_FILE_NAME)).use {
Files.newBufferedWriter(FileHelper.toPath(CONFIG_FILE_NAME)).use {
mapper.writeValue(it, persistent)
}
} catch (e: IOException) {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/app/extractors/Extractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Extractor : ExtractorInterface {
in GoExtractor.FILE_EXTS -> GoExtractor()
in ObjectiveCExtractor.FILE_EXTS -> ObjectiveCExtractor()
in SwiftExtractor.FILE_EXTS -> SwiftExtractor()
in KotlinExtractor.FILE_EXTS -> KotlinExtractor()
else -> CommonExtractor()
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/app/extractors/JavaExtractor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class JavaExtractor : ExtractorInterface {
override fun tokenize(line: String): List<String> {
val importRegex = Regex("""^(.*import)\s[^\n]*""")
val commentRegex = Regex("""^([^\n]*//)[^\n]*""")
val packageRegex = Regex("""^(.*package)\s[^\n]*""")
var newLine = importRegex.replace(line, "")
newLine = commentRegex.replace(newLine, "")
newLine = packageRegex.replace(newLine, "")
return super.tokenize(newLine)
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/kotlin/app/extractors/KotlinExtractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2017 Sourcerer Inc. All Rights Reserved.
// Author: Liubov Yaronskaya ([email protected])

package app.extractors

import app.model.CommitStats
import app.model.DiffFile

class KotlinExtractor : ExtractorInterface {
companion object {
val LANGUAGE_NAME = "kotlin"
val FILE_EXTS = listOf("kt")
val LIBRARIES = ExtractorInterface.getLibraries(LANGUAGE_NAME)
val evaluator by lazy {
ExtractorInterface.getLibraryClassifier(LANGUAGE_NAME)
}
}

override fun extract(files: List<DiffFile>): List<CommitStats> {
files.map { file -> file.language = LANGUAGE_NAME }
return super.extract(files)
}

override fun extractImports(fileContent: List<String>): List<String> {
val imports = mutableSetOf<String>()

val regex = Regex("""import\s+(\w+[.\w+]*)""")
fileContent.forEach {
val res = regex.find(it)
if (res != null) {
val importedName = res.groupValues[1]
LIBRARIES.forEach { library ->
if (importedName.startsWith(library)) {
imports.add(library)
}
}
}
}

return imports.toList()
}

override fun tokenize(line: String): List<String> {
val importRegex = Regex("""^(.*import)\s[^\n]*""")
val commentRegex = Regex("""^([^\n]*//)[^\n]*""")
val packageRegex = Regex("""^(.*package)\s[^\n]*""")
var newLine = importRegex.replace(line, "")
newLine = commentRegex.replace(newLine, "")
newLine = packageRegex.replace(newLine, "")
return super.tokenize(newLine)
}

override fun getLineLibraries(line: String,
fileLibraries: List<String>): List<String> {

return super.getLineLibraries(line, fileLibraries, evaluator, LANGUAGE_NAME)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/app/hashers/CodeLongevity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class CodeLongevity(private val serverRepo: Repo,
catch(e: Exception) { throw Exception("No branch") }

val df = DiffFormatter(DisabledOutputStream.INSTANCE)
val dataPath = FileHelper.getPath(serverRepo.rehash, "longevity")
val dataPath = FileHelper.toPath(serverRepo.rehash, "longevity")

init {
df.setRepository(repo)
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/app/hashers/RepoHasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import app.config.Configurator
import app.model.Author
import app.model.LocalRepo
import app.model.Repo
import app.utils.FileHelper.toPath
import app.utils.HashingException
import app.utils.RepoHelper
import org.eclipse.jgit.api.Git
Expand All @@ -17,11 +18,11 @@ import java.io.IOException
import kotlin.collections.HashSet

class RepoHasher(private val localRepo: LocalRepo, private val api: Api,
private val configurator: Configurator) {
private val configurator: Configurator) {
var serverRepo: Repo = Repo()

init {
if (!RepoHelper.isValidRepo(localRepo.path)) {
if (!RepoHelper.isValidRepo(localRepo.path.toPath())) {
throw IllegalArgumentException("Invalid repo $localRepo")
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/app/ui/AddRepoState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import app.Logger
import app.api.Api
import app.config.Configurator
import app.model.LocalRepo
import app.utils.FileHelper.toPath
import app.utils.RepoHelper
import app.utils.UiHelper

Expand All @@ -28,12 +29,13 @@ class AddRepoState constructor(private val context: Context,
if (configurator.getLocalRepos().isEmpty()) {
Logger.print("Add at least one valid repository.")
} else {
break // User finished to add repos.
break // User finished to add repos.
}
} else {
if (RepoHelper.isValidRepo(pathString)) {
Logger.print("Added git repository at $pathString.")
val localRepo = LocalRepo(pathString)
val path = pathString.toPath()
if (RepoHelper.isValidRepo(path)) {
Logger.print("Added git repository at $path.")
val localRepo = LocalRepo(path.toString())
localRepo.hashAllContributors = UiHelper.confirm("Do you "
+ "want to hash commits of all contributors?",
defaultIsYes = true)
Expand Down
16 changes: 13 additions & 3 deletions src/main/kotlin/app/utils/FileHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package app.utils

import app.Logger
import java.io.File
import java.net.URLDecoder
import java.nio.file.Files
Expand All @@ -17,7 +18,7 @@ object FileHelper {
private val jarPath = getJarPath()
private val settingsPath = jarPath.resolve(dirName)

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

fun getFile(name: String, vararg parts: String): File {
return getPath(name, *parts).toFile()
return toPath(name, *parts).toFile()
}

fun notExists(name:String, vararg parts: String): Boolean {
return Files.notExists(getPath(name, *parts))
return Files.notExists(toPath(name, *parts))
}

fun getFileExtension(path: String): String {
Expand All @@ -47,4 +48,13 @@ object FileHelper {
// Removing jar filename.
return root.resolve(fullPath.subpath(0, fullPath.nameCount - 1))
}

fun String.toPath(): Path {
val substitutePath = if (this.startsWith("~" + File.separator)) {
System.getProperty("user.home") + this.substring(1)
} else { this }
val pathTemp = Paths.get(substitutePath).toAbsolutePath().normalize()
println(pathTemp.toString())
return pathTemp
}
}
9 changes: 5 additions & 4 deletions src/main/kotlin/app/utils/RepoHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.Repository
import java.io.File
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.nio.file.Paths

/**
* Class for utility functions on repos.
*/
object RepoHelper {
fun isValidRepo(path: String): Boolean {
fun isValidRepo(path: Path): Boolean {
if (!isDirectory(path)) {
return false
}
Expand All @@ -27,7 +28,7 @@ object RepoHelper {
var repository: Repository? = null
val commitId: ObjectId?
try {
git = Git.open(File(path))
git = Git.open(path.toFile())
repository = git.repository
commitId = CommitCrawler.getDefaultBranchHead(git)
} catch (e: Exception) {
Expand All @@ -44,9 +45,9 @@ object RepoHelper {
return false
}

fun isDirectory(path: String): Boolean {
fun isDirectory(path: Path): Boolean {
return try {
Paths.get(path).toFile().isDirectory
path.toFile().isDirectory
} catch (e: InvalidPathException) {
Logger.error(e, "Invalid path")
false
Expand Down
56 changes: 56 additions & 0 deletions src/main/resources/data/libraries/kotlin_libraries.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
com.winterbe.expekt
com.github.vassilibykov.adventkt
me.lazmaid.kraph
com.github.kittinunf.fuel
imgui
org.mapdb
uy.kohesive.kovert
io.reactivex.rxkotlin
com.compass.snail
io.kweb
com.github.andrewoma.kwery
com.github.pgutkowski.kgraphql
io.vertx
org.spekframework.spek2
com.oneeyedmen.konsent
com.github.salomonbrys.kotson
org.jire.kton
io.kotlintest
ktx
org.kotlinprimavera
org.kottpd
com.almasb.fxgl
org.wasabifx.wasabi
com.fboldog.ext4klaxon
net.yested
ua.com.lavi.komock
kotlinx.nosql
spark
com.github.kittinunf.result
com.nivabit.kuery
io.thelandscape.krawler
io.tekniq
com.beust.klaxon
com.github.shyiko.levelkt
io.javalin
kategory
tornadofx
kotliquery
khttp
ovr
com.hexagonkt
com.nhaarman.mockito_kotlin
io.ktor
com.codepoetics.klenses
io.polymorphicpanda.kspec
com.natpryce.hamkrest
org.http4k
com.vaadin
io.mockk
kara
org.funktionale
com.github.fluidsonic.fluid.json
com.squareup.sqldelight
org.amshove.kluent
com.danneu.kog
org.jetbrains.exposed
19 changes: 19 additions & 0 deletions src/test/kotlin/test/tests/extractors/ExtractorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class ExtractorTest : Spek({
assertExtractsLineLibraries("grpc",
line, CExtractor())
}

it("kotlin extractor extracts the library") {
val line = "FuelManager.instance.apply {"
assertExtractsLineLibraries("com.github.kittinunf.fuel",
line, KotlinExtractor())
}
}

given("code line doesn't use libraries" ) {
Expand Down Expand Up @@ -161,6 +167,11 @@ class ExtractorTest : Spek({
val line = "int main(int argc, char **argv) {"
assertExtractsNoLibraries(line, CExtractor())
}

it("kotlin extractor returns empty list") {
val line = "val password = \"P@\$\\\$vv0|2|)\""
assertExtractsNoLibraries(line, KotlinExtractor())
}
}

given("import name.h") {
Expand All @@ -179,6 +190,14 @@ class ExtractorTest : Spek({
}
}

given("line contains import") {
it("kotlin extractor extracts import") {
val line = "import kategory.optics.*"
val lib = "kategory"
assertExtractsImport(lib, line, KotlinExtractor())
}
}

given("import cv2 or cv") {
it("imports opencv") {
val lib = "opencv"
Expand Down
Loading

0 comments on commit 05d076b

Please sign in to comment.