Skip to content

Commit a755c17

Browse files
authored
feat: add support for JBang options (jbangArgs) in JBangTask
1 parent 6d41c0f commit a755c17

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ following properties
3636
|===
3737
| Property | Type | Option | System | Environment | Default | Required
3838
| script | String | jbang-script | jbang.script | JBANG_SCRIPT | | {required-icon}
39+
| jbangArgs | List<String> | jbang-jbang-args | jbang.jbangArgs | JBANG_JBANG_ARGS | [ ] | {optional-icon}
3940
| args | List<String> | jbang-args | jbang.args | JBANG_ARGS | [ ] | {optional-icon}
4041
| trusts | List<String> | jbang-trusts | jbang.trusts | JBANG_TRUSTS | [ ] | {optional-icon}
4142
| version | String | jbang-version | jbang.version | JBANG_VERSION | latest | {optional-icon}

src/main/groovy/dev/jbang/gradle/tasks/JBangTask.groovy

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class JBangTask extends DefaultTask {
7171

7272
private final StringState script
7373
private final StringState version
74+
private final ListState jbangArgs
7475
private final ListState args
7576
private final ListState trusts
7677
private final DirectoryState installDir
@@ -81,6 +82,7 @@ class JBangTask extends DefaultTask {
8182

8283
script = SimpleStringState.of(this, 'jbang.script', '')
8384
version = SimpleStringState.of(this, 'jbang.version', 'latest')
85+
jbangArgs = SimpleListState.of(this, 'jbang.jbangArgs', [])
8486
args = SimpleListState.of(this, 'jbang.args', [])
8587
trusts = SimpleListState.of(this, 'jbang.trusts', [])
8688
installDir = SimpleDirectoryState.of(this, 'jbang.install.dir', jbangCacheDirectory.get())
@@ -96,6 +98,11 @@ class JBangTask extends DefaultTask {
9698
getVersion().set(version)
9799
}
98100

101+
@Option(option = 'jbang-jbang-args', description = 'JBang arguments to be used by JBang when running the script (if any) (OPTIONAL).')
102+
void setJbangArgs(String jbangArgs) {
103+
if (jbangArgs) getJbangArgs().set(jbangArgs.split(',').toList())
104+
}
105+
99106
@Option(option = 'jbang-args', description = 'The arguments to be used in the JBang script (if any) (OPTIONAL).')
100107
void setArgs(String args) {
101108
if (args) getArgs().set(args.split(',').toList())
@@ -118,6 +125,11 @@ class JBangTask extends DefaultTask {
118125
version.property
119126
}
120127

128+
@Internal
129+
ListProperty<String> getJbangArgs() {
130+
jbangArgs.property
131+
}
132+
121133
@Internal
122134
ListProperty<String> getArgs() {
123135
args.property
@@ -146,6 +158,12 @@ class JBangTask extends DefaultTask {
146158
version.provider
147159
}
148160

161+
@Input
162+
@Optional
163+
Provider<List<String>> getResolvedJbangArgs() {
164+
jbangArgs.provider
165+
}
166+
149167
@Input
150168
@Optional
151169
Provider<List<String>> getResolvedArgs() {
@@ -271,7 +289,12 @@ class JBangTask extends DefaultTask {
271289
return
272290
}
273291
List<String> command = command()
274-
command.add(findJBangExecutable() + ' trust add ' + String.join(' ', getResolvedTrusts().get()))
292+
String trustCommand = findJBangExecutable() + ' trust add ' + String.join(' ', getResolvedTrusts().get())
293+
command.add(trustCommand)
294+
295+
// Log the user-friendly trust command
296+
logger.lifecycle("Executing JBang trust command: ${trustCommand}")
297+
275298
ProcessResult result = execute(command)
276299
int exitValue = result.getExitValue()
277300
if (exitValue != 0 && exitValue != 1) {
@@ -283,19 +306,29 @@ class JBangTask extends DefaultTask {
283306
List<String> command = command()
284307
// A single string is needed, because if "sh -c jbang" is used for execution, the parameters need to be passed as single string
285308
StringBuilder executable = new StringBuilder(findJBangExecutable())
286-
executable.append(' run ').append("\"").append(getResolvedScript().get()).append("\"")
309+
executable.append(' run ')
310+
if (getResolvedJbangArgs().get()) {
311+
executable.append(' ').append(String.join(' ', getResolvedJbangArgs().get())).append(' ')
312+
}
313+
314+
executable.append(getResolvedScript().get())
287315
if (getResolvedArgs().get()) {
288316
executable.append(' ').append(String.join(' ', getResolvedArgs().get()))
289317
}
318+
290319
command.add(executable.toString())
320+
321+
// Log the user-friendly JBang command
322+
logger.lifecycle("Executing JBang command: ${executable.toString()}")
323+
291324
ProcessResult result = execute(command)
292325
if (result.getExitValue() != 0) {
293326
throw new IllegalStateException('Error while executing JBang. Exit code: ' + result.getExitValue())
294327
}
295328
}
296329

297330
private ProcessResult execute(List<String> command) throws BuildException {
298-
logger.info "jbang command = $command"
331+
logger.debug "Full command with shell wrapper: $command"
299332
try {
300333
return new ProcessExecutor()
301334
.command(command)
@@ -349,4 +382,4 @@ class JBangTask extends DefaultTask {
349382
}
350383
}
351384
}
352-
}
385+
}

0 commit comments

Comments
 (0)