Skip to content

Create a default Run Configuration for Idea #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
26 changes: 24 additions & 2 deletions src/main/kotlin/kscript/app/AppHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private fun bytesToHex(buffer: ByteArray): String {
fun numLines(str: String) = str.split("\r\n|\r|\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().size


fun launchIdeaWithKscriptlet(scriptFile: File, dependencies: List<String>, customRepos: List<MavenRepo>, includeURLs: List<URL>): String {
fun launchIdeaWithKscriptlet(scriptFile: File, userArgs: List<String>, dependencies: List<String>, customRepos: List<MavenRepo>, includeURLs: List<URL>): String {
requireInPath("idea", "Could not find 'idea' in your PATH. It can be created in IntelliJ under `Tools -> Create Command-line Launcher`")

infoMsg("Setting up idea project from ${scriptFile}")
Expand All @@ -218,6 +218,28 @@ fun launchIdeaWithKscriptlet(scriptFile: File, dependencies: List<String>, custo
// .run { File(this, "kscript_tmp_project") }
// .apply { mkdir() }

File(tmpProjectDir, ".idea/runConfigurations/")
.run {
mkdirs()
}
File(tmpProjectDir, ".idea/runConfigurations/Main.xml").writeText(
"""
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Main" type="BashConfigurationType" factoryName="Bash">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="INTERPRETER_PATH" value="kscript" />
<option name="PROJECT_INTERPRETER" value="false" />
<option name="WORKING_DIRECTORY" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SCRIPT_NAME" value="${'$'}PROJECT_DIR${'$'}/src/${scriptFile.name}" />
<option name="PARAMETERS" value="${userArgs.joinToString(" ")}" />
<module name="" />
<method v="2" />
</configuration>
</component>
""".trimIndent()
)

val stringifiedDeps = dependencies.map { " compile \"$it\"" }.joinToString("\n")
val stringifiedRepos = customRepos.map { " maven {\n url '${it.url}'\n }\n" }.joinToString("\n")

Expand Down Expand Up @@ -270,7 +292,7 @@ private fun URL.fileName() = this.toURI().path.split("/").last()

private fun createSymLink(link: File, target: File) {
try {
Files.createSymbolicLink(link.toPath(), target.absoluteFile.toPath());
Files.createSymbolicLink(link.toPath(), target.absoluteFile.toPath())
} catch (e: IOException) {
errorMsg("Failed to create symbolic link to script. Copying instead...")
target.copyTo(link)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/kscript/app/Kscript.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fun main(args: Array<String>) {

// Create temporary dev environment
if (docopt.getBoolean("idea")) {
println(launchIdeaWithKscriptlet(rawScript, dependencies, customRepos, includeURLs))
println(launchIdeaWithKscriptlet(rawScript, userArgs, dependencies, customRepos, includeURLs))
exitProcess(0)
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export -f kscript_nocall
assert_raises 'tmpDir=$(kscript_nocall --idea test/resources/includes/include_variations.kts | cut -f2 -d" " | xargs echo); cd $tmpDir && gradle build' 0

## Ensure relative includes with in shebang mode
assert_raises resources/includes/shebang_mode_includes 0
assert_raises "${KSCRIPT_HOME}/test/resources/includes/shebang_mode_includes" 0

## support diamond-shaped include schemes (see #133)
assert_raises 'tmpDir=$(kscript_nocall --idea test/resources/includes/diamond.kts | cut -f2 -d" " | xargs echo); cd $tmpDir && gradle build' 0
Expand Down