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 # @BLURB: support eclass for unpacking VCS snapshot tarballs for
8 # software written in the Go programming language
10 # This eclass provides a convenience src_unpack() which unpacks the
11 # first tarball mentioned in SRC_URI to its appropriate location in
12 # ${WORKDIR}/${P}, treating ${WORKDIR}/${P} as a go workspace.
13 # Also, it provides a downstream method of vendoring packages.
15 # The location where the tarball is extracted is defined as
16 # ${WORKDIR}/${P}/src/${EGO_PN}. The location of vendored packages is
17 # defined as ${WORKDIR}/${P}/src/${EGO_PN%/*}/vendor to match Go's
20 # The typical use case is VCS snapshots coming from github, bitbucket
21 # and similar services.
23 # Please note that this eclass currently handles only tarballs
24 # (.tar.gz), but support for more formats may be added in the future.
29 # EGO_PN=github.com/user/package
31 # "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
32 # "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
35 # inherit golang-vcs-snapshot
37 # SRC_URI="https://github.com/example/${PN}/tarball/v${PV} -> ${P}.tar.gz
41 # The above example will extract the tarball to
42 # ${WORKDIR}/${P}/src/github.com/user/package
43 # and add the vendored tarballs to ${WORKDIR}/src/${EGO_PN}/vendor
49 *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
52 EXPORT_FUNCTIONS src_unpack
54 # @ECLASS-VARIABLE: EGO_VENDOR
56 # This variable contains a list of vendored packages.
57 # The items of this array are strings that contain the
58 # import path and the git commit hash for a vendored package.
59 # If the import path does not start with github.com, the third argument
60 # can be used to point to a github repository.
62 declare -arg EGO_VENDOR
64 _golang-vcs-snapshot_set_vendor_uri() {
67 for lib in "${EGO_VENDOR[@]}"; do
69 if [[ -n ${lib[2]} ]]; then
70 EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
72 EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
75 readonly EGO_VENDOR_URI
78 _golang-vcs-snapshot_set_vendor_uri
79 unset -f _golang-vcs-snapshot_set_vendor_uri
81 _golang-vcs-snapshot_dovendor() {
82 local VENDOR_PATH=$1 VENDORPN=$2 TARBALL=$3
83 rm -fr "${VENDOR_PATH}/${VENDORPN}" || die
84 mkdir -p "${VENDOR_PATH}/${VENDORPN}" || die
85 tar -C "${VENDOR_PATH}/${VENDORPN}" -x --strip-components 1\
86 -f "${DISTDIR}"/${TARBALL} || die
89 # @FUNCTION: golang-vcs-snapshot_src_unpack
91 # Extract the first archive from ${A} to the appropriate location for GOPATH.
92 golang-vcs-snapshot_src_unpack() {
93 local lib vendor_path x
97 mkdir -p "${WORKDIR}/${P}/src/${EGO_PN%/...}" || die
98 tar -C "${WORKDIR}/${P}/src/${EGO_PN%/...}" -x --strip-components 1 \
99 -f "${DISTDIR}/${x}" || die
101 if [[ -n "${EGO_VENDOR}" ]]; then
102 vendor_path="${WORKDIR}/${P}/src/${EGO_PN%/...}/vendor"
103 mkdir -p "${vendor_path}" || die
104 for lib in "${EGO_VENDOR[@]}"; do
106 if [[ -n ${lib[2]} ]]; then
107 einfo "Vendoring ${lib[0]} ${lib[2]//\//-}-${lib[1]}.tar.gz"
108 _golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
109 ${lib[2]//\//-}-${lib[1]}.tar.gz
111 einfo "Vendoring ${lib[0]} ${lib[0]//\//-}-${lib[1]}.tar.gz"
112 _golang-vcs-snapshot_dovendor "${vendor_path}" ${lib[0]} \
113 ${lib[0]//\//-}-${lib[1]}.tar.gz