Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use {basename} in template file-refs #1318

Open
nandorholozsnyak opened this issue Mar 20, 2022 · 3 comments · May be fixed by #1319
Open

Unable to use {basename} in template file-refs #1318

nandorholozsnyak opened this issue Mar 20, 2022 · 3 comments · May be fixed by #1319
Labels
bug Something isn't working

Comments

@nandorholozsnyak
Copy link
Contributor

Describe the bug
JBang templates are providing ways to reuse the file input name coming as an input from the user. It can be {filename} and {basename}.
If the input is Example.java (jbang init Example.java) the {filename} variable will be Example.java and the {basename} will be Example. That is cool, BUT, look at the following use case:

...
"q-aws-lambda-tf": {
      "file-refs": {
        "{filename}": "aws/aws-lambda.java.qute",
        "generate-jar": "aws/generate-jar",
        "generate-native-image": "aws/generate-native-image",
        "application.properties": "aws/application.properties",
        "lambda-aws-{basename}.tf": "aws/lambda-aws.tf.qute"
      },
...

If I would like to go with this approach (jbang init -t=q-aws-lambda-tf Example.java) the last entry with "lambda-aws-{basename}.tf": "aws/lambda-aws.tf.qute" will cause an error for the Init command because its extension is not .java, it is .tf and the Init commands checks if the {basename} variable is found in a file entry key, then it validates that the entry's key extension matches the input file extension.

Here is the code for the check:

static Path resolveBaseName(String refTarget, String refSource, String outName) {
		String result = refTarget;
		if (dev.jbang.cli.Template.TPL_FILENAME_PATTERN.matcher(refTarget).find()
				|| dev.jbang.cli.Template.TPL_BASENAME_PATTERN.matcher(refTarget).find()) {
			String baseName = Util.base(outName);
			String outExt = Util.extension(outName);
			String targetExt = Util.extension(refTarget);
			if (targetExt.isEmpty()) {
				targetExt = refSource.endsWith(".qute") ? Util.extension(Util.base(refSource))
						: Util.extension(refSource);
			}
			if (!outExt.isEmpty() && !outExt.equals(targetExt)) {
				throw new ExitException(BaseCommand.EXIT_INVALID_INPUT,
						"Template expects " + targetExt + " extension, not " + outExt);
			}
			result = dev.jbang.cli.Template.TPL_FILENAME_PATTERN.matcher(result).replaceAll(outName);
			result = dev.jbang.cli.Template.TPL_BASENAME_PATTERN.matcher(result).replaceAll(baseName);
		}
		return Paths.get(result);
	}

In my opinion the two variables (filename and basename) should be two different uses cases, I would like to use the {basename} variable in other entries as well, the filename variable is totally understandable, but the basename should be allowed to use with different extensions.

To Reproduce
Above.

Expected behavior
Let the {basename} variable be used for other file entries as well.

JBang version
0.91

@nandorholozsnyak nandorholozsnyak added the bug Something isn't working label Mar 20, 2022
nandorholozsnyak added a commit to nandorholozsnyak/jbang that referenced this issue Mar 20, 2022
- Test for case when {basename} is being used in a template file ref
- fixes jbangdev#1318
@nandorholozsnyak
Copy link
Contributor Author

@quintesse @maxandersen what do you think about it?

@maxandersen
Copy link
Collaborator

I think it should be ok.

It's just important the final list of file names are unique and deterministic which still seem to be the case here.

@nandorholozsnyak
Copy link
Contributor Author

I think it should be ok.

It's just important the final list of file names are unique and deterministic which still seem to be the case here.

Nothing should be broken. A PR is open for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants