Skip to content

fix: Unable to use {basename} in template file-refs #1319

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/dev/jbang/cli/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ private void applyTemplateProperties(dev.jbang.catalog.Template tpl) {

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);
if (dev.jbang.cli.Template.TPL_FILENAME_PATTERN.matcher(refTarget).find()) {
String outExt = Util.extension(outName);
String targetExt = Util.extension(refTarget);
if (targetExt.isEmpty()) {
Expand All @@ -140,6 +138,9 @@ static Path resolveBaseName(String refTarget, String refSource, String outName)
"Template expects " + targetExt + " extension, not " + outExt);
}
result = dev.jbang.cli.Template.TPL_FILENAME_PATTERN.matcher(result).replaceAll(outName);
}
if (dev.jbang.cli.Template.TPL_BASENAME_PATTERN.matcher(refTarget).find()) {
String baseName = Util.base(outName);
result = dev.jbang.cli.Template.TPL_BASENAME_PATTERN.matcher(result).replaceAll(baseName);
}
return Paths.get(result);
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/dev/jbang/cli/TestInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ void testInitMultipleFilesWrongName() throws IOException {
testFailMultipleFiles("{filename}", "edit.md", "edit.java", true, 2);
}

@Test
void testInitMultipleFilesWrongName2() throws IOException {
testFailMultipleFiles("{basename}.java", "edit.md", "edit.java", true, 2);
}

void testInitMultipleFiles(String targetName, String initName, String outName, boolean abs) throws IOException {
Path outFile = setupInitMultipleFiles(targetName, initName, abs);
int result = JBang.getCommandLine().execute("init", "-t=name", outFile.toString());
Expand Down Expand Up @@ -324,4 +329,55 @@ void testInitPropertiesIgnoringPropertyDefaults() throws IOException {

}

@Test
void testInitUsingTemplateWithFilenameAndBasename() throws IOException {
Path cwd = Util.getCwd();
Path javaFileQute = Files.write(cwd.resolve("file1.java.qute"), "Hello World".getBytes());
Path tfFileQute = Files.write(cwd.resolve("file2.tf.qute"), "Hello World from .tf file".getBytes());
Path outJava = cwd.resolve("result.java");
Path outTf = cwd.resolve("prefixed-result.tf");

JBang .getCommandLine()
.execute("template", "add", "-f", cwd.toString(), "--name=template-with-more-files",
"{filename}" + "=" + javaFileQute.toAbsolutePath().toString(),
"prefixed-{basename}.tf" + "=" + tfFileQute.toAbsolutePath().toString());

assertThat(outJava.toFile().exists(), not(true));

int result = JBang .getCommandLine()
.execute("init", "--verbose", "--template=template-with-more-files",
outJava.toAbsolutePath().toString());

assertThat(result, is(0));
assertThat(outJava.toFile().exists(), is(true));
assertThat(outTf.toFile().exists(), is(true));

String javaContent = Util.readString(outJava);
String tfContent = Util.readString(outTf);

assertThat(javaContent, containsString("Hello World"));
assertThat(tfContent, containsString("Hello World from .tf file"));

}

@Test
void testResolveBaseNameShouldResolveFilenameOrBasename() {
// Use case when the {filename} should be resolved to the input name -
// result.java
String scriptName = "result.java";
Path path = Init.resolveBaseName("{filename}", "template-for-script.java", scriptName);
assertThat(path.toString(), containsString("result.java"));

// Use case when the {filename} can not be resolved because the input is
// result.java but the reference source has .tf extension not .java
ExitException exitException = assertThrows(ExitException.class,
() -> Init.resolveBaseName("{filename}", "template-for-script.tf", scriptName));
String exceptionMessage = "Template expects tf extension, not java";
assertThat(exitException.getMessage(), containsString(exceptionMessage));

// Use case when the {basename} is being used with prefix and with not .java
// extension, and the reference soure also has the .tf extension
path = Init.resolveBaseName("template-for-{basename}.tf", "template-for-script.tf", scriptName);
assertThat(path.toString(), containsString("template-for-result.tf"));
}
}