Skip to content

Instantly share code, notes, and snippets.

@VonC
Last active May 16, 2016 16:12
Show Gist options
  • Save VonC/51fe08c5bd79334b6e1ccdd498d4d2bc to your computer and use it in GitHub Desktop.
Save VonC/51fe08c5bd79334b6e1ccdd498d4d2bc to your computer and use it in GitHub Desktop.
Windows bat script to vendorize a github repo in a vendor subfolder of a go project
@echo off
setlocal enabledelayedexpansion
rem set pkg=github.com/jroimartin/gocui
set pkg=%1
if "%pkg%"=="" (
echo expect package name github.com/user/project
exit /b 1
)
if exist "vendor/%pkg%" (
echo '%pkg%' already vendorized
cd vendor
call :clean
cd ..
exit /b 0
)
set PATH=%PATH%;%GOROOT%/bin
SET pwd=%cd%
pushd %pwd%
if not exist vendor (
mkdir vendor
)
cd vendor
set GOPATH=%pwd%\vendor
echo GOPATH='%GOPATH%'
go get %pkg%
rem go list -f "{{.ImportPath}} {{.Deps}}" github.com/jroimartin/gocui/...
call :buildList %pkg%
echo list='%list%'
call :processList
echo deps='%deps%'
rem goto :end
call :vendorise %pkg%
call :processDeps
call :clean
:end
popd
endlocal
goto:eof
:buildList
set url=%1
rem http://stackoverflow.com/questions/28166249/how-to-list-installed-go-packages
go list -f "{{.ImportPath}} {{.Deps}}" %url%/...
FOR /F "tokens=2 delims=^[" %%i IN ('go list -f "{{.ImportPath}} {{.Deps}}" %url%/...') DO (set list=%%i)
FOR /F "tokens=1 delims=^]" %%i IN ('echo %list%') DO (set list=%%i)
goto:eof
:processList
rem http://stackoverflow.com/questions/6966558/batch-file-for-f-tokens
for /f "tokens=1* delims= " %%a in ("%list%") do (
set adep=%%a
set list=%%b
set sep=
rem echo adep='!adep!'
rem http://stackoverflow.com/questions/29655065/check-if-batch-variable-starts-with
IF "!adep:~0,6!"=="github" (
if not "%deps%" == "" set sep= &@echo off
set deps=%deps%!sep!!adep!
)
IF "!adep:~0,13!"=="golang.org/x/" (
if not "%deps%" == "" set sep= &@echo off
set deps=%deps%!sep!!adep!
)
set adep=
)
rem echo L='%list%'
rem echo D='%deps%'
if not "%list%" == "" goto :processList
goto :eof
:processDeps
for /f "tokens=1* delims= " %%a in ("%deps%") do (
set apkg=%%a
set deps=%%b
echo vendorise submodule '!apkg!'
rem ^('!deps!'^)*
call :vendorise !apkg!
)
if not "%deps%" == "" goto :processDeps
goto :eof
:vendorise
set counter=0
set apkgn=%1
set apkg=
set asep=
rem echo "process apkgn='%apkgn%' '!apkgn!'"
:nextVar
for /F "tokens=1* delims=/" %%a in ("%apkgn%") do (
set /A counter=%counter%+1
set apkg=!apkg!!asep!%%a
set asep=/
set apkgn=%%b
)
if %counter% equ 3 goto :goon
if defined apkgn goto :nextVar
:goon
rem echo "all done: apkg='!apkg!'(1)"
rem echo "all done: apkg='%apkg%'(2)"
echo "process apkg='%apkg%'"
rem exit /b 0
echo dir='vendor/%apkg%'
if not exist "%apkg%" (
REM TODO replace golang.org\x with go.googlesource.com
REM TODO trim everything after \x
set anurl=%apkg%
if "!anurl:~0,17!"=="golang.org/x/net/" (
set anurl=go.googlesource.com/net
set apkg=golang.org/x/net
)
REM echo anurl='!anurl!'
REM echo apkg='!apkg!'
echo git submodule add -- https://!anurl! !apkg!
git submodule add -- https://!anurl! !apkg!
)
goto:eof
:clean
rm -Rf src
rm -Rf pkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment