diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0c9bb7b..61bd2e7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -255,6 +255,55 @@ jobs: prerelease: ${{ inputs.prerelease }} args: ${{ matrix.args }} + - name: Package portable Windows archive + if: matrix.platform == 'windows-latest' + shell: bash + run: | + VERSION="${{ needs.prepare-release.outputs.version }}" + TAG="${{ needs.prepare-release.outputs.tag }}" + + echo "Packaging portable Windows x64 zip for Scribl $VERSION ($TAG)..." + + EXE_PATH="" + if [ -f "src-tauri/target/release/scribl.exe" ]; then + EXE_PATH="src-tauri/target/release/scribl.exe" + elif [ -f "target/release/scribl.exe" ]; then + EXE_PATH="target/release/scribl.exe" + else + EXE_PATH=$(find . -type f -name "scribl.exe" | grep -v "bundle" | head -n 1 || true) + fi + + if [ -z "$EXE_PATH" ] || [ ! -f "$EXE_PATH" ]; then + echo "::error::Could not find scribl.exe in target release directories" + exit 1 + fi + echo "Found scribl.exe at: $EXE_PATH" + + PORTABLE_DIR="scribl_${VERSION}_x64_portable" + rm -rf "$PORTABLE_DIR" + mkdir -p "$PORTABLE_DIR/data/config/themes" + + cp "$EXE_PATH" "$PORTABLE_DIR/" + [ -f "LICENSE" ] && cp "LICENSE" "$PORTABLE_DIR/" + [ -f "README.md" ] && cp "README.md" "$PORTABLE_DIR/" + + echo "Scribl Portable Mode" > "$PORTABLE_DIR/data/README.txt" + echo "====================" >> "$PORTABLE_DIR/data/README.txt" + echo "This folder contains all configuration files and local webview cache/storage." >> "$PORTABLE_DIR/data/README.txt" + echo "- data/config/init.lua: Lua configuration & keybindings" >> "$PORTABLE_DIR/data/README.txt" + echo "- data/config/themes/: Custom themes" >> "$PORTABLE_DIR/data/README.txt" + echo "- data/EBWebView/ or data/Local Storage/: Webview cache and autosaved canvas drawings" >> "$PORTABLE_DIR/data/README.txt" + echo "" >> "$PORTABLE_DIR/data/README.txt" + echo "Keep the 'data' folder in the same directory as scribl.exe to remain completely self-contained." >> "$PORTABLE_DIR/data/README.txt" + + ZIP_NAME="scribl_${VERSION}_x64_portable.zip" + 7z a "$ZIP_NAME" "$PORTABLE_DIR" + + echo "Uploading $ZIP_NAME to release $TAG..." + gh release upload "$TAG" "$ZIP_NAME" --clobber + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + publish-release: name: Publish Release needs: [prepare-release, build-tauri] diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 7e0882f..ef0c7bb 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -71,8 +71,21 @@ fn reveal_themes_folder(app: tauri::AppHandle) -> Result<(), String> { .map_err(|e| e.to_string()) } +pub fn apply_portable_directories_override( + config: &mut tauri::Config, + portable_dir: Option, +) { + if let Some(dir) = portable_dir { + config.app.app_directories_override = + Some(tauri::utils::config::AppDirectoriesOverride::Root(dir)); + } +} + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + let mut context = tauri::generate_context!(); + apply_portable_directories_override(context.config_mut(), watcher::get_portable_data_dir()); + tauri::Builder::default() .plugin(tauri_plugin_opener::init()) .setup(|app| { @@ -148,7 +161,7 @@ pub fn run() { reveal_themes_folder, get_app_version ]) - .run(tauri::generate_context!()) + .run(context) .expect("error while running tauri application"); } @@ -156,6 +169,26 @@ pub fn run() { mod tests { use super::*; + #[test] + fn test_portable_app_directories_override() { + let temp = std::env::temp_dir().join("scribl_test_app_dir_override"); + let data_dir = temp.join("data"); + + let mut config = tauri::Config::default(); + assert!(config.app.app_directories_override.is_none()); + + apply_portable_directories_override(&mut config, Some(data_dir.clone())); + + assert_eq!( + config.app.app_directories_override, + Some(tauri::utils::config::AppDirectoriesOverride::Root(data_dir)) + ); + + let mut config_non_portable = tauri::Config::default(); + apply_portable_directories_override(&mut config_non_portable, None); + assert!(config_non_portable.app.app_directories_override.is_none()); + } + #[test] fn test_version_format() { let base = env!("CARGO_PKG_VERSION"); diff --git a/src-tauri/src/watcher.rs b/src-tauri/src/watcher.rs index a0f8113..715d1bc 100644 --- a/src-tauri/src/watcher.rs +++ b/src-tauri/src/watcher.rs @@ -3,7 +3,31 @@ use std::path::PathBuf; use std::sync::mpsc::channel; use tauri::{AppHandle, Emitter}; +pub fn get_portable_data_dir() -> Option { + if let Ok(override_dir) = std::env::var("SCRIBL_PORTABLE_DATA_DIR") { + let p = PathBuf::from(override_dir); + if p.is_dir() { + return Some(p); + } + } + get_portable_data_dir_from(std::env::current_exe().ok().as_deref()) +} + +pub fn get_portable_data_dir_from(exe_path: Option<&std::path::Path>) -> Option { + if let Some(parent) = exe_path.and_then(|p| p.parent()) { + let data_dir = parent.join("data"); + if data_dir.is_dir() { + return Some(data_dir); + } + } + None +} + pub fn get_config_dir() -> PathBuf { + if let Some(portable_dir) = get_portable_data_dir() { + return portable_dir.join("config"); + } + if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") { PathBuf::from(xdg).join("scribl") } else if let Some(home) = dirs::home_dir() { @@ -280,3 +304,66 @@ pub fn spawn_config_watcher(app_handle: AppHandle) { } }); } + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + + static ENV_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + #[test] + fn test_portable_data_dir_none_when_no_data_folder() { + let temp = std::env::temp_dir().join("scribl_test_non_portable"); + let _ = fs::create_dir_all(&temp); + let fake_exe = temp.join("scribl.exe"); + let _ = fs::write(&fake_exe, b""); + + let portable = get_portable_data_dir_from(Some(&fake_exe)); + assert!( + portable.is_none(), + "expected None when data/ does not exist" + ); + let _ = fs::remove_dir_all(&temp); + } + + #[test] + fn test_portable_data_dir_detected_when_data_folder_exists() { + let temp = std::env::temp_dir().join("scribl_test_portable"); + let data_dir = temp.join("data"); + let _ = fs::create_dir_all(&data_dir); + let fake_exe = temp.join("scribl.exe"); + let _ = fs::write(&fake_exe, b""); + + let portable = get_portable_data_dir_from(Some(&fake_exe)); + assert_eq!(portable, Some(data_dir.clone())); + + let _ = fs::remove_dir_all(&temp); + } + + #[test] + fn test_portable_mode_config_dir_resolution() { + let _guard = ENV_MUTEX.lock().unwrap(); + let temp = std::env::temp_dir().join("scribl_test_portable_config"); + let data_dir = temp.join("data"); + let _ = fs::create_dir_all(&data_dir); + + unsafe { + std::env::set_var("SCRIBL_PORTABLE_DATA_DIR", &data_dir); + } + + let config_dir = get_config_dir(); + assert_eq!(config_dir, data_dir.join("config")); + + let init_lua = get_init_lua_path(); + assert_eq!(init_lua, data_dir.join("config").join("init.lua")); + + let themes_dir = get_themes_dir(); + assert_eq!(themes_dir, data_dir.join("config").join("themes")); + + unsafe { + std::env::remove_var("SCRIBL_PORTABLE_DATA_DIR"); + } + let _ = fs::remove_dir_all(&temp); + } +}