Skip to content

Commit 78d95e8

Browse files
egor-nholgerbrandl
authored andcommitted
Added support for shortened URLs (#187)
1 parent eaa0f2d commit 78d95e8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,20 @@ fun createTmpScript(scriptText: String, extension: String = "kts"): File {
138138

139139
fun fetchFromURL(scriptURL: String): File {
140140
val urlHash = md5(scriptURL)
141-
val urlExtension = if (scriptURL.endsWith(".kt")) "kt" else "kts"
141+
val scriptText = URL(scriptURL).readText()
142+
val urlExtension = when {
143+
scriptURL.endsWith(".kt") -> "kt"
144+
scriptURL.endsWith(".kts") -> "kts"
145+
else -> if (scriptText.contains("fun main")) {
146+
"kt"
147+
} else {
148+
"kts"
149+
}
150+
}
142151
val urlCache = File(KSCRIPT_CACHE_DIR, "/url_cache_${urlHash}.$urlExtension")
143152

144153
if (!urlCache.isFile) {
145-
urlCache.writeText(URL(scriptURL).readText())
154+
urlCache.writeText(scriptText)
146155
}
147156

148157
return urlCache

test/test_suite.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ assert "kscript i_do_not_exist.kts 2>&1" "[kscript] [ERROR] Could not read scrip
7777

7878
## make sure that it runs with remote URLs
7979
assert "kscript https://raw.githubusercontent.com/holgerbrandl/kscript/master/test/resources/url_test.kts" "I came from the internet"
80+
assert "kscript https://git.io/fxHBv" "main was called"
8081

8182
## there are some dependencies which are not jar, but maybe pom, aar, ..
8283
## make sure they work, too

0 commit comments

Comments
 (0)