@@ -147,4 +147,74 @@ private fun bytesToHex(buffer: ByteArray): String {
147147fun 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+ }
0 commit comments