From 1504149635cbde0366bf6ba5b6cc3365bb2e71bc Mon Sep 17 00:00:00 2001 From: Tlntin Date: Wed, 28 Dec 2022 23:26:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=85=8D=E5=A5=97pake-cli?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/builders/common.ts | 128 +++++++++++++++++++++++- bin/builders/tauriConf.js | 4 +- bin/cli.ts | 9 +- bin/defaults.ts | 5 + bin/types.ts | 15 +++ dist/cli.js | 198 ++++++++++++++++++++++++++++++++------ 6 files changed, 323 insertions(+), 36 deletions(-) diff --git a/bin/builders/common.ts b/bin/builders/common.ts index 06b2d19..07e14ba 100644 --- a/bin/builders/common.ts +++ b/bin/builders/common.ts @@ -15,6 +15,7 @@ export async function promptText(message: string, initial?: string) { return response.content; } + export async function mergeTauriConfig( url: string, options: PakeAppOptions, @@ -26,6 +27,11 @@ export async function mergeTauriConfig( fullscreen, transparent, resizable, + userAgent, + showMenu, + showSystemTray, + systemTrayIcon, + // iter_copy_file, identifier, name, } = options; @@ -56,10 +62,84 @@ export async function mergeTauriConfig( } } + // logger.warn(JSON.stringify(tauriConf.pake.windows, null, 4)); + Object.assign(tauriConf.pake.windows[0], { url, ...tauriConfWindowOptions }); + // 判断一下url类型,是文件还是网站 + // 如果是文件,则需要将该文件以及所在文件夹下的所有文件拷贝到src目录下(待做) + + // const src_exists = await fs.stat("src") + // .then(() => true) + // .catch(() => false); + // if (!src_exists) { + // fs.mkdir("src") + // } else { + // fs.rm + // } + const url_exists = await fs.stat(url) + .then(() => true) + .catch(() => false); + if (url_exists) { + tauriConf.pake.windows[0].url_type = "local"; + const file_name = path.basename(url); + // const dir_name = path.dirname(url); + const url_path = path.join("dist/", file_name); + await fs.copyFile(url, url_path); + tauriConf.pake.windows[0].url = file_name; + tauriConf.pake.windows[0].url_type = "local"; + } else { + tauriConf.pake.windows[0].url_type = "web"; + } + + // 处理user-agent + logger.warn(userAgent); + if (userAgent.length > 0) { + if (process.platform === "win32") { + tauriConf.pake.user_agent.windows = userAgent; + } + + if (process.platform === "linux") { + tauriConf.pake.user_agent.linux = userAgent; + } + + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = userAgent; + } + } + + // 处理菜单栏 + if (showMenu) { + if (process.platform === "win32") { + tauriConf.pake.menu.windows = true; + } + + if (process.platform === "linux") { + tauriConf.pake.menu.linux = true; + } + + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = true; + } + } + + // 处理托盘 + if (showSystemTray) { + if (process.platform === "win32") { + tauriConf.pake.system_tray.windows = true; + } + + if (process.platform === "linux") { + tauriConf.pake.system_tray.linux = true; + } + + if (process.platform === "darwin") { + tauriConf.pake.system_tray.macos = true; + } + } - Object.assign(tauriConf.tauri.windows[0], { url, ...tauriConfWindowOptions }); tauriConf.package.productName = name; tauriConf.tauri.bundle.identifier = identifier; + + // 处理应用图标 const exists = await fs.stat(options.icon) .then(() => true) .catch(() => false); @@ -97,7 +177,40 @@ export async function mergeTauriConfig( logger.warn("the custom icon path may not exists. we will use default icon to replace it"); } + // 处理托盘自定义图标 + let useDefaultIcon = true; // 是否使用默认托盘图标 + if (systemTrayIcon.length > 0) { + const icon_exists = await fs.stat(systemTrayIcon) + .then(() => true) + .catch(() => false); + if (icon_exists) { + // 需要判断图标格式,默认只支持ico和png两种 + let iconExt = path.extname(systemTrayIcon).toLowerCase(); + if (iconExt == ".png" || iconExt == ".icon") { + useDefaultIcon = false; + const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`); + tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`; + await fs.copyFile(systemTrayIcon, trayIcoPath); + } else { + logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`); + logger.warn(`system tray icon file will not change with default.`); + } + } else { + logger.warn(`${systemTrayIcon} not exists!`) + logger.warn(`system tray icon file will not change with default.`); + } + } + // 处理托盘默认图标 + if (useDefaultIcon) { + if (process.platform === "linux" || process.platform === "win32") { + tauriConf.tauri.systemTray.iconPath = tauriConf.tauri.bundle.icon[0]; + } else { + tauriConf.tauri.systemTray.iconPath = "png/icon_512.png"; + } + } + + // 保存配置文件 let configPath = ""; switch (process.platform) { case "win32": { @@ -117,13 +230,22 @@ export async function mergeTauriConfig( let bundleConf = {tauri: {bundle: tauriConf.tauri.bundle}}; await fs.writeFile( configPath, - Buffer.from(JSON.stringify(bundleConf), 'utf-8') + Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8') ); + const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json') + await fs.writeFile( + pakeConfigPath, + Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8') + ); + + let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); + delete tauriConf2.pake; + delete tauriConf2.tauri.bundle; const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json') await fs.writeFile( configJsonPath, - Buffer.from(JSON.stringify(tauriConf), 'utf-8') + Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8') ); } diff --git a/bin/builders/tauriConf.js b/bin/builders/tauriConf.js index 2b5fd20..53122b8 100644 --- a/bin/builders/tauriConf.js +++ b/bin/builders/tauriConf.js @@ -1,11 +1,13 @@ import CommonConf from '../../src-tauri/tauri.conf.json'; +import pakeConf from '../../src-tauri/pake.json'; import WinConf from '../../src-tauri/tauri.windows.conf.json'; import MacConf from '../../src-tauri/tauri.macos.conf.json'; import LinuxConf from '../../src-tauri/tauri.linux.conf.json'; let tauriConf = { package: CommonConf.package, - tauri: CommonConf.tauri + tauri: CommonConf.tauri, + pake: pakeConf } switch (process.platform) { case "win32": { diff --git a/bin/cli.ts b/bin/cli.ts index 14a6d0e..174564a 100644 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -23,6 +23,13 @@ program .option('--no-resizable', 'whether the window can be resizable', DEFAULT_PAKE_OPTIONS.resizable) .option('--fullscreen', 'makes the packaged app start in full screen', DEFAULT_PAKE_OPTIONS.fullscreen) .option('--transparent', 'transparent title bar', DEFAULT_PAKE_OPTIONS.transparent) + .option('--user-agent ', 'custom user agent', DEFAULT_PAKE_OPTIONS.userAgent) + .option('--show-menu', 'show menu in app', DEFAULT_PAKE_OPTIONS.showMenu) + .option('--show-system-tray', 'show system tray in app', DEFAULT_PAKE_OPTIONS.showSystemTray) + .option('--system-tray-icon ', 'custom system tray icon', DEFAULT_PAKE_OPTIONS.systemTrayIcon) + // .option('--iter-copy-file', + // 'copy all static file to pake app when url is a static file', + // DEFAULT_PAKE_OPTIONS.iter_copy_file) .option('--debug', 'debug', DEFAULT_PAKE_OPTIONS.transparent) .action(async (url: string, options: PakeCliOptions) => { checkUpdateTips(); @@ -41,7 +48,7 @@ program await builder.prepare(); const appOptions = await handleInputOptions(options, url); - + // logger.warn(JSON.stringify(appOptions, null, 4)); builder.build(url, appOptions); }); diff --git a/bin/defaults.ts b/bin/defaults.ts index fb5cbff..c28cdb5 100644 --- a/bin/defaults.ts +++ b/bin/defaults.ts @@ -7,6 +7,11 @@ export const DEFAULT_PAKE_OPTIONS: PakeCliOptions = { fullscreen: false, resizable: true, transparent: false, + userAgent: '', + showMenu: false, + showSystemTray: false, + // iter_copy_file: false, + systemTrayIcon: '', debug: false, }; diff --git a/bin/types.ts b/bin/types.ts index 9610edb..d0ec34e 100644 --- a/bin/types.ts +++ b/bin/types.ts @@ -20,6 +20,21 @@ export interface PakeCliOptions { /** 是否开启沉浸式头部,默认为 false 不开启 ƒ*/ transparent: boolean; + /** 自定义UA,默认为不开启 ƒ*/ + userAgent: string; + + /** 开启菜单栏,MacOS默认开启,Windows,Linux默认不开启 ƒ*/ + showMenu: boolean; + + /** 开启系统托盘,MacOS默认不开启,Windows,Linux默认开启 ƒ*/ + showSystemTray: boolean; + + /** 托盘图标, Windows、Linux默认和应用图标共用一样的,MacOS需要提别提供, 格式为png或者ico */ + systemTrayIcon: string; + + // /** 递归拷贝,当url为本地文件路径时候,将文件所在文件夹下面的所有子文件都拷贝到pake静态文件夹,默认不开启 */ + // iter_copy_file: false; + /** 调试模式,会输出更多日志 */ debug: boolean; } diff --git a/dist/cli.js b/dist/cli.js index 468a13b..1a5f29f 100644 --- a/dist/cli.js +++ b/dist/cli.js @@ -47,6 +47,11 @@ const DEFAULT_PAKE_OPTIONS = { fullscreen: false, resizable: true, transparent: false, + userAgent: '', + showMenu: false, + showSystemTray: false, + // iter_copy_file: false, + systemTrayIcon: '', debug: false, }; @@ -1624,7 +1629,9 @@ function promptText(message, initial) { } function mergeTauriConfig(url, options, tauriConf) { return __awaiter(this, void 0, void 0, function* () { - const { width, height, fullscreen, transparent, resizable, identifier, name, } = options; + const { width, height, fullscreen, transparent, resizable, userAgent, showMenu, showSystemTray, systemTrayIcon, + // iter_copy_file, + identifier, name, } = options; const tauriConfWindowOptions = { width, height, @@ -1650,9 +1657,73 @@ function mergeTauriConfig(url, options, tauriConf) { process.exit(); } } - Object.assign(tauriConf.tauri.windows[0], Object.assign({ url }, tauriConfWindowOptions)); + // logger.warn(JSON.stringify(tauriConf.pake.windows, null, 4)); + Object.assign(tauriConf.pake.windows[0], Object.assign({ url }, tauriConfWindowOptions)); + // 判断一下url类型,是文件还是网站 + // 如果是文件,则需要将该文件以及所在文件夹下的所有文件拷贝到src目录下(待做) + // const src_exists = await fs.stat("src") + // .then(() => true) + // .catch(() => false); + // if (!src_exists) { + // fs.mkdir("src") + // } else { + // fs.rm + // } + const url_exists = yield fs.stat(url) + .then(() => true) + .catch(() => false); + if (url_exists) { + tauriConf.pake.windows[0].url_type = "local"; + const file_name = path.basename(url); + // const dir_name = path.dirname(url); + const url_path = path.join("dist/", file_name); + yield fs.copyFile(url, url_path); + tauriConf.pake.windows[0].url = file_name; + tauriConf.pake.windows[0].url_type = "local"; + } + else { + tauriConf.pake.windows[0].url_type = "web"; + } + // 处理user-agent + logger.warn(userAgent); + if (userAgent.length > 0) { + if (process.platform === "win32") { + tauriConf.pake.user_agent.windows = userAgent; + } + if (process.platform === "linux") { + tauriConf.pake.user_agent.linux = userAgent; + } + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = userAgent; + } + } + // 处理菜单栏 + if (showMenu) { + if (process.platform === "win32") { + tauriConf.pake.menu.windows = true; + } + if (process.platform === "linux") { + tauriConf.pake.menu.linux = true; + } + if (process.platform === "darwin") { + tauriConf.pake.user_agent.macos = true; + } + } + // 处理托盘 + if (showSystemTray) { + if (process.platform === "win32") { + tauriConf.pake.system_tray.windows = true; + } + if (process.platform === "linux") { + tauriConf.pake.system_tray.linux = true; + } + if (process.platform === "darwin") { + tauriConf.pake.system_tray.macos = true; + } + } tauriConf.package.productName = name; tauriConf.tauri.bundle.identifier = identifier; + // 处理应用图标 const exists = yield fs.stat(options.icon) .then(() => true) .catch(() => false); @@ -1691,6 +1762,41 @@ function mergeTauriConfig(url, options, tauriConf) { else { logger.warn("the custom icon path may not exists. we will use default icon to replace it"); } + // 处理托盘自定义图标 + let useDefaultIcon = true; // 是否使用默认托盘图标 + if (systemTrayIcon.length > 0) { + const icon_exists = yield fs.stat(systemTrayIcon) + .then(() => true) + .catch(() => false); + if (icon_exists) { + // 需要判断图标格式,默认只支持ico和png两种 + let iconExt = path.extname(systemTrayIcon).toLowerCase(); + if (iconExt == ".png" || iconExt == ".icon") { + useDefaultIcon = false; + const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`); + tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`; + yield fs.copyFile(systemTrayIcon, trayIcoPath); + } + else { + logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`); + logger.warn(`system tray icon file will not change with default.`); + } + } + else { + logger.warn(`${systemTrayIcon} not exists!`); + logger.warn(`system tray icon file will not change with default.`); + } + } + // 处理托盘默认图标 + if (useDefaultIcon) { + if (process.platform === "linux" || process.platform === "win32") { + tauriConf.tauri.systemTray.iconPath = tauriConf.tauri.bundle.icon[0]; + } + else { + tauriConf.tauri.systemTray.iconPath = "png/icon_512.png"; + } + } + // 保存配置文件 let configPath = ""; switch (process.platform) { case "win32": { @@ -1707,9 +1813,14 @@ function mergeTauriConfig(url, options, tauriConf) { } } let bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } }; - yield fs.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf), 'utf-8')); + yield fs.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8')); + const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json'); + yield fs.writeFile(pakeConfigPath, Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8')); + let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); + delete tauriConf2.pake; + delete tauriConf2.tauri.bundle; const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json'); - yield fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf), 'utf-8')); + yield fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8')); }); } @@ -1859,36 +1970,56 @@ function checkRustInstalled() { } var tauri$3 = { - windows: [ - { - url: "https://weread.qq.com/", - transparent: true, - fullscreen: false, - width: 1200, - height: 780, - resizable: true - } - ], security: { csp: null }, updater: { active: false + }, + systemTray: { + iconPath: "/home/tlntin/data/code/rust_study/Pake/src-tauri/png/code_512.png", + iconAsTemplate: true } }; -var build = { - devPath: "../dist", - distDir: "../dist", - beforeBuildCommand: "", - beforeDevCommand: "" -}; var CommonConf = { "package": { - productName: "WeRead", + productName: "baidu", version: "1.0.0" }, - tauri: tauri$3, - build: build + tauri: tauri$3 +}; + +var windows = [ + { + url: "https://www.baidu.com", + transparent: false, + fullscreen: false, + width: 1200, + height: 780, + resizable: true, + url_type: "web" + } +]; +var user_agent = { + macos: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15", + linux: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", + windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" +}; +var menu = { + macos: true, + linux: true, + windows: false +}; +var system_tray = { + macos: false, + linux: true, + windows: true +}; +var pakeConf = { + windows: windows, + user_agent: user_agent, + menu: menu, + system_tray: system_tray }; var tauri$2 = { @@ -1963,10 +2094,9 @@ var MacConf = { var tauri = { bundle: { icon: [ - "png/weread_256.ico", - "png/weread_512.png" + "/home/tlntin/data/code/rust_study/Pake/src-tauri/png/code_512.png" ], - identifier: "com.tw93.weread", + identifier: "pake-f9751d", active: true, category: "DeveloperTool", copyright: "", @@ -1982,10 +2112,7 @@ var tauri = { "librsvg2-dev", "gnome-video-effects", "gnome-video-effects-extra" - ], - files: { - "/usr/share/applications/com-tw93-weread.desktop": "assets/com-tw93-weread.desktop" - } + ] }, externalBin: [ ], @@ -2005,7 +2132,8 @@ var LinuxConf = { let tauriConf = { package: CommonConf.package, - tauri: CommonConf.tauri + tauri: CommonConf.tauri, + pake: pakeConf }; switch (process.platform) { case "win32": { @@ -2301,6 +2429,13 @@ program .option('--no-resizable', 'whether the window can be resizable', DEFAULT_PAKE_OPTIONS.resizable) .option('--fullscreen', 'makes the packaged app start in full screen', DEFAULT_PAKE_OPTIONS.fullscreen) .option('--transparent', 'transparent title bar', DEFAULT_PAKE_OPTIONS.transparent) + .option('--user-agent ', 'custom user agent', DEFAULT_PAKE_OPTIONS.userAgent) + .option('--show-menu', 'show menu in app', DEFAULT_PAKE_OPTIONS.showMenu) + .option('--show-system-tray', 'show system tray in app', DEFAULT_PAKE_OPTIONS.showSystemTray) + .option('--system-tray-icon ', 'custom system tray icon', DEFAULT_PAKE_OPTIONS.systemTrayIcon) + // .option('--iter-copy-file', + // 'copy all static file to pake app when url is a static file', + // DEFAULT_PAKE_OPTIONS.iter_copy_file) .option('--debug', 'debug', DEFAULT_PAKE_OPTIONS.transparent) .action((url, options) => __awaiter(void 0, void 0, void 0, function* () { checkUpdateTips(); @@ -2315,6 +2450,7 @@ program const builder = BuilderFactory.create(); yield builder.prepare(); const appOptions = yield handleOptions(options, url); + // logger.warn(JSON.stringify(appOptions, null, 4)); builder.build(url, appOptions); })); program.parse();