summaryrefslogtreecommitdiff
blob: 975b23df45f4c37522dd6e0dfde755dc358e5e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: golang-vcs-snapshot.eclass
# @MAINTAINER:
# William Hubbs <williamh@gentoo.org>
# @SUPPORTED_EAPIS: 5 6
# @BLURB: support eclass for unpacking VCS snapshot tarballs for
# software written in the Go programming language
# @DESCRIPTION:
# This eclass provides a convenience src_unpack() which unpacks the
# first tarball mentioned in SRC_URI to its appropriate location in
# ${WORKDIR}/${P}, treating ${WORKDIR}/${P} as a go workspace.
# Also, it provides a downstream method of vendoring packages.
#
# The location where the tarball is extracted is defined as
# ${WORKDIR}/${P}/src/${EGO_PN}. The location of vendored packages is
# defined as ${WORKDIR}/${P}/src/${EGO_PN%/*}/vendor to match Go's
# vendoring setup.
#
# The typical use case is VCS snapshots coming from github, bitbucket
# and similar services.
#
# Please note that this eclass currently handles only tarballs
# (.tar.gz), but support for more formats may be added in the future.
#
# @EXAMPLE:
#
# @CODE
# EGO_PN=github.com/user/package
# EGO_VENDOR=(
#	"github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
# )
#
# inherit golang-vcs-snapshot
#
# SRC_URI="https://github.com/example/${PN}/tarball/v${PV} -> ${P}.tar.gz
# ${EGO_VENDOR_URI}"
# @CODE
#
# The above example will extract the tarball to
# ${WORKDIR}/${P}/src/github.com/user/package
# and add the vendored tarballs to ${WORKDIR}/src/${EGO_PN}/vendor

inherit golang-base

case ${EAPI:-0} in
	5|6) ;;
	*) die "${ECLASS} API in EAPI ${EAPI} not yet established."
esac

EXPORT_FUNCTIONS src_unpack

# @ECLASS-VARIABLE: EGO_VENDOR
# @DESCRIPTION:
# This variable contains a list of vendored packages.
# The items of this array are strings that contain the
# import path and the git commit hash for a vendored package.
# If the import path does not start with github.com, the third argument
# can be used to point to a github repository.

declare -arg EGO_VENDOR

_golang-vcs-snapshot_set_vendor_uri() {
	EGO_VENDOR_URI=
	local lib
	for lib in "${EGO_VENDOR[@]}"; do
		lib=(${lib})
		if [[ -n ${lib[2]} ]]; then
			EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
		else
			EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
		fi
	done
	readonly EGO_VENDOR_URI
}

_golang-vcs-snapshot_set_vendor_uri
unset -f _golang-vcs-snapshot_set_vendor_uri

_golang-vcs-snapshot_dovendor() {
	local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
	rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
	mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
	tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
		-f "${DISTDIR}"/${TARBALL} || die
}

# @FUNCTION: golang-vcs-snapshot_src_unpack
# @DESCRIPTION:
# Extract the first archive from ${A} to the appropriate location for GOPATH.
golang-vcs-snapshot_src_unpack() {
	local lib vendor_path x
	ego_pn_check
	set -- ${A}
	x="$1"
	mkdir -p "${WORKDIR}/${P}/src/${EGO_PN%/...}" || die
	tar -C "${WORKDIR}/${P}/src/${EGO_PN%/...}" -x --strip-components 1 \
		-f "${DISTDIR}/${x}" || die

	if [[ -n "${EGO_VENDOR}" ]]; then
		vendor_path="${WORKDIR}/${P}/src/${EGO_PN%/...}/vendor"
		mkdir -p "${vendor_path}" || die
		for lib in "${EGO_VENDOR[@]}"; do
			lib=(${lib})
			if [[ -n ${lib[2]} ]]; then
				einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz"
				_golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
					${lib[2]//\//-}-${lib[1]}.tar.gz
			else
				einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz"
				_golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
				${lib[0]//\//-}-${lib[1]}.tar.gz
			fi
		done
	fi
}