Skip to content

Commit

Permalink
fix(portable): portable bundle issue (#335)
Browse files Browse the repository at this point in the history
* fix(portable): portable bundle issue

* refactor: clean up unsafe code

* chore: add author field in changelog
  • Loading branch information
greenhat616 committed Jan 28, 2024
1 parent e9abc47 commit c9ea3cd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 42 deletions.
13 changes: 12 additions & 1 deletion .github/conventional-changelog/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"use strict";
const config = require("conventional-changelog-conventionalcommits");

module.exports = config({
const GIT_COMMIT_WITH_AUTHOR_FORMAT =
"%B%n-hash-%n%H%n-gitTags-%n%d%n-committerDate-%n%ci%n-authorName-%n%an%n-authorEmail-%n%ae%n-gpgStatus-%n%G?%n-gpgSigner-%n%GS";
const extraCommitMsg = `by {{authorName}}`;

const configs = config({
types: [
{
type: "feat",
Expand Down Expand Up @@ -37,3 +41,10 @@ module.exports = config({
},
],
});

config.gitRawCommitsOpts.format = GIT_COMMIT_WITH_AUTHOR_FORMAT;
config.writerOpts.commitPartial =
config.writerOpts.commitPartial.replace(/\n*$/, "") +
` {{#if @root.linkReferences~}}${extraCommitMsg}{{~/if}}\n`;

module.exports = configs;
60 changes: 26 additions & 34 deletions backend/tauri/src/utils/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::core::handle;
use anyhow::Result;
use std::path::PathBuf;
use std::{path::PathBuf, sync::OnceLock};
use tauri::{
api::path::{home_dir, resource_dir},
Env, PackageInfo,
Env,
};

#[cfg(not(feature = "verge-dev"))]
Expand All @@ -15,12 +16,10 @@ static VERGE_CONFIG: &str = "verge.yaml";
static PROFILE_YAML: &str = "profiles.yaml";
static STORAGE_DB: &str = "storage";

static mut RESOURCE_DIR: Option<PathBuf> = None;

/// portable flag
#[allow(unused)]
#[cfg(target_os = "windows")]
static mut PORTABLE_FLAG: bool = false;
static PORTABLE_FLAG: OnceLock<bool> = OnceLock::new();

pub static APP_VERSION: &str = env!("NYANPASU_VERSION");

Expand All @@ -30,16 +29,12 @@ pub fn get_app_version() -> &'static str {

#[cfg(target_os = "windows")]
pub fn get_portable_flag() -> bool {
unsafe { PORTABLE_FLAG }
}

pub fn get_resource_dir() -> Option<PathBuf> {
unsafe { RESOURCE_DIR.clone() }
*PORTABLE_FLAG.get().unwrap_or(&false)
}

/// initialize portable flag
#[cfg(target_os = "windows")]
pub unsafe fn init_portable_flag() -> Result<()> {
pub fn init_portable_flag() -> Result<()> {
use tauri::utils::platform::current_exe;

let exe = current_exe()?;
Expand All @@ -48,10 +43,11 @@ pub unsafe fn init_portable_flag() -> Result<()> {
let dir = PathBuf::from(dir).join(".config/PORTABLE");

if dir.exists() {
PORTABLE_FLAG = true;
PORTABLE_FLAG.get_or_init(|| true);
return Ok(());
}
}

PORTABLE_FLAG.get_or_init(|| false);
Ok(())
}

Expand All @@ -61,7 +57,7 @@ pub fn app_home_dir() -> Result<PathBuf> {
{
use tauri::utils::platform::current_exe;

if !get_portable_flag() {
if !PORTABLE_FLAG.get().unwrap_or(&false) {
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get app home dir"))?
.join(".config")
Expand All @@ -84,18 +80,17 @@ pub fn app_home_dir() -> Result<PathBuf> {
}

/// get the resources dir
pub fn app_resources_dir(package_info: &PackageInfo) -> Result<PathBuf> {
let res_dir = resource_dir(package_info, &Env::default())
.ok_or(anyhow::anyhow!("failed to get the resource dir"))?
.join("resources");

unsafe {
RESOURCE_DIR = Some(res_dir.clone());
}

Ok(res_dir)
pub fn app_resources_dir() -> Result<PathBuf> {
let handle = handle::Handle::global();
let app_handle = handle.app_handle.lock();
if let Some(app_handle) = app_handle.as_ref() {
let res_dir = resource_dir(app_handle.package_info(), &Env::default())
.ok_or(anyhow::anyhow!("failed to get the resource dir"))?
.join("resources");
return Ok(res_dir);
};
Err(anyhow::anyhow!("failed to get the resource dir"))
}

/// profiles dir
pub fn app_profiles_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("profiles"))
Expand All @@ -122,21 +117,18 @@ pub fn storage_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join(STORAGE_DB))
}

#[allow(unused)]
pub fn app_res_dir() -> Result<PathBuf> {
get_resource_dir().ok_or(anyhow::anyhow!("failed to get the resource dir"))
pub fn clash_pid_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join("clash.pid"))
}

pub fn clash_pid_path() -> Result<PathBuf> {
Ok(get_resource_dir()
.ok_or(anyhow::anyhow!("failed to get the resource dir"))?
.join("clash.pid"))
#[cfg(windows)]
pub fn service_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("service"))
}

#[cfg(windows)]
pub fn service_path() -> Result<PathBuf> {
let res_dir = get_resource_dir().ok_or(anyhow::anyhow!("failed to get the resource dir"))?;
Ok(res_dir.join("clash-verge-service.exe"))
Ok(service_dir()?.join("clash-verge-service.exe"))
}

#[cfg(windows)]
Expand Down
9 changes: 3 additions & 6 deletions backend/tauri/src/utils/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use log4rs::{
encode::pattern::PatternEncoder,
};
use std::fs;
use tauri::PackageInfo;

/// initialize this instance's log file
fn init_log() -> Result<()> {
Expand Down Expand Up @@ -76,9 +75,7 @@ fn init_log() -> Result<()> {
/// before tauri setup
pub fn init_config() -> Result<()> {
#[cfg(target_os = "windows")]
unsafe {
let _ = dirs::init_portable_flag();
}
let _ = dirs::init_portable_flag();

let _ = init_log();

Expand Down Expand Up @@ -124,9 +121,9 @@ pub fn init_config() -> Result<()> {

/// initialize app resources
/// after tauri setup
pub fn init_resources(package_info: &PackageInfo) -> Result<()> {
pub fn init_resources() -> Result<()> {
let app_dir = dirs::app_home_dir()?;
let res_dir = dirs::app_resources_dir(package_info)?;
let res_dir = dirs::app_resources_dir()?;

if !app_dir.exists() {
let _ = fs::create_dir_all(&app_dir);
Expand Down
2 changes: 1 addition & 1 deletion backend/tauri/src/utils/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn resolve_setup(app: &mut App) {

handle::Handle::global().init(app.app_handle());

log_err!(init::init_resources(app.package_info()));
log_err!(init::init_resources());

// 处理随机端口
let enable_random_port = Config::verge().latest().enable_random_port.unwrap_or(false);
Expand Down
5 changes: 5 additions & 0 deletions scripts/portable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ async function resolvePortable() {
if (process.platform !== "win32") return;

const releaseDir = path.join("backend/target/release");
const configDir = path.join(releaseDir, ".config");

if (!(await fs.pathExists(releaseDir))) {
throw new Error("could not found the release dir");
}

await fs.ensureDir(configDir);
await fs.createFile(path.join(configDir, "PORTABLE"));

const zip = new AdmZip();

zip.addLocalFile(path.join(releaseDir, "Clash Nyanpasu.exe"));
zip.addLocalFile(path.join(releaseDir, "clash.exe"));
zip.addLocalFile(path.join(releaseDir, "mihomo.exe"));
zip.addLocalFile(path.join(releaseDir, "clash-rs.exe"));
zip.addLocalFolder(path.join(releaseDir, "resources"), "resources");
zip.addLocalFolder(configDir, ".config");

const { version } = packageJson;

Expand Down

0 comments on commit c9ea3cd

Please sign in to comment.