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.

10.8 KB
Raw Blame
import path from 'path';
import os from 'os';
import fsExtra from 'fs-extra';
import BaseBuilder from './BaseBuilder';
import { PakeAppOptions } from '@/types';
import { shellExec, type ShellCommand } from '@/utils/shell';
import { execa } from 'execa';
import { generateLinuxPackageName } from '@/utils/name';
import {
  LINUX_TARGET_TYPES,
  filterLinuxTargets,
  needsTemporaryDebForZst,
} from '@/utils/targets';
import logger from '@/options/logger';
import { detectPackageManager } from './env';

export default class LinuxBuilder extends BaseBuilder {
  private buildFormat: string;
  private buildArch: string;
  private currentBuildType: string = '';
  private compiled = false;

  constructor(options: PakeAppOptions) {
    super(options);

    const target = options.targets || 'deb';
    if (target.includes('-arm64')) {
      this.buildFormat = target.replace('-arm64', '');
      this.buildArch = 'arm64';
    } else {
      this.buildFormat = target;
      this.buildArch = this.resolveTargetArch('auto');
    }

    this.options.targets = this.buildFormat;
  }

  getReportArch(): string {
    return this.buildArch;
  }

  getFileName() {
    const { name = 'pake-app', targets } = this.options;
    const version = this.options.appVersion;
    const buildType =
      this.currentBuildType || targets.split(',').map((t) => t.trim())[0];

    let arch: string;
    if (this.buildArch === 'arm64') {
      arch =
        buildType === 'rpm' || buildType === 'appimage' ? 'aarch64' : 'arm64';
    } else if (this.buildArch === 'x64') {
      arch = buildType === 'rpm' ? 'x86_64' : 'amd64';
    } else {
      arch = this.buildArch;
    }

    if (this.currentBuildType === 'rpm') {
      return `${name}-${version}-1.${arch}`;
    }

    return `${name}_${version}_${arch}`;
  }

  async build(url: string) {
    this.compiled = false;
    this.currentBuildType = '';
    // --no-bundle: build the executable once with no per-format packaging loop.
    if (this.options.bundle === false) {
      await this.buildAndCopy(url, 'deb');
      return;
    }

    const targets = filterLinuxTargets(this.options.targets);
    if (targets.length === 0) {
      throw new Error(
        `No valid Linux target in "${this.options.targets}". Valid targets: ${LINUX_TARGET_TYPES.join(', ')}.`,
      );
    }
    const useTemporaryDebForZst = needsTemporaryDebForZst(targets);

    if (targets.length > 1) {
      await this.prepareBuild(url);
      const command = this.getBuildCommand(await detectPackageManager());
      command.args.push('--no-bundle');
      await this.runBuildCommand(command, 'compile');
      this.compiled = true;
    }

    // With a single explicit target, fail fast. With multiple targets (the
    // distro-aware default, or an explicit comma list) keep building the rest
    // when one fails, so a usable installer is still produced, e.g. AppImage
    // survives a .deb bundler abort on RPM-based distros.
    const isolateFailures = targets.length > 1;
    const failed: string[] = [];
    let firstError: Error | null = null;

    for (const target of targets) {
      this.currentBuildType = target;
      try {
        if (target === 'zst') {
          if (useTemporaryDebForZst) {
            await this.buildAndCopy(url, 'deb', false);
          }
          await this.createArchPackageFromDeb({
            removeSourceDeb: useTemporaryDebForZst,
          });
        } else {
          await this.buildAndCopy(url, target);
        }
      } catch (error) {
        const err = error instanceof Error ? error : new Error(String(error));
        if (!isolateFailures) {
          throw err;
        }
        if (!firstError) {
          firstError = err;
        }
        failed.push(target);
        logger.warn(
          `✼ Failed to build "${target}" target: ${err.message.split('\n')[0]}`,
        );
      }
    }

    // Every requested target failed: surface the first real error.
    if (firstError && failed.length === targets.length) {
      throw firstError;
    }

    if (failed.length > 0) {
      logger.warn(
        `✼ Skipped failed Linux targets: ${failed.join(', ')}. Other formats built successfully.`,
      );
    }
  }

  private async ensureArchPackagingTools() {
    const requiredTools = [
      { tool: 'ar', pacmanPackage: 'binutils' },
      { tool: 'bsdtar', pacmanPackage: 'libarchive' },
    ];
    for (const { tool, pacmanPackage } of requiredTools) {
      try {
        await execa(tool, ['--version'], { stdio: 'ignore' });
      } catch {
        throw new Error(
          `Building a zst package requires "${tool}". Install it first, e.g. "sudo pacman -S ${pacmanPackage}".`,
        );
      }
    }
  }

