Repository navigation
Conversation
|
I tested with Gradle 7.4.2. |
|
OK, there were three packaging tests. I've commented one out (it was looking to execute a non-existent jar file, and we don't do that now, anyway - we execute scripts that contain jars inside them). The other two tests pass after some minor changes. |
|
Hi! Your code works wonderfully 👍 I think that we should apply it to the branch kscript_4.1 as there is a new development. In this branch, I have added OS independent paths, so testing multi-OS behavior should be easier. There are only a few changes that may affect your patch:
So please rebase this patch to kscript_4.1. I will add also some comments to your code. |
| } | ||
|
|
||
| //Capsule: https://github.com/ngyewch/gradle-capsule-plugin | ||
| fun hangingIndents(s: String, n: Int): String { |
There was a problem hiding this comment.
Do we really need this function? Kotlin has nice support for stripping unnecessary margins. In the branch kscript_4.1 I am using .trimMargin() which allows controlling margins very precisely. If it does not cover everything please describe shortly use case here, so that it will be easier to understand what is missing.
There was a problem hiding this comment.
trimMargin() trims margins. This function adds a margin of n spaces on all except the first line. This is so that multi-line dependencies look nice in the output. It's purely cosmetic, but with it, the dependencies section in the package's build.gradle.kts looks something like this:
dependencies {
implementation(files("/home/vinay/.kscript/cache/jar_db71153912c566b4d192ee7f36d85a8a/scriplet.jar"))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.6.21")
implementation("com.github.ajalt.clikt:clikt:3.4.2")
}
Without it, you get
dependencies {
implementation(files("/home/vinay/.kscript/cache/jar_cfdf0ec49939ebcc9ca0b9a9cbe98f4c/scriplet.jar"))
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.6.21")
implementation("com.github.ajalt.clikt:clikt:3.4.2")
}
So functionally the same, but looks uglier.
There was a problem hiding this comment.
It is a very specific functionality (add indent to all lines except the first line), which can be replaced by creating the template in a bit different way. I have done it like this:
Please notice that the text, which should be inserted is left-justified, and is not following "normal" indentation. I think it is not worth adding an additional indentation function, just to solve this specific use case. Bear in mind that it should be then tested, you should consider corner cases, etc.
| ) + script.dependencies | ||
|
|
||
| val capsuleApp = jarArtifact.execClassName | ||
| val baseName = script.scriptName.dropExtension() |
There was a problem hiding this comment.
dropExtension shouldn't be needed in kscript_4.1
There was a problem hiding this comment.
Thanks, will bear in mind when I merge to that
| } | ||
|
|
||
| /* | ||
| * Old code here for now, but will be removed soon! |
There was a problem hiding this comment.
Please remove old, commented code.
| } | ||
| } | ||
| val opts = parts.joinToString(" ").trim() | ||
| hdr = hdr.replace("java -jar", "java $opts -jar") |
There was a problem hiding this comment.
It will be better to create a function for the generation of exectuteHeader and pass $opts as an argument to this function than to replace the value textually.
There was a problem hiding this comment.
Not sure exactly what you mean, but I'll have a go.
| tasks.register("makeScript") { | ||
| dependsOn(":jar") | ||
| doLast { | ||
| val hdr = layout.projectDirectory.toString() |
There was a problem hiding this comment.
Please use more descriptive names for variables. Is it possible to simplify this code?
| executor.createPackage(basePath) | ||
|
|
||
| basePath.resolve("build/libs/appName").toFile().setExecutable(true) | ||
| val ef = basePath.resolve("build/libs/$baseName").toFile() |
There was a problem hiding this comment.
Please use descriptive names -> executableFile
| val ef = basePath.resolve("build/libs/$baseName").toFile() | ||
| ef.setExecutable(true) | ||
|
|
||
| infoMsg("Finished packaging '${script.scriptName}'; executable path: ${basePath}/build/libs/$baseName") |
There was a problem hiding this comment.
This message is not printed if the package is already in cache. Can we print the executable path every time, even if it comes from the cache? As we do not copy file (which I think is okay) we should at least communicate full executable path to the user.
Currently, this is printed whenever new package is created:
[kscript] Packaging script 'url_test.kts' into standalone executable...
[kscript] Finished packaging 'url_test.kts'; executable path: /home/vagrant/.kscript/cache/package_5a501be28df8e8a4ace2de5f8162d0ee/build/libs/url_test
[kscript] Package created in: /home/vagrant/.kscript/cache/package_5a501be28df8e8a4ace2de5f8162d0ee
And only this if it comes from cache:
[kscript] Package created in: /home/vagrant/.kscript/cache/package_5a501be28df8e8a4ace2de5f8162d0ee
There was a problem hiding this comment.
The current behavior is unintuitive when the package is already in cache. The exact intended behavior of --package is unclear - with a clean cache, the packaged executable is placed in the current directory at the end. With a populated cache, it isn't (even if it is absent) - this doesn't seem intuitive. I would expect the following behavior:
if (package not in cache) {
create package in cache
}
if (built executable not in cache) {
build package using Gradle
report success or failure of build
}
if (built executable in cache) {
copy executable to current directory
}
report package location and executable path in cache, or package location and error information
Isn't this better than what we have now?
There was a problem hiding this comment.
I am not a big fan of automatically copying files around - I would rather clearly communicate to the user where is the resulting file. Then it will be just enough to check in Cache.getOrCreatePackage() if the package file is already there or not. Currently, there is only a check for directory - if it exists then it is assumed that the package exists, which might be the wrong assumption. So definitely cache condition has to be improved: if there is a directory, but no package, PackageCreator should be started again. From my point of view, I would rather not copy files to the current directory, but in all cases (cached/not cached) communicate to user where is package located.
There was a problem hiding this comment.
An additional advantage of not copying files in 4.1 will be that PackageCreator would be more testable with only Junit Tests. With OsPath there are mostly no references to the underlying system, except for some utility classes, which possibly can be mocked.
|
OK, I've addressed most of the comments and added questions where things are unclear. Please take a look, and I will then look at merging with 4.1 in a separate PR. |
aartiPl
left a comment
There was a problem hiding this comment.
Please rebase on 4.1 branch - I think we shouldn't introduce any more breaking changes in 4.0
| } | ||
|
|
||
| //Capsule: https://github.com/ngyewch/gradle-capsule-plugin | ||
| fun hangingIndents(s: String, n: Int): String { |
There was a problem hiding this comment.
It is a very specific functionality (add indent to all lines except the first line), which can be replaced by creating the template in a bit different way. I have done it like this:
Please notice that the text, which should be inserted is left-justified, and is not following "normal" indentation. I think it is not worth adding an additional indentation function, just to solve this specific use case. Bear in mind that it should be then tested, you should consider corner cases, etc.
| val ef = basePath.resolve("build/libs/$baseName").toFile() | ||
| ef.setExecutable(true) | ||
|
|
||
| infoMsg("Finished packaging '${script.scriptName}'; executable path: ${basePath}/build/libs/$baseName") |
There was a problem hiding this comment.
I am not a big fan of automatically copying files around - I would rather clearly communicate to the user where is the resulting file. Then it will be just enough to check in Cache.getOrCreatePackage() if the package file is already there or not. Currently, there is only a check for directory - if it exists then it is assumed that the package exists, which might be the wrong assumption. So definitely cache condition has to be improved: if there is a directory, but no package, PackageCreator should be started again. From my point of view, I would rather not copy files to the current directory, but in all cases (cached/not cached) communicate to user where is package located.
| val ef = basePath.resolve("build/libs/$baseName").toFile() | ||
| ef.setExecutable(true) | ||
|
|
||
| infoMsg("Finished packaging '${script.scriptName}'; executable path: ${basePath}/build/libs/$baseName") |
There was a problem hiding this comment.
An additional advantage of not copying files in 4.1 will be that PackageCreator would be more testable with only Junit Tests. With OsPath there are mostly no references to the underlying system, except for some utility classes, which possibly can be mocked.
Done, see #370. Note that |
Sure, but that's a change to existing behavior, isn't it? For example, the packaging tests expect the executable to be placed in the current directory. Of course, they could be changed, but the cache package location for the executable could change every time because of hash changes. So I'm not the current simplistic Bash-based tests will cope ... |
OK, I'll see about removing it. Edit: |
|
This PR was ported to the 4.1 branch and is now merged. |

Packaging should now work with Gradle 7.