Skip to content

Commit ce9a74f

Browse files
rossbacherAndreas Rossbacher
andauthored
Add Kotlin run configuration for .kt based scripts. (#284)
Co-authored-by: Andreas Rossbacher <[email protected]>
1 parent 35c1862 commit ce9a74f

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

src/main/kotlin/kscript/app/AppHelpers.kt

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ fun launchIdeaWithKscriptlet(scriptFile: File,
207207
includeURLs: List<URL>,
208208
compilerOpts: String): String {
209209
val intellijCommand = System.getenv("KSCRIPT_IDEA_COMMAND") ?: "idea"
210+
val gradleCommand = System.getenv("KSCRIPT_GRADLE_COMMAND") ?: "gradle"
211+
210212
requireInPath("$intellijCommand", "Could not find '$intellijCommand' in your PATH. You must set the command used to launch your intellij as 'KSCRIPT_IDEA_COMMAND' env property")
211213

212214
infoMsg("Setting up idea project from ${scriptFile}")
@@ -229,20 +231,10 @@ fun launchIdeaWithKscriptlet(scriptFile: File,
229231
mkdirs()
230232
}
231233
File(tmpProjectDir, ".idea/runConfigurations/Main.xml").writeText(
232-
"""
233-
<component name="ProjectRunConfigurationManager">
234-
<configuration default="false" name="Main" type="BashConfigurationType" factoryName="Bash">
235-
<option name="INTERPRETER_OPTIONS" value="" />
236-
<option name="INTERPRETER_PATH" value="kscript" />
237-
<option name="PROJECT_INTERPRETER" value="false" />
238-
<option name="WORKING_DIRECTORY" value="" />
239-
<option name="PARENT_ENVS" value="true" />
240-
<option name="SCRIPT_NAME" value="${'$'}PROJECT_DIR${'$'}/src/${scriptFile.name}" />
241-
<option name="PARAMETERS" value="${userArgs.joinToString(" ")}" />
242-
<module name="" />
243-
<method v="2" />
244-
</configuration>
245-
</component>
234+
"""
235+
<component name="ProjectRunConfigurationManager">
236+
${runConfig(scriptFile, tmpProjectDir, userArgs)}
237+
</component>
246238
""".trimIndent()
247239
)
248240

@@ -351,11 +343,50 @@ $kotlinOptions
351343
}
352344

353345
val projectPath = tmpProjectDir.absolutePath
346+
347+
// Create gradle wrapper
348+
requireInPath("$gradleCommand", "Could not find '$gradleCommand' in your PATH. You must set the command used to launch your intellij as 'KSCRIPT_GRADLE_COMMAND' env property")
349+
runProcess("$gradleCommand wrapper", wd = tmpProjectDir)
350+
354351
infoMsg("Project set up at $projectPath")
355352

356353
return "$intellijCommand \"$projectPath\""
357354
}
358355

356+
private fun runConfig(scriptFile: File, tmpProjectDir: File, userArgs: List<String>): String {
357+
return if (scriptFile.extension == "kt") {
358+
"""
359+
<configuration name="${scriptFile.name.substringBeforeLast(".")}" type="JetRunConfigurationType">
360+
<module name="${tmpProjectDir.name}.main" />
361+
<option name="VM_PARAMETERS" value="" />
362+
<option name="PROGRAM_PARAMETERS" value="" />
363+
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
364+
<option name="ALTERNATIVE_JRE_PATH" />
365+
<option name="PASS_PARENT_ENVS" value="true" />
366+
<option name="MAIN_CLASS_NAME" value="${scriptFile.name.substringBeforeLast(".").capitalize()}Kt" />
367+
<option name="WORKING_DIRECTORY" value="" />
368+
<method v="2">
369+
<option name="Make" enabled="true" />
370+
</method>
371+
</configuration>
372+
""".trimIndent()
373+
} else {
374+
"""
375+
<configuration default="false" name="Main" type="BashConfigurationType" factoryName="Bash">
376+
<option name="INTERPRETER_OPTIONS" value="" />
377+
<option name="INTERPRETER_PATH" value="kscript" />
378+
<option name="PROJECT_INTERPRETER" value="false" />
379+
<option name="WORKING_DIRECTORY" value="" />
380+
<option name="PARENT_ENVS" value="true" />
381+
<option name="SCRIPT_NAME" value="${'$'}PROJECT_DIR${'$'}/src/${scriptFile.name}" />
382+
<option name="PARAMETERS" value="${userArgs.joinToString(" ")}" />
383+
<module name="" />
384+
<method v="2" />
385+
</configuration>
386+
""".trimIndent()
387+
}
388+
}
389+
359390
private fun URL.fileName() = this.toURI().path.split("/").last()
360391

361392
private fun createSymLink(link: File, target: File) {

0 commit comments

Comments
 (0)