1
0
Fixes:
	- Merge function:
		*no longer has the potential to truncate super long lines.
		*no more issues with exclamation marks in user_pref lines.

Improvements:
	- Overall better performance due to ECHO syntax changes.
	- Merge function on steroids! Faster than ever, and no longer generates temporary files at all. As it always should have been.

Changes, Additions, Substractions:
	- Leading spaces are no longer ignored by the merge function. Lines to be merged must begin with user_pref.
	- Added header with name, author, version.
	- Added help sub-menu.
	- Added special message when no override files are found when using -multiOverrides.
	- Formatting changes.
This commit is contained in:
claustromaniac 2017-12-16 17:55:08 +00:00
parent fbd9f344af
commit 2f5ae9b294

View File

@ -1,40 +1,23 @@
@ECHO OFF @ECHO OFF & SETLOCAL EnableDelayedExpansion
TITLE ghacks user.js updater TITLE ghacks user.js updater
REM ### ghacks-user.js updater for Windows REM ### ghacks-user.js updater for Windows
REM ## author: @claustromaniac REM ## author: @claustromaniac
REM ## version: 3.2 REM ## version: 4.0b1
SET _myname=%~n0 SET _myname=%~n0
SET _myparams=%* SET _myparams=%*
SETLOCAL EnableDelayedExpansion
:parse :parse
IF "%~1"=="" ( IF "%~1"=="" ( GOTO endparse )
GOTO endparse IF /I "%~1"=="-unattended" ( SET _ua=1 )
) IF /I "%~1"=="-log" ( SET _log=1 )
IF /I "%~1"=="-unattended" ( IF /I "%~1"=="-logp" ( SET _log=1 & SET _logp=1 )
SET _ua=1 IF /I "%~1"=="-multioverrides" ( SET _multi=1 )
) IF /I "%~1"=="-merge" ( SET _merge=1 )
IF /I "%~1"=="-log" ( IF /I "%~1"=="-updatebatch" ( SET _updateb=1 )
SET _log=1
)
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
)
SHIFT SHIFT
GOTO parse GOTO parse
:endparse :endparse
ECHO.
IF DEFINED _updateb ( IF DEFINED _updateb (
REM The normal flow here goes from phase 1 to phase 2 and then phase 3. REM The normal flow here goes from phase 1 to phase 2 and then phase 3.
IF NOT "!_myname:~0,9!"=="[updated]" ( IF NOT "!_myname:~0,9!"=="[updated]" (
@ -44,19 +27,16 @@ IF DEFINED _updateb (
REM * Begin the normal routine REM * Begin the normal routine
REN "[updated]!_myname!.bat" "[updated]!_myname!.bat.old" REN "[updated]!_myname!.bat" "[updated]!_myname!.bat.old"
DEL /F "[updated]!_myname!.bat.old" DEL /F "[updated]!_myname!.bat.old"
ECHO Script updated^^! CALL :message "Script updated^^!
ECHO.
TIMEOUT 3 >nul TIMEOUT 3 >nul
CLS CLS
ECHO.
GOTO begin GOTO begin
) )
REM ## Phase 1 ## REM ## Phase 1 ##
REM * Download new batch and name it [updated]*.bat REM * Download new batch and name it [updated]*.bat
REM * Start that script in a new CMD window REM * Start that script in a new CMD window
REM * Exit REM * Exit
ECHO Updating script... CALL :message "Updating script..."
ECHO.
REM Uncomment the next line and comment the powershell call for testing. REM Uncomment the next line and comment the powershell call for testing.
REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat" REM COPY /B /V /Y "!_myname!.bat" "[updated]!_myname!.bat"
( (
@ -66,15 +46,13 @@ IF DEFINED _updateb (
START /min CMD /C "[updated]!_myname!.bat" !_myparams! START /min CMD /C "[updated]!_myname!.bat" !_myparams!
EXIT /B EXIT /B
) ELSE ( ) ELSE (
ECHO Failed. Make sure PowerShell is allowed internet access. CALL :message "Failed. Make sure PowerShell is allowed internet access."
ECHO.
TIMEOUT 120 >nul TIMEOUT 120 >nul
EXIT /B EXIT /B
) )
) ELSE ( ) ELSE (
IF "!_myname!"=="[updated]" ( IF "!_myname!"=="[updated]" (
ECHO The [updated] label is reserved. Rename this script and try again. CALL :message "The [updated] label is reserved. Rename this script and try again."
ECHO.
TIMEOUT 300 >nul TIMEOUT 300 >nul
) ELSE ( ) ELSE (
REM ## Phase 2 ##: The [updated]*.bat script will: REM ## Phase 2 ##: The [updated]*.bat script will:
@ -92,180 +70,187 @@ IF DEFINED _updateb (
) )
) )
:begin :begin
ECHO:
ECHO:
ECHO: ########################################
ECHO: #### user.js Updater for Windows ####
ECHO: #### by claustromaniac ####
ECHO: #### v4.0 ####
ECHO: ########################################
ECHO:
SET /A "_line=0" SET /A "_line=0"
IF NOT EXIST user.js ( IF NOT EXIST user.js (
ECHO user.js not detected in the current directory. CALL :message "user.js not detected in the current directory."
) ELSE ( ) ELSE (
FOR /F "skip=1 tokens=1,2 delims=:" %%G IN (user.js) DO ( FOR /F "skip=1 tokens=1,2 delims=:" %%G IN (user.js) DO (
SET /A "_line+=1" SET /A "_line+=1"
IF !_line! GEQ 4 ( IF !_line! GEQ 4 ( GOTO exitloop )
GOTO exitloop IF !_line! EQU 1 ( SET _name=%%H )
) IF !_line! EQU 2 ( SET _date=%%H )
IF !_line! EQU 1 ( IF !_line! EQU 3 ( SET _version=%%G )
SET _name=%%H
)
IF !_line! EQU 2 (
SET _date=%%H
)
IF !_line! EQU 3 (
SET _version=%%G
)
) )
:exitloop :exitloop
IF !_line! GEQ 4 ( IF !_line! GEQ 4 (
IF /I NOT "!_name!"=="!_name:ghacks=X!" ( IF /I NOT "!_name!"=="!_name:ghacks=!" (
ECHO ghacks user.js !_version:~2!,!_date! CALL :message "ghacks user.js !_version:~2!,!_date!"
) ELSE ( ) ELSE (
ECHO Current user.js version not recognised. CALL :message "Current user.js version not recognised."
) )
) ELSE ( ) ELSE (
ECHO Current user.js version not recognised. CALL :message "Current user.js version not recognised."
) )
) )
ECHO. ECHO:
IF NOT DEFINED _ua ( IF NOT DEFINED _ua (
ECHO. CALL :message "This batch should be run from your Firefox profile directory."
ECHO This batch should be run from your Firefox profile directory. It will download the latest version of ghacks user.js from github and then append any of your own changes from user-overrides.js to it. ECHO: It will download the latest version of ghacks user.js from github and then
ECHO. CALL :message "append any of your own changes from user-overrides.js to it."
ECHO Visit the wiki for more detailed information. CALL :message "Visit the wiki for more detailed information."
ECHO. ECHO:
CHOICE /M "Continue" TIMEOUT 1 /nobreak >nul
IF ERRORLEVEL 2 ( EXIT /B ) CHOICE /C SHE /N /M "Start [S] Help [H] Exit [E]"
CLS
IF ERRORLEVEL 3 ( EXIT /B )
IF ERRORLEVEL 2 ( GOTO :showhelp )
) )
CLS
ECHO.
IF DEFINED _log ( IF DEFINED _log (
CALL :log >>user.js-update-log.txt 2>&1 CALL :log >>user.js-update-log.txt 2>&1
IF DEFINED _logp ( IF DEFINED _logp ( START user.js-update-log.txt )
START user.js-update-log.txt
)
EXIT /B EXIT /B
:log :log
ECHO ################################################################## ECHO:##################################################################
ECHO. CALL :message "%date%, %time%"
ECHO %date%, %time%
ECHO.
) )
IF EXIST user.js.old.bak ( DEL /F user.js.old.bak ) IF EXIST user.js.old.bak ( DEL /F user.js.old.bak )
IF EXIST user.js ( IF EXIST user.js (
IF EXIST user.js.bak ( REN user.js.bak user.js.old.bak ) IF EXIST user.js.bak ( REN user.js.bak user.js.old.bak )
REN user.js user.js.bak REN user.js user.js.bak
ECHO Current user.js file backed up. CALL :message "Current user.js file backed up.
ECHO.
) )
ECHO Retrieving latest user.js file from github repository... 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')"
) >nul 2>&1 ) >nul 2>&1
ECHO.
IF EXIST user.js ( IF EXIST user.js (
IF DEFINED _multi ( IF DEFINED _multi (
ECHO Multiple overrides enabled. List of files found: FORFILES /P user.js-overrides /M *.js >nul
FORFILES /P user.js-overrides /M *.js IF NOT ERRORLEVEL 1 (
IF %ERRORLEVEL% EQU 0 (
IF DEFINED _merge ( IF DEFINED _merge (
ECHO. CALL :message "Merging..."
ECHO Merging... COPY /B /V /Y user.js-overrides\*.js user-overrides-merged.js
ECHO. CALL :merge user-overrides-merged.js
COPY /B /V /Y user.js-overrides\*.js user-overrides COPY /B /V /Y user.js+user-overrides-merged.js user.js
CALL :merge user-overrides user-overrides-merged.js CALL :merge user.js
COPY /B /V /Y user.js+user-overrides-merged.js updatertempfile
CALL :merge updatertempfile user.js
) ELSE ( ) ELSE (
ECHO. CALL :message "Appending..."
ECHO Appending...
ECHO.
COPY /B /V /Y user.js+"user.js-overrides\*.js" user.js COPY /B /V /Y user.js+"user.js-overrides\*.js" user.js
) )
) ) ELSE ( CALL :message "No override files found." )
ECHO. ECHO:
) ELSE ( ) ELSE (
IF EXIST "user-overrides.js" ( IF EXIST "user-overrides.js" (
IF DEFINED _merge (
ECHO Merging user-overrides.js...
COPY /B /V /Y user.js+user-overrides.js updatertempfile
CALL :merge updatertempfile user.js
) ELSE (
ECHO Appending user-overrides.js...
ECHO.
COPY /B /V /Y user.js+"user-overrides.js" "user.js" COPY /B /V /Y user.js+"user-overrides.js" "user.js"
) IF DEFINED _merge (
CALL :message "Merging user-overrides.js..."
CALL :merge user.js
) ELSE ( ) ELSE (
ECHO user-overrides.js not found. CALL :message "user-overrides.js appended."
) )
ECHO. ) ELSE ( CALL :message "user-overrides.js not found." )
ECHO:
) )
ECHO Handling backups... CALL :message "Handling backups..."
SET "changed=" SET "changed="
IF EXIST user.js.bak ( IF EXIST user.js.bak (
FC user.js.bak user.js >nul && SET "changed=false" || SET "changed=true" FC user.js.bak user.js >nul && SET "changed=false" || SET "changed=true"
) )
ECHO. ECHO:
ECHO.
IF "!changed!"=="true" ( IF "!changed!"=="true" (
IF EXIST user.js.old.bak DEL /F user.js.old.bak IF EXIST user.js.old.bak DEL /F user.js.old.bak
ECHO Update complete. CALL :message "Update complete."
) ELSE ( ) ELSE (
IF "!changed!"=="false" ( IF "!changed!"=="false" (
DEL /F user.js.bak DEL /F user.js.bak
IF EXIST user.js.old.bak REN user.js.old.bak user.js.bak IF EXIST user.js.old.bak REN user.js.old.bak user.js.bak
ECHO Update completed without changes. CALL :message "Update completed without changes."
) ELSE ( ) ELSE ( CALL :message "Update complete." )
ECHO Update complete.
) )
) ECHO:
ECHO.
) ELSE ( ) ELSE (
IF EXIST user.js.bak ( REN user.js.bak user.js ) 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 ) IF EXIST user.js.old.bak ( REN user.js.old.bak user.js.bak )
ECHO. CALL :message "Update failed. Make sure PowerShell is allowed internet access."
ECHO Update failed. Make sure PowerShell is allowed internet access. CALL :message "No changes were made."
ECHO.
ECHO No changes were made.
ECHO.
) )
IF NOT DEFINED _log ( IF NOT DEFINED _log (
IF NOT DEFINED _ua PAUSE IF NOT DEFINED _ua ( PAUSE )
) )
EXIT /B EXIT /B
REM ###### Merge function ###### REM ########### Message Function ###########
:merge :message
IF EXIST updatertempfile1 ( DEL /F updatertempfile1 ) ECHO:
SETLOCAL disabledelayedexpansion ECHO: %~1
( ECHO:
FOR /F "tokens=1,* delims=]" %%G IN ('FIND /n /v "" ^< "%~1"') DO (
SET "_pref=%%H"
SETLOCAL enabledelayedexpansion
SET "_temp=!_pref: =!"
IF /I "user_pref"=="!_temp:~0,9!" (
IF /I NOT "user.js.parrot"=="!_temp:~12,14!" (
FOR /F "delims=," %%S IN ("!_pref!") DO (
SET "_pref=%%S"
)
SET _pref=!_pref:"=""!
FIND /I "!_pref!" updatertempfile1 >nul 2>&1
IF ERRORLEVEL 1 (
FOR /F "tokens=* delims=" %%X IN ('FIND /I "!_pref!" %~1') DO (
SET "_temp=%%X"
SET "_temp=!_temp: =!"
IF /I "user_pref"=="!_temp:~0,9!" (
SET "_pref=%%X"
)
)
ECHO(!_pref!
ECHO(!_pref!>>updatertempfile1
)
) ELSE (
ECHO(!_pref!
)
) ELSE (
ECHO(!_pref!
)
ENDLOCAL
)
)>%~2
ENDLOCAL
DEL /F %1 updatertempfile1 >nul
GOTO :EOF GOTO :EOF
REM ############################ 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=:," %%I IN ('FINDSTR /N "^" "%~1"') DO (
IF NOT [user_pref("_user.js.parrot"]==[%%I] (
IF DEFINED %%I (
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%K IN ("!%%I!") DO (
ENDLOCAL
IF NOT "%%K"=="ALREADY MERGED" (
ECHO:%%I,%%K
SET "%%I=ALREADY MERGED"
)
)
) ELSE (
IF "%%J"=="" (
ECHO:%%I
) ELSE (
ECHO:%%I,%%J
)
)
) ELSE (
ECHO:%%I,%%J
)
)
)>updatertempfile
MOVE /Y updatertempfile "%~1" >nul
ENDLOCAL
GOTO :EOF
REM ############### Help ##################
:showhelp
MODE 80,38
CLS
CALL :message "Available switches (case-insensitive):"
CALL :message " -log"
ECHO: Writes the console output to a logfile (user.js-update-log.txt)
CALL :message " -logP"
ECHO: Like log, but also opens the logfile after updating.
CALL :message " -merge"
ECHO: Merges overrides instead of appending them. Comments and _user.js.parrot
ECHO: lines are appended normally. Overrides for inactive (commented out)
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
ECHO: directory for quick reference. It contains only the merged data from
ECHO: override files and can be safely discarded after updating, or used as the
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.
CALL :message " -multiOverrides"
ECHO: uses 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
ECHO: alphabetical order.
CALL :message " -updatebatch"
ECHO: The script will update itself on execution.
CALL :message ""
PAUSE
CLS
MODE 80,25
GOTO :begin
REM #####################################