Skip to content

Commit

Permalink
feat: add basic pickering
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 6, 2023
1 parent 473087e commit 9a9c421
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ chapi = "2.1.3"
archguard = "2.0.7"
codedb = "0.1.2"

kotlinxCoroutines="1.7.3"

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
Expand All @@ -30,6 +32,8 @@ shadow = "com.github.johnrengelman.shadow:8.1.1"
kotlin-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-plugin-dev = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
# a mighty tiny command line interface
# Coroutines
coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinxCoroutines" }

clikt = { group = "com.github.ajalt.clikt", name = "clikt", version.ref = "clikt" }

Expand Down
1 change: 1 addition & 0 deletions unit-picker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {

implementation(libs.clikt)
implementation(libs.serialization.json)
implementation(libs.coroutines.core)

implementation(libs.chapi.domain)
implementation(libs.chapi.java)
Expand Down
6 changes: 5 additions & 1 deletion unit-picker/src/main/kotlin/cc/unitmesh/pick/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import cc.unitmesh.pick.picker.PickerConfig
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import kotlinx.coroutines.runBlocking

class Picker : CliktCommand() {
val url by option("-u", "--url", help = "url to pick code").default(".")

override fun run() {
val config = PickerConfig(url = url)
CodePicker(config).execute()

runBlocking {
CodePicker(config).execute()
}
}
}

Expand Down
31 changes: 22 additions & 9 deletions unit-picker/src/main/kotlin/cc/unitmesh/pick/picker/CodePicker.kt
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
package cc.unitmesh.pick.picker

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import org.archguard.action.checkout.GitSourceSettings
import org.archguard.action.checkout.executeGitCheckout
import org.archguard.scanner.analyser.count.LanguageService
import org.jetbrains.annotations.TestOnly
import java.nio.file.Files
import java.nio.file.Path
import kotlin.coroutines.coroutineContext

class CodePicker(val config: PickerConfig) {
private val logger = org.slf4j.LoggerFactory.getLogger(javaClass)
private val languageService = LanguageService()

fun execute() {
// 1. check config.url is a valid url or path
val codeDir = checkoutCode(config.url, config.branch, config.baseDir)
suspend fun execute() {
val scope = CoroutineScope(coroutineContext)
scope.launch {
val codeDir = checkoutCode(config.url, config.branch, config.baseDir)
.toFile().canonicalFile

// 2. walkdir select files to tree
val walkdirChannel = Channel<PickJob>()

// 3. generate tree to jsonl
logger.info("start picker")

launch {
PickDirectoryWalker(walkdirChannel).start(codeDir.toString())
walkdirChannel.close()
}

logger.info("stop picker")

// 3. generate tree to jsonl
}
}

@TestOnly
fun checkoutCode(url: String, branch: String, baseDir: String): Path {
if (!gitUrlRegex.matches(url)) {
return Path.of(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class DirectoryJob(
// ".git", ".hg", ".svn"
val PathDenyList: List<String> = listOf(".git", ".hg", ".svn")

class DirectoryWalker(
class PickDirectoryWalker(
val output: Channel<PickJob>,
val excludes: List<Regex> = listOf()
) {
val logger = org.slf4j.LoggerFactory.getLogger(javaClass)
private var root: String = ""
private val ignores: MutableList<IgnoreMatcher> = mutableListOf()
private val dirChannels = mutableListOf<Channel<DirectoryJob>>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cc.unitmesh.pick.picker;

import org.junit.jupiter.api.Test
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlin.test.assertEquals
import org.junit.jupiter.api.Test

class CodePickerTest {
@Test
Expand Down Expand Up @@ -58,11 +61,11 @@ class CodePickerTest {
@Test
fun shouldCheckoutTestCode() {
val picker = CodePicker(
PickerConfig(
url = "https://github.com/unit-mesh/unit-eval-testing"
)
PickerConfig(url = "https://github.com/unit-mesh/unit-eval-testing")
)

picker.execute()
// CoroutineScope(Dispatchers.IO).launch {
// picker.execute()
// }
}
}

0 comments on commit 9a9c421

Please sign in to comment.