Skip to content

Commit 6fe5625

Browse files
authored
Merge pull request #59 from devchat-ai/improve-workflows-installation
Improve workflows installation
2 parents 02e0f29 + 9b44c20 commit 6fe5625

7 files changed

Lines changed: 58 additions & 21 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "tools"]
55
path = tools
66
url = https://github.com/devchat-ai/devchat-vscode-tools.git
7+
[submodule "workflows"]
8+
path = workflows
9+
url = https://gitlab.com/devchat-ai/workflows.git

.idea/vcs.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ tasks.register<Copy>("copyTools") {
4444
into(layout.buildDirectory.dir("tmp/copyTools/tools"))
4545
}
4646

47+
tasks.register<Copy>("copyWorkflows") {
48+
from(layout.projectDirectory.dir("workflows")) { exclude(".git/**", ".gitignore") }
49+
into(layout.buildDirectory.dir("tmp/copyWorkflows/workflows"))
50+
}
51+
4752
tasks.register<Exec>("buildGUI") {
4853
commandLine("yarn", "idea")
4954
workingDir(layout.projectDirectory.dir("gui"))
@@ -53,6 +58,7 @@ sourceSets {
5358
main {
5459
resources {
5560
srcDir(layout.buildDirectory.dir("tmp/copyTools"))
61+
srcDir(layout.buildDirectory.dir("tmp/copyWorkflows"))
5662
}
5763
}
5864
}
@@ -74,7 +80,7 @@ tasks {
7480
}
7581

7682
processResources {
77-
dependsOn("copyTools")
83+
dependsOn("copyTools", "copyWorkflows")
7884
}
7985

8086
publishPlugin {

src/main/kotlin/ai/devchat/cli/PythonEnvManager.kt

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PythonEnvManager(private val workDir: String) {
4545
Log.info("Mamba already installed at: " + dstFile.path)
4646
}
4747

48-
fun createEnv(name: String, version: String = "3.11.4"): PythonEnv {
48+
fun createEnv(name: String, version: String = "3.11.4", retries: Int = 5): PythonEnv {
4949
Log.info("Python environment is creating.")
5050
val errPrefix = "Error occurred during Python environment creation:"
5151
val pyenv = PythonEnv(Paths.get(mambaWorkDir, "envs", name).toString())
@@ -64,23 +64,34 @@ class PythonEnvManager(private val workDir: String) {
6464
"--yes"
6565
)
6666
Log.info("Preparing to create python environment by: $command")
67-
try {
68-
ProcessBuilder(*command).start().also { process ->
69-
process.inputStream.bufferedReader().forEachLine { Log.info("[Mamba installation] $it") }
70-
val exitCode = process.waitFor()
71-
if (exitCode != 0) throw RuntimeException(
72-
"Command execution failed with exit code: $exitCode"
67+
68+
var remainingRetries: Int = retries
69+
while (remainingRetries-- > 0) {
70+
try {
71+
ProcessBuilder(*command).start().also { process ->
72+
process.inputStream.bufferedReader().forEachLine { Log.info("[Mamba installation] $it") }
73+
val exitCode = process.waitFor()
74+
if (exitCode == 0) {
75+
Log.info("Python is installed in: $pythonBinPath")
76+
return pyenv
77+
}
78+
Log.warn(
79+
"$errPrefix Command execution failed with exit code $exitCode, $remainingRetries retries left"
80+
)
81+
}
82+
} catch (e: IOException) {
83+
e.printStackTrace()
84+
Log.warn(
85+
"$errPrefix Exception occurred while executing the command: ${e.message}, $remainingRetries retries left"
86+
)
87+
} catch (e: InterruptedException) {
88+
Thread.currentThread().interrupt()
89+
Log.warn(
90+
"$errPrefix Command execution was interrupted: ${e.message}, $remainingRetries retries left"
7391
)
7492
}
75-
} catch (e: IOException) {
76-
throw RuntimeException("$errPrefix Command execution failed with exception: " + e.message, e)
77-
} catch (e: InterruptedException) {
78-
Thread.currentThread().interrupt()
79-
throw RuntimeException("$errPrefix Command execution was interrupted: " + e.message, e)
8093
}
81-
82-
Log.info("Python is installed in: $pythonBinPath")
83-
return pyenv
94+
throw RuntimeException("$errPrefix Maximum retries exceed")
8495
}
8596
}
8697

src/main/kotlin/ai/devchat/common/PathUtils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object PathUtils {
1010
var pythonForWorkflows: String = "python"
1111
val pythonPath: String = Paths.get(workPath, "site-packages").toString()
1212

13-
fun copyResourceDirToPath(resourceDir: String, outputPath: String) {
13+
fun copyResourceDirToPath(resourceDir: String, outputPath: String): String {
1414
val uri = javaClass.getResource(resourceDir)!!.toURI()
1515
val path = if (uri.scheme == "jar") {
1616
val fileSystem = try {
@@ -46,5 +46,7 @@ object PathUtils {
4646
return FileVisitResult.CONTINUE
4747
}
4848
})
49+
50+
return outputPath
4951
}
5052
}

src/main/kotlin/ai/devchat/idea/DevChatSetupThread.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,33 @@ class DevChatSetupThread : Thread() {
4040
Paths.get(workDir, "site-packages").toString()
4141
)
4242

43-
PathUtils.pythonCommand = getSystemPython(minimalPythonVersion) ?: envManager.createEnv(
44-
"devchat", defaultPythonVersion
45-
).pythonCommand
43+
PathUtils.pythonCommand = getSystemPython(minimalPythonVersion) ?: (
44+
if (OSInfo.isWindows) Paths.get(
45+
PathUtils.copyResourceDirToPath(
46+
"/tools/python-3.11.6-embed-amd64",
47+
Paths.get(workDir, "python-win").toString()
48+
), "python.exe").toString()
49+
else envManager.createEnv(
50+
"devchat", defaultPythonVersion
51+
).pythonCommand
52+
)
4653
DevChatConfig(Paths.get(workDir, "config.yml").toString()).writeDefaultConfig()
4754
}
4855

4956
private fun setupWorkflows(envManager: PythonEnvManager) {
57+
val workflowsDir = File(Paths.get(workDir, "workflows").toString())
58+
if (!workflowsDir.exists()) workflowsDir.mkdirs()
59+
PathUtils.copyResourceDirToPath(
60+
"/workflows",
61+
Paths.get(workflowsDir.path, "sys").toString()
62+
)
5063
try {
5164
DevChatWrapper().run(mutableListOf("update-sys" to null))
5265
} catch (e: Exception) {
5366
Log.warn("Failed to update-sys.")
5467
}
5568
listOf("sys", "org", "usr")
56-
.map { Paths.get(workDir, "workflows", it, "requirements.txt").toString() }
69+
.map { Paths.get(workflowsDir.path, it, "requirements.txt").toString() }
5770
.firstOrNull { File(it).exists() }
5871
?.let {
5972
val workflowEnv = envManager.createEnv("devchat-commands", defaultPythonVersion)

workflows

Submodule workflows added at c0c2609

0 commit comments

Comments
 (0)