Skip to content

Commit 49bc16b

Browse files
authored
feat: process css and scss (#165) (#187)
* feat: process css and scss (#165) * feat: add CSSExtractor * chore: fix pr * wip: process sass extension * wip: fix pr * wip: rename file
1 parent fe61846 commit 49bc16b

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class CommonExtractor : ExtractorInterface {
2929
reversedMap.put("html", listOf("html", "htm"))
3030
reversedMap.put("j", listOf("ijs"))
3131
reversedMap.put("julia", listOf("jl"))
32-
reversedMap.put("kotlin", listOf("kt"))
3332
reversedMap.put("lisp", listOf("lisp", "lsp", "l"))
3433
reversedMap.put("lua", listOf("lua"))
3534
reversedMap.put("makefile", listOf("makefile"))
@@ -74,6 +73,7 @@ class CommonExtractor : ExtractorInterface {
7473
file
7574
} else null
7675
}
76+
7777
return super.extract(files)
7878
}
7979
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 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 CssExtractor : ExtractorInterface {
10+
companion object {
11+
val LANGUAGE_NAME = "css"
12+
val FILE_EXTS = listOf("css", "scss", "less", "sass")
13+
}
14+
15+
override fun extract(files: List<DiffFile>): List<CommitStats> {
16+
files.map { file -> file.language = LANGUAGE_NAME }
17+
val stats = FILE_EXTS.filter { it != "css" }.map { extension ->
18+
val result = files.filter { it.extension == extension }
19+
.fold(Pair(0, 0)) { total, file ->
20+
val currentNumAdded = file.getAllAdded()
21+
.filter { it.isNotBlank() }.size
22+
val currentNumDeleted = file.getAllDeleted()
23+
.filter { it.isNotBlank() }.size
24+
Pair(total.first + currentNumAdded,
25+
total.second + currentNumDeleted)}.toList()
26+
27+
CommitStats(numLinesAdded = result[0],
28+
numLinesDeleted = result[1],
29+
type = Extractor.TYPE_LIBRARY,
30+
tech = extension)
31+
}.filter { it.numLinesAdded > 0 || it.numLinesDeleted > 0 }
32+
33+
return stats + super.extract(files)
34+
}
35+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Extractor : ExtractorInterface {
3131
in ObjectiveCExtractor.FILE_EXTS -> ObjectiveCExtractor()
3232
in SwiftExtractor.FILE_EXTS -> SwiftExtractor()
3333
in KotlinExtractor.FILE_EXTS -> KotlinExtractor()
34+
in CssExtractor.FILE_EXTS -> CssExtractor()
3435
else -> CommonExtractor()
3536
}
3637
}

src/test/kotlin/test/tests/hashers/CommitHasherTest.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,54 @@ class CommitHasherTest : Spek({
294294
}
295295
}
296296

297+
given("commits with scss stats") {
298+
299+
val lines = listOf("first line in css file", "",
300+
"third line in css file")
301+
302+
val author = Author(userName, userEmail)
303+
304+
val testRepoPath = "../testrepo-extractor-"
305+
val testRepo = TestRepo(testRepoPath + "css")
306+
307+
val mockApi = MockApi(mockRepo = repo)
308+
val observable = CommitCrawler.getObservable(testRepo.git, repo)
309+
310+
it("sends stats") {
311+
for (i in 0..lines.size - 1) {
312+
val line = lines[i]
313+
val fileName = "file$i.scss"
314+
testRepo.createFile(fileName, listOf(line))
315+
testRepo.commit(message = "$line in $fileName", author = author)
316+
}
317+
318+
val errors = mutableListOf<Throwable>()
319+
320+
val rehashes = (0..lines.size - 1).map { "r$it" }
321+
322+
CommitHasher(repo, mockApi, rehashes, emails)
323+
.updateFromObservable(observable, { e -> errors.add(e) })
324+
if (errors.size > 0) {
325+
println(errors[0].message)
326+
}
327+
assertEquals(0, errors.size)
328+
329+
val syntaxStats = mockApi.receivedAddedCommits
330+
.fold(mutableListOf<CommitStats>()) { allStats, commit ->
331+
allStats.addAll(commit.stats)
332+
allStats
333+
}.filter { it.type == Extractor.TYPE_LIBRARY }
334+
335+
val scssStats = syntaxStats.filter { it.tech == "scss" }
336+
assertEquals(2, scssStats.size)
337+
assertEquals(2, scssStats.map { it.numLinesAdded }.sum())
338+
assertEquals(0, scssStats.map { it.numLinesDeleted }.sum())
339+
}
340+
341+
afterGroup {
342+
testRepo.destroy()
343+
}
344+
}
345+
297346
cleanRepos()
298347
})

0 commit comments

Comments
 (0)