Advertisement
AlvinSeville7cf

installer generator.bat

Mar 17th, 2021
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.05 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. call :init
  5. set /a "error_status=%errorlevel%"
  6. if %error_status% neq 0 exit /b %error_status%
  7.  
  8. set /a "i=0"
  9. :copy_options
  10.     set "option=%~1"
  11.     if defined option (
  12.         set "args[%i%]=%option%"
  13.         shift
  14.         set /a "i+=1"
  15.         goto copy_options
  16.     )
  17. set /a "args_count=i"
  18.  
  19. set /a "i=0"
  20. :main_loop
  21.     set /a "j=i + 1"
  22.     call set "option=%%args[%i%]%%"
  23.     call set "value=%%args[%j%]%%"
  24.  
  25.     if %i% geq %args_count% goto main_loop_break
  26.  
  27.     set /a "is___version=FALSE"
  28.  
  29.     if "%option%" == "--version" set /a "is___version=TRUE"
  30.     if "%option%" == "-v" set /a "is___version=TRUE"
  31.  
  32.     if %is___version% equ %TRUE% (
  33.         call :__version_handler
  34.         exit /b %SUCCESS_EC%
  35.     )
  36.  
  37.     set /a "is___help=FALSE"
  38.  
  39.     if "%option%" == "--help" set /a "is___help=TRUE"
  40.     if "%option%" == "-h" set /a "is___help=TRUE"
  41.  
  42.     if %is___help% equ %TRUE% (
  43.         call :__help_handler
  44.         exit /b %SUCCESS_EC%
  45.     )
  46.  
  47.     set /a "is___name=FALSE"
  48.  
  49.     if "%option%" == "--name" set /a "is___name=TRUE"
  50.     if "%option%" == "-n" set /a "is___name=TRUE"
  51.  
  52.     if %is___name% equ %TRUE% (
  53.         set "name=%value%"
  54.         set /a "i+=2"
  55.         goto main_loop
  56.     )
  57.  
  58.     set /a "is___archive=FALSE"
  59.  
  60.     if "%option%" == "--archive" set /a "is___archive=TRUE"
  61.     if "%option%" == "-a" set /a "is___archive=TRUE"
  62.  
  63.     if %is___archive% equ %TRUE% (
  64.         set "archive=%value%"
  65.         set /a "i+=2"
  66.         goto main_loop
  67.     )
  68.  
  69.     set /a "is___directory=FALSE"
  70.  
  71.     if "%option%" == "--directory" set /a "is___directory=TRUE"
  72.     if "%option%" == "-d" set /a "is___directory=TRUE"
  73.  
  74.     if %is___directory% equ %TRUE% (
  75.         set "directory=%value%"
  76.         set /a "i+=2"
  77.         goto main_loop
  78.     )
  79.  
  80.     echo %WRONG_OPTION_SPECIFIED_MSG%
  81.     exit /b %WRONG_OPTION_SPECIFIED_EC%
  82.  
  83. :main_loop_break
  84. if not defined name (
  85.     echo %NAME_NOT_SPECIFIED_MSG%
  86.     exit /b %NAME_NOT_SPECIFIED_EC%
  87. )
  88. if not defined archive (
  89.     echo %ARCHIVE_NOT_SPECIFIED_MSG%
  90.     exit /b %ARCHIVE_NOT_SPECIFIED_EC%
  91. )
  92. if not defined directory (
  93.     echo %DIRECTORY_NOT_SPECIFIED_MSG%
  94.     exit /b %DIRECTORY_NOT_SPECIFIED_EC%
  95. )
  96.  
  97. (
  98.     echo @echo off
  99.     echo setlocal
  100.     echo.
  101.     echo call :init
  102.     echo set /a "error_status=%%errorlevel%%"
  103.     echo if %%error_status%% neq 0 exit /b %%error_status%%
  104.     echo.
  105.     echo set /a "i=0"
  106.     echo :copy_options
  107.     echo     set "option=%%~1"
  108.     echo     if defined option ^(
  109.     echo         set "args[%%i%%]=%%option%%"
  110.     echo         shift
  111.     echo         set /a "i+=1"
  112.     echo         goto copy_options
  113.     echo     ^)
  114.     echo set /a "args_count=i"
  115.     echo.
  116.     echo set /a "i=0"
  117.     echo :main_loop
  118.     echo     set /a "j=i + 1"
  119.     echo     call set "option=%%%%args[%%i%%]%%%%"
  120.     echo     call set "value=%%%%args[%%j%%]%%%%"
  121.     echo.
  122.     echo     if %%i%% geq %%args_count%% goto main_loop_break
  123.     echo.
  124.     echo     set /a "is___version=FALSE"
  125.     echo.
  126.     echo     if "%%option%%" == "--version" set /a "is___version=TRUE"
  127.     echo     if "%%option%%" == "-v" set /a "is___version=TRUE"
  128.     echo.
  129.     echo     if %%is___version%% equ %%TRUE%% ^(
  130.     echo         call :__version_handler
  131.     echo         exit /b %%SUCCESS_EC%%
  132.     echo     ^)
  133.     echo.
  134.     echo     set /a "is___help=FALSE"
  135.     echo.
  136.     echo     if "%%option%%" == "--help" set /a "is___help=TRUE"
  137.     echo     if "%%option%%" == "-h" set /a "is___help=TRUE"
  138.     echo.
  139.     echo     if %%is___help%% equ %%TRUE%% ^(
  140.     echo         call :__help_handler
  141.     echo         exit /b %%SUCCESS_EC%%
  142.     echo     ^)
  143.     echo.
  144.     echo     set /a "is___uninstall=FALSE"
  145.     echo.
  146.     echo     if "%%option%%" == "--uninstall" set /a "is___uninstall=TRUE"
  147.     echo     if "%%option%%" == "-u" set /a "is___uninstall=TRUE"
  148.     echo.
  149.     echo     if %%is___uninstall%% equ %%TRUE%% ^(
  150.     echo         set /a "is_uninstall=TRUE"
  151.     echo         set /a "i+=1"
  152.     echo         goto main_loop
  153.     echo     ^)
  154.     echo.
  155.     echo     echo %%WRONG_OPTION_SPECIFIED_MSG%%
  156.     echo     exit /b %%WRONG_OPTION_SPECIFIED_EC%%
  157.     echo.
  158.     echo :main_loop_break
  159.     echo if %%is_uninstall%% equ %%TRUE%% ^(
  160.     echo     if not exist "%%directory%%" ^(
  161.     echo        echo %%BATCHTEMPLATE_IS_NOT_INSTALLED_MSG%%
  162.     echo        exit /b %%BATCHTEMPLATE_IS_NOT_INSTALLED_EC%%
  163.     echo     ^)
  164.     echo     setlocal enabledelayedexpansion
  165.     echo     rem setx PATH %%PATH:%directory%=%%
  166.     echo     echo y^| rd /s "%%directory%%" 2^> nul ^> nul
  167.     echo     set /a "error_status=!errorlevel!"
  168.     echo     if !error_status! equ 0 (
  169.     echo         echo batchtemplate successfully uninstalled from %%directory%%.
  170.     echo     ^) else ^(
  171.     echo         echo Unexpected error uccured. Possibly there is no access to %%directory%% folder or another process uses it. Please run installer as admin and kill processes use %%directory%% folder.
  172.     echo     ^)
  173.     echo     exit /b !error_status!
  174.     echo ^)
  175.     echo.
  176.     echo if exist "%%directory%%" ^(
  177.     echo     setlocal enabledelayedexpansion
  178.     echo     choice /m "Do you want reinstall batchtemplate in %%directory%%?"
  179.     echo     set /a "answer=!errorlevel!"
  180.     echo     if !answer! equ 2 ^(
  181.     echo         echo %%BATCHTEMPLATE_REINSTALL_REJECTED_MSG%%
  182.     echo         exit /b %%BATCHTEMPLATE_REINSTALL_REJECTED_EC%%
  183.     echo     ^)
  184.     echo     if !answer! equ 1 ^(
  185.     echo         echo y^| rd /q /s "%%directory%%" 2^> nul ^> nul
  186.     echo         set /a "error_status=!errorlevel!"
  187.     echo         if !error_status! neq 0 ^(
  188.     echo             echo Unexpected error uccured. Possibly there is no access to %%directory%% folder or another process uses it. Please run installer as admin and kill processes use %%directory%% folder.
  189.     echo             exit /b !error_status!
  190.     echo         ^)
  191.     echo     ^)
  192.     echo ^)
  193.     echo.
  194.     echo mkdir "%%directory%%"
  195.     echo chdir "%%directory%%"
  196.     echo.
  197.     echo certutil -decode "%%self%%" "%%archive_name%%" ^> nul
  198.     echo 7z x "%%archive_name%%" ^> nul
  199.     echo rem setx PATH "%%directory%%!!%%PATH%%"
  200.     echo del /q "%%archive_name%%"
  201.     echo exit /b %%SUCCESS_EC%%
  202.     echo.
  203.     echo :init
  204.     echo     set /a "SUCCESS_EC=0"
  205.     echo     set /a "WRONG_OPTION_SPECIFIED_EC=1"
  206.     echo     set /a "SEVENZ_COMMAND_NOT_FOUND_EC=2"
  207.     echo     set /a "BATCHTEMPLATE_IS_NOT_INSTALLED_EC=3"
  208.     echo     set /a "BATCHTEMPLATE_REINSTALL_REJECTED_EC=4"
  209.     echo.
  210.     echo     set "WRONG_OPTION_SPECIFIED_MSG=Option you've passed is not recognized."
  211.     echo     set "SEVENZ_COMMAND_NOT_FOUND_MSG=7z command isn't found. Please install 7-Zip archiver."
  212.     echo     set "BATCHTEMPLATE_IS_NOT_INSTALLED_MSG=batchtemplate isn't installed to uninstall it."
  213.     echo     set "BATCHTEMPLATE_REINSTALL_REJECTED_MSG=batchtemplate reinstall is rejected."
  214.     echo.
  215.     echo     set /a "TRUE=0"
  216.     echo     set /a "FALSE=1"
  217.     echo.
  218.     echo     set "self=%%~f0"
  219.     echo     set "directory=%directory%"
  220.     echo     set "temp=temp.txt"
  221.     echo     set "archive_name=%archive%"
  222.     echo.
  223.     echo     set /a "is_uninstall=FALSE"
  224.     echo     7z --help 2^> nul ^> nul ^|^| ^(
  225.     echo        echo %%SEVENZ_COMMAND_NOT_FOUND_MSG%%
  226.     echo        exit /b %%SEVENZ_COMMAND_NOT_FOUND_EC%%
  227.     echo     ^)
  228.     echo exit /b %%SUCCESS_EC%%
  229.     echo.
  230.     echo :__version_handler
  231.     echo     echo 1.0 - 2021
  232.     echo exit /b %%SUCCESS_EC%%
  233.     echo.
  234.     echo :__help_handler
  235.     echo     echo Description:
  236.     echo     echo    Installes or uninstalles batchtemplate script.
  237.     echo     echo.
  238.     echo     echo Syntax:
  239.     echo     echo    - %name% [options]
  240.     echo     echo.
  241.     echo     echo Options:
  242.     echo     echo    - -u^^^|--uninstall - specifies whether uninstallation is performed instead of installation
  243.     echo     echo.
  244.     echo     echo Examples:
  245.     echo     echo    %name% --version
  246.     echo     echo    %name%
  247.     echo     echo    %name% --uninstall
  248.     echo     echo.
  249.     echo     echo Author:
  250.     echo     echo    Alvin Seville ^^^<AlvinSeville7cf@gmail.com^^^>
  251.     echo exit /b %%SUCCESS_EC%%
  252.     echo.
  253. ) > "%name%"
  254.  
  255. 7z a "%archive%" batchtemplate.bat > nul
  256.  
  257. if exist "%temp%" del /q "%temp%"
  258. certutil -encode "%archive%" "%temp%" > nul
  259. type "%temp%" >> "%name%"
  260. del /q "%temp%"
  261.  
  262. exit /b %SUCCESS_EC%
  263.  
  264. :init
  265.     set /a "SUCCESS_EC=0"
  266.     set /a "WRONG_OPTION_SPECIFIED_EC=1"
  267.     set /a "NAME_NOT_SPECIFIED_EC=2"
  268.     set /a "ARCHIVE_NOT_SPECIFIED_EC=2"
  269.     set /a "DIRECTORY_NOT_SPECIFIED_EC=2"
  270.     set /a "SEVENZ_COMMAND_NOT_FOUND_EC=3"
  271.  
  272.     set "WRONG_OPTION_SPECIFIED_MSG=Option you've passed is not recognized. Installer hasn't been regenerated."
  273.     set "NAME_NOT_SPECIFIED_MSG=--name value isn't specified. Installer hasn't been regenerated."
  274.     set "ARCHIVE_NOT_SPECIFIED_MSG=--archive value isn't specified. Installer hasn't been regenerated."
  275.     set "DIRECTORY_NOT_SPECIFIED_MSG=--directory value isn't specified. Installer hasn't been regenerated."
  276.     set "SEVENZ_COMMAND_NOT_FOUND_MSG=7z command isn't found. Please install 7-Zip archiver. Installer hasn't been regenerated."
  277.  
  278.     set /a "TRUE=0"
  279.     set /a "FALSE=1"
  280.  
  281.     set "temp=temp.txt"
  282.  
  283.     7z --help 2> nul > nul || (
  284.         echo %SEVENZ_COMMAND_NOT_FOUND_MSG%
  285.         exit /b %SEVENZ_COMMAND_NOT_FOUND_EC%
  286.     )
  287. exit /b %SUCCESS_EC%
  288.  
  289. :__version_handler
  290.     echo 1.0 - 2021
  291. exit /b %SUCCESS_EC%
  292.  
  293. :__help_handler
  294.     echo Description:
  295.     echo    Generates installer that installs batchtemplate.bat.
  296.     echo.
  297.     echo Syntax:
  298.     echo    - installergenerator [options]
  299.     echo.
  300.     echo Options:
  301.     echo    - -n^|--name - installer name
  302.     echo    - -a^|--archive - archive name
  303.     echo    - -d^|--directory - installation directory
  304.     echo.
  305.     echo Examples:
  306.     echo    installergenerator --version
  307.     echo    installergenerator --name installer.bat --archive release.7z --directory "C:\Program files\AlvinSeville7cfSoft\batchtemplate\"
  308.     echo.
  309.     echo Author:
  310.     echo    Alvin Seville ^<AlvinSeville7cf@gmail.com^>
  311. exit /b %SUCCESS_EC%
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement