Skip to content

Fix #335: Remove the need for the Gradle capsule plugin. - #364

Closed
vsajip wants to merge 5 commits into
kscripting:masterfrom
vsajip:packaging
Closed

vsajip wants to merge 5 commits into
kscripting:masterfrom
vsajip:packaging

Conversation

@vsajip

@vsajip vsajip commented Jun 10, 2022

Copy link
Copy Markdown
Contributor

Packaging should now work with Gradle 7.

@vsajip

vsajip commented Jun 10, 2022

Copy link
Copy Markdown
Contributor Author

I tested with Gradle 7.4.2.

@vsajip vsajip mentioned this pull request Jun 10, 2022
@vsajip

vsajip commented Jun 10, 2022

Copy link
Copy Markdown
Contributor Author

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.

@aartiPl

aartiPl commented Jun 19, 2022

Copy link
Copy Markdown
Collaborator

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:

  1. All occurrences of Path were changed to OsPath. Generally, all common functionality should be covered in OsPathExt.kt
  2. In parsed script fileName is just a file name without extension - you don't have to use dropExtension in your code
  3. test_suite.sh -> linux_suite.sh
  4. Unit test for CommandResolver still fails - I was working on Windows cmd support, but it is really deeply screwed up, so it takes a lot of time

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

obraz

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()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropExtension shouldn't be needed in kscript_4.1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will bear in mind when I merge to that

}

/*
* Old code here for now, but will be removed soon!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove old, commented code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
}
val opts = parts.joinToString(" ").trim()
hdr = hdr.replace("java -jar", "java $opts -jar")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exactly what you mean, but I'll have a go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

tasks.register("makeScript") {
dependsOn(":jar")
doLast {
val hdr = layout.projectDirectory.toString()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use more descriptive names for variables. Is it possible to simplify this code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

executor.createPackage(basePath)

basePath.resolve("build/libs/appName").toFile().setExecutable(true)
val ef = basePath.resolve("build/libs/$baseName").toFile()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use descriptive names -> executableFile

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

val ef = basePath.resolve("build/libs/$baseName").toFile()
ef.setExecutable(true)

infoMsg("Finished packaging '${script.scriptName}'; executable path: ${basePath}/build/libs/$baseName")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@vsajip vsajip Jun 20, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@aartiPl aartiPl Jun 21, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@aartiPl aartiPl Jun 21, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vsajip

vsajip commented Jun 20, 2022

Copy link
Copy Markdown
Contributor Author

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.

@vsajip
vsajip requested a review from aartiPl June 20, 2022 08:26

@aartiPl aartiPl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

obraz

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")

@aartiPl aartiPl Jun 21, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

@aartiPl aartiPl Jun 21, 2022 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vsajip

vsajip commented Jun 21, 2022

Copy link
Copy Markdown
Contributor Author

Please rebase on 4.1 branch - I think we shouldn't introduce any more breaking changes in 4.0

Done, see #370. Note that --package was already broken, hence this issue.

@vsajip

vsajip commented Jun 21, 2022

Copy link
Copy Markdown
Contributor Author

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.

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 ...

@vsajip

vsajip commented Jun 21, 2022 •

Copy link
Copy Markdown
Contributor Author

I think it is not worth adding an additional indentation function

OK, I'll see about removing it.

Edit: prependIndent() only indents the first line, so you get an ugly result in build.gradle.kts. Still, it works, so I don't mind removing the hangingIndent() and the call to it.

@aartiPl

aartiPl commented Jul 1, 2022

Copy link
Copy Markdown
Collaborator

This PR was ported to the 4.1 branch and is now merged.

@aartiPl aartiPl closed this Jul 1, 2022
@vsajip
vsajip deleted the packaging branch July 1, 2022 17:56
@vsajip
vsajip restored the packaging branch July 1, 2022 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants