Skip to content

Commit

Permalink
feat: init basic worker for language
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 6, 2023
1 parent 2193b31 commit 811a47f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 48 deletions.
28 changes: 12 additions & 16 deletions unit-picker/src/main/kotlin/cc/unitmesh/pick/picker/CodePicker.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package cc.unitmesh.pick.picker

import cc.unitmesh.pick.worker.WorkerDispatch
import cc.unitmesh.pick.worker.WorkerManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.archguard.action.checkout.GitSourceSettings
import org.archguard.action.checkout.executeGitCheckout
import org.archguard.rule.common.Language
import org.archguard.scanner.analyser.count.FileJob
import org.archguard.scanner.analyser.count.LanguageWorker
import java.nio.file.Files
import java.nio.file.Path

Expand All @@ -22,7 +24,9 @@ class CodePicker(private val config: PickerConfig) {

logger.info("start picker")

val walkdirChannel = Channel<PickJob>()
val languageWorker = LanguageWorker()
val workerManager = WorkerManager()
val walkdirChannel = Channel<FileJob>()

launch {
launch {
Expand All @@ -31,10 +35,14 @@ class CodePicker(private val config: PickerConfig) {
}
launch {
for (fileJob in walkdirChannel) {
val lang: Language? = fileJob.language.toSupportLanguage()
languageWorker.processFile(fileJob)?.let {
workerManager.addJob(PickJob.from(it))
}
}

workerManager.runAll()
}
}
}.join()

logger.info("stop picker")

Expand Down Expand Up @@ -108,15 +116,3 @@ class CodePicker(private val config: PickerConfig) {
}
}

private fun String.toSupportLanguage(): Language? {
return when (this.lowercase()) {
"java" -> Language.JAVA
"kotlin" -> Language.KOTLIN
"csharp", "c#" -> Language.CSHARP
"python" -> Language.PYTHON
"go" -> Language.GO
"typescript" -> Language.TYPESCRIPT
"javascript" -> Language.JAVASCRIPT
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cc.unitmesh.pick.picker

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import org.archguard.scanner.analyser.count.FileJob
import org.archguard.scanner.analyser.count.LanguageWorker
import org.archguard.scanner.analyser.ignore.Gitignore
import org.archguard.scanner.analyser.ignore.IgnoreMatcher
Expand All @@ -18,7 +19,7 @@ class DirectoryJob(
val PathDenyList: List<String> = listOf(".git", ".hg", ".svn")

class PickDirectoryWalker(
val output: Channel<PickJob>,
val output: Channel<FileJob>,
val excludes: List<Regex> = listOf()
) {
val logger = org.slf4j.LoggerFactory.getLogger(javaClass)
Expand All @@ -34,7 +35,7 @@ class PickDirectoryWalker(
if (!file.isDirectory) {
launch {
LanguageWorker.createFileJob(file).let {
output.send(PickJob.from(it))
output.send(it)
}
}
} else {
Expand Down Expand Up @@ -113,7 +114,7 @@ class PickDirectoryWalker(
if (!isDir) {
LanguageWorker.createFileJob(file).let {
launch {
output.send(PickJob.from(it))
output.send(it)
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cc.unitmesh.pick.picker

import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.archguard.scanner.analyser.count.FileJob
import java.security.MessageDigest

@Serializable
class PickJob(
var language: String = "",
var possibleLanguages: List<String> = listOf(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ import org.archguard.scanner.analyser.count.FileJob
* - by Horizontal (with Import File):
* - by Vertical (with History Change):
*/
class JavaFileWorker : AbstractWorker() {
val packageTree: Map<String, FileJob> = mapOf()
/// [Coco](https://github.com/inherd/coco) high change and long line, means is important file, and need to be checked.
class JavaLangWorker : LangWorker() {
val jobs: MutableList<PickJob> = mutableListOf()
val packageTree: MutableMap<String, PickJob> = mutableMapOf()

override suspend fun start(job: PickJob) = coroutineScope {
val packageRegex = Regex("package\\s+([a-zA-Z0-9_\\.]+);")
val extLength = ".java".length

override fun addJob(job: PickJob) {
this.jobs.add(job)
val packageMatch = packageRegex.find(job.content.decodeToString())
if (packageMatch != null) {
val packageName = packageMatch.groupValues[1]
val className = job.filename.substring(0, job.filename.length - extLength)
val fullClassName = "$packageName.$className"
packageTree[fullClassName] = job
}
}

override suspend fun start() = coroutineScope {
// 1. read directory to a collection of files for FileJob

// 2. check package information from line 1?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.picker.PickJob

abstract class LangWorker {
abstract fun addJob(job: PickJob)
abstract suspend fun start()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.picker.PickJob
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.archguard.rule.common.Language

class WorkerManager {
private val workers: Map<Language, LangWorker> = mapOf(
Language.JAVA to JavaLangWorker(),
)

fun addJob(job: PickJob) {
val language = job.language.toSupportLanguage()
// print job serial
val worker = workers[language]
worker?.addJob(job)
}

suspend fun runAll() {
workers.forEach { (_, worker) ->
worker.start()
}
}
}

private fun String.toSupportLanguage(): Language? {
return when (this.lowercase()) {
"java" -> Language.JAVA
"kotlin" -> Language.KOTLIN
"csharp", "c#" -> Language.CSHARP
"python" -> Language.PYTHON
"go" -> Language.GO
"typescript" -> Language.TYPESCRIPT
"javascript" -> Language.JAVASCRIPT
else -> null
}
}

0 comments on commit 811a47f

Please sign in to comment.