1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: golang-vcs-snapshot.eclass
6 # William Hubbs <williamh@gentoo.org>
7 # @SUPPORTED_EAPIS: 5 6 7
8 # @BLURB: support eclass for unpacking VCS snapshot tarballs for
9 # software written in the Go programming language
11 # This eclass provides a convenience src_unpack() which unpacks the
12 # first tarball mentioned in SRC_URI to its appropriate location in
13 # ${WORKDIR}/${P}, treating ${WORKDIR}/${P} as a go workspace.
14 # Also, it provides a downstream method of vendoring packages.
16 # The location where the tarball is extracted is defined as
17 # ${WORKDIR}/${P}/src/${EGO_PN}. The location of vendored packages is
18 # defined as ${WORKDIR}/${P}/src/${EGO_PN%/*}/vendor to match Go's
21 # The typical use case is VCS snapshots coming from github, bitbucket
22 # and similar services.
24 # Please note that this eclass currently handles only tarballs
25 # (.tar.gz), but support for more formats may be added in the future.
30 # EGO_PN=github.com/user/package
32 # "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
33 # "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
36 # inherit golang-vcs-snapshot
38 # SRC_URI="https://github.com/example/${PN}/tarball/v${PV} -> ${P}.tar.gz
42 # The above example will extract the tarball to
43 # ${WORKDIR}/${P}/src/github.com/user/package
44 # and add the vendored tarballs to ${WORKDIR}/src/${EGO_PN}/vendor
50 *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
53 EXPORT_FUNCTIONS src_unpack
55 # @ECLASS-VARIABLE: EGO_VENDOR
57 # This variable contains a list of vendored packages.
58 # The items of this array are strings that contain the
59 # import path and the git commit hash for a vendored package.
60 # If the import path does not start with github.com, the third argument
61 # can be used to point to a github repository.
63 declare -arg EGO_VENDOR
65 _golang-vcs-snapshot_set_vendor_uri() {
68 for lib in "${EGO_VENDOR[@]}"; do
70 if [[ -n ${lib[2]} ]]; then
71 EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
73 EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
76 readonly EGO_VENDOR_URI
79 _golang-vcs-snapshot_set_vendor_uri
80 unset -f _golang-vcs-snapshot_set_vendor_uri
82 _golang-vcs-snapshot_dovendor() {
83 local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
84 rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
85 mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
86 tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
87 -f "${DISTDIR}"/${TARBALL} || die
90 # @FUNCTION: golang-vcs-snapshot_src_unpack
92 # Extract the first archive from ${A} to the appropriate location for GOPATH.
93 golang-vcs-snapshot_src_unpack() {
94 local lib vendor_path x
98 mkdir -p "${WORKDIR}/${P}/src/${EGO_PN%/...}" || die
99 tar -C "${WORKDIR}/${P}/src/${EGO_PN%/...}" -x --strip-components 1 \
100 -f "${DISTDIR}/${x}" || die
102 if [[ -n "${EGO_VENDOR}" ]]; then
103 vendor_path="${WORKDIR}/${P}/src/${EGO_PN%/...}/vendor"
104 mkdir -p "${vendor_path}" || die
105 for lib in "${EGO_VENDOR[@]}"; do
107 if [[ -n ${lib[2]} ]]; then
108 einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz"
109 _golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
110 ${lib[2]//\//-}-${lib[1]}.tar.gz
112 einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz"
113 _golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
114 ${lib[0]//\//-}-${lib[1]}.tar.gz