Skip to content

Commit

Permalink
feat(rust): try to handle working path issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 5, 2024
1 parent b4e52ac commit cd7419d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SingleProjectCodePicker(private val config: InsPickerOption) {
val language = SupportedLang.from(config.language.lowercase())
?: throw IllegalArgumentException("unsupported language: ${config.language}")

val workerManager = WorkerManager(WorkerContext.fromConfig(language, config))
val workerManager = WorkerManager(WorkerContext.fromConfig(language, config, codeDir))
workerManager.init(codeDir, language)

return instructions(codeDir, languageWorker, workerManager, language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ data class InsPickerOption(
return baseDir + File.separator + repoFileName() + ".jsonl"
}

fun projectDir(): String {
return baseDir + File.separator + encodeFileName(url) + File.separator + encodeFileName(branch)
}

fun repoFileName() = "${encodeFileName(url)}_${encodeFileName(branch)}_${language}.jsonl"

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.archguard.scanner.core.sca.CompositionDependency
data class ProjectContext(
var language: SupportedLang = SupportedLang.JAVA,
var compositionDependency: List<CompositionDependency> = listOf(),
val baseDir: String = ""
val codeDir: String = ""
) {
var testFrameworks: List<String>
var coreFrameworks: List<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cc.unitmesh.quality.CodeQualityType
import kotlinx.serialization.Serializable
import org.archguard.scanner.core.sca.CompositionDependency
import org.jetbrains.annotations.TestOnly
import java.io.File

@Serializable
data class WorkerContext(
Expand Down Expand Up @@ -42,7 +43,7 @@ data class WorkerContext(
)
}

fun fromConfig(language: SupportedLang, insPickerOption: InsPickerOption): WorkerContext {
fun fromConfig(language: SupportedLang, insPickerOption: InsPickerOption, codeDir: File): WorkerContext {
return WorkerContext(
insPickerOption.codeStrategyTypes,
insPickerOption.codeQualityTypes,
Expand All @@ -60,7 +61,7 @@ data class WorkerContext(
),
project = ProjectContext(
language = language,
baseDir = insPickerOption.baseDir,
codeDir = codeDir.absolutePath
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import cc.unitmesh.pick.worker.base.LangWorker
import cc.unitmesh.pick.worker.job.InstructionFileJob
import chapi.ast.rustast.RustAnalyser
import org.slf4j.Logger
import java.io.File
import java.nio.file.Path

class RustWorker(override val workerContext: WorkerContext) : LangWorker {
override val jobs: MutableList<InstructionFileJob> = mutableListOf()
Expand All @@ -16,6 +18,11 @@ class RustWorker(override val workerContext: WorkerContext) : LangWorker {
this.jobs.add(job)
try {
val container = RustAnalyser().analysis(job.code, job.fileSummary.location)
val relativePath = Path.of(workerContext.project.codeDir).relativize(Path.of(job.fileSummary.location))
container.PackageName = calculatePackageName(
relativePath.toString().replace("/", "::").replace("\\", ".")
)

// update error packageName in here with a path
job.codeLines = job.code.lines()
container.DataStructures.map { ds ->
Expand All @@ -32,4 +39,65 @@ class RustWorker(override val workerContext: WorkerContext) : LangWorker {
e.printStackTrace()
}
}


// TODO: remove follow code after we Update Chapi >= 2.2.4
companion object {
val LIB_RS = "lib.rs"
val MAIN_RS = "main.rs"

/**
* Calculates the package name for a given file name in the Kotlin language.
*
* @param fileName the name of the file for which the package name needs to be calculated
* @return the calculated package name as a string
*
* The `calculatePackageName` method is used to determine the package name based on the given file name in the Kotlin language. The package name is an essential part of organizing code in Kotlin and is used to group related classes and files together.
*
* The package name is derived from the file name using the following rules:
* - If the file name is "src/main.rs", the package name will be "main".
* - If the file name is "enfer_core/src/lib.rs", the package name will be "enfer_core".
* - If the file name is "enfer_core/src/document.rs", the package name will be "enfer_core::document".
*
* It is important to note that the package name is calculated based on the file path and may differ from the actual package declaration in the file.
*
* Example usage:
* ```kotlin
* val fileName = "enfer_core/src/document.rs"
* val packageName = calculatePackageName(fileName)
* println(packageName) // Output: enfer_core::document
* ```
*/
fun calculatePackageName(fileName: String): String {
val modulePath = fileName.substringBeforeLast("src")
.substringBeforeLast(File.separator)

val paths = fileName.substringAfterLast("src").split(File.separator)

// if pathSize == 1, it means the file is in the root directory
if (paths.size == 1) {
return if (fileName.endsWith(LIB_RS) || fileName.endsWith(MAIN_RS)) {
""
} else {
fileName.substringBeforeLast(".")
}
}

// if modulePath is empty, use paths as package names
val packageName = paths
.filter { it.isNotEmpty() && it != MAIN_RS && it != LIB_RS }
.joinToString("::") { it.substringBeforeLast(".") }

if (modulePath.isEmpty()) {
return packageName
}

// if modulePath is not empty, use modulePath as package name
if (packageName.isEmpty()) {
return modulePath
}

return "$modulePath::$packageName"
}
}
}

0 comments on commit cd7419d

Please sign in to comment.