Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 257 additions & 79 deletions docs/source/ui.rst

Large diffs are not rendered by default.

1,523 changes: 198 additions & 1,325 deletions pycharm_plugin/src/main/kotlin/com/projspec/toolwindow/HtmlContent.kt

Large diffs are not rendered by default.

Large diffs are not rendered by default.

179 changes: 157 additions & 22 deletions pycharm_plugin/src/main/kotlin/com/projspec/util/ProjspecRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,41 @@ object ProjspecRunner {
* (a JSON string) so remote projects (s3://, gcs://, …) can be re-scanned
* with their filesystem credentials/flags intact.
*/
fun runScan(path: String, storageOptions: String? = null): CliResult {
val args = mutableListOf(cli, "scan", "--library")
if (!storageOptions.isNullOrBlank()) {
args.add("--storage_options")
args.add(storageOptions)
fun runScan(path: String, storageOptions: String? = null): CliResult {
val args = mutableListOf(cli, "scan", "--library")
if (!storageOptions.isNullOrBlank()) {
args.add("--storage_options")
args.add(storageOptions)
}
args.add(path)
return run(args)
}

/**
* CLI fallback for the file-browser's directory scan panel.
* Calls `projspec.filebrowser.scan_directory` via a short Python script so
* the result always has the `{url, project, error}` shape that
* `showProjectInPanel` expects, even for directories that contain no
* recognised projspec spec types (where `projspec scan --json-out` would
* produce no output).
*/
fun runScanDirectory(url: String, storageOptions: String? = null): String {
val soArg = storageOptions?.takeIf { it.isNotBlank() } ?: "null"
val script = """
import json, sys
url = sys.argv[1]
so = json.loads(sys.argv[2]) if sys.argv[2] != 'null' else None
try:
from projspec.filebrowser import scan_directory
result = scan_directory(url, storage_options=so)
except Exception as e:
result = {'url': url, 'project': None, 'error': str(e)}
print(json.dumps(result))
""".trimIndent()
return when (val r = run(listOf("python", "-c", script, url, soArg))) {
is CliResult.Success -> extractJson(r.stdout).ifBlank { "{}" }
is CliResult.Failure -> "{}"
}
args.add(path)
return run(args)
}

/** `projspec create <spec> <path>` — create a new spec inside a project. */
Expand Down Expand Up @@ -89,11 +116,99 @@ object ProjspecRunner {
walk(pu.Enum)
print(json.dumps(out))
""".trimIndent()
return try {
val cmd = GeneralCommandLine(listOf("python3", "-c", script))
val output = CapturingProcessHandler(cmd).runProcess(30_000)
if (output.isTimeout || output.exitCode != 0) "{}" else output.stdout.trim().ifBlank { "{}" }
} catch (_: Exception) { "{}" }
return when (val r = run(listOf("python", "-c", script))) {
is CliResult.Success -> r.stdout.trim().ifBlank { "{}" }
is CliResult.Failure -> "{}"
}
}

// -------------------------------------------------------------------------
// Filebrowser CLI wrappers
// -------------------------------------------------------------------------

/**
* Generic filebrowser subcommand call. Returns the raw stdout string
* (JSON) or an empty JSON object on failure.
*/
fun runFbCall(vararg args: String): String {
val fullArgs = listOf(cli) + args.toList()
return when (val r = run(fullArgs)) {
is CliResult.Success -> extractJson(r.stdout).ifBlank { "{}" }
is CliResult.Failure -> "{}"
}
}

/** `projspec filebrowser inspect-as-project <url>` — file metadata + project-shaped dict. */
fun runFbInspectAsProject(url: String, storageOptions: String?): String {
val args = mutableListOf(cli, "filebrowser", "inspect-as-project", url)
if (!storageOptions.isNullOrBlank()) { args.add("--storage-options"); args.add(storageOptions) }
return when (val r = run(args)) {
is CliResult.Success -> extractJson(r.stdout).ifBlank { "{}" }
is CliResult.Failure -> """{"url":"$url","project":null,"error":"${r.message.replace("\"","'")}"}"""
}
}

/** `projspec filebrowser browse <url> [--storage-options JSON]` */
fun runFbBrowse(url: String, storageOptions: String?): String {
val args = mutableListOf(cli, "filebrowser", "browse", url)
if (!storageOptions.isNullOrBlank()) { args.add("--storage-options"); args.add(storageOptions) }
return when (val r = run(args)) {
is CliResult.Success -> extractJson(r.stdout).ifBlank { "{}" }
is CliResult.Failure -> """{"url":"$url","entries":[],"error":"${r.message.replace("\"","'")}"}"""
}
}

/** `projspec filebrowser write-file <url>` (content via stdin/tmp file) */
fun runFbWriteFile(url: String, content: String, storageOptions: String?) {
// Write content to a temp file then pass via --content-file
val tmp = java.io.File.createTempFile("projspec_fb_", ".txt")
try {
tmp.writeText(content, Charsets.UTF_8)
val args = mutableListOf(cli, "filebrowser", "write-file", url,
"--content-file", tmp.absolutePath)
if (!storageOptions.isNullOrBlank()) { args.add("--storage-options"); args.add(storageOptions) }
run(args)
} finally { tmp.delete() }
}

/** `projspec filebrowser delete <url> [--recursive] [--storage-options JSON]` */
fun runFbDelete(url: String, recursive: Boolean, storageOptions: String?) {
val args = mutableListOf(cli, "filebrowser", "delete", url)
if (recursive) args.add("--recursive")
if (!storageOptions.isNullOrBlank()) { args.add("--storage-options"); args.add(storageOptions) }
run(args)
}

/** `projspec filebrowser move <src> <dst> [--storage-options JSON]` */
fun runFbMove(src: String, dst: String, storageOptions: String?) {
val args = mutableListOf(cli, "filebrowser", "move", src, dst)
if (!storageOptions.isNullOrBlank()) { args.add("--storage-options"); args.add(storageOptions) }
run(args)
}

/** `projspec filebrowser mkdir <url> [--storage-options JSON]` */
fun runFbMkdir(url: String, storageOptions: String?) {
val args = mutableListOf(cli, "filebrowser", "mkdir", url)
if (!storageOptions.isNullOrBlank()) { args.add("--storage-options"); args.add(storageOptions) }
run(args)
}

/** `projspec filebrowser bookmarks add <url> [--label LABEL]` → JSON bookmarks list */
fun runFbBookmarkAdd(url: String, label: String?, storageOptions: String?): String {
val args = mutableListOf(cli, "filebrowser", "bookmarks", "add", url)
if (!label.isNullOrBlank()) { args.add("--label"); args.add(label) }
return when (val r = run(args)) {
is CliResult.Success -> extractJson(r.stdout).ifBlank { "[]" }
is CliResult.Failure -> "[]"
}
}

/** `projspec filebrowser bookmarks remove <url>` → JSON bookmarks list */
fun runFbBookmarkRemove(url: String): String {
return when (val r = run(listOf(cli, "filebrowser", "bookmarks", "remove", url))) {
is CliResult.Success -> extractJson(r.stdout).ifBlank { "[]" }
is CliResult.Failure -> "[]"
}
}

// -------------------------------------------------------------------------
Expand All @@ -103,28 +218,48 @@ object ProjspecRunner {
/**
* Execute an external command and capture stdout/stderr.
*
* Runs via the login shell (`$SHELL -l -c`) so the user's PATH
* (conda envs, pyenv, Homebrew, etc.) is available exactly as it
* would be in a terminal — even when launched from a macOS GUI app.
*
* Every call is logged with args, duration, and result.
*
* NOTE: blocks the calling thread for up to 60 s. Never call from the EDT.
*/
fun run(args: List<String>): CliResult {
// Shell-quote each argument so spaces and special chars survive
fun quote(s: String) = "'" + s.replace("'", "'\\''") + "'"
val shellCmd = args.joinToString(" ") { quote(it) }
val shell = System.getenv("SHELL")?.takeIf { it.isNotBlank() } ?: "/bin/sh"
val fullArgs = listOf(shell, "-l", "-c", shellCmd)

PluginLogger.info("CLI call: $shellCmd")
val t0 = System.currentTimeMillis()
return try {
val commandLine = GeneralCommandLine(args)
val commandLine = GeneralCommandLine(fullArgs)
val handler = CapturingProcessHandler(commandLine)
val output = handler.runProcess(60_000)
val ms = System.currentTimeMillis() - t0

when {
output.isTimeout ->
output.isTimeout -> {
PluginLogger.error("CLI timeout after 60 s: $shellCmd")
CliResult.Failure("projspec timed out after 60 s", -1)
output.exitCode != 0 ->
CliResult.Failure(
output.stderr.ifBlank { output.stdout }
.ifBlank { "Exit code ${output.exitCode}" },
output.exitCode,
)
else ->
}
output.exitCode != 0 -> {
val err = output.stderr.ifBlank { output.stdout }.ifBlank { "Exit code ${output.exitCode}" }
PluginLogger.warn("CLI exit ${output.exitCode} (${ms}ms): $shellCmd\n stderr: ${output.stderr.trim().take(500)}\n stdout: ${output.stdout.trim().take(500)}")
CliResult.Failure(err, output.exitCode)
}
else -> {
PluginLogger.info("CLI ok (${ms}ms): $shellCmd stdout(${output.stdout.length}b)")
CliResult.Success(output.stdout)
}
}
} catch (e: Exception) {
CliResult.Failure("Failed to launch projspec: ${e.message}")
val ms = System.currentTimeMillis() - t0
PluginLogger.error("CLI exception (${ms}ms): $shellCmd\n ${e.javaClass.simpleName}: ${e.message}")
CliResult.Failure("Failed to launch: ${e.message}")
}
}

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ test = ["pytest", "pytest-cov", "django", "streamlit", "copier", "jinja2-time",
qt = ["pyqt>5,<6", "pyqtwebengin>5,<6"]
textual = ["textual>=0.80"]
ipywidget = ["anywidget>=0.9"]
serve = ["fastapi>=0.100", "uvicorn[standard]>=0.23"]

[project.scripts]
projspec = "projspec.__main__:main"
projspec-qt = "projspec.qtapp.main:main"
projspec-tui = "projspec.textapp.main:main"
projspec-server = "projspec.server:main"

[tool.poetry.extras]
po_test = ["pytest"]
Expand Down
Loading
Loading