Skip to content

Commit c83679c

Browse files
authored
feat: pack resources into jar (#37)
* feat: pack resources into jar * chore: edit comment
1 parent efe0f5a commit c83679c

16 files changed

+50
-55
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ class CExtractor : ExtractorInterface {
1919
}
2020

2121
override fun extractImports(fileContent: List<String>): List<String> {
22-
val libraries = mutableSetOf<String>()
22+
val imports = mutableSetOf<String>()
2323

2424
val regex = Regex("""#include\s+["<](\w+)[/\w+]*\.\w+[">]""")
2525
fileContent.forEach {
2626
val res = regex.find(it)
2727
if (res != null) {
2828
val lineLib = res.groupValues.last()
29-
libraries.add(lineLib)
29+
imports.add(lineLib)
3030
}
3131
}
3232

33-
return libraries.toList()
33+
return imports.toList()
3434
}
3535
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class CSharpExtractor : ExtractorInterface {
1212
companion object {
1313
val LANGUAGE_NAME = "cs"
1414
val FILE_EXTS = listOf("cs")
15+
val LIBRARIES = ExtractorInterface.getLibraries("cs")
1516
}
1617

1718
override fun extract(files: List<DiffFile>): List<CommitStats> {
@@ -20,27 +21,21 @@ class CSharpExtractor : ExtractorInterface {
2021
}
2122

2223
override fun extractImports(fileContent: List<String>): List<String> {
23-
val libraries = mutableSetOf<String>()
24-
25-
// TODO(anatoly): Load file statically.
26-
val csLibraries = File("data/libraries/cs_libraries.txt")
27-
.inputStream().bufferedReader()
28-
.readLines()
29-
.toSet()
24+
val imports = mutableSetOf<String>()
3025

3126
val regex = Regex("""using\s+(\w+[.\w+]*)""")
3227
fileContent.forEach {
3328
val res = regex.find(it)
3429
if (res != null) {
3530
val importedName = res.groupValues[1]
36-
csLibraries.forEach { library ->
31+
LIBRARIES.forEach { library ->
3732
if (importedName.startsWith(library)) {
38-
libraries.add(library)
33+
imports.add(library)
3934
}
4035
}
4136
}
4237
}
4338

44-
return libraries.toList()
39+
return imports.toList()
4540
}
4641
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ class CppExtractor : ExtractorInterface {
1919
}
2020

2121
override fun extractImports(fileContent: List<String>): List<String> {
22-
val libraries = mutableSetOf<String>()
22+
val imports = mutableSetOf<String>()
2323

2424
val regex = Regex("""#include\s+["<](\w+)[/\w+]*\.\w+[">]""")
2525
fileContent.forEach {
2626
val res = regex.find(it)
2727
if (res != null) {
2828
val lineLib = res.groupValues.last()
29-
libraries.add(lineLib)
29+
imports.add(lineLib)
3030
}
3131
}
3232

33-
return libraries.toList()
33+
return imports.toList()
3434
}
3535
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import app.model.DiffFile
88
import app.model.CommitStats
99

1010
interface ExtractorInterface {
11+
companion object {
12+
fun getLibraries(name: String): Set<String> {
13+
return ExtractorInterface::class.java.classLoader
14+
.getResourceAsStream("data/libraries/${name}_libraries.txt")
15+
.bufferedReader().readLines().toSet()
16+
}
17+
}
18+
1119
fun extract(files: List<DiffFile>): List<CommitStats> {
1220
files.map { file ->
1321
file.old.imports = extractImports(file.old.content)
@@ -29,4 +37,6 @@ interface ExtractorInterface {
2937
fun extractImports(fileContent: List<String>): List<String> {
3038
return listOf()
3139
}
40+
41+
3242
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ class GoExtractor : ExtractorInterface {
1919
}
2020

2121
override fun extractImports(fileContent: List<String>): List<String> {
22-
val libraries = mutableSetOf<String>()
22+
val imports = mutableSetOf<String>()
2323

2424
val singleImportRegex = Regex("""import\s+"(\w+)"""")
2525
fileContent.forEach {
2626
val res = singleImportRegex.find(it)
2727
if (res != null) {
2828
val lineLib = res.groupValues.last()
29-
libraries.add(lineLib)
29+
imports.add(lineLib)
3030
}
3131
}
3232
val multipleImportRegex = Regex("""import[\s\t\n]+\((.+?)\)""",
3333
RegexOption.DOT_MATCHES_ALL)
3434
val contentJoined = fileContent.joinToString(separator = "")
3535
multipleImportRegex.findAll(contentJoined).forEach { matchResult ->
36-
libraries.addAll(matchResult.groupValues.last()
36+
imports.addAll(matchResult.groupValues.last()
3737
.split(Regex("""(\t+|\n+|\s+)"""))
3838
.filter { it.isNotEmpty() }
3939
.map { it -> it.replace("\"", "") })
4040
}
4141

42-
return libraries.toList()
42+
return imports.toList()
4343
}
4444
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class JavaExtractor : ExtractorInterface {
1212
companion object {
1313
val LANGUAGE_NAME = "java"
1414
val FILE_EXTS = listOf("java")
15+
val LIBRARIES = ExtractorInterface.getLibraries("java")
1516
}
1617

1718
val KEYWORDS = listOf("abstract", "continue", "for", "new", "switch",
@@ -56,27 +57,21 @@ class JavaExtractor : ExtractorInterface {
5657
}
5758

5859
override fun extractImports(fileContent: List<String>): List<String> {
59-
val libraries = mutableSetOf<String>()
60-
61-
// TODO(anatoly): Load file statically.
62-
val javaLibraries = File("data/libraries/java_libraries.txt")
63-
.inputStream().bufferedReader()
64-
.readLines()
65-
.toSet()
60+
val imports = mutableSetOf<String>()
6661

6762
val regex = Regex("""import\s+(\w+[.\w+]*)""")
6863
fileContent.forEach {
6964
val res = regex.find(it)
7065
if (res != null) {
7166
val importedName = res.groupValues[1]
72-
javaLibraries.forEach { library ->
67+
LIBRARIES.forEach { library ->
7368
if (importedName.startsWith(library)) {
74-
libraries.add(library)
69+
imports.add(library)
7570
}
7671
}
7772
}
7873
}
7974

80-
return libraries.toList()
75+
return imports.toList()
8176
}
8277
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ package app.extractors
66

77
import app.model.CommitStats
88
import app.model.DiffFile
9-
import java.io.File
109

1110
class JavascriptExtractor : ExtractorInterface {
1211
companion object {
1312
val LANGUAGE_NAME = "js"
1413
val FILE_EXTS = listOf("js")
14+
val LIBRARIES = ExtractorInterface.getLibraries("js")
1515
}
1616

1717
override fun extract(files: List<DiffFile>): List<CommitStats> {
@@ -20,20 +20,14 @@ class JavascriptExtractor : ExtractorInterface {
2020
}
2121

2222
override fun extractImports(fileContent: List<String>): List<String> {
23-
val libraries = mutableSetOf<String>()
24-
25-
// TODO(anatoly): Load file statically.
26-
val jsLibraries = File("data/libraries/js_libraries.txt")
27-
.inputStream().bufferedReader()
28-
.readLines()
29-
.toSet()
23+
val imports = mutableSetOf<String>()
3024

3125
val splitRegex =
3226
Regex("""\s+|,|;|:|\\*|\n|\(|\)|\\[|]|\{|}|\+|=|\.|>|<|#|@|\$""")
3327
val fileTokens = fileContent.joinToString(separator = " ")
3428
.split(splitRegex)
35-
libraries.addAll(fileTokens.filter { token -> token in jsLibraries })
29+
imports.addAll(fileTokens.filter { token -> token in LIBRARIES })
3630

37-
return libraries.toList()
31+
return imports.toList()
3832
}
3933
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ObjectiveCExtractor : ExtractorInterface {
1919
}
2020

2121
override fun extractImports(fileContent: List<String>): List<String> {
22-
val libraries = mutableSetOf<String>()
22+
val imports = mutableSetOf<String>()
2323

2424
val sharpImportIncludeRegex =
2525
Regex("""#(import|include)\s+[">](\w+)[/\w+]*\.\w+[">]""")
@@ -30,10 +30,10 @@ class ObjectiveCExtractor : ExtractorInterface {
3030
atImportRegex.findAll(it)
3131
if (res.toList().isNotEmpty()) {
3232
val lineLib = res.toList().map { it.groupValues }.last().last()
33-
libraries.add(lineLib)
33+
imports.add(lineLib)
3434
}
3535
}
3636

37-
return libraries.toList()
37+
return imports.toList()
3838
}
3939
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PhpExtractor : ExtractorInterface {
1919
}
2020

2121
override fun extractImports(fileContent: List<String>): List<String> {
22-
val libraries = mutableSetOf<String>()
22+
val imports = mutableSetOf<String>()
2323

2424
val useRegex = Regex("""use\s+(\w+)[\\\w+]*""")
2525
val requireIncludeRegex = Regex("""(require|require_once|include|""" +
@@ -28,10 +28,10 @@ class PhpExtractor : ExtractorInterface {
2828
val res = useRegex.findAll(it) + requireIncludeRegex.findAll(it)
2929
if (res.toList().isNotEmpty()) {
3030
val lineLib = res.toList().map { it.groupValues }.last().last()
31-
libraries.add(lineLib)
31+
imports.add(lineLib)
3232
}
3333
}
3434

35-
return libraries.toList()
35+
return imports.toList()
3636
}
3737
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PythonExtractor : ExtractorInterface {
1919
}
2020

2121
override fun extractImports(fileContent: List<String>): List<String> {
22-
val libraries = mutableSetOf<String>()
22+
val imports = mutableSetOf<String>()
2323

2424
val regex =
2525
Regex("""(from\s+(\w+)[.\w+]*\s+import|import\s+(\w+[,\s*\w+]*))""")
@@ -28,10 +28,10 @@ class PythonExtractor : ExtractorInterface {
2828
if (res != null) {
2929
val lineLibs = res.groupValues.last { it != "" }
3030
.split(Regex(""",\s*"""))
31-
libraries.addAll(lineLibs)
31+
imports.addAll(lineLibs)
3232
}
3333
}
3434

35-
return libraries.toList()
35+
return imports.toList()
3636
}
3737
}

0 commit comments

Comments
 (0)