Skip to content

Commit bcee348

Browse files
committed
added --idea launcher for kscriptlets
1 parent fbd6ae5 commit bcee348

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,74 @@ private fun bytesToHex(buffer: ByteArray): String {
147147
fun numLines(str: String) = str.split("\r\n|\r|\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().size
148148

149149

150-
fun info(msg: String) = System.err.println(msg)
150+
fun info(msg: String) = System.err.println(msg)
151+
152+
153+
fun launchIdeaWithKscriptlet(scriptFile: File, dependencies: List<String>): String {
154+
System.err.println("Setting up idea project from ${scriptFile}")
155+
156+
// val tmpProjectDir = createTempDir("edit_kscript", suffix="")
157+
// .run { File(this, "kscript_tmp_project") }
158+
// .apply { mkdir() }
159+
160+
161+
// fixme use tmp instead of cachdir. Fails for now because idea gradle import does not seem to like tmp
162+
val tmpProjectDir = KSCRIPT_CACHE_DIR
163+
.run { File(this, "kscript_tmp_project__${scriptFile.name}") }
164+
.apply { mkdir() }
165+
// val tmpProjectDir = File("/Users/brandl/Desktop/")
166+
// .run { File(this, "kscript_tmp_project") }
167+
// .apply { mkdir() }
168+
169+
val stringifiedDeps = dependencies.map { " compile \"$it\"" }.joinToString("\n")
170+
171+
val gradleScript = """
172+
group 'com.github.holgerbrandl.kscript.editor'
173+
version '0.1-SNAPSHOT'
174+
175+
apply plugin: 'java'
176+
apply plugin: 'kotlin'
177+
178+
179+
dependencies {
180+
compile "org.jetbrains.kotlin:kotlin-stdlib:${'$'}kotlin_version"
181+
$stringifiedDeps
182+
}
183+
184+
repositories {
185+
mavenCentral()
186+
jcenter()
187+
}
188+
189+
sourceSets {
190+
main {
191+
java {
192+
srcDirs 'src'
193+
}
194+
}
195+
}
196+
197+
buildscript {
198+
ext.kotlin_version = '1.1.4'
199+
200+
repositories {
201+
jcenter()
202+
}
203+
204+
dependencies {
205+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${'$'}kotlin_version"
206+
}
207+
}
208+
"""
209+
210+
File(tmpProjectDir, "build.gradle").writeText(gradleScript)
211+
212+
// also copy script reource in
213+
File(tmpProjectDir, "src").apply {
214+
mkdir()
215+
scriptFile.copyTo(File(this, scriptFile.name))
216+
217+
}
218+
219+
return "idea ${tmpProjectDir.absolutePath}"
220+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Use '--self-update' to wipe cached script jars and urls
3535
3636
Options:
3737
-i --interactive Create interactive shell with dependencies as declared in script
38+
--idea Open script in temporary Intellij session
3839
3940
Copyright : 2017 Holger Brandl
4041
License : MIT
@@ -102,6 +103,14 @@ fun main(args: Array<String>) {
102103
.flatMap { it.split(";", ",", " ") }
103104
.map(String::trim)
104105

106+
107+
// Create temopary dev environment
108+
if (docopt.getBoolean("idea")) {
109+
println(launchIdeaWithKscriptlet(scriptFile, dependencies))
110+
exitProcess(0)
111+
}
112+
113+
105114
val classpath = resolveDependencies(dependencies)
106115

107116
// Extract kotlin arguments

0 commit comments

Comments
 (0)