Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: align Linux icon handling with Tauri bundle names

Antonio Carelli6903bb6d0259
diff --git a/bin/helpers/merge.ts b/bin/helpers/merge.ts
index d3aa3c20..252e37bb 100644
--- a/bin/helpers/merge.ts
+++ b/bin/helpers/merge.ts
@@ -139,6 +139,28 @@ async function handleLocalFile(
}
}
+export function buildLinuxDesktopContent(
+ name: string,
+ title: string | undefined,
+ linuxBinaryName: string,
+): string {
+ const chineseName = title && /[\u4e00-\u9fa5]/.test(title) ? title : null;
+
+ return `[Desktop Entry]
+Version=1.0
+Type=Application
+Name=${name}
+${chineseName ? `Name[zh_CN]=${chineseName}` : ''}
+Comment=${name}
+Exec=${linuxBinaryName}
+Icon=${linuxBinaryName}
+Categories=Network;WebBrowser;Utility;
+MimeType=text/html;text/xml;application/xhtml_xml;
+StartupNotify=true
+Terminal=false
+`;
+}
+
async function mergeLinuxConfig(
options: PakeAppOptions,
name: string,
@@ -155,23 +177,11 @@ async function mergeLinuxConfig(
const linuxName = generateLinuxPackageName(name);
const desktopFileName = `com.pake.${linuxName}.desktop`;
- const iconName = `${linuxName}_512`;
- const { title } = options;
-
- const chineseName = title && /[\u4e00-\u9fa5]/.test(title) ? title : null;
- const desktopContent = `[Desktop Entry]
-Version=1.0
-Type=Application
-Name=${name}
-${chineseName ? `Name[zh_CN]=${chineseName}` : ''}
-Comment=${name}
-Exec=${linuxBinaryName}
-Icon=${iconName}
-Categories=Network;WebBrowser;Utility;
-MimeType=text/html;text/xml;application/xhtml_xml;
-StartupNotify=true
-Terminal=false
-`;
+ const desktopContent = buildLinuxDesktopContent(
+ name,
+ options.title,
+ linuxBinaryName,
+ );
const srcAssetsDir = path.join(npmDirectory, 'src-tauri/assets');
const srcDesktopFilePath = path.join(srcAssetsDir, desktopFileName);
diff --git a/bin/options/icon.ts b/bin/options/icon.ts
index ca0c2966..9346f441 100644
--- a/bin/options/icon.ts
+++ b/bin/options/icon.ts
@@ -282,6 +282,22 @@ async function convertIconFormat(
}
}
+async function isLinuxBundleIconReady(iconPath: string): Promise<boolean> {
+ if (!IS_LINUX || path.extname(iconPath).toLowerCase() !== '.png') {
+ return false;
+ }
+
+ try {
+ const { width, height } = await sharp(iconPath).metadata();
+ return (
+ width === PLATFORM_CONFIG.linux.size &&
+ height === PLATFORM_CONFIG.linux.size
+ );
+ } catch {
+ return false;
+ }
+}
+
/**
* Processes downloaded or local icon for platform-specific format
*/
@@ -295,7 +311,7 @@ async function processIcon(
const ext = path.extname(iconPath).toLowerCase();
const isCorrectFormat =
(IS_WIN && ext === '.ico') ||
- (IS_LINUX && ext === '.png') ||
+ (IS_LINUX && (await isLinuxBundleIconReady(iconPath))) ||
(!IS_WIN && !IS_LINUX && ext === '.icns');
if (isCorrectFormat) {
diff --git a/dist/cli.js b/dist/cli.js
index 791c668c..1387ed56 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -596,30 +596,31 @@ async function handleLocalFile(url, useLocalFile, tauriConf) {
tauriConf.pake.windows[0].url_type = 'web';
}
}
-async function mergeLinuxConfig(options, name, tauriConf, linuxBinaryName) {
- const linuxBundle = tauriConf.bundle.linux;
- if (!linuxBundle) {
- throw new Error('Linux bundle configuration is missing from tauri.linux.conf.json; cannot build Linux target.');
- }
- delete linuxBundle.deb.files;
- const linuxName = generateLinuxPackageName(name);
- const desktopFileName = `com.pake.${linuxName}.desktop`;
- const iconName = `${linuxName}_512`;
- const { title } = options;
+function buildLinuxDesktopContent(name, title, linuxBinaryName) {
const chineseName = title && /[\u4e00-\u9fa5]/.test(title) ? title : null;
- const desktopContent = `[Desktop Entry]
+ return `[Desktop Entry]
Version=1.0
Type=Application
Name=${name}
${chineseName ? `Name[zh_CN]=${chineseName}` : ''}
Comment=${name}
Exec=${linuxBinaryName}
-Icon=${iconName}
+Icon=${linuxBinaryName}
Categories=Network;WebBrowser;Utility;
MimeType=text/html;text/xml;application/xhtml_xml;
StartupNotify=true
Terminal=false
`;
+}
+async function mergeLinuxConfig(options, name, tauriConf, linuxBinaryName) {
+ const linuxBundle = tauriConf.bundle.linux;
+ if (!linuxBundle) {
+ throw new Error('Linux bundle configuration is missing from tauri.linux.conf.json; cannot build Linux target.');
+ }
+ delete linuxBundle.deb.files;
+ const linuxName = generateLinuxPackageName(name);
+ const desktopFileName = `com.pake.${linuxName}.desktop`;
+ const desktopContent = buildLinuxDesktopContent(name, options.title, linuxBinaryName);
const srcAssetsDir = path.join(npmDirectory, 'src-tauri/assets');
const srcDesktopFilePath = path.join(srcAssetsDir, desktopFileName);
await fsExtra.ensureDir(srcAssetsDir);
@@ -2274,6 +2275,19 @@ async function convertIconFormat(inputPath, appName) {
return null;
}
}
+async function isLinuxBundleIconReady(iconPath) {
+ if (!IS_LINUX || path.extname(iconPath).toLowerCase() !== '.png') {
+ return false;
+ }
+ try {
+ const { width, height } = await sharp(iconPath).metadata();
+ return (width === PLATFORM_CONFIG.linux.size &&
+ height === PLATFORM_CONFIG.linux.size);
+ }
+ catch {
+ return false;
+ }
+}
/**
* Processes downloaded or local icon for platform-specific format
*/
@@ -2283,7 +2297,7 @@ async function processIcon(iconPath, appName) {
// Check if already in correct platform format
const ext = path.extname(iconPath).toLowerCase();
const isCorrectFormat = (IS_WIN && ext === '.ico') ||
- (IS_LINUX && ext === '.png') ||
+ (IS_LINUX && (await isLinuxBundleIconReady(iconPath))) ||
(!IS_WIN && !IS_LINUX && ext === '.icns');
if (isCorrectFormat) {
return await copyWindowsIconIfNeeded(iconPath, appName);
diff --git a/tests/unit/linux-desktop.test.ts b/tests/unit/linux-desktop.test.ts
new file mode 100644
index 00000000..5ca5db6d
--- /dev/null
+++ b/tests/unit/linux-desktop.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest';
+
+import { buildLinuxDesktopContent } from '../../bin/helpers/merge';
+
+describe('buildLinuxDesktopContent', () => {
+ it('uses the Tauri main binary name for freedesktop icon lookup', () => {
+ const desktopContent = buildLinuxDesktopContent(
+ 'Example App',
+ undefined,
+ 'pake-example-app',
+ );
+
+ expect(desktopContent).toContain('Exec=pake-example-app');
+ expect(desktopContent).toContain('Icon=pake-example-app');
+ expect(desktopContent).not.toContain('Icon=example-app_512');
+ });
+
+ it('keeps the localized Chinese name when title contains Chinese text', () => {
+ const desktopContent = buildLinuxDesktopContent(
+ 'MiaoYan',
+ '妙言',
+ 'pake-miaoyan',
+ );
+
+ expect(desktopContent).toContain('Name=MiaoYan');
+ expect(desktopContent).toContain('Name[zh_CN]=妙言');
+ });
+});
diff --git a/tests/unit/linux-icon.test.ts b/tests/unit/linux-icon.test.ts
new file mode 100644
index 00000000..fc66543f
--- /dev/null
+++ b/tests/unit/linux-icon.test.ts
@@ -0,0 +1,81 @@
+import os from 'os';
+import path from 'path';
+import fsExtra from 'fs-extra';
+import sharp from 'sharp';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import { DEFAULT_PAKE_OPTIONS } from '../../bin/defaults';
+import type { PakeAppOptions } from '../../bin/types';
+
+const tempDirs: string[] = [];
+
+async function makeTempDir() {
+ const tempDir = await fsExtra.mkdtemp(path.join(os.tmpdir(), 'pake-icon-'));
+ tempDirs.push(tempDir);
+ return tempDir;
+}
+
+async function makePngIcon(size: number): Promise<string> {
+ const tempDir = await makeTempDir();
+ const iconPath = path.join(tempDir, `icon-${size}.png`);
+ await sharp({
+ create: {
+ width: size,
+ height: size,
+ channels: 4,
+ background: { r: 40, g: 120, b: 220, alpha: 1 },
+ },
+ })
+ .png()
+ .toFile(iconPath);
+ return iconPath;
+}
+
+async function importLinuxIconModule() {
+ vi.resetModules();
+ vi.doMock('@/utils/platform', () => ({
+ IS_MAC: false,
+ IS_WIN: false,
+ IS_LINUX: true,
+ }));
+ return await import('../../bin/options/icon');
+}
+
+function makeOptions(icon: string): PakeAppOptions {
+ return {
+ ...DEFAULT_PAKE_OPTIONS,
+ identifier: 'com.pake.test',
+ name: 'Linux App',
+ icon,
+ };
+}
+
+describe('Linux icon handling', () => {
+ afterEach(async () => {
+ vi.doUnmock('@/utils/platform');
+ vi.resetModules();
+ await Promise.all(tempDirs.splice(0).map((dir) => fsExtra.remove(dir)));
+ });
+
+ it('keeps a 512px PNG icon without reconverting it', async () => {
+ const sourceIcon = await makePngIcon(512);
+ const { handleIcon } = await importLinuxIconModule();
+
+ await expect(handleIcon(makeOptions(sourceIcon))).resolves.toBe(sourceIcon);
+ });
+
+ it('converts a non-512px PNG icon to a Linux bundle-ready icon', async () => {
+ const sourceIcon = await makePngIcon(128);
+ const { handleIcon } = await importLinuxIconModule();
+
+ const result = await handleIcon(makeOptions(sourceIcon));
+ const metadata = await sharp(result).metadata();
+ const generatedRoot = path.dirname(path.dirname(result));
+ tempDirs.push(generatedRoot);
+
+ expect(result).not.toBe(sourceIcon);
+ expect(path.basename(result)).toBe('linux-app_512.png');
+ expect(metadata.width).toBe(512);
+ expect(metadata.height).toBe(512);
+ });
+});