From e195aceb5434d49e7ace288b5e3075f262e00e09 Mon Sep 17 00:00:00 2001 From: claustromaniac <20734810+claustromaniac@users.noreply.github.com> Date: Mon, 15 Jan 2018 22:15:30 -0300 Subject: [PATCH] Updater 4.3 Changes: -The script doesn't touch the user.js file until it really has to. -The merge function is a bit smarter parsing files, at no significant cost. See examples below. -Minor syntactic changes here and there. Additions: -New -multiBackups argument. I personally intend to use it to compare files and quickly review changes. --- updater.bat | 101 ++++++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/updater.bat b/updater.bat index b9c0c68..bab4d74 100644 --- a/updater.bat +++ b/updater.bat @@ -3,7 +3,7 @@ TITLE ghacks user.js updater REM ## ghacks-user.js updater for Windows REM ## author: @claustromaniac -REM ## version: 4.2 +REM ## version: 4.3 REM ## instructions: https://github.com/ghacksuserjs/ghacks-user.js/wiki/3.3-Updater-Scripts SET _myname=%~n0 @@ -16,6 +16,7 @@ IF /I "%~1"=="-logp" (SET _log=1 & SET _logp=1) IF /I "%~1"=="-multioverrides" (SET _multi=1) IF /I "%~1"=="-merge" (SET _merge=1) IF /I "%~1"=="-updatebatch" (SET _updateb=1) +IF /I "%~1"=="-multibackups" (SET _multibackups=1) SHIFT GOTO parse :endparse @@ -76,14 +77,14 @@ ECHO: ECHO: ######################################## ECHO: #### user.js Updater for Windows #### ECHO: #### by claustromaniac #### -ECHO: #### v4.2 #### +ECHO: #### v4.3 #### ECHO: ######################################## ECHO: SET /A "_line=0" IF NOT EXIST user.js ( CALL :message "user.js not detected in the current directory." ) ELSE ( - FOR /F "skip=1 tokens=1,2 delims=:" %%G IN (user.js) DO ( + FOR /F "skip=1 tokens=1,* delims=:" %%G IN (user.js) DO ( SET /A "_line+=1" IF !_line! GEQ 4 (GOTO exitloop) IF !_line! EQU 1 (SET _name=%%H) @@ -123,17 +124,12 @@ IF DEFINED _log ( ECHO:################################################################## CALL :message "%date%, %time%" ) -IF EXIST user.js.old.bak (DEL /F user.js.old.bak) -IF EXIST user.js ( - IF EXIST user.js.bak (REN user.js.bak user.js.old.bak) - REN user.js user.js.bak - CALL :message "Current user.js file backed up." -) +IF EXIST user.js.new (DEL /F "user.js.new") CALL :message "Retrieving latest user.js file from github repository..." ( - powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/user.js', 'user.js')" + powershell -Command "(New-Object Net.WebClient).DownloadFile('https://github.com/ghacksuserjs/ghacks-user.js/raw/master/user.js', 'user.js.new')" ) >nul 2>&1 -IF EXIST user.js ( +IF EXIST user.js.new ( IF DEFINED _multi ( FORFILES /P user.js-overrides /M *.js >nul 2>&1 IF NOT ERRORLEVEL 1 ( @@ -141,45 +137,49 @@ IF EXIST user.js ( CALL :message "Merging..." COPY /B /V /Y user.js-overrides\*.js user-overrides-merged.js CALL :merge user-overrides-merged.js - COPY /B /V /Y user.js+user-overrides-merged.js user.js - CALL :merge user.js + COPY /B /V /Y user.js.new+user-overrides-merged.js user.js.new + CALL :merge user.js.new ) ELSE ( CALL :message "Appending..." - COPY /B /V /Y user.js+"user.js-overrides\*.js" user.js + COPY /B /V /Y user.js.new+"user.js-overrides\*.js" user.js.new ) ) ELSE (CALL :message "No override files found.") ECHO: ) ELSE ( IF EXIST "user-overrides.js" ( - COPY /B /V /Y user.js+"user-overrides.js" "user.js" + COPY /B /V /Y user.js.new+"user-overrides.js" "user.js.new" IF DEFINED _merge ( CALL :message "Merging user-overrides.js..." - CALL :merge user.js + CALL :merge user.js.new ) ELSE ( CALL :message "user-overrides.js appended." ) ) ELSE (CALL :message "user-overrides.js not found.") ECHO: ) - CALL :message "Handling backups..." - SET "changed=" - IF EXIST user.js.bak ( - FC user.js.bak user.js >nul && SET "changed=false" || SET "changed=true" + IF EXIST user.js ( + FC user.js.new user.js >nul && SET "_changed=false" || SET "_changed=true" ) - IF "!changed!"=="true" ( - IF EXIST user.js.old.bak DEL /F user.js.old.bak + IF "!_changed!"=="true" ( + CALL :message "Backing up..." + IF DEFINED _multibackups ( + MOVE /Y user.js "user-backup-!date:/=-!_!time::=.!.js" >nul + ) ELSE ( + MOVE /Y user.js user.js.bak >nul + ) + REN user.js.new user.js CALL :message "Update complete." ) ELSE ( - IF "!changed!"=="false" ( - DEL /F user.js.bak - IF EXIST user.js.old.bak REN user.js.old.bak user.js.bak + IF "!_changed!"=="false" ( + DEL /F user.js.new >nul CALL :message "Update completed without changes." - ) ELSE (CALL :message "Update complete.") + ) ELSE ( + REN user.js.new user.js + REN CALL :message "Update complete." + ) ) ECHO: ) ELSE ( - IF EXIST user.js.bak (REN user.js.bak user.js) - IF EXIST user.js.old.bak (REN user.js.old.bak user.js.bak) CALL :message "Update failed. Make sure PowerShell is allowed internet access." ECHO: No changes were made. ) @@ -200,27 +200,31 @@ REM ############ Merge function ############ :merge SETLOCAL DisableDelayedExpansion ( - FOR /F "tokens=1,* delims=," %%G IN ('FINDSTR /B /I /C:"user_pref" "%~1"') DO (SET "%%G=%%H") + FOR /F tokens^=2^,^*^ delims^=^'^" %%G IN ('FINDSTR /B /R /C:"user_pref.*\)" "%~1"') DO ( + IF NOT "%%G"=="" ( + IF NOT "%%H"=="" (SET "%%G=%%H") + ) + ) FOR /F "tokens=1,* delims=:" %%I IN ('FINDSTR /N "^" "%~1"') DO ( - IF ""=="%%J" ( - ECHO: + SET "_temp=%%J" + SETLOCAL EnableDelayedExpansion + IF "!_temp:)=!"=="!_temp!" ( + ENDLOCAL & ECHO:%%J ) ELSE ( - FOR /F "delims=," %%K IN ("%%J") DO ( - IF NOT [user_pref("_user.js.parrot"]==[%%K] ( - IF DEFINED %%K ( - SETLOCAL EnableDelayedExpansion - FOR /F "delims=" %%L IN ("!%%K!") DO ( - ENDLOCAL - IF NOT "%%L"=="ALREADY MERGED" ( - ECHO:%%K,%%L - SET "%%K=ALREADY MERGED" + IF NOT "!_temp:~0,9!"=="user_pref" ( + ENDLOCAL & ECHO:%%J + ) ELSE ( + ENDLOCAL + FOR /F tokens^=2^ delims^=^'^" %%K IN ("%%J") DO ( + IF NOT "_user.js.parrot"=="%%K" ( + IF DEFINED %%K ( + SETLOCAL EnableDelayedExpansion + FOR /F "delims=" %%L IN ("!%%K!") DO ( + ENDLOCAL & ECHO:user_pref("%%K"%%L + SET "%%K=" ) ) - ) ELSE ( - ECHO:%%J - ) - ) ELSE ( - ECHO:%%J + ) ELSE (ECHO:%%J) ) ) ) @@ -231,7 +235,7 @@ ENDLOCAL GOTO :EOF REM ############### Help ################## :showhelp -MODE 80,43 +MODE 80,46 CLS CALL :message "Available arguments (case-insensitive):" CALL :message " -log" @@ -239,7 +243,7 @@ ECHO: Write the console output to a logfile (user.js-update-log.txt) CALL :message " -logP" ECHO: Like -log, but also open the logfile after updating. CALL :message " -merge" -ECHO: Merge overrides instead of appending them. One-line comments and +ECHO: Merge overrides instead of appending them. Single-line comments and ECHO: _user.js.parrot lines are appended normally. Overrides for inactive ECHO: user.js prefs will be appended. When -Merge and -MultiOverrides are used ECHO: together, a user-overrides-merged.js file is also generated in the root @@ -248,6 +252,9 @@ ECHO: override files and can be safely discarded after updating, or used as ECHO: new user-overrides.js. When there are conflicting records for the same ECHO: pref, the value of the last one declared will be used. Visit the wiki ECHO: for usage examples and more detailed information. +CALL :message " -multiBackups" +ECHO: Don't overwrite previous backups. New backups are still created only +ECHO: when the updated user.js is different from the previous one. CALL :message " -multiOverrides" ECHO: Use any and all .js files in a user.js-overrides sub-folder as overrides ECHO: instead of the default user-overrides.js file. Files are appended in