Skip to content

Commit a857ab8

Browse files
committed
fixed java9 support (fixes #41)
1 parent aaa722e commit a857ab8

File tree

5 files changed

+93
-52
lines changed

5 files changed

+93
-52
lines changed

NEWS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
## v2.0.0
1+
## v2.0.X
22

33
* Reimplemented in kotlin (fixes [#36](https://github.com/holgerbrandl/kscript/issues/36))
44
* Added cygwin support (fixes [#39](https://github.com/holgerbrandl/kscript/issues/39))
55
* Added `//INCLUDE` directive (fixes [#34](https://github.com/holgerbrandl/kscript/issues/34)
6+
* Fixed: interactive mode is not correctly started when using stdin as script argument ([#40](https://github.com/holgerbrandl/kscript/issues/40)
7+
* Fixed compatibility with java9 ([#41](https://github.com/holgerbrandl/kscript/issues/41))
68

79

810
## v1.5.1

notes.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11

22

3+
## macos setup
4+
5+
change java version
6+
https://stackoverflow.com/questions/21964709/how-to-set-or-change-the-default-java-jdk-version-on-os-x
7+
8+
```
9+
export JAVA_HOME=`/usr/libexec/java_home -v 9.0.1`
10+
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_151`
11+
```
12+
313
## launcher
414

515
https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package kscript.app
22

33
import java.io.File
4+
import java.net.URL
5+
import java.nio.file.Files
6+
import java.nio.file.Paths
7+
import java.security.MessageDigest
48
import kotlin.system.exitProcess
59

610
data class ProcessResult(val command: String, val exitCode: Int, val stdout: String, val stderr: String) {
@@ -55,10 +59,12 @@ object ShellUtils {
5559

5660
}
5761

62+
5863
fun errorMsg(msg: String) {
5964
System.err.println("[ERROR] " + msg)
6065
}
6166

67+
6268
fun errorIf(value: Boolean, lazyMessage: () -> Any) {
6369
if (value) {
6470
errorMsg(lazyMessage().toString())
@@ -70,3 +76,75 @@ fun quit(status: Int): Nothing {
7076
print(if (status == 0) "true" else "false")
7177
exitProcess(status)
7278
}
79+
80+
/** see discussion on https://github.com/holgerbrandl/kscript/issues/15*/
81+
fun guessKotlinHome(): String? {
82+
return evalBash("KOTLIN_RUNNER=1 JAVACMD=echo kotlinc").stdout.run {
83+
"kotlin.home=([^\\s]*)".toRegex()
84+
.find(this)?.groups?.get(1)?.value
85+
}
86+
}
87+
88+
89+
fun createTmpScript(scriptText: String): File {
90+
return File(SCRIPT_TEMP_DIR, "scriptlet.${md5(scriptText)}.kts").apply {
91+
writeText(scriptText)
92+
}
93+
}
94+
95+
96+
fun fetchFromURL(scriptURL: String): File? {
97+
val urlHash = md5(scriptURL)
98+
val urlCache = File(KSCRIPT_CACHE_DIR, "/url_cache_${urlHash}.kts")
99+
100+
if (!urlCache.isFile) {
101+
urlCache.writeText(URL(scriptURL).readText())
102+
}
103+
104+
return urlCache
105+
}
106+
107+
108+
fun md5(byteProvider: () -> ByteArray): String {
109+
// from https://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java
110+
val md = MessageDigest.getInstance("MD5")
111+
md.update(byteProvider())
112+
113+
// disabled to support java9 which dropeed DataTypeConverter
114+
// md.update(byteProvider())
115+
// val digestInHex = DatatypeConverter.printHexBinary(md.digest()).toLowerCase()
116+
117+
val digestInHex = bytesToHex(md.digest()).toLowerCase()
118+
119+
return digestInHex.substring(0, 16)
120+
}
121+
122+
fun md5(msg: String) = md5 { msg.toByteArray() }
123+
124+
125+
fun md5(file: File) = md5 { Files.readAllBytes(Paths.get(file.toURI())) }
126+
127+
128+
// from https://github.com/frontporch/pikitis/blob/master/src/test/kotlin/repacker.tests.kt
129+
private fun bytesToHex(buffer: ByteArray): String {
130+
val HEX_CHARS = "0123456789ABCDEF".toCharArray()
131+
132+
val len = buffer.count()
133+
val result = StringBuffer(len * 2)
134+
var ix = 0
135+
while (ix < len) {
136+
val octet = buffer[ix].toInt()
137+
val firstIndex = (octet and 0xF0).ushr(4)
138+
val secondIndex = octet and 0x0F
139+
result.append(HEX_CHARS[firstIndex])
140+
result.append(HEX_CHARS[secondIndex])
141+
ix++
142+
}
143+
return result.toString()
144+
}
145+
146+
147+
fun numLines(str: String) = str.split("\r\n|\r|\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().size
148+
149+
150+
fun info(msg: String) = System.err.println(msg)

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

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import java.io.File
77
import java.io.FileInputStream
88
import java.io.InputStreamReader
99
import java.net.URL
10-
import java.nio.file.Files
11-
import java.nio.file.Paths
12-
import java.security.MessageDigest
13-
import javax.xml.bind.DatatypeConverter
1410
import kotlin.system.exitProcess
1511

1612

@@ -241,14 +237,6 @@ fun versionCheck() {
241237
}
242238
}
243239

244-
/** see discussion on https://github.com/holgerbrandl/kscript/issues/15*/
245-
private fun guessKotlinHome(): String? {
246-
return evalBash("KOTLIN_RUNNER=1 JAVACMD=echo kotlinc").stdout.run {
247-
"kotlin.home=([^\\s]*)".toRegex()
248-
.find(this)?.groups?.get(1)?.value
249-
}
250-
}
251-
252240
fun prepareScript(scriptResource: String): File {
253241
var scriptFile: File?
254242

@@ -321,40 +309,3 @@ fun prepareScript(scriptResource: String): File {
321309
}
322310

323311

324-
fun createTmpScript(scriptText: String): File {
325-
return File(SCRIPT_TEMP_DIR, "scriptlet.${md5(scriptText)}.kts").apply {
326-
writeText(scriptText)
327-
}
328-
}
329-
330-
fun fetchFromURL(scriptURL: String): File? {
331-
val urlHash = md5(scriptURL)
332-
val urlCache = File(KSCRIPT_CACHE_DIR, "/url_cache_${urlHash}.kts")
333-
334-
if (!urlCache.isFile) {
335-
urlCache.writeText(URL(scriptURL).readText())
336-
}
337-
338-
return urlCache
339-
}
340-
341-
342-
fun md5(byteProvider: () -> ByteArray): String {
343-
// from https://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java
344-
val md = MessageDigest.getInstance("MD5")
345-
md.update(byteProvider())
346-
347-
val digestInHex = DatatypeConverter.printHexBinary(md.digest()).toLowerCase()
348-
349-
return digestInHex.substring(0, 16)
350-
}
351-
352-
fun md5(msg: String) = md5 { msg.toByteArray() }
353-
354-
fun md5(file: File) = md5 { Files.readAllBytes(Paths.get(file.toURI())) }
355-
356-
357-
private fun numLines(str: String) =
358-
str.split("\r\n|\r|\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray().size
359-
360-
fun info(msg: String) = System.err.println(msg)

test/test_suite.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/bin/bash -x
2-
#/usr/bin/env bash +x
1+
#!/usr/bin/env bash
2+
#/bin/bash -x
33

44
export DEBUG="--verbose"
55

0 commit comments

Comments
 (0)