This eclass adds a src_unpack function that supports the EGO_VENDOR
variable for vendoring modules.
---
 eclass/go-module-vendor.eclass | 133 +++++++++++++++++++++++++++++++++
 1 file changed, 133 insertions(+)
 create mode 100644 eclass/go-module-vendor.eclass

diff --git a/eclass/go-module-vendor.eclass b/eclass/go-module-vendor.eclass
new file mode 100644
index 00000000000..af9007df411
--- /dev/null
+++ b/eclass/go-module-vendor.eclass
@@ -0,0 +1,133 @@
+# Copyright 2019 gentoo authors
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: go-module-vendor.eclass
+# @MAINTAINER:
+# William Hubbs <[email protected]>
+# @SUPPORTED_EAPIS: 7
+# @BLURB: Eclass for building software written in the go
+# programming language that uses go modules and does not vendor.
+# @DESCRIPTION:
+# This eclass provides a src_unpack function which supports vendoring
+# dependencies for software written in the go programming language that
+# uses go modules.
+#
+# You will know the software you are packaging uses modules because
+# it will have files named go.sum and go.mod in its top-level source
+# directory. If it does not have these files, use the golang-* eclasses.
+#
+# If there is also a directory named vendor in the top level source directory
+# of your package, use the golang-module eclass instead of this one.
+#
+# This eclass provides a src_unpack function which unpacks the 
+# first tarball mentioned in SRC_URI to the usual location and unpacks
+# any modules mentioned in EGO_VENDOR to ${S}/vendor.
+#
+# Please note that this eclass currently handles only tarballs
+# (.tar.gz), but support for more formats may be added in the future.
+#
+# Since Go programs are statically linked, it is important that your ebuild's
+# LICENSE= setting includes the licenses of all statically linked
+# dependencies. So please make sure it is accurate.
+#
+# @EXAMPLE:
+#
+# @CODE
+# EGO_VENDOR=(
+#      "github.com/xenolf/lego 6cac0ea7d8b28c889f709ec7fa92e92b82f490dd"
+# "golang.org/x/crypto 453249f01cfeb54c3d549ddb75ff152ca243f9d8 
github.com/golang/crypto"
+# )
+#
+# inherit go-module-vendor
+#
+# SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> 
${P}.tar.gz
+# ${EGO_VENDOR_URI}"
+# @CODE
+#
+# The above example will extract the tarball to ${S} and
+# extract the contents of EGO_VENDOR to ${S}/vendor.
+
+inherit go-module
+
+case ${EAPI:-0} in
+       7) ;;
+       *) die "${ECLASS} API in EAPI ${EAPI} not yet established."
+esac
+
+if [[ -z ${_GO_MODULE_VENDOR} ]]; then
+
+_GO_MODULE_VENDOR=1
+
+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
+
+_go-module-vendor_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
+}
+
+_go-module-vendor_set_vendor_uri
+unset -f _go-module-vendor_set_vendor_uri
+
+_go-module-vendor_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: go-module-vendor_src_unpack
+# @DESCRIPTION:
+# Extract the first archive from ${A} to ${S}, then extract
+# any sources mentioned in ${EGO_VENDOR} to ${S}/vendor.
+go-module-vendor_src_unpack() {
+       local lib vendor_path x
+       set -- ${A}
+       x="$1"
+       mkdir -p "${S}" || die
+       tar -C "${S}/" -x --strip-components 1 \
+               -f "${DISTDIR}/${x}" || die
+
+       if [[ -d "${S}/vendor" ]]; then
+               eerror "Upstream for ${P}.ebuild vendors dependencies."
+               die "This ebuild should inherit go-module.eclass"
+       fi
+
+       if [[ -n "${EGO_VENDOR}" ]]; then
+               vendor_path="${S}/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"
+                               _go-module_dovendor "${vendor_path}" ${lib[0]} \
+                                       ${lib[2]//\//-}-${lib[1]}.tar.gz
+                       else
+                               einfo "Vendoring ${lib[0]} 
${lib[0]//\//-}-${lib[1]}.tar.gz"
+                               _go-module_dovendor "${vendor_path}" ${lib[0]} \
+                               ${lib[0]//\//-}-${lib[1]}.tar.gz
+                       fi
+               done
+       fi
+}
+
+fi
-- 
2.21.0


Reply via email to