github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/hack/.vendor-helpers.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  PROJECT=github.com/docker/docker
     4  
     5  # Downloads dependencies into vendor/ directory
     6  mkdir -p vendor
     7  
     8  if ! go list github.com/docker/docker/docker &> /dev/null; then
     9  	rm -rf .gopath
    10  	mkdir -p .gopath/src/github.com/docker
    11  	ln -sf ../../../.. .gopath/src/${PROJECT}
    12  	export GOPATH="${PWD}/.gopath:${PWD}/vendor"
    13  fi
    14  export GOPATH="$GOPATH:${PWD}/vendor"
    15  
    16  find='find'
    17  if [ "$(go env GOHOSTOS)" = 'windows' ]; then
    18  	find='/usr/bin/find'
    19  fi
    20  
    21  clone() {
    22  	local vcs="$1"
    23  	local pkg="$2"
    24  	local rev="$3"
    25  	local url="$4"
    26  
    27  	: ${url:=https://$pkg}
    28  	local target="vendor/src/$pkg"
    29  
    30  	echo -n "$pkg @ $rev: "
    31  
    32  	if [ -d "$target" ]; then
    33  		echo -n 'rm old, '
    34  		rm -rf "$target"
    35  	fi
    36  
    37  	echo -n 'clone, '
    38  	case "$vcs" in
    39  		git)
    40  			git clone --quiet --no-checkout "$url" "$target"
    41  			( cd "$target" && git checkout --quiet "$rev" && git reset --quiet --hard "$rev" )
    42  			;;
    43  		hg)
    44  			hg clone --quiet --updaterev "$rev" "$url" "$target"
    45  			;;
    46  	esac
    47  
    48  	echo -n 'rm VCS, '
    49  	( cd "$target" && rm -rf .{git,hg} )
    50  
    51  	echo -n 'rm vendor, '
    52  	( cd "$target" && rm -rf vendor Godeps/_workspace )
    53  
    54  	echo done
    55  }
    56  
    57  # get an ENV from the Dockerfile with support for multiline values
    58  _dockerfile_env() {
    59  	local e="$1"
    60  	awk '
    61  		$1 == "ENV" && $2 == "'"$e"'" {
    62  			sub(/^ENV +([^ ]+) +/, "");
    63  			inEnv = 1;
    64  		}
    65  		inEnv {
    66  			if (sub(/\\$/, "")) {
    67  				printf "%s", $0;
    68  				next;
    69  			}
    70  			print;
    71  			exit;
    72  		}
    73  	' ${DOCKER_FILE:="Dockerfile"}
    74  }
    75  
    76  clean() {
    77  	local packages=(
    78  		"${PROJECT}/docker" # package main
    79  		"${PROJECT}/integration-cli" # external tests
    80  	)
    81  	local dockerPlatforms=( ${DOCKER_ENGINE_OSARCH:="linux/amd64"} $(_dockerfile_env DOCKER_CROSSPLATFORMS) )
    82  	local dockerBuildTags="$(_dockerfile_env DOCKER_BUILDTAGS)"
    83  	local buildTagCombos=(
    84  		''
    85  		'experimental'
    86  		'pkcs11'
    87  		"$dockerBuildTags"
    88  		"daemon $dockerBuildTags"
    89  		"daemon cgo $dockerBuildTags"
    90  		"experimental $dockerBuildTags"
    91  		"experimental daemon $dockerBuildTags"
    92  		"experimental daemon cgo $dockerBuildTags"
    93  		"pkcs11 $dockerBuildTags"
    94  		"pkcs11 daemon $dockerBuildTags"
    95  		"pkcs11 daemon cgo $dockerBuildTags"
    96  	)
    97  
    98  	echo
    99  
   100  	echo -n 'collecting import graph, '
   101  	local IFS=$'\n'
   102  	local imports=( $(
   103  		for platform in "${dockerPlatforms[@]}"; do
   104  			export GOOS="${platform%/*}";
   105  			export GOARCH="${platform##*/}";
   106  			for buildTags in "${buildTagCombos[@]}"; do
   107  				go list -e -tags "$buildTags" -f '{{join .Deps "\n"}}' "${packages[@]}"
   108  				go list -e -tags "$buildTags" -f '{{join .TestImports "\n"}}' "${packages[@]}"
   109  			done
   110  		done | grep -vE "^${PROJECT}" | sort -u
   111  	) )
   112  	imports=( $(go list -e -f '{{if not .Standard}}{{.ImportPath}}{{end}}' "${imports[@]}") )
   113  	unset IFS
   114  
   115  	echo -n 'pruning unused packages, '
   116  	findArgs=(
   117  		# This directory contains only .c and .h files which are necessary
   118  		-path vendor/src/github.com/mattn/go-sqlite3/code
   119  	)
   120  
   121  	# This package is required to build the Etcd client,
   122  	# but Etcd hard codes a local Godep full path.
   123  	# FIXME: fix_rewritten_imports fixes this problem in most platforms
   124  	# but it fails in very small corner cases where it makes the vendor
   125  	# script to remove this package.
   126  	# See: https://github.com/docker/docker/issues/19231
   127  	findArgs+=( -or -path vendor/src/github.com/ugorji/go/codec )
   128  	for import in "${imports[@]}"; do
   129  		[ "${#findArgs[@]}" -eq 0 ] || findArgs+=( -or )
   130  		findArgs+=( -path "vendor/src/$import" )
   131  	done
   132  
   133  	local IFS=$'\n'
   134  	local prune=( $($find vendor -depth -type d -not '(' "${findArgs[@]}" ')') )
   135  	unset IFS
   136  	for dir in "${prune[@]}"; do
   137  		$find "$dir" -maxdepth 1 -not -type d -not -name 'LICENSE*' -not -name 'COPYING*' -exec rm -v -f '{}' ';'
   138  		rmdir "$dir" 2>/dev/null || true
   139  	done
   140  
   141  	echo -n 'pruning unused files, '
   142  	$find vendor -type f -name '*_test.go' -exec rm -v '{}' ';'
   143  	$find vendor -type f -name 'Vagrantfile' -exec rm -v '{}' ';'
   144  
   145  	# These are the files that are left over after fix_rewritten_imports is run.
   146  	echo -n 'pruning .orig files, '
   147  	$find vendor -type f -name '*.orig' -exec rm -v '{}' ';'
   148  
   149  	echo done
   150  }
   151  
   152  # Fix up hard-coded imports that refer to Godeps paths so they'll work with our vendoring
   153  fix_rewritten_imports () {
   154         local pkg="$1"
   155         local remove="${pkg}/Godeps/_workspace/src/"
   156         local target="vendor/src/$pkg"
   157  
   158         echo "$pkg: fixing rewritten imports"
   159         $find "$target" -name \*.go -exec sed -i'.orig' -e "s|\"${remove}|\"|g" {} \;
   160  }