Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 13.21 KB | None | 0 0
  1. @echo off
  2.  
  3. setLocal EnableExtensions EnableDelayedExpansion
  4.  
  5. set LEIN_VERSION=2.8.1
  6.  
  7. if "%LEIN_VERSION:~-9%" == "-SNAPSHOT" (
  8.     set SNAPSHOT=YES
  9. ) else (
  10.     set SNAPSHOT=NO
  11. )
  12.  
  13. set ORIGINAL_PWD=%CD%
  14. :: If ORIGINAL_PWD ends with a backslash (such as C:\),
  15. :: we need to escape it with a second backslash.
  16. if "%ORIGINAL_PWD:~-1%x" == "\x" set "ORIGINAL_PWD=%ORIGINAL_PWD%\"
  17.  
  18. call :FIND_DIR_CONTAINING_UPWARDS project.clj
  19. if "%DIR_CONTAINING%" neq "" cd "%DIR_CONTAINING%"
  20.  
  21. :: LEIN_JAR and LEIN_HOME variables can be set manually.
  22. :: Only set LEIN_JAR manually if you know what you are doing.
  23. :: Having LEIN_JAR pointing to one version of Leiningen as well as
  24. :: having a different version in PATH has been known to cause problems.
  25.  
  26. if "x%LEIN_HOME%" == "x" (
  27.     set LEIN_HOME=!USERPROFILE!\.lein
  28. )
  29. SET RC=1
  30.  
  31. if "x%LEIN_JAR%" == "x" set "LEIN_JAR=!LEIN_HOME!\self-installs\leiningen-!LEIN_VERSION!-standalone.jar"
  32.  
  33. if "%1" == "self-install" goto SELF_INSTALL
  34. if "%1" == "upgrade"      goto UPGRADE
  35. if "%1" == "downgrade"    goto UPGRADE
  36.  
  37. if not exist "%~dp0..\src\leiningen\version.clj" goto RUN_NO_CHECKOUT
  38.  
  39.     :: Running from source checkout.
  40.     call :SET_LEIN_ROOT "%~dp0.."
  41.  
  42.  
  43.     set "bootstrapfile=!LEIN_ROOT!\leiningen-core\.lein-bootstrap"
  44.     rem in .lein-bootstrap there is only one line where each path is concatenated to each other via a semicolon, there's no semicolon at the end
  45.     rem each path is NOT inside double quotes and may contain spaces (even semicolons but this is not supported here) in their names,
  46.     rem  but they won't/cannot contain double quotes " or colons :  in their names (at least on windows it's not allowed/won't work)
  47.    
  48.     rem tested when folders contain spaces and when LEIN_ROOT contains semicolon
  49.    
  50.    
  51.     if not "x%DEBUG%" == "x" echo LEIN_ROOT=!LEIN_ROOT!
  52.    
  53.     rem if not "%LEIN_ROOT:;=%" == "%LEIN_ROOT%" (
  54.  
  55.    
  56.     rem oddly enough /G:/ should've worked but doesn't where / they say it's console
  57.     rem findstr is C:\Windows\System32\findstr.exe
  58.     echo.!LEIN_ROOT! | findstr /C:";" >nul 2>&1 && (
  59.         rem aka errorlevel is 0 aka the string ";" was found
  60.         echo Your folder structure !LEIN_ROOT! contains at least one semicolon in its name
  61.         echo This is not allowed and would break things with the generated bootstrap file
  62.         echo Please correct this by renaming the folders to not contain semicolons in their name
  63.         del !bootstrapfile! >nul 2>&1
  64.         echo You'll also have to recreate the bootstrap file just to be sure it has semicolon-free names inside
  65.         echo the bootstrap file ^(which was just deleted^) is: !bootstrapfile!
  66.         echo  and the info on how to do that is:
  67.         goto RUN_BOOTSTRAP
  68.     )
  69.  
  70.     if not exist !bootstrapfile! goto NO_DEPENDENCIES
  71.  
  72.     findstr \^" "!bootstrapfile!" >nul 2>&1
  73.     if errorlevel 1 goto PARSE_BOOTSTRAPFILE
  74.         echo double quotes detected inside file: !bootstrapfile!
  75.         echo this should not be happening
  76.         goto RUN_BOOTSTRAP
  77.  
  78. :PARSE_BOOTSTRAPFILE
  79. rem will proceed to set LEIN_LIBS and surround each path from bootstrap file in double quotes and separate it from others with a semicolon
  80. rem the paths inside the bootstrap file do not already contain double quotes but may contain spaces
  81.     rem note worthy: the following won't work due to a hard 1022bytes limit truncation in the variable that was set
  82.     rem set /p LEIN_LIBS=<!bootstrapfile!
  83.     rem so this will work instead:
  84.     rem for /f "usebackq delims=" %%j in (!bootstrapfile!) do set LEIN_LIBS=%%j
  85.     rem just  set LEIN_LIBS="%%j"  is uglier/hacky but would also work here instead of the below:
  86.     for /f "usebackq delims=" %%j in ("!bootstrapfile!") do (
  87.         set tmpline=%%j
  88.         call :PROCESSPATH
  89.     )
  90.  
  91.     rem remove trailing semicolon, if any
  92.     if "!LEIN_LIBS:~-1!x" == ";x" SET LEIN_LIBS=!LEIN_LIBS:~0,-1!
  93.     if not "x%DEBUG%" == "x" echo LEIN_LIBS=!LEIN_LIBS!
  94.  
  95.     if "x!LEIN_LIBS!" == "x" goto NO_DEPENDENCIES
  96.  
  97.  
  98.     rem semicolons in pathes are not supported, spaces are supported by quoting CLASSPATH as a whole
  99.     rem (no end semicolon required)
  100.     set CLASSPATH=!LEIN_LIBS!;!LEIN_ROOT!\src;!LEIN_ROOT!\resources
  101.  
  102.     :: Apply context specific CLASSPATH entries
  103.     if exist "%~dp0..\.lein-classpath" (
  104.         for /f "tokens=* delims= " %%i in ("%~dp0..\.lein-classpath") do (
  105.             set CONTEXT_CP=%%i
  106.         )
  107.  
  108.         if NOT "x!CONTEXT_CP!"=="x" (
  109.             set CLASSPATH=!CONTEXT_CP!;!CLASSPATH!
  110.         )
  111.     )
  112.     goto SETUP_JAVA
  113.  
  114. :RUN_NO_CHECKOUT
  115.  
  116.     :: Not running from a checkout.
  117.     if not exist "%LEIN_JAR%" goto NO_LEIN_JAR
  118.     set CLASSPATH=%LEIN_JAR%
  119.  
  120.     if exist ".lein-classpath" (
  121.         for /f "tokens=* delims= " %%i in (.lein-classpath) do (
  122.             set CONTEXT_CP=%%i
  123.         )
  124.  
  125.         if NOT "x!CONTEXT_CP!"=="x" (
  126.             set CLASSPATH=!CONTEXT_CP!;!CLASSPATH!
  127.         )
  128.     )
  129.  
  130. :SETUP_JAVA
  131.  
  132. if not "x%DEBUG%" == "x" echo CLASSPATH=!CLASSPATH!
  133. :: ##################################################
  134.  
  135. if "x!JAVA_CMD!" == "x" set JAVA_CMD=java
  136. if "x!LEIN_JAVA_CMD!" == "x" set LEIN_JAVA_CMD=%JAVA_CMD%
  137.  
  138. rem remove quotes from around java commands
  139. for /f "usebackq delims=" %%i in ('!JAVA_CMD!') do set JAVA_CMD=%%~i
  140. for /f "usebackq delims=" %%i in ('!LEIN_JAVA_CMD!') do set LEIN_JAVA_CMD=%%~i
  141.  
  142. if "x%JVM_OPTS%" == "x" set JVM_OPTS=%JAVA_OPTS%
  143. goto RUN
  144.  
  145. :DownloadFile
  146. set LAST_HTTP_CLIENT=
  147. rem parameters: TargetFileName Address
  148. if "x%HTTP_CLIENT%" == "x" goto TRY_POWERSHELL
  149.     %HTTP_CLIENT% %1 %2
  150.     SET RC=%ERRORLEVEL%
  151.     goto EXITRC
  152.  
  153. :TRY_POWERSHELL
  154. :call powershell -? >nul 2>&1
  155. :if NOT ERRORLEVEL 0 goto TRY_WGET
  156.     set LAST_HTTP_CLIENT=powershell
  157.     powershell -Command "& {param($a,$f) $client = New-Object System.Net.WebClient;  $client.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials; $client.DownloadFile($a, $f)}" ""%2"" ""%1""
  158.     SET RC=%ERRORLEVEL%
  159.     goto EXITRC
  160.  
  161. :TRY_WGET
  162. call wget --help >nul 2>&1
  163. if NOT ERRORLEVEL 0 goto TRY_CURL
  164.     set LAST_HTTP_CLIENT=wget
  165.     call wget -O %1 %2
  166.     SET RC=%ERRORLEVEL%
  167.     goto EXITRC
  168.  
  169. :TRY_CURL
  170. call curl --help >nul 2>&1
  171. if NOT ERRORLEVEL 0 GOTO NO_HTTP_CLIENT
  172.    rem We set CURL_PROXY to a space character below to pose as a no-op argument
  173.     set LAST_HTTP_CLIENT=curl
  174.     set CURL_PROXY=
  175.     if NOT "x%HTTPS_PROXY%" == "x" set CURL_PROXY="-x %HTTPS_PROXY%"
  176.     call curl %CURL_PROXY% -f -L -o  %1 %2
  177.     SET RC=%ERRORLEVEL%
  178.     goto EXITRC
  179.  
  180. :NO_LEIN_JAR
  181. echo.
  182. echo %LEIN_JAR% can not be found.
  183. echo You can try running "lein self-install"
  184. echo or change LEIN_JAR environment variable
  185. echo or edit lein.bat to set appropriate LEIN_JAR path.
  186. echo.
  187. goto EXITRC
  188.  
  189. :NO_DEPENDENCIES
  190. echo.
  191. echo Leiningen is missing its dependencies.
  192. :RUN_BOOTSTRAP
  193. echo Please run "lein bootstrap" in the leiningen-core/ directory
  194. echo with a stable release of Leiningen. See CONTRIBUTING.md for details.
  195. echo.
  196. goto EXITRC
  197.  
  198. :SELF_INSTALL
  199. if exist "%LEIN_JAR%" (
  200.     echo %LEIN_JAR% already exists. Delete and retry.
  201.     goto EXITRC
  202. )
  203.  
  204. for %%f in ("%LEIN_JAR%") do set LEIN_INSTALL_DIR="%%~dpf"
  205. if not exist %LEIN_INSTALL_DIR% mkdir %LEIN_INSTALL_DIR%
  206.  
  207. echo Downloading Leiningen now...
  208.  
  209. set LEIN_JAR_URL=https://github.com/technomancy/leiningen/releases/download/%LEIN_VERSION%/leiningen-%LEIN_VERSION%-standalone.zip
  210. call :DownloadFile "%LEIN_JAR%.pending" "%LEIN_JAR_URL%"
  211. SET RC=%ERRORLEVEL%
  212. if not %RC% == 0 goto DOWNLOAD_FAILED
  213. if not exist "%LEIN_JAR%.pending" goto DOWNLOAD_FAILED
  214. move /y "%LEIN_JAR%.pending" "%LEIN_JAR%" >nul 2>&1
  215. SET RC=%ERRORLEVEL%
  216. goto EXITRC
  217.  
  218. :DOWNLOAD_FAILED
  219. SET RC=3
  220. del "%LEIN_JAR%.pending" >nul 2>&1
  221. echo.
  222. echo Failed to download %LEIN_JAR_URL%
  223. echo.
  224. echo It is possible that the download failed due to "powershell",
  225. echo "curl" or "wget"'s inability to retrieve GitHub's security certificate.
  226. echo The suggestions below do not check certificates, so use this only if
  227. echo you understand the security implications of not doing so.
  228. echo.
  229.  
  230. if "%LAST_HTTP_CLIENT%" == "powershell" (
  231.   echo The PowerShell failed to download the latest Leiningen version.
  232.   echo Try to use "curl" or "wget" to download Leiningen by setting up
  233.   echo the HTTP_CLIENT environment variable with one of the following
  234.   echo values:
  235.   echo.
  236.   echo   a^) set HTTP_CLIENT=wget --no-check-certificate -O
  237.   echo   b^) set HTTP_CLIENT=curl -f -L -k -o
  238.   echo.
  239.   echo NOTE: Make sure to *not* add double quotes when setting the value
  240.   echo       of HTTP_CLIENT
  241. )
  242.  
  243. if "%LAST_HTTP_CLIENT%" == "curl" (
  244.   echo Curl failed to download the latest Leiningen version.
  245.   echo Try to use "wget" to download Leiningen by setting up
  246.   echo the HTTP_CLIENT environment variable with one of the following
  247.   echo values:
  248.   echo.
  249.   echo   a^) set HTTP_CLIENT=wget --no-check-certificate -O
  250.   echo.
  251.   echo NOTE: Make sure to *not* add double quotes when setting the value
  252.   echo       of HTTP_CLIENT
  253.   echo.
  254.   echo If neither curl nor wget can download Leiningen, please seek
  255.   echo for help on Leiningen's GitHub project issues page.
  256. )
  257.  
  258. if "%LAST_HTTP_CLIENT%" == "wget" (
  259.   echo Curl failed to download the latest Leiningen version.
  260.   echo Try to use "wget" to download Leiningen by setting up
  261.   echo the HTTP_CLIENT environment variable with one of the following
  262.   echo values:
  263.   echo.
  264.   echo.   a^) set HTTP_CLIENT=curl -f -L -k -o
  265.   echo.
  266.   echo NOTE: make sure *not* to add double quotes to set the value of
  267.   echo       HTTP_CLIENT
  268.   echo.
  269.   echo If neither curl nor wget can download Leiningen, please seek
  270.   echo for help on Leiningen's GitHub project issues page.
  271. )
  272.  
  273. if %SNAPSHOT% == YES echo See README.md for SNAPSHOT build instructions.
  274. echo.
  275. goto EOF
  276.  
  277.  
  278. :UPGRADE
  279. set LEIN_BAT=%~dp0%~nx0
  280. set TARGET_VERSION=%2
  281. if "x%2" == "x" set TARGET_VERSION=stable
  282. echo The script at %LEIN_BAT% will be upgraded to the latest %TARGET_VERSION% version.
  283. set /P ANSWER=Do you want to continue (Y/N)?
  284. if /i {%ANSWER%}=={y}   goto YES_UPGRADE
  285. if /i {%ANSWER%}=={yes} goto YES_UPGRADE
  286. echo Aborted.
  287. goto EXITRC
  288.  
  289.  
  290. :YES_UPGRADE
  291. echo Downloading latest Leiningen batch script...
  292.  
  293. set LEIN_BAT_URL=https://github.com/technomancy/leiningen/raw/%TARGET_VERSION%/bin/lein.bat
  294. set TEMP_BAT=%~dp0temp-lein-%RANDOM%%RANDOM%.bat
  295. call :DownloadFile "%LEIN_BAT%.pending" "%LEIN_BAT_URL%"
  296. if ERRORLEVEL 0 goto EXEC_UPGRADE
  297.     del "%LEIN_BAT%.pending" >nul 2>&1
  298.     echo Failed to download %LEIN_BAT_URL%
  299.     goto EXITRC
  300. :EXEC_UPGRADE
  301. move /y "%LEIN_BAT%.pending" "%TEMP_BAT%" >nul 2>&1
  302. echo.
  303. echo Upgrading...
  304. set LEIN_JAR=
  305. call "%TEMP_BAT%" self-install
  306. (
  307.   rem This is self-modifying batch code. Use brackets to pre-load the exit command.
  308.   rem This way, script execution does not depend on whether the replacement script
  309.   rem has that command at the *very same* file position as the calling batch file.
  310.    move /y "%TEMP_BAT%" "%LEIN_BAT%" >nul 2>&1
  311.    exit /B %ERRORLEVEL%
  312. )
  313.  
  314. :NO_HTTP_CLIENT
  315. echo.
  316. echo ERROR: Neither PowerShell, Wget, or Curl could be found.
  317. echo        Make sure at least one of these tools is installed
  318. echo        and is in PATH. You can get them from URLs below:
  319. echo.
  320. echo PowerShell: "http://www.microsoft.com/powershell"
  321.  
  322. rem echo Wget:       "http://users.ugent.be/~bpuype/wget/"
  323. rem Note: Stale URL. HTTP 404.
  324. rem Alternative: wget64.exe compiled by J. Simoncic, rename to wget.exe
  325. rem MD5 1750c130c5daca8b347d3f7e34824c9b
  326. rem Check: https://www.virustotal.com/en/file/abf507f8240ed41aac74c9df6de558c88c2f11d7770f0298135f1cc544b9c08b/analysis/
  327. echo Wget:       "https://eternallybored.org/misc/wget/"
  328.  
  329. echo Curl:       "http://curl.haxx.se/dlwiz/?type=bin&os=Win32&flav=-&ver=2000/XP"
  330. echo.
  331. goto EXITRC
  332.  
  333.  
  334. :SET_LEIN_ROOT
  335. set LEIN_ROOT=%~f1
  336. goto EOF
  337.  
  338. :: Find directory containing filename supplied in first argument
  339. :: looking in current directory, and looking up the parent
  340. :: chain until we find it, or run out
  341. :: returns result in %DIR_CONTAINING%
  342. :: empty string if we don't find it
  343. :FIND_DIR_CONTAINING_UPWARDS
  344. set DIR_CONTAINING=%CD%
  345. set LAST_DIR=
  346.  
  347. :LOOK_AGAIN
  348. if "%DIR_CONTAINING%" == "%LAST_DIR%" (
  349.    :: didn't find it
  350.     set DIR_CONTAINING=
  351.     goto EOF
  352. )
  353.  
  354. if EXIST "%DIR_CONTAINING%\%1" (
  355.    :: found it - use result in DIR_CONTAINING
  356.     goto EOF
  357. )
  358.  
  359. set LAST_DIR=%DIR_CONTAINING%
  360. call :GET_PARENT_PATH "%DIR_CONTAINING%\.."
  361. set DIR_CONTAINING=%PARENT_PATH%
  362. goto LOOK_AGAIN
  363.  
  364. :GET_PARENT_PATH
  365. set PARENT_PATH=%~f1
  366. goto EOF
  367.  
  368.  
  369. :RUN
  370. :: We need to disable delayed expansion here because the %* variable
  371. :: may contain bangs (as in test!). There may also be special
  372. :: characters inside the TRAMPOLINE_FILE.
  373. setLocal DisableDelayedExpansion
  374.  
  375. set "TRAMPOLINE_FILE=%TEMP%\lein-trampoline-%RANDOM%.bat"
  376. del "%TRAMPOLINE_FILE%" >nul 2>&1
  377.  
  378. set ERRORLEVEL=
  379. set RC=0
  380. "%LEIN_JAVA_CMD%" -client %LEIN_JVM_OPTS% ^
  381.  -Dclojure.compile.path="%DIR_CONTAINING%/target/classes" ^
  382.  -Dleiningen.original.pwd="%ORIGINAL_PWD%" ^
  383.  -cp "%CLASSPATH%" clojure.main -m leiningen.core.main %*
  384. SET RC=%ERRORLEVEL%
  385. if not %RC% == 0 goto EXITRC
  386.  
  387. if not exist "%TRAMPOLINE_FILE%" goto EOF
  388. call "%TRAMPOLINE_FILE%"
  389. del "%TRAMPOLINE_FILE%" >nul 2>&1
  390. goto EOF
  391.  
  392.  
  393. :PROCESSPATH
  394. rem will surround each path with double quotes before appending it to LEIN_LIBS
  395.     for /f "tokens=1* delims=;" %%a in ("%tmpline%") do (
  396.         set LEIN_LIBS=!LEIN_LIBS!"%%a";
  397.         set tmpline=%%b
  398.     )
  399.     if not "%tmpline%" == "" goto PROCESSPATH
  400.     goto EOF
  401.  
  402. :EXITRC
  403. exit /B %RC%
  404.  
  405. :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement