Merge pull request #4 from shumengya/cursor/fix-non-square-icon-b4e4

fix(ci): 自动处理非正方形 logo 图标
This commit is contained in:
cursor[bot]
2026-05-30 08:15:35 +00:00
committed by GitHub
4 changed files with 684 additions and 190 deletions

View File

@@ -1,7 +1,9 @@
import { execSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import sharp from "sharp";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, "../..");
@@ -37,16 +39,48 @@ function findIconSource(baseDir) {
return null;
}
export function findIconSourceForJob(root, jobId, distDir) {
const buildDir = jobId ? path.join(root, "builds", jobId) : null;
export function findIconSourceForJob(rootDir, jobId, dist) {
const buildDir = jobId ? path.join(rootDir, "builds", jobId) : null;
if (buildDir && fs.existsSync(buildDir)) {
const fromUpload = findIconSource(buildDir);
if (fromUpload) return fromUpload;
}
return findIconSource(distDir);
return findIconSource(dist);
}
export function generateAppIcons(options = {}) {
async function ensureSquareIconSource(iconSource) {
const meta = await sharp(iconSource).metadata();
const width = meta.width ?? 0;
const height = meta.height ?? 0;
if (!width || !height) {
throw new Error(`Unable to read icon dimensions: ${iconSource}`);
}
if (width === height) {
return iconSource;
}
const size = Math.min(width, height);
const left = Math.floor((width - size) / 2);
const top = Math.floor((height - size) / 2);
const squarePath = path.join(
os.tmpdir(),
`web2app-icon-${path.basename(iconSource, path.extname(iconSource))}.png`,
);
await sharp(iconSource)
.extract({ left, top, width: size, height: size })
.png()
.toFile(squarePath);
console.log(
`Normalized icon to square by center crop (${width}x${height} -> ${size}x${size})`,
);
return squarePath;
}
export async function generateAppIcons(options = {}) {
const { jobId } = options;
if (!fs.existsSync(distDir)) {
console.log("No dist directory, keeping default icons");
@@ -59,8 +93,9 @@ export function generateAppIcons(options = {}) {
return false;
}
const squareSource = await ensureSquareIconSource(iconSource);
console.log(`Generating app icons from: ${path.relative(root, iconSource)}`);
execSync(`npx tauri icon "${iconSource}"`, {
execSync(`npx tauri icon "${squareSource}"`, {
cwd: templateDir,
stdio: "inherit",
});
@@ -74,5 +109,5 @@ const isCli =
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isCli) {
generateAppIcons({ jobId: process.env.JOB_ID });
await generateAppIcons({ jobId: process.env.JOB_ID });
}

View File

@@ -99,6 +99,6 @@ if (fs.existsSync(androidConfPath)) {
patchTauriConfig(androidConfPath);
}
generateAppIcons({ jobId });
await generateAppIcons({ jobId });
console.log("Template prepared successfully");

824
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -18,6 +18,7 @@
"devDependencies": {
"adm-zip": "^0.5.17",
"concurrently": "^9.1.2",
"sharp": "^0.34.2",
"typescript": "~5.6.2",
"wrangler": "^3.99.0"
}