  private async createArchPackageFromDeb({
    removeSourceDeb,
  }: {
    removeSourceDeb: boolean;
  }) {
    const { name = 'pake-app' } = this.options;
    const packageName = generateLinuxPackageName(name);
    const version = this.options.appVersion;
    const arch = this.buildArch === 'arm64' ? 'aarch64' : 'x86_64';
    const debPath = path.resolve(`${name}.deb`);
    const packagePath = path.resolve(
      `${name}-${version}-1-${arch}.pkg.tar.zst`,
    );
    await this.ensureArchPackagingTools();
    const workDir = await fsExtra.mkdtemp(path.join(os.tmpdir(), 'pake-arch-'));
    const dataDir = path.join(workDir, 'data');
    const controlDir = path.join(workDir, 'control');

    try {
      await fsExtra.ensureDir(dataDir);
      await fsExtra.ensureDir(controlDir);
      await shellExec({
        executable: 'ar',
        args: ['x', debPath],
        cwd: controlDir,
      });
      const dataArchive = (await fsExtra.readdir(controlDir)).find((file) =>
        file.startsWith('data.tar'),
      );
      if (!dataArchive) {
        throw new Error(`Could not find data.tar payload in ${debPath}`);
      }

      await shellExec({
        executable: 'tar',
        args: ['-xf', path.join(controlDir, dataArchive), '-C', dataDir],
      });
      const installedSize = await this.getPathSize(dataDir);
      const pkgInfo = `pkgname = ${packageName}
pkgbase = ${packageName}
pkgver = ${version}-1
pkgdesc = ${name} Pake app
url = https://github.com/tw93/Pake
builddate = ${Math.floor(Date.now() / 1000)}
packager = Pake
size = ${installedSize}
arch = ${arch}
license = custom
depend = cairo
depend = desktop-file-utils
depend = gdk-pixbuf2
depend = glib2
depend = gtk3
depend = hicolor-icon-theme
depend = libsoup3
depend = pango
depend = webkit2gtk-4.1
`;
      await fsExtra.writeFile(path.join(dataDir, '.PKGINFO'), pkgInfo);
      await fsExtra.writeFile(
        path.join(dataDir, '.INSTALL'),
        `post_install() {
  gtk-update-icon-cache -q -t -f usr/share/icons/hicolor
  update-desktop-database -q usr/share/applications
}

post_upgrade() {
  post_install
}

post_remove() {
  gtk-update-icon-cache -q -t -f usr/share/icons/hicolor
  update-desktop-database -q usr/share/applications
}
`,
      );
      await shellExec({
        executable: 'bsdtar',
        args: [
          '--zstd',
          '-cf',
          packagePath,
          '-C',
          dataDir,
          '.PKGINFO',
          '.INSTALL',
          'usr',
        ],
      });
      await this.recordArtifact(packagePath, 'zst');
      logger.success('✔ Build success!');
      logger.success('✔ App installer located in', packagePath);
    } finally {
      if (removeSourceDeb) {
        await fsExtra.remove(debPath);
        this.removeArtifact(debPath);
      }
      await fsExtra.remove(workDir);
    }
  }

  // Override buildAndCopy to ensure currentBuildType is synced if called directly, though the loop above handles it most of the time.
  async buildAndCopy(url: string, target: string, logSuccess = true) {
    this.currentBuildType = target;
    if (!this.compiled) {
      await super.buildAndCopy(url, target, logSuccess);
      return;
    }
    const packageManager = await detectPackageManager();
    const args = ['run', 'tauri'];
    if (packageManager === 'npm') args.push('--');
    args.push(
      'bundle',
      '--config',
      path.join('src-tauri', '.pake', 'tauri.conf.json'),
    );
    args.push(
      '--bundles',
      target,
      '--features',
      this.getBuildFeatures().join(','),
    );
    if (this.options.debug) args.push('--debug');
    if (this.options.debug || target === 'appimage' || process.env.PAKE_VERBOSE)
      args.push('--verbose');
    if (this.buildArch === 'arm64')
      args.push('--target', this.getTauriTarget('arm64', 'linux')!);
    await this.runBuildCommand({ executable: packageManager, args }, target);
    await this.copyBuildArtifacts(target, logSuccess);
  }

  protected getBuildCommand(packageManager: string = 'pnpm'): ShellCommand {
    const configPath = path.join('src-tauri', '.pake', 'tauri.conf.json');

    const buildTarget =
      this.buildArch === 'arm64'
        ? (this.getTauriTarget(this.buildArch, 'linux') ?? undefined)
        : undefined;

    const fullCommand = this.buildBaseCommand(
      packageManager,
      configPath,
      buildTarget,
    );

    // --no-bundle: build the executable only, skipping .deb/.rpm/.appimage
    // packaging entirely (e.g. RPM-based distros where the bundler aborts).
    if (this.options.bundle === false) {
      fullCommand.args.push('--no-bundle');
      return fullCommand;
    }

    if (this.currentBuildType) {
      fullCommand.args.push('--bundles', this.currentBuildType);
    }

    // Enable verbose output for AppImage builds when debugging or PAKE_VERBOSE is set.
    // AppImage builds often fail with minimal error messages from linuxdeploy,
    // so verbose mode helps diagnose issues like strip failures and missing dependencies.
    if (
      this.currentBuildType === 'appimage' &&
      (this.options.targets.includes('appimage') ||
        this.options.debug ||
        process.env.PAKE_VERBOSE)
    ) {
      fullCommand.args.push('--verbose');
    }

    return fullCommand;
  }

  protected getBasePath(): string {
    const basePath = this.options.debug ? 'debug' : 'release';

    if (this.buildArch === 'arm64') {
      const target = this.getTauriTarget(this.buildArch, 'linux');
      if (!target) {
        throw new Error(
          `Unsupported architecture: ${this.buildArch} for Linux`,
        );
      }
      return path.join(this.getCargoTargetDir(), target, basePath, 'bundle');
    }

    return super.getBasePath();
  }

  protected getFileType(target: string): string {
    if (target === 'appimage') {
      return 'AppImage';
    }
    return super.getFileType(target);
  }

  protected hasArchSpecificTarget(): boolean {
    return this.buildArch === 'arm64';
  }

  protected getArchSpecificPath(): string {
    if (this.buildArch === 'arm64') {
      const target = this.getTauriTarget(this.buildArch, 'linux');
      if (!target) {
        throw new Error(
          `Unsupported architecture: ${this.buildArch} for Linux`,
        );
      }
      return path.join(this.getCargoTargetDir(), target);
    }
    return super.getArchSpecificPath();
  }
}