From a30f0c7c64ebc128cbc511e15eee43739293eb8b Mon Sep 17 00:00:00 2001 From: Northword Date: Tue, 11 Jul 2023 18:36:04 +0800 Subject: [PATCH 1/6] Add prefix for locale files when build --- scripts/build.mjs | 26 ++++++++++++++++++++++++-- src/utils/locale.ts | 9 +++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 21d4554..1104b6a 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -84,6 +84,19 @@ function dateFormat(fmt, date) { return fmt; } +function addAddonRefToFlt(fltContent) { + const lines = fltContent.split("\n"); + const prefixedLines = lines.map((line) => { + if (line.match(/^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/gm)) { + // https://regex101.com/r/lQ9x5p/1 + return `${config.addonRef}-${line}`; + } else { + return line; + } + }); + return prefixedLines.join("\n"); +} + async function main() { const t = new Date(); const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); @@ -121,7 +134,6 @@ async function main() { /__buildVersion__/g, /__buildTime__/g, ]; - const replaceTo = [author, description, homepage, version, buildTime]; replaceFrom.push( @@ -129,6 +141,9 @@ async function main() { ); replaceTo.push(...Object.values(config)); + replaceFrom.push(/(data-l10n-id=")(.*")/gm); + replaceTo.push(`$1${config.addonRef}-$2`); + const optionsAddon = { files: [ join(buildDir, "**/*.xhtml"), @@ -144,11 +159,18 @@ async function main() { }; const replaceResult = sync(optionsAddon); + + const replaceResultFlt = sync({ + files: [join(buildDir, "addon/**/*.ftl")], + processor: [addAddonRefToFlt], + }); + console.log( "[Build] Run replace in ", replaceResult .filter((f) => f.hasChanged) - .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`) + .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`), + replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`) ); console.log("[Build] Replace OK"); diff --git a/src/utils/locale.ts b/src/utils/locale.ts index 516dec5..2642675 100644 --- a/src/utils/locale.ts +++ b/src/utils/locale.ts @@ -63,16 +63,17 @@ function _getString( localString: string, options: { branch?: string | undefined; args?: Record } = {} ): string { + const localStringWithPrefix = `${config.addonRef}-${localString}`; const { branch, args } = options; const pattern = addon.data.locale?.current.formatMessagesSync([ - { id: localString, args }, + { id: localStringWithPrefix, args }, ])[0]; if (!pattern) { - return localString; + return localStringWithPrefix; } if (branch && pattern.attributes) { - return pattern.attributes[branch] || localString; + return pattern.attributes[branch] || localStringWithPrefix; } else { - return pattern.value || localString; + return pattern.value || localStringWithPrefix; } } From 9e2553661df7601a5360b81340db2909a541ae67 Mon Sep 17 00:00:00 2001 From: Northword Date: Tue, 11 Jul 2023 18:41:58 +0800 Subject: [PATCH 2/6] Add edbuild target to firefox 102 --- scripts/build.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/build.mjs b/scripts/build.mjs index 1104b6a..14faf60 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -120,6 +120,7 @@ async function main() { __env__: `"${env.NODE_ENV}"`, }, bundle: true, + target: "firefox102", outfile: join(buildDir, "addon/chrome/content/scripts/index.js"), // Don't turn minify on // minify: true, From ab845ddb354335c38395d82601f7eb89f3f8a647 Mon Sep 17 00:00:00 2001 From: Northword Date: Tue, 11 Jul 2023 19:08:13 +0800 Subject: [PATCH 3/6] Update reg exp for data-l10n-id --- scripts/build.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 14faf60..2b78100 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -142,7 +142,7 @@ async function main() { ); replaceTo.push(...Object.values(config)); - replaceFrom.push(/(data-l10n-id=")(.*")/gm); + replaceFrom.push(/(data-l10n-id=")(\S*")/gm); replaceTo.push(`$1${config.addonRef}-$2`); const optionsAddon = { From 4d7f61ee0bf748c64b2c81add16ada7356e37983 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 19:53:24 +0800 Subject: [PATCH 4/6] Replace xhtml locale keys that only exist in ftl --- scripts/build.mjs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 2b78100..0882e45 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -18,6 +18,9 @@ import details from "../package.json" assert { type: "json" }; const { name, author, description, homepage, version, config } = details; +const localeMessage = new Set(); +const localeMessageMiss = new Set(); + function copyFileSync(source, target) { var targetFile = target; @@ -87,8 +90,12 @@ function dateFormat(fmt, date) { function addAddonRefToFlt(fltContent) { const lines = fltContent.split("\n"); const prefixedLines = lines.map((line) => { - if (line.match(/^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/gm)) { - // https://regex101.com/r/lQ9x5p/1 + // https://regex101.com/r/lQ9x5p/1 + const match = line.match( + /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m + ); + if (match) { + localeMessage.add(match.groups.message); return `${config.addonRef}-${line}`; } else { return line; @@ -97,6 +104,21 @@ function addAddonRefToFlt(fltContent) { return prefixedLines.join("\n"); } +function replaceLocaleIdInXHtml(input) { + const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; + matchs.map((match) => { + if (localeMessage.has(match[2])) { + input = input.replace( + match[0], + `${match[1]}="${config.addonRef}-${match[2]}"` + ); + } else { + localeMessageMiss.add(match[1]); + } + }); + return input; +} + async function main() { const t = new Date(); const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); @@ -142,9 +164,6 @@ async function main() { ); replaceTo.push(...Object.values(config)); - replaceFrom.push(/(data-l10n-id=")(\S*")/gm); - replaceTo.push(`$1${config.addonRef}-$2`); - const optionsAddon = { files: [ join(buildDir, "**/*.xhtml"), @@ -166,14 +185,28 @@ async function main() { processor: [addAddonRefToFlt], }); + const replaceResultXhtml = sync({ + files: [join(buildDir, "addon/**/*.xhtml")], + processor: [replaceLocaleIdInXHtml], + }); + console.log( "[Build] Run replace in ", replaceResult .filter((f) => f.hasChanged) .map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`), - replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`) + replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`), + replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`) ); + if (localeMessageMiss.size !== 0) { + console.warn( + `[Build] [Warn] Fluent message [${new Array( + ...localeMessageMiss + )}] do not exsit in addon's locale files.` + ); + } + console.log("[Build] Replace OK"); // Walk the builds/addon/locale folder's sub folders and rename *.ftl to addonRef-*.ftl From 3e6b744307ac1c68c03635d0c06a5f0a96cd2280 Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 20:04:52 +0800 Subject: [PATCH 5/6] Refactor the functions in main into separate functions --- scripts/build.mjs | 180 ++++++++++++++++++++++++---------------------- 1 file changed, 94 insertions(+), 86 deletions(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index 0882e45..c5799d1 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -18,8 +18,9 @@ import details from "../package.json" assert { type: "json" }; const { name, author, description, homepage, version, config } = details; -const localeMessage = new Set(); -const localeMessageMiss = new Set(); +const t = new Date(); +const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", new Date()); +const buildDir = "build"; function copyFileSync(source, target) { var targetFile = target; @@ -87,69 +88,32 @@ function dateFormat(fmt, date) { return fmt; } -function addAddonRefToFlt(fltContent) { - const lines = fltContent.split("\n"); - const prefixedLines = lines.map((line) => { - // https://regex101.com/r/lQ9x5p/1 - const match = line.match( - /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m - ); - if (match) { - localeMessage.add(match.groups.message); - return `${config.addonRef}-${line}`; - } else { - return line; +function renameLocaleFiles() { + const localeDir = join(buildDir, "addon/locale"); + const localeFolders = readdirSync(localeDir, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name); + + for (const localeSubFolder of localeFolders) { + const localeSubDir = join(localeDir, localeSubFolder); + const localeSubFiles = readdirSync(localeSubDir, { + withFileTypes: true, + }) + .filter((dirent) => dirent.isFile()) + .map((dirent) => dirent.name); + + for (const localeSubFile of localeSubFiles) { + if (localeSubFile.endsWith(".ftl")) { + renameSync( + join(localeSubDir, localeSubFile), + join(localeSubDir, `${config.addonRef}-${localeSubFile}`) + ); + } } - }); - return prefixedLines.join("\n"); + } } -function replaceLocaleIdInXHtml(input) { - const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; - matchs.map((match) => { - if (localeMessage.has(match[2])) { - input = input.replace( - match[0], - `${match[1]}="${config.addonRef}-${match[2]}"` - ); - } else { - localeMessageMiss.add(match[1]); - } - }); - return input; -} - -async function main() { - const t = new Date(); - const buildTime = dateFormat("YYYY-mm-dd HH:MM:SS", t); - const buildDir = "build"; - - console.log( - `[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[ - env.NODE_ENV, - ]}` - ); - - clearFolder(buildDir); - - copyFolderRecursiveSync("addon", buildDir); - - copyFileSync("update-template.json", "update.json"); - - await build({ - entryPoints: ["src/index.ts"], - define: { - __env__: `"${env.NODE_ENV}"`, - }, - bundle: true, - target: "firefox102", - outfile: join(buildDir, "addon/chrome/content/scripts/index.js"), - // Don't turn minify on - // minify: true, - }).catch(() => exit(1)); - - console.log("[Build] Run esbuild OK"); - +function replaceString() { const replaceFrom = [ /__author__/g, /__description__/g, @@ -180,14 +144,45 @@ async function main() { const replaceResult = sync(optionsAddon); + const localeMessage = new Set(); + const localeMessageMiss = new Set(); + const replaceResultFlt = sync({ files: [join(buildDir, "addon/**/*.ftl")], - processor: [addAddonRefToFlt], + processor: (fltContent) => { + const lines = fltContent.split("\n"); + const prefixedLines = lines.map((line) => { + // https://regex101.com/r/lQ9x5p/1 + const match = line.match( + /^(?[a-zA-Z]\S*)([ ]*=[ ]*)(?.*)$/m + ); + if (match) { + localeMessage.add(match.groups.message); + return `${config.addonRef}-${line}`; + } else { + return line; + } + }); + return prefixedLines.join("\n"); + }, }); const replaceResultXhtml = sync({ files: [join(buildDir, "addon/**/*.xhtml")], - processor: [replaceLocaleIdInXHtml], + processor: (input) => { + const matchs = [...input.matchAll(/(data-l10n-id)="(\S*)"/g)]; + matchs.map((match) => { + if (localeMessage.has(match[2])) { + input = input.replace( + match[0], + `${match[1]}="${config.addonRef}-${match[2]}"` + ); + } else { + localeMessageMiss.add(match[1]); + } + }); + return input; + }, }); console.log( @@ -206,32 +201,45 @@ async function main() { )}] do not exsit in addon's locale files.` ); } +} + +async function esbuild() { + await build({ + entryPoints: ["src/index.ts"], + define: { + __env__: `"${env.NODE_ENV}"`, + }, + bundle: true, + target: "firefox102", + outfile: join(buildDir, "addon/chrome/content/scripts/index.js"), + // Don't turn minify on + // minify: true, + }).catch(() => exit(1)); +} + +async function main() { + console.log( + `[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[ + env.NODE_ENV, + ]}` + ); + + clearFolder(buildDir); + + copyFolderRecursiveSync("addon", buildDir); + + copyFileSync("update-template.json", "update.json"); + + await esbuild(); + + console.log("[Build] Run esbuild OK"); + + replaceString(); console.log("[Build] Replace OK"); // Walk the builds/addon/locale folder's sub folders and rename *.ftl to addonRef-*.ftl - const localeDir = join(buildDir, "addon/locale"); - const localeFolders = readdirSync(localeDir, { withFileTypes: true }) - .filter((dirent) => dirent.isDirectory()) - .map((dirent) => dirent.name); - - for (const localeSubFolder of localeFolders) { - const localeSubDir = join(localeDir, localeSubFolder); - const localeSubFiles = readdirSync(localeSubDir, { - withFileTypes: true, - }) - .filter((dirent) => dirent.isFile()) - .map((dirent) => dirent.name); - - for (const localeSubFile of localeSubFiles) { - if (localeSubFile.endsWith(".ftl")) { - renameSync( - join(localeSubDir, localeSubFile), - join(localeSubDir, `${config.addonRef}-${localeSubFile}`) - ); - } - } - } + renameLocaleFiles(); console.log("[Build] Addon prepare OK"); From b08fd4642b4fac9682fb80727a6c6978021bea2e Mon Sep 17 00:00:00 2001 From: Northword Date: Wed, 12 Jul 2023 20:18:25 +0800 Subject: [PATCH 6/6] Fix typo in match index --- scripts/build.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build.mjs b/scripts/build.mjs index c5799d1..7e7c6f8 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -178,7 +178,7 @@ function replaceString() { `${match[1]}="${config.addonRef}-${match[2]}"` ); } else { - localeMessageMiss.add(match[1]); + localeMessageMiss.add(match[2]); } }); return input;