sys-libs/libnih: stable 1.0.3-r4 for hppa, bug #724174
[gentoo.git] / eclass / golang-vcs-snapshot.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: golang-vcs-snapshot.eclass
5 # @MAINTAINER:
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
10 # @DESCRIPTION:
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.
15 #
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
19 # vendoring setup.
20 #
21 # The typical use case is VCS snapshots coming from github, bitbucket
22 # and similar services.
23 #
24 # Please note that this eclass currently handles only tarballs
25 # (.tar.gz), but support for more formats may be added in the future.
26 #
27 # @EXAMPLE:
28 #
29 # @CODE
30 # EGO_PN=github.com/user/package
31 # EGO_VENDOR=(
32 #       "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
33 # "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 github.com/golang/crypto"
34 # )
35 #
36 # inherit golang-vcs-snapshot
37 #
38 # SRC_URI="https://github.com/example/${PN}/tarball/v${PV} -> ${P}.tar.gz
39 # ${EGO_VENDOR_URI}"
40 # @CODE
41 #
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
45
46 inherit golang-base
47
48 case ${EAPI:-0} in
49         5|6|7) ;;
50         *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
51 esac
52
53 EXPORT_FUNCTIONS src_unpack
54
55 # @ECLASS-VARIABLE: EGO_VENDOR
56 # @DESCRIPTION:
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.
62
63 declare -arg EGO_VENDOR
64
65 _golang-vcs-snapshot_set_vendor_uri() {
66         EGO_VENDOR_URI=
67         local lib
68         for lib in "${EGO_VENDOR[@]}"; do
69                 lib=(${lib})
70                 if [[ -n ${lib[2]} ]]; then
71                         EGO_VENDOR_URI+=" https://${lib[2]}/archive/${lib[1]}.tar.gz -> ${lib[2]//\//-}-${lib[1]}.tar.gz"
72                 else
73                         EGO_VENDOR_URI+=" https://${lib[0]}/archive/${lib[1]}.tar.gz -> ${lib[0]//\//-}-${lib[1]}.tar.gz"
74                 fi
75         done
76         readonly EGO_VENDOR_URI
77 }
78
79 _golang-vcs-snapshot_set_vendor_uri
80 unset -f _golang-vcs-snapshot_set_vendor_uri
81
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
88 }
89
90 # @FUNCTION: golang-vcs-snapshot_src_unpack
91 # @DESCRIPTION:
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
95         ego_pn_check
96         set -- ${A}
97         x="$1"
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
101
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
106                         lib=(${lib})
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
111                         else
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
115                         fi
116                 done
117         fi
118 }