Skip to content

Commit

Permalink
build: separate browser builds (#1)
Browse files Browse the repository at this point in the history
ci: fix broken build for Chrome
  • Loading branch information
Wassup789 authored Apr 23, 2024
1 parent 71843dc commit 76ad540
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 22 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ jobs:
- run: corepack enable
- run: yarn install
- run: yarn run prod
- run: zip -r dist/browser/browser.zip dist/browser/*
- name: Package for Chrome
working-directory: dist/browser/chrome
run: zip -r ../garmin-connect-enhancements.unpackaged.chrome.zip *
- name: Package for Firefox / Gecko
working-directory: dist/browser/gecko
run: zip -r ../garmin-connect-enhancements.unpackaged.gecko.zip *
- name: Archive production artifacts
uses: actions/upload-artifact@v4
with:
name: dist
path: |
dist/browser/browser.zip
dist/browser/garmin-connect-enhancements.unpackaged.chrome.zip
dist/browser/garmin-connect-enhancements.unpackaged.gecko.zip
dist/userscript/garmin-connect-enhancements.user.js
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ jobs:
token: ${{ secrets.PAT }}
fail_on_unmatched_files: true
files: |
dist/browser/browser.zip
dist/browser/garmin-connect-enhancements.unpackaged.chrome.zip
dist/browser/garmin-connect-enhancements.unpackaged.gecko.zip
dist/userscript/garmin-connect-enhancements.user.js
4 changes: 3 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Run `yarn run build:prod`
## Build and distributable directories breakdown
* `build/` _(contains the raw build files)_
* `dist/`
* `browser/` _(contains the browser extension distributables)_
* `browser/`
* `chrome/` _(contains the Chrome extension files)_
* `gecko/` _(contains the Firefox add-on files)_
* `userscript/` _(contains the UserScript distributable)_

## Build & Watch For Changes
Expand Down
78 changes: 65 additions & 13 deletions scripts/setup-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,79 @@ const filePath = "./build/app.js",
outDir = "./dist/browser",
outPath = `${outDir}/app.js`;

const filesToCopy = [
["./build/app.js", path.join(outDir, "app.js")],
["./build/browser-inject.js", path.join(outDir, "content-script.js")],
["./assets/logo-16.png", path.join(outDir, "logo-16.png")],
["./assets/logo-32.png", path.join(outDir, "logo-32.png")],
["./assets/logo-48.png", path.join(outDir, "logo-48.png")],
["./assets/logo-128.png", path.join(outDir, "logo-128.png")],
/**
* @type {[string, string][]}
*/
const baseFilesToCopy = [
["./build/app.js", "app.js"],
["./build/browser-inject.js", "content-script.js"],
["./assets/logo-16.png", "logo-16.png"],
["./assets/logo-32.png", "logo-32.png"],
["./assets/logo-48.png", "logo-48.png"],
["./assets/logo-128.png", "logo-128.png"],
];

async function run() {
const manifestContent = (await fs.readFile("./templates/browser/manifest.json", "utf8"))
.replace("[VERSION]", JSON.stringify(version))
.replace("[DESCRIPTION]", JSON.stringify(description));
await fs.mkdir(outDir, { recursive: true });
/**
* @param {string} directory
*/
async function createEmptyDirectory(directory) {
await fs.mkdir(directory, { recursive: true });

for (const existingFile of await fs.readdir(directory)) {
await fs.unlink(path.join(directory, existingFile));
}
}

/**
* @param {"chrome"|"gecko"} target
* @param {string} targetDir
* @param {string} rawManifest
*/
async function writeManifestForTarget(target, targetDir, rawManifest) {
const manifestData = JSON.parse(rawManifest);

await fs.writeFile(path.join(outDir, "manifest.json"), manifestContent, "utf-8");
manifestData["version"] = version;
manifestData["description"] = description;

if (target === "chrome") {
delete manifestData["browser_specific_settings"];
}

await fs.writeFile(path.join(targetDir, "manifest.json"), JSON.stringify(manifestData, null, 2), "utf-8");
}

/**
* @param {string} targetDir
*/
async function copyFilesToDirectory(targetDir) {
const filesToCopy = baseFilesToCopy.map((e) => [e[0], path.join(targetDir, e[1])]);
for (const [source, output] of filesToCopy) {
await fs.writeFile(output, await fs.readFile(source));
}
}

/**
* @param {"chrome"|"gecko"} target
* @param {string} rawManifest
*/
async function packageForTarget(target, rawManifest) {
const baseDir = path.join(outDir, target);

await createEmptyDirectory(baseDir);
await writeManifestForTarget(target, baseDir, rawManifest);
await copyFilesToDirectory(baseDir);
}

async function run() {
const rawManifest = await fs.readFile("./templates/browser/manifest.json", "utf8");

console.log("Packaging for chrome...");
await packageForTarget("chrome", rawManifest);

console.log("Packaging for gecko...");
await packageForTarget("gecko", rawManifest);

console.log();
console.log(`Browser extension contents updated at ${outDir}`);
}

Expand Down
16 changes: 11 additions & 5 deletions templates/browser/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 3,
"name": "Enhancements for Garmin Connect",
"version": [VERSION],
"description": [DESCRIPTION],
"version": "",
"description": "",
"icons": {
"16": "logo-16.png",
"32": "logo-32.png",
Expand All @@ -14,13 +14,19 @@
"js": [
"content-script.js"
],
"matches": ["*://connect.garmin.com/*"]
"matches": [
"*://connect.garmin.com/*"
]
}
],
"web_accessible_resources": [
{
"resources": ["app.js"],
"matches": ["*://connect.garmin.com/*"]
"resources": [
"app.js"
],
"matches": [
"*://connect.garmin.com/*"
]
}
],
"browser_specific_settings": {
Expand Down

0 comments on commit 76ad540

Please sign in to comment.