Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
feat(sourcecode): add for gbk count lines support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 25, 2022
1 parent 120e282 commit 42005b8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
@@ -1,24 +1,30 @@
package com.thoughtworks.archguard.git.scanner.model

import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.isReadable

val TEN_MB = 10 * 1024 * 1024

private const val ONE_KB = 1024
private const val TEN_MB = 10 * 1024 * 1024

class LineCounter {
companion object {
fun byPath(path: String): Long {
val file = File(path).toPath()
if(!file.isReadable()) {
if (!file.isReadable()) {
println("Error path: $path")
return 0
}

return try {
val size = Files.size(file)
if (size < TEN_MB) {
Files.lines(file).count()
tryCountLines(file, size)
} else {
0
}
Expand All @@ -28,5 +34,19 @@ class LineCounter {
0
}
}

private fun tryCountLines(file: Path, size: Long): Long {
var bufferedReader = BufferedReader(InputStreamReader(FileInputStream(file.toFile()), "utf-8"))
var count = bufferedReader.lines().count()

val mayBeGbkEncoding = (count == 0L) && (size > ONE_KB)
if (mayBeGbkEncoding) {
bufferedReader = BufferedReader(InputStreamReader(FileInputStream(file.toFile()), "GBK"))
count = bufferedReader.lines().count()
}

return count

}
}
}
@@ -0,0 +1,15 @@
package com.thoughtworks.archguard.git.scanner.model

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.nio.file.Paths

internal class LineCounterTest {
@Test
internal fun testForGbkFile() {
val resource = this.javaClass.classLoader.getResource("gbkfiles/hello.go")!!
val path = Paths.get(resource.toURI()).toFile().absolutePath

assertEquals(9, LineCounter.byPath(path))
}
}
Expand Up @@ -5,7 +5,6 @@ import chapi.app.analyser.config.ChapiConfig
import chapi.domain.core.CodeDataStruct
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.thoughtworks.archguard.scanner.sourcecode.backend.CSharpApiAnalyser
import com.thoughtworks.archguard.scanner.sourcecode.frontend.FrontendApiAnalyser
Expand Down
Expand Up @@ -8,7 +8,7 @@ import com.thoughtworks.archguard.scanner.sourcecode.frontend.ContainerService
class CSharpApiAnalyser {
var resources: List<ContainerResource> = listOf()

fun analysisByNode(node: CodeDataStruct, workspace: String) {
fun analysisByNode(node: CodeDataStruct, _workspace: String) {
val routeAnnotation = node.filterAnnotations("RoutePrefix", "Route")
if (routeAnnotation.isNotEmpty() || node.NodeName.endsWith("Controller")) {
var baseUrl = ""
Expand Down

0 comments on commit 42005b8

Please sign in to comment.