Refactor code formatting and fix paths

This commit is contained in:
mauro-balades 2024-09-30 22:41:59 +02:00
parent 7f6da82ec4
commit 97d630b78f
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB
13 changed files with 233 additions and 124 deletions

View file

@ -24,4 +24,3 @@ jobs:
- name: Test
run: npm test

View file

@ -1,6 +1,6 @@
{
"name": "@zen-browser/surfer",
"version": "1.4.21",
"version": "1.4.22",
"description": "Simplifying building firefox forks!",
"main": "index.js",
"bin": {

View file

@ -173,7 +173,7 @@ export const build = async (options: Options): Promise<void> => {
log.info('Starting build...')
let exit = await genericBuild(prettyHost, options.ui);
process.exit(exit ? 0 : 1);
let exit = await genericBuild(prettyHost, options.ui)
process.exit(exit ? 0 : 1)
}
}

View file

@ -20,7 +20,7 @@ import {
resolveAddonDownloadUrl,
unpackAddon,
} from './addon'
import { configPath } from '../../utils';
import { configPath } from '../../utils'
import fs from 'fs-extra'
export function shouldSetupFirefoxSource() {
@ -68,16 +68,27 @@ async function unpackFirefoxSource(name: string): Promise<void> {
log.info(`Unpacking ${resolve(MELON_TMP_DIR, name)} to ${ENGINE_DIR}`)
if (process.platform === 'win32') {
log.info('Unpacking Firefox source on Windows (7z)')
await execa('7z', ['x', resolve(MELON_TMP_DIR, name), '-o' + resolve(MELON_TMP_DIR, name.replace('.tar.xz', '.tar'))]);
await execa('7z', [
'x',
resolve(MELON_TMP_DIR, name),
'-o' + resolve(MELON_TMP_DIR, name.replace('.tar.xz', '.tar')),
])
log.info('Unpacking Firefox source again without the .xz extension')
await execa('7z', ['x', resolve(MELON_TMP_DIR, name.replace('.tar.xz', '.tar')), '-o' + MELON_TMP_DIR]);
const archiveDir = resolve(MELON_TMP_DIR, 'firefox-' + config.version.version);
await execa('7z', [
'x',
resolve(MELON_TMP_DIR, name.replace('.tar.xz', '.tar')),
'-o' + MELON_TMP_DIR,
])
const archiveDir = resolve(
MELON_TMP_DIR,
'firefox-' + config.version.version
)
if (existsSync(ENGINE_DIR)) {
// remove the existing engine directory
fs.removeSync(ENGINE_DIR);
fs.removeSync(ENGINE_DIR)
}
log.info('Moving Firefox source to engine directory');
fs.moveSync(archiveDir, ENGINE_DIR);
log.info('Moving Firefox source to engine directory')
fs.moveSync(archiveDir, ENGINE_DIR)
return
}
@ -91,7 +102,7 @@ async function unpackFirefoxSource(name: string): Promise<void> {
ENGINE_DIR,
].filter(Boolean) as string[],
{
shell: BASH_PATH
shell: BASH_PATH,
}
)
log.info(`Unpacked Firefox source to ${ENGINE_DIR}`)

View file

@ -22,9 +22,9 @@ const machPath = resolve(ENGINE_DIR, 'mach')
async function getLocales() {
// l10n/supported-languages is a list of locales divided by newlines
// open the file and split it by newlines
const localesText = await readFile("l10n/supported-languages", "utf-8");
log.info(`Found locales:\n${localesText}`);
return localesText.split("\n");
const localesText = await readFile('l10n/supported-languages', 'utf-8')
log.info(`Found locales:\n${localesText}`)
return localesText.split('\n')
}
export const surferPackage = async () => {
@ -55,14 +55,14 @@ export const surferPackage = async () => {
)
const currentCWD = process.cwd()
const zenMacDestDir = join(currentCWD, 'zen-browser');
const zenMacDestDir = join(currentCWD, 'zen-browser')
if (!process.env.SURFER_SIGNING_MODE) {
await dispatch(machPath, arguments_, ENGINE_DIR, true)
// Merge language packs
for (const locale of await getLocales()) {
const arguments_ = ["build", `merge-${locale}`]
const arguments_ = ['build', `merge-${locale}`]
log.info(
`Packaging \`${config.binaryName}\` with args ${JSON.stringify(
@ -73,50 +73,94 @@ export const surferPackage = async () => {
await dispatch(machPath, arguments_, ENGINE_DIR, true)
}
log.info("Copying language packs")
log.info('Copying language packs')
await dispatch(machPath, ['package-multi-locale', '--locales', ...(await getLocales())], ENGINE_DIR, true)
await dispatch(
machPath,
['package-multi-locale', '--locales', ...(await getLocales())],
ENGINE_DIR,
true
)
// If we are on macos, run "mach macos-sign" to sign the app and repack it
if ((process as any).surferPlatform == 'darwin') {
log.info('Signing the app')
const dmgFile = (await readdir(join(OBJ_DIR, 'dist'))).find((file) => file.endsWith('.dmg'))
const dmgFile = (await readdir(join(OBJ_DIR, 'dist'))).find((file) =>
file.endsWith('.dmg')
)
if (!dmgFile) {
log.error('Could not find the dmg file')
return;
return
}
log.debug('Copying the dmg file to the current working directory')
// extract the dmg file
const dmgPath = join(OBJ_DIR, 'dist', dmgFile);
const dmgPath = join(OBJ_DIR, 'dist', dmgFile)
await dispatch(machPath, ['python', '-m', 'mozbuild.action.unpack_dmg', dmgPath, zenMacDestDir], ENGINE_DIR, true);
await dispatch(
machPath,
['python', '-m', 'mozbuild.action.unpack_dmg', dmgPath, zenMacDestDir],
ENGINE_DIR,
true
)
log.info('Signing the app')
if (process.env.MACOS_APPLE_DEVELOPER_ID) {
log.info('Signing the app with the developer id')
await dispatch('chmod', ['+x', '../build/codesign/codesign.bash'], ENGINE_DIR, true);
await dispatch('../build/codesign/codesign.bash', [
'-a', join(zenMacDestDir, 'Zen Browser.app'),
'-i', process.env.MACOS_APPLE_DEVELOPER_ID,
'-b', '../build/codesign/browser.developer.entitlements.xml',
'-p', '../build/codesign/plugin-container.developer.entitlements.xml'
], ENGINE_DIR, true);
await dispatch(
'chmod',
['+x', '../build/codesign/codesign.bash'],
ENGINE_DIR,
true
)
await dispatch(
'../build/codesign/codesign.bash',
[
'-a',
join(zenMacDestDir, 'Zen Browser.app'),
'-i',
process.env.MACOS_APPLE_DEVELOPER_ID,
'-b',
'../build/codesign/browser.developer.entitlements.xml',
'-p',
'../build/codesign/plugin-container.developer.entitlements.xml',
],
ENGINE_DIR,
true
)
}
log.info('Stapling the app');
await dispatch("xcrun", ['stapler', 'staple', join(zenMacDestDir, 'Zen Browser.app')], ENGINE_DIR, true);
log.info('Repacking the app');
const brandingPath = join(ENGINE_DIR, 'browser', 'branding', brandingKey);
log.info('Stapling the app')
await dispatch(
'xcrun',
['stapler', 'staple', join(zenMacDestDir, 'Zen Browser.app')],
ENGINE_DIR,
true
)
log.info('Repacking the app')
const brandingPath = join(ENGINE_DIR, 'browser', 'branding', brandingKey)
await remove(dmgPath)
await dispatch(machPath, ['python', '-m', 'mozbuild.action.make_dmg',
'--volume-name', 'Zen Browser',
'--icon', join(brandingPath, 'firefox.icns'),
'--background', join(brandingPath, 'background.png'),
'--dsstore', join(brandingPath, 'dsstore'),
zenMacDestDir,
dmgPath], ENGINE_DIR, true);
await dispatch(
machPath,
[
'python',
'-m',
'mozbuild.action.make_dmg',
'--volume-name',
'Zen Browser',
'--icon',
join(brandingPath, 'firefox.icns'),
'--background',
join(brandingPath, 'background.png'),
'--dsstore',
join(brandingPath, 'dsstore'),
zenMacDestDir,
dmgPath,
],
ENGINE_DIR,
true
)
}
}
@ -189,7 +233,12 @@ export const surferPackage = async () => {
}
}
const marPath = await createMarFile(version, channel, brandingDetails.release.github, zenMacDestDir)
const marPath = await createMarFile(
version,
channel,
brandingDetails.release.github,
zenMacDestDir
)
dynamicConfig.set('marPath', marPath)
await generateBrowserUpdateFiles()
@ -210,7 +259,12 @@ function getCurrentBrandName(): string {
return config.brands[brand].brandFullName
}
async function createMarFile(version: string, channel: string, github?: { repo: string }, zenMacDestDir?: string): Promise<string> {
async function createMarFile(
version: string,
channel: string,
github?: { repo: string },
zenMacDestDir?: string
): Promise<string> {
log.info(`Creating mar file...`)
let marBinary: string = windowsPathToUnix(
join(OBJ_DIR, 'dist/host/bin', 'mar')
@ -228,8 +282,8 @@ async function createMarFile(version: string, channel: string, github?: { repo:
? join(zenMacDestDir, `${getCurrentBrandName()}.app`)
: join(OBJ_DIR, 'dist', config.binaryName)
const marPath = resolve(DIST_DIR, 'output.mar');
log.debug(`Writing MAR to ${DIST_DIR} from ${binary}`);
const marPath = resolve(DIST_DIR, 'output.mar')
log.debug(`Writing MAR to ${DIST_DIR} from ${binary}`)
await configDispatch('./tools/update-packaging/make_full_update.sh', {
args: [
// The mar output location

View file

@ -41,8 +41,13 @@ export const BRANDING_DIR = join(CONFIGS_DIR, 'branding')
const BRANDING_STORE = join(ENGINE_DIR, 'browser', 'branding')
const BRANDING_FF = join(BRANDING_STORE, 'unofficial')
const REQUIRED_FILES = ['logo.png', 'logo-mac.png', 'firefox.ico', 'firefox64.ico']
const BRANDING_NSIS = 'branding.nsi';
const REQUIRED_FILES = [
'logo.png',
'logo-mac.png',
'firefox.ico',
'firefox64.ico',
]
const BRANDING_NSIS = 'branding.nsi'
const CSS_REPLACE_REGEX = new RegExp(
'#130829|hsla\\(235, 43%, 10%, .5\\)',
@ -87,11 +92,11 @@ async function setupImages(configPath: string, outputPath: string) {
// Firefox doesn't use 512 by 512, but we need it to generate ico files later
await every([16, 22, 24, 32, 48, 64, 128, 256, 512], async (size) => {
const logoPath = join(configPath, `logo${size}.png`);
if (!filesExist([logoPath])) throw new Error(`Missing logo${size}.png`);
const logoPath = join(configPath, `logo${size}.png`)
if (!filesExist([logoPath])) throw new Error(`Missing logo${size}.png`)
const outputPathLogo = join(outputPath, `default${size}.png`);
await copyFile(logoPath, outputPathLogo);
const outputPathLogo = join(outputPath, `default${size}.png`)
await copyFile(logoPath, outputPathLogo)
return true
})
@ -178,9 +183,11 @@ async function copyMozFiles(
(file) => !existsSync(join(outputPath, file.replace(BRANDING_FF, '')))
)
const css = files.filter((file) => extname(file).includes('css'));
const css = files.filter((file) => extname(file).includes('css'))
const everythingElse = files.filter((file) => !css.includes(file) && !file.includes(BRANDING_NSIS));
const everythingElse = files.filter(
(file) => !css.includes(file) && !file.includes(BRANDING_NSIS)
)
for (const [contents, path] of css
.map((filePath) => [
@ -196,12 +203,22 @@ async function copyMozFiles(
writeFileSync(path, contents)
}
const brandingNsis = files.filter((file) => file.includes(BRANDING_NSIS));
console.assert(brandingNsis.length == 1, 'There should only be one branding.nsi file');
const outputBrandingNsis = join(outputPath, brandingNsis[0].replace(BRANDING_FF, ''));
const configureProfileBrandingPath = join(outputPath, 'pref', 'firefox-branding.js');
log.debug('Configuring branding.nsi into ' + outputBrandingNsis);
configureBrandingNsis(outputBrandingNsis, brandingConfig);
const brandingNsis = files.filter((file) => file.includes(BRANDING_NSIS))
console.assert(
brandingNsis.length == 1,
'There should only be one branding.nsi file'
)
const outputBrandingNsis = join(
outputPath,
brandingNsis[0].replace(BRANDING_FF, '')
)
const configureProfileBrandingPath = join(
outputPath,
'pref',
'firefox-branding.js'
)
log.debug('Configuring branding.nsi into ' + outputBrandingNsis)
configureBrandingNsis(outputBrandingNsis, brandingConfig)
// Copy everything else from the default firefox branding directory
for (const file of everythingElse) {
@ -209,7 +226,7 @@ async function copyMozFiles(
copyFileSync(file, join(outputPath, file.replace(BRANDING_FF, '')))
}
configureProfileBranding(configureProfileBrandingPath, brandingConfig);
configureProfileBranding(configureProfileBrandingPath, brandingConfig)
}
// =============================================================================
@ -243,18 +260,23 @@ export async function apply(name: string): Promise<void> {
await copyMozFiles(outputPath, brandingConfig)
await addOptionalIcons(configPath, outputPath)
setUpdateURLs();
setUpdateURLs()
}
function configureBrandingNsis(brandingNsis: string, brandingConfig: {
backgroundColor: string
brandShorterName: string
brandShortName: string
brandFullName: string
brandingGenericName: string
brandingVendor: string
}) {
writeFileSync(brandingNsis, `
function configureBrandingNsis(
brandingNsis: string,
brandingConfig: {
backgroundColor: string
brandShorterName: string
brandShortName: string
brandFullName: string
brandingGenericName: string
brandingVendor: string
}
) {
writeFileSync(
brandingNsis,
`
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
@ -331,35 +353,44 @@ function configureBrandingNsis(brandingNsis: string, brandingConfig: {
!define INSTALL_INSTALLING_TEXT_COLOR 0xFFFFFF
# This color is written as 0x00BBGGRR because it's actually a COLORREF value.
!define PROGRESS_BAR_BACKGROUND_COLOR 0xFFAA00
`);
`
)
}
function addOptionalIcons(brandingPath: string, outputPath: string) {
// move all icons in the top directory and inside "content/" into the branding directory
const icons = readdirSync(brandingPath);
const iconsContent = readdirSync(join(brandingPath, 'content'));
const icons = readdirSync(brandingPath)
const iconsContent = readdirSync(join(brandingPath, 'content'))
for (const icon of icons) {
if (icon.includes('content')) continue;
log.info(`Copying ${icon} to ${outputPath}`);
copyFileSync(join(brandingPath, icon), join(outputPath, icon));
if (icon.includes('content')) continue
log.info(`Copying ${icon} to ${outputPath}`)
copyFileSync(join(brandingPath, icon), join(outputPath, icon))
}
for (const icon of iconsContent) {
log.info(`Copying ${icon} to ${outputPath}`);
copyFileSync(join(brandingPath, 'content', icon), join(outputPath, 'content', icon));
log.info(`Copying ${icon} to ${outputPath}`)
copyFileSync(
join(brandingPath, 'content', icon),
join(outputPath, 'content', icon)
)
}
}
function configureProfileBranding(brandingPath: string, brandingConfig: {
backgroundColor: string
brandShorterName: string
brandShortName: string
brandFullName: string
brandingGenericName: string
brandingVendor: string
}) {
writeFileSync(brandingPath, `
function configureProfileBranding(
brandingPath: string,
brandingConfig: {
backgroundColor: string
brandShorterName: string
brandShortName: string
brandFullName: string
brandingGenericName: string
brandingVendor: string
}
) {
writeFileSync(
brandingPath,
`
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@ -384,15 +415,16 @@ pref("app.releaseNotesURL.prompt", "https://zen-browser.app/release-notes/%VERSI
// Number of usages of the web console.
// If this is less than 5, then pasting code into the web console is disabled
pref("devtools.selfxss.count", 5);
`);
`
)
}
function setUpdateURLs() {
const sufix = (compatMode && (process as any).surferPlatform !== 'macos')
? '-generic' : '';
const baseURL = `URL=https://@MOZ_APPUPDATE_HOST@/updates/browser/%BUILD_TARGET%/%CHANNEL%${sufix}/update.xml`;
const appIni = join(ENGINE_DIR, 'build', 'application.ini.in');
const appIniContents = readFileSync(appIni).toString();
const updatedAppIni = appIniContents.replace(/URL=.*update.xml/g, baseURL);
writeFileSync(appIni, updatedAppIni);
const sufix =
compatMode && (process as any).surferPlatform !== 'macos' ? '-generic' : ''
const baseURL = `URL=https://@MOZ_APPUPDATE_HOST@/updates/browser/%BUILD_TARGET%/%CHANNEL%${sufix}/update.xml`
const appIni = join(ENGINE_DIR, 'build', 'application.ini.in')
const appIniContents = readFileSync(appIni).toString()
const updatedAppIni = appIniContents.replace(/URL=.*update.xml/g, baseURL)
writeFileSync(appIni, updatedAppIni)
}

View file

@ -32,7 +32,7 @@ const ausPlatformsMap = {
'Darwin_x86_64-gcc3',
],
macosArm: ['Darwin_aarch64-gcc3'],
win64: ['WINNT_x86_64-msvc', 'WINNT_x86_64-msvc-x64']
win64: ['WINNT_x86_64-msvc', 'WINNT_x86_64-msvc-x64'],
}
export async function getPlatformConfig() {
@ -52,13 +52,19 @@ function getReleaseMarName(releaseInfo: ReleaseInfo): string | undefined {
switch ((process as any).surferPlatform) {
case 'win32': {
return compatMode ? releaseInfo.archives["windows-compat"] : releaseInfo.archives["windows"];
return compatMode
? releaseInfo.archives['windows-compat']
: releaseInfo.archives['windows']
}
case 'darwin': {
return compatMode ? releaseInfo.archives["macos-x64"] : releaseInfo.archives["macos-aarch64"];
return compatMode
? releaseInfo.archives['macos-x64']
: releaseInfo.archives['macos-aarch64']
}
case 'linux': {
return compatMode ? releaseInfo.archives["linux-compat"] : releaseInfo.archives["linux"];
return compatMode
? releaseInfo.archives['linux-compat']
: releaseInfo.archives['linux']
}
}
}
@ -71,7 +77,12 @@ function getReleaseMarURL(releaseInfo: ReleaseInfo) {
// The user is using github to distribute release binaries for this version.
if (releaseInfo.github) {
completeMarURL = `https://github.com/${releaseInfo.github.repo}/releases/download/${releaseInfo.displayVersion}/${releaseMarName}`
let releaseVersion = releaseInfo.displayVersion
const channel = dynamicConfig.get('brand') as string
if (channel === 'twilight') {
releaseVersion = 'twilight'
}
completeMarURL = `https://github.com/${releaseInfo.github.repo}/releases/download/${releaseVersion}/${releaseMarName}`
log.info(`Using '${completeMarURL}' as the distribution url`)
} else {
log.warning(
@ -96,7 +107,8 @@ async function writeUpdateFileToDisk(
}
}
) {
const suffix = (compatMode && (process as any).surferPlatform !== 'macos') ? '-generic' : ''
const suffix =
compatMode && (process as any).surferPlatform !== 'macos' ? '-generic' : ''
const xmlPath = join(
DIST_DIR,
'update',

View file

@ -93,7 +93,9 @@ if (process.platform == 'win32') {
BASH_PATH = execa.sync('where.exe bash.exe').stdout.toString()
if (!BASH_PATH.includes('bash.exe')) {
BASH_PATH = process.env.SURFER_SIGNING_MODE ? 'C:\\mozilla-build\\msys2\\usr\\bin\\bash.exe' : '';
BASH_PATH = process.env.SURFER_SIGNING_MODE
? 'C:\\mozilla-build\\msys2\\usr\\bin\\bash.exe'
: ''
if (BASH_PATH === '') {
log.error('Could not find bash, aborting')
}

View file

@ -2,12 +2,12 @@
function getPlatform() {
if (process.env.SURFER_PLATFORM) {
return process.env.SURFER_PLATFORM;
return process.env.SURFER_PLATFORM
}
return process.platform;
return process.platform
}
Object.defineProperty(process, 'surferPlatform', { value: getPlatform() });
Object.defineProperty(process, 'surferPlatform', { value: getPlatform() })
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
@ -48,7 +48,7 @@ export const bin_name = BIN_NAME
const programVersions = []
export const compatMode = process.env.SURFER_COMPAT == 'true';
export const compatMode = process.env.SURFER_COMPAT == 'true'
for (const brand in config.brands) {
const brandConfig = config.brands[brand]

2
src/types.d.ts vendored
View file

@ -29,4 +29,4 @@ export interface CmdOption {
export type CmdFlagPlatform = NodeJS.Platform
declare module 'mount-dmg';
declare module 'mount-dmg'

View file

@ -62,13 +62,13 @@ export interface ReleaseInfo {
}
archives?: {
"windows"?: string,
"macos-aarch64"?: string,
"macos-x64"?: string,
"linux"?: string,
windows?: string
'macos-aarch64'?: string
'macos-x64'?: string
linux?: string
"windows-compat"?: string,
"linux-compat"?: string,
'windows-compat'?: string
'linux-compat'?: string
}
}

View file

@ -23,9 +23,8 @@ import { log } from '../log'
*/
export const windowsPathToUnix = (path: string): string =>
(process as any).platform == 'win32'
?
(process.env.SURFER_SIGNING_MODE ? "/c" : "")
+ path.replace(/[\\]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '')
? (process.env.SURFER_SIGNING_MODE ? '/c' : '') +
path.replace(/[\\]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '')
: path
export async function walkDirectory(directory: string): Promise<string[]> {