Skip to content

Commit 04114b4

Browse files
committed
Fixes for tests
Signed-off-by: Marcin Kuszczak <[email protected]>
1 parent 1c97c21 commit 04114b4

File tree

11 files changed

+85
-88
lines changed

11 files changed

+85
-88
lines changed

src/main/kotlin/io/github/kscripting/kscript/KscriptHandler.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import io.github.kscripting.kscript.util.FileUtils.getArtifactsRecursively
1414
import io.github.kscripting.kscript.util.Logger.info
1515
import io.github.kscripting.kscript.util.Logger.infoMsg
1616
import io.github.kscripting.kscript.util.Logger.warnMsg
17-
import io.github.kscripting.shell.model.ScriptType
1817
import java.net.URI
1918

2019
class KscriptHandler(
@@ -90,12 +89,6 @@ class KscriptHandler(
9089
return
9190
}
9291

93-
// Even if we just need and support the @file:EntryPoint directive in case of kt-class
94-
// files, we extract it here to fail if it was used in kts files.
95-
if (script.entryPoint != null && script.scriptLocation.scriptType == ScriptType.KTS) {
96-
throw IllegalStateException("@file:EntryPoint directive is just supported for kt class files")
97-
}
98-
9992
val jar = cache.getOrCreateJar(script.digest) { basePath ->
10093
JarArtifactCreator(executor).create(basePath, script, resolvedDependencies)
10194
}

src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.kscripting.kscript.code
22

33
import io.github.kscripting.kscript.model.KotlinOpt
4-
import io.github.kscripting.kscript.model.PackageName
54
import io.github.kscripting.shell.model.ScriptType
65
import org.intellij.lang.annotations.Language
76
import java.time.ZonedDateTime
@@ -45,21 +44,6 @@ object Templates {
4544
""".trimStart().trimMargin()
4645
}
4746

48-
fun createWrapperForScript(packageName: PackageName, className: String): String {
49-
val classReference = packageName.value + "." + className
50-
51-
return """
52-
|class Main_${className}{
53-
| companion object {
54-
| @JvmStatic
55-
| fun main(args: Array<String>) {
56-
| val script = Main_${className}::class.java.classLoader.loadClass("$classReference")
57-
| script.getDeclaredConstructor(Array<String>::class.java).newInstance(args);
58-
| }
59-
| }
60-
|}""".trimStart().trimMargin()
61-
}
62-
6347
fun createRunConfig(rootScriptName: String, rootScriptType: ScriptType, userArgs: List<String>): String {
6448
val rootFileName = rootScriptName + rootScriptType.extension
6549
val userArgsString = userArgs.joinToString(" ") { it }
@@ -104,14 +88,14 @@ object Templates {
10488

10589

10690
fun createUsageInfo(selfName: String) =
107-
"""|Usage:
91+
"""|Usage:
10892
| $selfName [options] <script> [<script_args>]...
10993
| $selfName --clear-cache [--development]
11094
| $selfName (--help | --version) [--development]""".trimMargin()
11195

11296

11397
fun createFooterInfo() =
114-
"""|
98+
"""|
11599
|Copyright : 2022 Holger Brandl, Marcin Kuszczak
116100
|Website : https://github.com/kscripting/kscript
117101
|License : MIT""".trimMargin()

src/main/kotlin/io/github/kscripting/kscript/creator/JarArtifactCreator.kt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package io.github.kscripting.kscript.creator
22

3-
import io.github.kscripting.kscript.code.Templates
43
import io.github.kscripting.kscript.model.Script
54
import io.github.kscripting.kscript.util.Executor
65
import io.github.kscripting.kscript.util.FileUtils
7-
import io.github.kscripting.shell.model.*
6+
import io.github.kscripting.shell.model.OsPath
7+
import io.github.kscripting.shell.model.writeText
88

99
data class JarArtifact(val path: OsPath, val execClassName: String)
1010

@@ -19,14 +19,11 @@ class JarArtifactCreator(private val executor: Executor) {
1919
.let { if ("^[0-9]".toRegex().containsMatchIn(it)) "_$it" else it }
2020

2121
// Define the entrypoint for the scriptlet jar
22-
val execClassName = if (script.scriptLocation.scriptType == ScriptType.KTS) {
23-
"Main_${className}"
24-
} else {
25-
"""${script.packageName.value}.${script.entryPoint?.value ?: "${className}Kt"}"""
26-
}
22+
val execClassName = "${script.entryPoint.value}Kt"
23+
2724

2825
val jarFile = basePath.resolve("scriplet.jar")
29-
val scriptFile = basePath.resolve(className + script.scriptLocation.scriptType.extension)
26+
val scriptFile = basePath.resolve("$className.kt")
3027
val execClassNameFile = basePath.resolve("scripletExecClassName.txt")
3128

3229
execClassNameFile.writeText(execClassName)
@@ -36,14 +33,6 @@ class JarArtifactCreator(private val executor: Executor) {
3633
val filesToCompile = mutableSetOf<OsPath>()
3734
filesToCompile.add(scriptFile)
3835

39-
// create main-wrapper for kts scripts
40-
if (script.scriptLocation.scriptType == ScriptType.KTS) {
41-
val wrapper = FileUtils.createFile(
42-
basePath.resolve("$execClassName.kt"), Templates.createWrapperForScript(script.packageName, className)
43-
)
44-
filesToCompile.add(wrapper)
45-
}
46-
4736
executor.compileKotlin(jarFile, resolvedDependencies, filesToCompile, script.compilerOpts)
4837

4938
return JarArtifact(jarFile, execClassName)

src/main/kotlin/io/github/kscripting/kscript/model/Script.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ data class Script(
88
val resolvedCode: String,
99

1010
val packageName: PackageName,
11-
val entryPoint: Entry?,
11+
val entryPoint: Entry,
1212
val importNames: Set<ImportName>,
1313

1414
val includes: Set<Include>,

src/main/kotlin/io/github/kscripting/kscript/resolver/ScriptResolver.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ScriptResolver(
1212
private val sectionResolver: SectionResolver,
1313
private val scriptingConfig: ScriptingConfig
1414
) {
15-
private val scripletName = "scriplet"
15+
private val scripletName = "Scriplet"
1616

1717
//level parameter - for how many levels should include be resolved
1818
//level 0 - do not resolve includes in base file and any other embedded
@@ -148,9 +148,17 @@ class ScriptResolver(
148148
resolutionContext
149149
)
150150

151+
// Even if we just need and support the @file:EntryPoint directive in case of kt-class
152+
// files, we extract it here to fail if it was used in kts files.
153+
if (resolutionContext.entryPoint != null && scriptLocation.scriptType == ScriptType.KTS) {
154+
throw IllegalStateException("@file:EntryPoint directive is just supported for kt class files")
155+
}
156+
151157
val scriptNode = ScriptNode(scriptLocation, sections)
152158
resolutionContext.scriptNodes.add(scriptNode)
159+
153160
resolutionContext.packageName = resolutionContext.packageName ?: PackageName("kscript.scriplet")
161+
resolutionContext.entryPoint = resolutionContext.entryPoint ?: Entry("${resolutionContext.packageName!!.value}.${scriptLocation.scriptName}")
154162

155163
val code = ScriptUtils.resolveCode(resolutionContext.packageName, resolutionContext.importNames, scriptNode)
156164

@@ -166,7 +174,7 @@ class ScriptResolver(
166174
scriptLocation,
167175
code,
168176
resolutionContext.packageName!!,
169-
resolutionContext.entryPoint,
177+
resolutionContext.entryPoint!!,
170178
resolutionContext.importNames,
171179
resolutionContext.includes,
172180
resolutionContext.dependencies,

src/main/kotlin/io/github/kscripting/kscript/util/ScriptUtils.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,37 @@ object ScriptUtils {
5252
sb.append("import ${it.value}\n")
5353
}
5454

55-
resolveSimpleCode(sb, scriptNode)
55+
var indent = ""
56+
var shouldPrependEmptyLine = false
57+
if (scriptNode.scriptLocation.scriptType == ScriptType.KTS) {
58+
sb.appendLine("fun main(args: Array<String>) {")
59+
indent = " "
60+
shouldPrependEmptyLine = true
61+
}
62+
63+
resolveSimpleCode(sb, scriptNode, shouldPrependEmptyLine = shouldPrependEmptyLine, indent = indent)
64+
65+
if (scriptNode.scriptLocation.scriptType == ScriptType.KTS) {
66+
sb.appendLine("}")
67+
}
5668

5769
return sb.toString()
5870
}
5971

60-
private fun resolveSimpleCode(sb: StringBuilder, scriptNode: ScriptNode, lastLineIsEmpty: Boolean = false) {
61-
var isLastLineEmpty = lastLineIsEmpty
72+
private fun resolveSimpleCode(
73+
sb: StringBuilder,
74+
scriptNode: ScriptNode,
75+
shouldPrependEmptyLine: Boolean = false,
76+
indent: String = ""
77+
) {
78+
var isLastLineEmpty = shouldPrependEmptyLine
6279

6380
for (section in scriptNode.sections) {
6481
val scriptNodes = section.scriptAnnotations.filterIsInstance<ScriptNode>()
6582

6683
if (scriptNodes.isNotEmpty()) {
6784
val subNode = scriptNodes.single()
68-
resolveSimpleCode(sb, subNode, isLastLineEmpty)
85+
resolveSimpleCode(sb, subNode, isLastLineEmpty, indent = indent)
6986
continue
7087
}
7188

@@ -75,7 +92,7 @@ object ScriptUtils {
7592
}
7693

7794
if (section.code.isNotEmpty() || (!isLastLineEmpty && section.code.isEmpty())) {
78-
sb.append(section.code).append('\n')
95+
sb.append(indent).append(section.code).append('\n')
7996
}
8097

8198
isLastLineEmpty = section.code.isEmpty()

src/test/kotlin/io/github/kscripting/kscript/code/GradleTemplatesTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class GradleTemplatesTest {
1818
scriptLocation = scriptLocation,
1919
resolvedCode = "code",
2020
packageName = PackageName("package"),
21-
entryPoint = null,
21+
entryPoint = Entry("Scriptlet"),
2222
importNames = setOf(),
2323
includes = setOf(),
2424
dependencies = setOf(),
@@ -66,7 +66,7 @@ class GradleTemplatesTest {
6666
scriptLocation = scriptLocation,
6767
resolvedCode = "code",
6868
packageName = PackageName("package"),
69-
entryPoint = null,
69+
entryPoint = Entry("Scriptlet"),
7070
importNames = setOf(),
7171
includes = setOf(),
7272
dependencies = setOf(),

src/test/kotlin/io/github/kscripting/kscript/resolver/ScriptResolverTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ScriptResolverTest {
5454

5555
assertThat(script).apply {
5656
prop(Script::packageName).isEqualTo(defaultPackageName)
57-
prop(Script::entryPoint).isEqualTo(null)
57+
prop(Script::entryPoint).isEqualTo(Entry("kscript.scriplet.template"))
5858
prop(Script::importNames).isEqualTo(
5959
setOf(
6060
ImportName("java.io.BufferedReader"),
@@ -103,7 +103,7 @@ class ScriptResolverTest {
103103

104104
assertThat(script).apply {
105105
prop(Script::packageName).isEqualTo(defaultPackageName)
106-
prop(Script::entryPoint).isEqualTo(null)
106+
prop(Script::entryPoint).isEqualTo(Entry("kscript.scriplet.include_variations"))
107107
prop(Script::importNames).isEmpty()
108108
prop(Script::includes).isEqualTo(
109109
setOf(
@@ -149,7 +149,7 @@ class ScriptResolverTest {
149149

150150
assertThat(script).apply {
151151
prop(Script::packageName).isEqualTo(defaultPackageName)
152-
prop(Script::entryPoint).isEqualTo(null)
152+
prop(Script::entryPoint).isEqualTo(Entry("kscript.scriplet.dup_include"))
153153
prop(Script::importNames).isEmpty()
154154
prop(Script::includes).isEqualTo(
155155
setOf(Include("dup_include_1.kt"), Include("dup_include_2.kt"))
@@ -163,5 +163,5 @@ class ScriptResolverTest {
163163
}
164164
}
165165

166-
private fun String.discardEmptyLines(): String = this.lines().filterNot { it.isEmpty() }.joinToString("\n")
166+
private fun String.discardEmptyLines(): String = this.lines().filterNot { it.isBlank() }.joinToString("\n")
167167
}

test/resources/consolidate_includes/expected.kts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ import java.io.File
55
import java.io.InputStream
66
import java.net.URL
77

8-
fun importStuff() = File(".")
8+
fun main(args: Array<String>) {
9+
fun importStuff() = File(".")
910

10-
importStuff()
11-
fun importMoreStuff() = File(".")
11+
importStuff()
12+
fun importMoreStuff() = File(".")
1213

13-
fun foo() = File(".").toURL()
14+
fun foo() = File(".").toURL()
1415

15-
fun include_4() = println("include_4")
16-
fun include_5() = BufferedReader::class
17-
fun include_6() = InputStream::class
16+
fun include_4() = println("include_4")
17+
fun include_5() = BufferedReader::class
18+
fun include_6() = InputStream::class
1819

19-
include_5()
20-
importMoreStuff()
20+
include_5()
21+
importMoreStuff()
22+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package kscript.scriplet
22

3-
4-
fun dup_include_2() = println("dup_include")
5-
dup_include_2()
3+
fun main(args: Array<String>) {
4+
fun dup_include_2() = println("dup_include")
5+
dup_include_2()
6+
}

0 commit comments

Comments
 (0)