Skip to content

Commit

Permalink
refactor: update builder design for projects
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 7, 2023
1 parent 8cac63b commit 2d6046d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CodePicker(private val config: PickerConfig) {
launch {
for (fileJob in walkdirChannel) {
languageWorker.processFile(fileJob)?.let {
workerManager.addJob(PickJob.from(it))
workerManager.addJob(InstructionJob.from(it))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import kotlinx.serialization.Serializable
import org.archguard.scanner.analyser.count.FileJob

@Serializable
class PickJob(
class InstructionJob(
var fileSummary: FileJob,
var code: String = "",
var container: CodeContainer? = null,
) {
companion object {
fun from(fileJob: FileJob): PickJob {
return PickJob(
fun from(fileJob: FileJob): InstructionJob {
return InstructionJob(
code = fileJob.content.decodeToString(),
fileSummary = fileJob
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.picker.PickJob
import cc.unitmesh.pick.picker.InstructionJob
import chapi.ast.javaast.JavaAnalyser
import kotlinx.coroutines.coroutineScope
import org.archguard.scanner.analyser.count.FileJob
Expand All @@ -21,19 +21,19 @@ import org.archguard.scanner.analyser.count.FileJob
* - by Vertical (with History Change):
*/
class JavaLangWorker : LangWorker() {
private val jobs: MutableList<PickJob> = mutableListOf()
private val packageTree: MutableMap<String, PickJob> = mutableMapOf()
private val jobs: MutableList<InstructionJob> = mutableListOf()
private val packageTree: MutableMap<String, InstructionJob> = mutableMapOf()

private val packageRegex = Regex("package\\s+([a-zA-Z0-9_.]+);")
private val extLength = ".java".length

override fun addJob(job: PickJob) {
override fun addJob(job: InstructionJob) {
this.jobs.add(job)
tryAddClassToTree(job.code, job)
job.container = JavaAnalyser().analysis(job.code, job.fileSummary.location)
}

private fun tryAddClassToTree(code: String, job: PickJob) {
private fun tryAddClassToTree(code: String, job: InstructionJob) {
val packageMatch = packageRegex.find(code)
if (packageMatch != null) {
val packageName = packageMatch.groupValues[1]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.picker.PickJob
import cc.unitmesh.pick.picker.InstructionJob

abstract class LangWorker {
abstract fun addJob(job: PickJob)
abstract fun addJob(job: InstructionJob)
abstract suspend fun start() : List<InstructionBuilder>
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package cc.unitmesh.pick.worker

import cc.unitmesh.pick.prompt.InstructionBuilder
import cc.unitmesh.pick.picker.PickJob
import cc.unitmesh.pick.picker.InstructionJob
import org.archguard.rule.common.Language

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

fun addJob(job: PickJob) {
fun addJob(job: InstructionJob) {
val language = job.fileSummary.language.toSupportLanguage()
val worker = workers[language]
worker?.addJob(job)
Expand Down

0 comments on commit 2d6046d

Please sign in to comment.