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: respect custom Cargo target directories

Andrew Barnesb1bc210eb244
diff --git a/bin/builders/BaseBuilder.ts b/bin/builders/BaseBuilder.ts
index 9b722466..130eb7af 100644
--- a/bin/builders/BaseBuilder.ts
+++ b/bin/builders/BaseBuilder.ts
@@ -404,9 +404,19 @@ export default abstract class BaseBuilder {
}
}
+ protected getCargoTargetDir(): string {
+ return process.env.CARGO_TARGET_DIR || path.join('src-tauri', 'target');
+ }
+
+ protected resolveBuildPath(npmDirectory: string, buildPath: string): string {
+ return path.isAbsolute(buildPath)
+ ? buildPath
+ : path.join(npmDirectory, buildPath);
+ }
+
protected getBasePath(): string {
const basePath = this.options.debug ? 'debug' : 'release';
- return `src-tauri/target/${basePath}/bundle/`;
+ return path.join(this.getCargoTargetDir(), basePath, 'bundle');
}
protected getBuildAppPath(
@@ -418,8 +428,7 @@ export default abstract class BaseBuilder {
const bundleDir =
fileType.toLowerCase() === 'app' ? 'macos' : fileType.toLowerCase();
return path.join(
- npmDirectory,
- this.getBasePath(),
+ this.resolveBuildPath(npmDirectory, this.getBasePath()),
bundleDir,
`${fileName}.${fileType}`,
);
@@ -459,14 +468,17 @@ export default abstract class BaseBuilder {
// Handle cross-platform builds
if (this.options.multiArch || this.hasArchSpecificTarget()) {
return path.join(
- npmDirectory,
- this.getArchSpecificPath(),
+ this.resolveBuildPath(npmDirectory, this.getArchSpecificPath()),
basePath,
binaryName,
);
}
- return path.join(npmDirectory, 'src-tauri/target', basePath, binaryName);
+ return path.join(
+ this.resolveBuildPath(npmDirectory, this.getCargoTargetDir()),
+ basePath,
+ binaryName,
+ );
}
/**
@@ -503,6 +515,6 @@ export default abstract class BaseBuilder {
* Get architecture-specific path for binary
*/
protected getArchSpecificPath(): string {
- return 'src-tauri/target'; // Override in subclasses if needed
+ return this.getCargoTargetDir(); // Override in subclasses if needed
}
}
diff --git a/bin/builders/LinuxBuilder.ts b/bin/builders/LinuxBuilder.ts
index 3af51e2f..6c3138e1 100644
--- a/bin/builders/LinuxBuilder.ts
+++ b/bin/builders/LinuxBuilder.ts
@@ -260,7 +260,12 @@ post_remove() {
if (this.buildArch === 'arm64') {
const target = this.getTauriTarget(this.buildArch, 'linux');
- return `src-tauri/target/${target}/${basePath}/bundle/`;
+ if (!target) {
+ throw new Error(
+ `Unsupported architecture: ${this.buildArch} for Linux`,
+ );
+ }
+ return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
}
return super.getBasePath();
@@ -280,7 +285,12 @@ post_remove() {
protected getArchSpecificPath(): string {
if (this.buildArch === 'arm64') {
const target = this.getTauriTarget(this.buildArch, 'linux');
- return `src-tauri/target/${target}`;
+ if (!target) {
+ throw new Error(
+ `Unsupported architecture: ${this.buildArch} for Linux`,
+ );
+ }
+ return path.join(this.getCargoTargetDir(), target);
}
return super.getArchSpecificPath();
}
diff --git a/bin/builders/MacBuilder.ts b/bin/builders/MacBuilder.ts
index ebf061d4..1bf52989 100644
--- a/bin/builders/MacBuilder.ts
+++ b/bin/builders/MacBuilder.ts
@@ -76,7 +76,11 @@ export default class MacBuilder extends BaseBuilder {
const actualArch = this.getActualArch();
const target = this.getTauriTarget(actualArch, 'darwin');
- return `src-tauri/target/${target}/${basePath}/bundle`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${actualArch} for macOS`);
+ }
+
+ return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
}
protected hasArchSpecificTarget(): boolean {
@@ -86,6 +90,9 @@ export default class MacBuilder extends BaseBuilder {
protected getArchSpecificPath(): string {
const actualArch = this.getActualArch();
const target = this.getTauriTarget(actualArch, 'darwin');
- return `src-tauri/target/${target}`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${actualArch} for macOS`);
+ }
+ return path.join(this.getCargoTargetDir(), target);
}
}
diff --git a/bin/builders/WinBuilder.ts b/bin/builders/WinBuilder.ts
index 4a3ffc80..7b1f85e0 100644
--- a/bin/builders/WinBuilder.ts
+++ b/bin/builders/WinBuilder.ts
@@ -2,6 +2,7 @@ import path from 'path';
import BaseBuilder from './BaseBuilder';
import { PakeAppOptions } from '@/types';
import tauriConfig from '@/helpers/tauriConfig';
+import { generateIdentifierSafeName } from '@/utils/name';
export default class WinBuilder extends BaseBuilder {
private buildFormat: string = 'msi';
@@ -39,7 +40,12 @@ export default class WinBuilder extends BaseBuilder {
protected getBasePath(): string {
const basePath = this.options.debug ? 'debug' : 'release';
const target = this.getTauriTarget(this.buildArch, 'win32');
- return `src-tauri/target/${target}/${basePath}/bundle/`;
+ if (!target) {
+ throw new Error(
+ `Unsupported architecture: ${this.buildArch} for Windows`,
+ );
+ }
+ return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
}
protected hasArchSpecificTarget(): boolean {
@@ -48,6 +54,19 @@ export default class WinBuilder extends BaseBuilder {
protected getArchSpecificPath(): string {
const target = this.getTauriTarget(this.buildArch, 'win32');
- return `src-tauri/target/${target}`;
+ if (!target) {
+ throw new Error(
+ `Unsupported architecture: ${this.buildArch} for Windows`,
+ );
+ }
+ return path.join(this.getCargoTargetDir(), target);
+ }
+
+ protected getRawBinaryPath(appName: string): string {
+ return `${appName}.exe`;
+ }
+
+ protected getBinaryName(appName: string): string {
+ return `pake-${generateIdentifierSafeName(appName)}.exe`;
}
}
diff --git a/dist/cli.js b/dist/cli.js
index d6e8dd24..cac395ae 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -1162,14 +1162,22 @@ class BaseBuilder {
return 0; // Disable proxy feature if version detection fails
}
}
+ getCargoTargetDir() {
+ return process.env.CARGO_TARGET_DIR || path.join('src-tauri', 'target');
+ }
+ resolveBuildPath(npmDirectory, buildPath) {
+ return path.isAbsolute(buildPath)
+ ? buildPath
+ : path.join(npmDirectory, buildPath);
+ }
getBasePath() {
const basePath = this.options.debug ? 'debug' : 'release';
- return `src-tauri/target/${basePath}/bundle/`;
+ return path.join(this.getCargoTargetDir(), basePath, 'bundle');
}
getBuildAppPath(npmDirectory, fileName, fileType) {
// For app bundles on macOS, the directory is 'macos', not 'app'
const bundleDir = fileType.toLowerCase() === 'app' ? 'macos' : fileType.toLowerCase();
- return path.join(npmDirectory, this.getBasePath(), bundleDir, `${fileName}.${fileType}`);
+ return path.join(this.resolveBuildPath(npmDirectory, this.getBasePath()), bundleDir, `${fileName}.${fileType}`);
}
/**
* Copy raw binary file to output directory
@@ -1196,9 +1204,9 @@ class BaseBuilder {
const binaryName = this.getBinaryName(appName);
// Handle cross-platform builds
if (this.options.multiArch || this.hasArchSpecificTarget()) {
- return path.join(npmDirectory, this.getArchSpecificPath(), basePath, binaryName);
+ return path.join(this.resolveBuildPath(npmDirectory, this.getArchSpecificPath()), basePath, binaryName);
}
- return path.join(npmDirectory, 'src-tauri/target', basePath, binaryName);
+ return path.join(this.resolveBuildPath(npmDirectory, this.getCargoTargetDir()), basePath, binaryName);
}
/**
* Get the output path for the raw binary file
@@ -1229,7 +1237,7 @@ class BaseBuilder {
* Get architecture-specific path for binary
*/
getArchSpecificPath() {
- return 'src-tauri/target'; // Override in subclasses if needed
+ return this.getCargoTargetDir(); // Override in subclasses if needed
}
}
BaseBuilder.ARCH_MAPPINGS = {
@@ -1315,7 +1323,10 @@ class MacBuilder extends BaseBuilder {
const basePath = this.options.debug ? 'debug' : 'release';
const actualArch = this.getActualArch();
const target = this.getTauriTarget(actualArch, 'darwin');
- return `src-tauri/target/${target}/${basePath}/bundle`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${actualArch} for macOS`);
+ }
+ return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
}
hasArchSpecificTarget() {
return true;
@@ -1323,7 +1334,10 @@ class MacBuilder extends BaseBuilder {
getArchSpecificPath() {
const actualArch = this.getActualArch();
const target = this.getTauriTarget(actualArch, 'darwin');
- return `src-tauri/target/${target}`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${actualArch} for macOS`);
+ }
+ return path.join(this.getCargoTargetDir(), target);
}
}
@@ -1354,14 +1368,26 @@ class WinBuilder extends BaseBuilder {
getBasePath() {
const basePath = this.options.debug ? 'debug' : 'release';
const target = this.getTauriTarget(this.buildArch, 'win32');
- return `src-tauri/target/${target}/${basePath}/bundle/`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${this.buildArch} for Windows`);
+ }
+ return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
}
hasArchSpecificTarget() {
return true;
}
getArchSpecificPath() {
const target = this.getTauriTarget(this.buildArch, 'win32');
- return `src-tauri/target/${target}`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${this.buildArch} for Windows`);
+ }
+ return path.join(this.getCargoTargetDir(), target);
+ }
+ getRawBinaryPath(appName) {
+ return `${appName}.exe`;
+ }
+ getBinaryName(appName) {
+ return `pake-${generateIdentifierSafeName(appName)}.exe`;
}
}
@@ -1556,7 +1582,10 @@ post_remove() {
const basePath = this.options.debug ? 'debug' : 'release';
if (this.buildArch === 'arm64') {
const target = this.getTauriTarget(this.buildArch, 'linux');
- return `src-tauri/target/${target}/${basePath}/bundle/`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${this.buildArch} for Linux`);
+ }
+ return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
}
return super.getBasePath();
}
@@ -1572,7 +1601,10 @@ post_remove() {
getArchSpecificPath() {
if (this.buildArch === 'arm64') {
const target = this.getTauriTarget(this.buildArch, 'linux');
- return `src-tauri/target/${target}`;
+ if (!target) {
+ throw new Error(`Unsupported architecture: ${this.buildArch} for Linux`);
+ }
+ return path.join(this.getCargoTargetDir(), target);
}
return super.getArchSpecificPath();
}
diff --git a/tests/unit/base-builder.test.ts b/tests/unit/base-builder.test.ts
index 6e97fe0a..380e64e8 100644
--- a/tests/unit/base-builder.test.ts
+++ b/tests/unit/base-builder.test.ts
@@ -15,6 +15,7 @@ vi.mock('@/utils/dir', () => ({
}));
import BaseBuilder from '@/builders/BaseBuilder';
+import WinBuilder from '@/builders/WinBuilder';
import {
_resetPackageManagerCache,
configureCargoRegistry,
@@ -32,6 +33,7 @@ class TestBuilder extends BaseBuilder {
}
const originalCnMirrorEnv = process.env[CN_MIRROR_ENV];
+const originalCargoTargetDir = process.env.CARGO_TARGET_DIR;
const tempDirs: string[] = [];
const GENERATED_MIRROR_CONFIG = `[source.crates-io]
@@ -95,6 +97,12 @@ describe('BaseBuilder guards', () => {
process.env[CN_MIRROR_ENV] = originalCnMirrorEnv;
}
+ if (originalCargoTargetDir === undefined) {
+ delete process.env.CARGO_TARGET_DIR;
+ } else {
+ process.env.CARGO_TARGET_DIR = originalCargoTargetDir;
+ }
+
await Promise.all(tempDirs.splice(0).map((dir) => fsExtra.remove(dir)));
});
@@ -300,6 +308,46 @@ describe('BaseBuilder guards', () => {
expect(command).toContain('--features cli-build');
});
+ it('copies Windows build artifacts from CARGO_TARGET_DIR when it is set', () => {
+ const cargoTargetDir = path.join(process.cwd(), '.short-cargo-target');
+ process.env.CARGO_TARGET_DIR = cargoTargetDir;
+
+ const builder = new WinBuilder({
+ debug: false,
+ name: 'ChatGPT',
+ targets: 'x64',
+ } as any);
+
+ const appPath = (builder as any).getBuildAppPath(
+ process.cwd(),
+ 'ChatGPT_1.0.0_x64_en-US',
+ 'msi',
+ );
+ const binaryPath = (builder as any).getRawBinarySourcePath(
+ process.cwd(),
+ 'ChatGPT',
+ );
+
+ expect(appPath).toBe(
+ path.join(
+ cargoTargetDir,
+ 'x86_64-pc-windows-msvc',
+ 'release',
+ 'bundle',
+ 'msi',
+ 'ChatGPT_1.0.0_x64_en-US.msi',
+ ),
+ );
+ expect(binaryPath).toBe(
+ path.join(
+ cargoTargetDir,
+ 'x86_64-pc-windows-msvc',
+ 'release',
+ 'pake-chatgpt.exe',
+ ),
+ );
+ });
+
it('tracks generated Pake config files in the Cargo build script', async () => {
const buildScript = await fsExtra.readFile(
path.join(process.cwd(), 'src-tauri', 'build.rs'),