Skip to content

Commit

Permalink
fix(ui5-task-copyright): xml declaration/preable must be before comme…
Browse files Browse the repository at this point in the history
…nt (#1022)
  • Loading branch information
petermuessig authored Jun 25, 2024
1 parent fc7f1f0 commit 12834b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
11 changes: 10 additions & 1 deletion packages/ui5-task-copyright/lib/copyright.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ module.exports = async ({ log, workspace, options }) => {
.map((line) => line.trimEnd())
.join("\n ");
if (!(xml["#comment"] && copyrightRegExp.test(xml["#comment"]))) {
await resource.setString(`<!--\n ${copyrightForXML}\n-->\n${code}`);
const copyrightString = `<!--\n ${copyrightForXML}\n-->`;
// split the xml into the xml declaration and the content
const parts = /(<\?xml.*\?>\s*)?([\s\S]*)/.exec(code);
if (parts?.length === 3 && typeof parts[1] === "string") {
// copyright comment is inserted after the xml declaration and before the content
await resource.setString(`${parts[1].trim()}\n${copyrightString}\n${parts[2]?.trim() || ""}`);
} else {
// no xml declaration found
await resource.setString(`${copyrightString}\n${code}`);
}
} else {
await resource.setString(code.replace(copyrightRegExp, copyrightForXML));
}
Expand Down
7 changes: 5 additions & 2 deletions packages/ui5-task-copyright/test/copyright.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ function startsWithCopyright(file, copyrightString) {
.join("\n * ");
return content.startsWith(`/*!\n * ${copyrightForJS}\n */\n`);
} else if (file.endsWith(".xml")) {
const parts = /(<\?xml.*\?>\s*)?([\s\S]*)/.exec(content);
const xmlContent = parts[2]?.trim() || content;
const copyrightForXML = copyrightString
.split(/\r?\n/)
.map((line) => line.trimEnd())
.join("\n ");
return content.startsWith(`<!--\n ${copyrightForXML}\n-->\n`);
return xmlContent.startsWith(`<!--\n ${copyrightForXML}\n-->\n`);
}
return false;
}

test("Inline copyright", async (t) => {
test.only("Inline copyright", async (t) => {
const ui5 = { yaml: path.resolve("./test/__assets__/ui5.inline.yaml") };
spawnSync(`ui5 build --config ${ui5.yaml} --dest ${t.context.tmpDir}/dist`, {
stdio: "inherit", // > don't include stdout in test output,
Expand All @@ -53,6 +55,7 @@ test("Inline copyright", async (t) => {
t.true(startsWithCopyright(path.resolve(t.context.tmpDir, "dist", "controller/Main-dbg.controller.js"), copyright));
t.true(startsWithCopyright(path.resolve(t.context.tmpDir, "dist", "view/App.view.xml"), copyright));
t.true(startsWithCopyright(path.resolve(t.context.tmpDir, "dist", "view/Main.view.xml"), copyright));
t.true(startsWithCopyright(path.resolve(t.context.tmpDir, "dist", "data/data.xml"), copyright));
});

test("Inline copyright + current year", async (t) => {
Expand Down
4 changes: 4 additions & 0 deletions showcases/ui5-tsapp/webapp/data/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<data>
<hello>world</hello>
</data>

0 comments on commit 12834b8

Please sign in to comment.