net-proxy/clash-for-windows-bin: Bump to new version, drop old version.
[gentoo-zh.git] / eclass / yarn.eclass
blobfc1a7091a1273eb8d05e4ab616eb469da68412fd
1 # Copyright 2022 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: yarn.eclass
5 # @MAINTAINER:
6 # liuyujielol <2073201758GD@gmail.com>
7 # @AUTHOR:
8 # liuyujielol <2073201758GD@gmail.com>
9 # @SUPPORTED_EAPIS: 7 8
10 # @BLURB: set up a basic env for building nodejs package which uses yarn.
11 # @DESCRIPTION:
12 # This eclass provides functions to prepare yarn dependencies for portage
13 # and makes yarn install them offline.
15 # @EXAMPLE:
17 # @CODE
19 # inherit yarn
21 # EYARN_LOCK=(
22 # "@ampproject/remapping/-/remapping-2.0.4.tgz"
23 # "ansi-html-community/-/ansi-html-community-0.0.8.tgz"
24 # )
26 # yarn_set_globals
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"
33 # src_unpack() {
34 # unpack "${P}.tar.gz"
35 # yarn_set_offline_mirror
36 # ...
37 # }
39 # src_prepare(){
40 # default
41 # yarn_offline_install
42 # ...
43 # }
45 # @CODE
47 if [[ ! ${_YARN_ECLASS} ]]; then
49 case ${EAPI} in
50 7|8) ;;
51 *) die "${ECLASS}: EAPI ${EAPI} unsupported."
52 esac
54 BDEPEND="
55 >=net-libs/nodejs-14.16
56 sys-apps/yarn
59 # @ECLASS-VARIABLE: EYARN_LOCK
60 # @REQUIRED
61 # @DESCRIPTION:
62 # This is an array based on yarn.lock file content
63 # from inside the target package.
64 # e.g.:
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
68 # @OUTPUT_VARIABLE
69 # @DESCRIPTION:
70 # Coverted real src_uri and corresponding filename.
72 # @ECLASS-VARIABLE: _YARN_RESOLVED_MAP
73 # @INTERNAL
74 # @DESCRIPTION:
75 # Variable for recording whether a distfile belongs to yarn.
76 declare -A -g _YARN_RESOLVED_MAP
78 # @ECLASS-VARIABLE: _YARN_RESOLVED_MAP_3RDPARTY
79 # @INTERNAL
80 # @DESCRIPTION:
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
85 # @INTERNAL
86 # @DESCRIPTION:
87 # The temporary offline mirror directory for YARN
88 _YARN_OFFLINE_MIRROR="${T}/yarn-mirror/"
90 # @ECLASS-VARIABLE: YARN_WORKDIR
91 # @DESCRIPTION:
92 # The source file directory for yarn to work with
93 # By default sets to ${S}
94 YARN_WORKDIR="${S}"
96 # @FUNCTION: yarn_set_globals
97 # @DESCRIPTION:
98 # Generate real src_uri variables
99 yarn_set_globals() {
100 debug-print-function "${FUNCNAME}" "$@"
102 # used make SRC_URI easier to read
103 local newline=$'\n'
104 local line
106 for line in "${EYARN_LOCK[@]}"; do
107 _distfile=${line//\//:2F}
108 # SRC_URI
109 _uri="mirror://yarn/${line}"
110 EYARN_LOCK_SRC_URI+=" ${_uri} -> ${_distfile}${newline}"
111 _YARN_RESOLVED_MAP["${_distfile}"]=1
112 done
114 _YARN_SET_GLOBALS_CALLED=1
117 # @FUNCTION: yarn_src_unpack
118 # @DESCRIPTION:
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.
122 yarn_src_unpack() {
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
132 local f df
133 for f in ${A}; do
134 if [[ -n ${_YARN_RESOLVED_MAP["${f}"]} ]]; then
135 df="$(echo ${f//:2F//} | sed -r -e 's#(@([^@/]+))?\/?([^@/]+)\/\-\/([^/]+).tgz#yarn-\1-\4.tgz#g')"
136 df="${df#yarn--}"
137 df="${df#yarn-}"
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
141 else
142 unpack "${f}"
144 done
145 else
146 default
148 #set yarn-offline-mirror
149 if [[ -e "${YARN_WORKDIR}" ]]; then
150 echo "yarn-offline-mirror \"${_YARN_OFFLINE_MIRROR}\"" >> "${YARN_WORKDIR}/.yarnrc" || die
151 else
152 die "the yarn workdir ${YARN_WORKDIR} does not exist"
156 # @FUNCTION: yarn_set_offline_mirror
157 # @DESCRIPTION:
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
171 # soft link targets
172 local f df
173 for f in ${A}; do
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')"
177 df="${df#yarn--}"
178 df="${df#yarn-}"
179 ln -s "${DISTDIR}/${f}" "${_YARN_OFFLINE_MIRROR}/${df}" || die
180 continue
182 if [[ -n ${_YARN_RESOLVED_MAP_3RDPARTY["${f}"]} ]]; then
183 ln -s "${DISTDIR}/${f}" "${_YARN_OFFLINE_MIRROR}/${f}" || die
185 done
186 # set yarn-offline-mirror
187 if [[ -e "${YARN_WORKDIR}" ]]; then
188 echo "yarn-offline-mirror \"${_YARN_OFFLINE_MIRROR}\"" >> "${YARN_WORKDIR}/.yarnrc" || die
189 else
190 die "the yarn workdir ${YARN_WORKDIR} does not exist"
194 # @FUNCTION: yarn_src_prepare
195 # @DESCRIPTION:
196 # General function for preparing source files with yarn.
197 yarn_src_prepare(){
198 default
199 yarn_offline_install
202 # @FUNCTION: yarn_offline_install
203 # @DESCRIPTION:
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
214 _YARN_ECLASS=1