1 # Copyright 2022 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
6 # liuyujielol <2073201758GD@gmail.com>
8 # liuyujielol <2073201758GD@gmail.com>
9 # @SUPPORTED_EAPIS: 7 8
10 # @BLURB: set up a basic env for building nodejs package which uses yarn.
12 # This eclass provides functions to prepare yarn dependencies for portage
13 # and makes yarn install them offline.
22 # "@ampproject/remapping/-/remapping-2.0.4.tgz"
23 # "ansi-html-community/-/ansi-html-community-0.0.8.tgz"
28 # SRC_URI="https://github.com/example/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz
29 # ${EYARN_LOCK_SRC_URI}"
31 # YARN_WORKDIR="${S}/web"
34 # unpack "${P}.tar.gz"
35 # yarn_set_offline_mirror
41 # yarn_offline_install
47 if [[ ! ${_YARN_ECLASS} ]]; then
51 *) die
"${ECLASS}: EAPI ${EAPI} unsupported."
55 >=net-libs/nodejs-14.16
59 # @ECLASS-VARIABLE: EYARN_LOCK
62 # This is an array based on yarn.lock file content
63 # from inside the target package.
65 # sed -r -n -e 's/^[ ]*resolved \"(.*)\#.*\"$/\1/g; s/https:\/\/registry.yarnpkg.com\//\0/g; s/https:\/\/registry.yarnpkg.com\///p' yarn.lock | sort | uniq | sed 's/\(.*\)/"\1"/g'
67 # @ECLASS-VARIABLE: EYARN_LOCK_SRC_URI
70 # Coverted real src_uri and corresponding filename.
72 # @ECLASS-VARIABLE: _YARN_RESOLVED_MAP
75 # Variable for recording whether a distfile belongs to yarn.
76 declare -A -g _YARN_RESOLVED_MAP
78 # @ECLASS-VARIABLE: _YARN_RESOLVED_MAP_3RDPARTY
81 # Variable for recording whether a distfile (from a 3rd party repository) belongs to yarn.
82 declare -A -g _YARN_RESOLVED_MAP_3RDPARTY
84 # @ECLASS-VARIABLE: _YARN_OFFLINE_MIRROR
87 # The temporary offline mirror directory for YARN
88 _YARN_OFFLINE_MIRROR
="${T}/yarn-mirror/"
90 # @ECLASS-VARIABLE: YARN_WORKDIR
92 # The source file directory for yarn to work with
93 # By default sets to ${S}
96 # @FUNCTION: yarn_set_globals
98 # Generate real src_uri variables
100 debug-print-function
"${FUNCNAME}" "$@"
102 # used make SRC_URI easier to read
106 for line
in "${EYARN_LOCK[@]}"; do
107 _distfile
=${line//\//:2F}
109 _uri
="mirror://yarn/${line}"
110 EYARN_LOCK_SRC_URI
+=" ${_uri} -> ${_distfile}${newline}"
111 _YARN_RESOLVED_MAP
["${_distfile}"]=1
114 _YARN_SET_GLOBALS_CALLED
=1
117 # @FUNCTION: yarn_src_unpack
119 # Soft link the local yarn pkg from ${DISTDIR} to ${_YARN_OFFLINE_MIRROR}
120 # for setting up yarn to use offline mirror and unpack other targets.
121 # NOTE:yarn_set_globals must be called before.
123 debug-print-function
"${FUNCNAME}" "$@"
125 if [[ "${#EYARN_RESOLVED[@]}" -gt 0 ]]; then
126 if [[ ! ${_YARN_SET_GLOBALS_CALLED} ]]; then
127 die
"yarn_set_globals must be called in global scope"
130 mkdir
-p "${_YARN_OFFLINE_MIRROR}" || die
134 if [[ -n ${_YARN_RESOLVED_MAP["${f}"]} ]]; then
135 df
="$(echo ${f//:2F//} | sed -r -e 's#(@([^@/]+))?\/?([^@/]+)\/\-\/([^/]+).tgz#yarn-\1-\4.tgz#g')"
138 ln -s "${DISTDIR}/${f}" "${_YARN_OFFLINE_MIRROR}/${df}" || die
139 elif [[ -n ${_YARN_RESOLVED_MAP_3RDPARTY["${f}"]} ]]; then
140 ln -s "${DISTDIR}/${f}" "${_YARN_OFFLINE_MIRROR}/${f//:2F//}" || die
148 #set yarn-offline-mirror
149 if [[ -e "${YARN_WORKDIR}" ]]; then
150 echo "yarn-offline-mirror \"${_YARN_OFFLINE_MIRROR}\"" >> "${YARN_WORKDIR}/.yarnrc" || die
152 die
"the yarn workdir ${YARN_WORKDIR} does not exist"
156 # @FUNCTION: yarn_set_offline_mirror
158 # If your ebuild redefines src_unpack and uses EYARN_RESOLVED you need to call
159 # this function in src_unpack.
160 # Soft link the local yarn pkg from ${DISTDIR} to ${_YARN_OFFLINE_MIRROR}
161 # for setting up yarn to use offline mirror.
162 # NOTE:yarn_set_globals must be called before.
163 yarn_set_offline_mirror
() {
164 debug-print-function
"${FUNCNAME}" "$@"
166 if [[ ! ${_YARN_SET_GLOBALS_CALLED} ]]; then
167 die
"yarn_set_globals must be called in global scope"
170 mkdir
-p "${_YARN_OFFLINE_MIRROR}" || die
174 if [[ -n ${_YARN_RESOLVED_MAP["${f}"]} ]]; then
175 # make the filename be the exract filename yarn wants
176 df
="$(echo ${f//:2F//} | sed -r -e 's#(@([^@/]+))?\/?([^@/]+)\/\-\/([^/]+).tgz#yarn-\1-\4.tgz#g')"
179 ln -s "${DISTDIR}/${f}" "${_YARN_OFFLINE_MIRROR}/${df}" || die
182 if [[ -n ${_YARN_RESOLVED_MAP_3RDPARTY["${f}"]} ]]; then
183 ln -s "${DISTDIR}/${f}" "${_YARN_OFFLINE_MIRROR}/${f}" || die
186 # set yarn-offline-mirror
187 if [[ -e "${YARN_WORKDIR}" ]]; then
188 echo "yarn-offline-mirror \"${_YARN_OFFLINE_MIRROR}\"" >> "${YARN_WORKDIR}/.yarnrc" || die
190 die
"the yarn workdir ${YARN_WORKDIR} does not exist"
194 # @FUNCTION: yarn_src_prepare
196 # General function for preparing source files with yarn.
202 # @FUNCTION: yarn_offline_install
204 # Let yarn install dependencies from offline mirror.
205 yarn_offline_install
() {
206 debug-print-function
"${FUNCNAME}" "$@"
208 cd "${YARN_WORKDIR}" || die
"cd failed"
209 yarn
install --offline || die
212 EXPORT_FUNCTIONS src_unpack src_prepare