Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #165 from koxudaxi/add_validation_for_poetry_execu…
Browse files Browse the repository at this point in the history
…table

Add validation for poetry executable
  • Loading branch information
koxudaxi committed Jan 26, 2021
2 parents a236f10 + 19beda5 commit a1e1729
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<idea-plugin url="https://github.com/koxudaxi/poetry-pycharm-plugin" require-restart="true">
<id>com.koxudaxi.poetry</id>
<name>Poetry</name>
<version>1.0.2</version>
<version>1.0.3</version>
<vendor email="koaxudai@gmail.com">Koudai Aono @koxudaxi</vendor>
<change-notes><![CDATA[
<h2>version 1.0.3</h2>
<p>Features</p>
<ul>
<li>Add validation for poetry executable [#165]</li>
</ul>
<h2>version 1.0.2</h2>
<p>Features</p>
<ul>
Expand Down
7 changes: 6 additions & 1 deletion src/com/koxudaxi/poetry/PyAddNewPoetryPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,16 @@ class PyAddNewPoetryPanel(private val project: Project?,
?: return ValidationInfo("Poetry executable is not found")
return when {
!executable.exists() -> ValidationInfo(PyBundle.message("python.sdk.file.not.found", executable.absolutePath))
!Files.isExecutable(executable.toPath()) || !executable.isFile -> ValidationInfo(PyBundle.message("python.sdk.cannot.execute", executable.absolutePath))
!Files.isExecutable(executable.toPath()) || !executable.isFile || !testPoetryExecutable(executable) ->
ValidationInfo(PyBundle.message("python.sdk.cannot.execute", executable.absolutePath))
else -> null
}
}

private fun testPoetryExecutable(executable: File): Boolean =
syncRunCommand(executable.parent, executable.absolutePath, "--version", defaultResult = false) { true }


/**
* Checks if the poetry for the project hasn't been already added.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/com/koxudaxi/poetry/poetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,23 @@ val poetryVersion: String?
it.split(' ').lastOrNull()
}

inline fun <reified T> syncRunCommand(projectPath: @SystemDependent String, command: String, vararg args: String, defaultResult: T, crossinline callback: (String) -> T): T {
return try {
ApplicationManager.getApplication().executeOnPooledThread<T> {
try {
val result = runCommand(projectPath, command, *args)
callback(result)
} catch (e: PyExecutionException) {
defaultResult
} catch (e: ProcessNotCreatedException) {
defaultResult
}
}.get(10, TimeUnit.SECONDS)
} catch (e: TimeoutException) {
defaultResult
}
}

inline fun <reified T> syncRunPoetry(projectPath: @SystemDependent String?, vararg args: String, defaultResult: T, crossinline callback: (String) -> T): T {
return try {
ApplicationManager.getApplication().executeOnPooledThread<T> {
Expand Down

0 comments on commit a1e1729

Please sign in to comment.