View | Details | Raw Unified | Return to bug 204577 | Differences between
and this patch

Collapse All | Expand All

(-)Mk/Uses/node.mk (+138 lines)
Line 0 Link Here
1
# $FreeBSD$
2
#
3
# Provide support for NodeJS based projects
4
#
5
# Feature:	node
6
# Usage:	USES=node
7
#
8
# MAINTAINER: portmgr@FreeBSD.org
9
10
.if !defined(_INCLUDE_USES_NODE_MK)
11
_INCLUDE_USES_NODE_MK=	yes
12
13
FETCH_DEPENDS=	npm:${PORTSDIR}/www/npm
14
BUILD_DEPENDS=	npm:${PORTSDIR}/www/npm
15
RUN_DEPENDS=	node:${PORTSDIR}/www/node
16
17
.if defined(NODE_MODULES)
18
FETCH_DEPENDS+=	jq:${PORTSDIR}/textproc/jq
19
JQ_CMD=		${LOCALBASE}/bin/jq
20
.endif
21
22
.if defined(NODE_GH_ACCOUNT)
23
.  if ! defined(NODE_GH_PROJECT)
24
NODE_GH_PROJECT=	${PORTNAME}
25
.  endif
26
.  if ! defined(NODE_GH_TAGNAME)
27
NODE_GH_TAGNAME=	${DISTVERSIONFULL}
28
.  endif
29
_NODE_PROJECT_FILE=	${NODE_GH_ACCOUNT}-${NODE_GH_PROJECT}-${NODE_GH_TAGNAME}-node-project.tar.gz
30
_NODE_DIST_FILE=	${NODE_GH_PROJECT}-${NODE_GH_TAGNAME}.tar.gz
31
_NODE_DIR=		${NODE_GH_PROJECT}-${NODE_GH_TAGNAME}
32
_NODE_GH_URL=		https://codeload.github.com/${NODE_GH_ACCOUNT}/${NODE_GH_PROJECT}/tar.gz/${NODE_GH_TAGNAME}?dummy=/${_NODE_PROJECT_FILE}
33
.endif
34
35
# commands
36
NPM_CMD=		${LOCALBASE}/bin/npm
37
NODE_CMD=		${LOCALBASE}/bin/node
38
39
# variables
40
NPM_ENV+=		C=cc CXX=c++
41
NPM_FETCH_ARGS=		--loglevel warn --ignore-scripts
42
NPM_BUILD_ARGS=		--loglevel warn
43
NPM_SHRINKWRAP_ARGS=	--dev
44
SHRINKWRAP_FILE=	${FILESDIR}/npm-shrinkwrap.json
45
NODE_INSTALL_BASE=	${LOCALBASE}/share/node-projects
46
NODE_INSTALL_DIR=	${NODE_INSTALL_BASE}/${NODE_GH_ACCOUNT}-${NODE_GH_PROJECT}
47
_NODE_MASTER_SITE_BACKUP_URLS= ${MASTER_SITE_BACKUP}${_NODE_DIST_FILE}
48
.if defined(NODE_SRCDIR)
49
_NODE_DO_CD=node_modules/${NODE_SUBDIR}
50
.else
51
_NODE_DO_CD=.
52
.endif
53
54
makeshrinkwrap: do-fetch
55
	@cd ${_DISTDIR}; \
56
		${TAR} xzf ${_NODE_DIST_FILE} && \
57
		cd ${_NODE_DIR} && \
58
		cd ${_NODE_DO_CD} && \
59
		${NPM_ENV} ${NPM_CMD} shrinkwrap ${NPM_SHRINKWRAP_ARGS} && \
60
		${CP} npm-shrinkwrap.json ${SHRINKWRAP_FILE} && \
61
		cd ${_DISTDIR} && \
62
		${RM} -r ${_NODE_DIR}
63
64
do-fetch:
65
	@${MKDIR} ${_DISTDIR}
66
	@cd ${_DISTDIR}; \
67
		force_fetch=false; \
68
		for afile in ${FORCE_FETCH}; do \
69
		  afile=$${afile##*/}; \
70
		  if [ "x$$afile" = "x${_NODE_DIST_FILE}" ]; then \
71
		    force_fetch=true; \
72
		  fi; \
73
		done; \
74
		if [ ! -f ${_NODE_DIST_FILE} -o "$$force_fetch" = "true" ]; then \
75
		        ${RM} -rf ${_NODE_PROJECT_FILE} ${_NODE_DIR} ${_NODE_DIST_FILE}; \
76
		        for url in ${_NODE_MASTER_SITE_BACKUP_URLS}; do \
77
		          if ${ECHO_MSG} "=> Attempting to fetch ${PORTNAME} from the master backup site" && \
78
		             ${SETENV} ${FETCH_ENV} ${FETCH_CMD} ${FETCH_BEFORE_ARGS} $${url} ${FETCH_AFTER_ARGS}; then \
79
		             return 0; \
80
		        fi; \
81
		        done; \
82
		        for url in ${_NODE_GH_URL}; do \
83
		          if ${ECHO_MSG} "=> Attempting to fetch ${PORTNAME} from github" && \
84
		             ${SETENV} ${FETCH_ENV} ${FETCH_CMD} ${FETCH_BEFORE_ARGS} $${url} ${FETCH_AFTER_ARGS} && \
85
		             ${ECHO_MSG} "=> Extracting node project source" && \
86
		             ${EXTRACT_CMD} ${EXTRACT_BEFORE_ARGS} ${_NODE_PROJECT_FILE} ${EXTRACT_AFTER_ARGS} && \
87
		             ${RM} ${_NODE_PROJECT_FILE} && \
88
		             ${ECHO_MSG} "=> Preparing the node project" && \
89
		             cd ${_NODE_DIR} && \
90
		             ([ -z "${NODE_SRCDIR}" ] || \
91
		                (${MKDIR} node_modules && \
92
		                 ${MV} ${NODE_SRCDIR} node_modules/${NODE_SUBDIR} \
93
		                ) \
94
		             ) && cd ${_NODE_DO_CD} && \
95
		             for m in ${NODE_MODULES}; do \
96
		               ${ECHO} $${m} | ${SED} -E "s/([[:alnum:]]+):([[:digit:].]+)/{\"dependencies\":{\"\1\":\"\2\"}}/" > package-delta.json && \
97
		               ${JQ_CMD} -s '.[0] * .[1]' package.json package-delta.json > package-new.json && \
98
		               ${MV} package-new.json package.json && \
99
		               ${RM} package-delta.json; \
100
		             done && \
101
		             ([ -f "${SHRINKWRAP_FILE}" ] || \
102
		              ${ECHO} "Warning: no shrinkwrap file, you need to run 'make makeshrinkwrap' >&2") && \
103
		             ([ ! -f "${SHRINKWRAP_FILE}" ] || \
104
		              ${CP} ${SHRINKWRAP_FILE} npm-shrinkwrap.json) && \
105
		             ${ECHO_MSG} "=> Downloading the project dependencies" && \
106
		             ${NPM_ENV} ${NPM_CMD} install ${NPM_FETCH_ARGS} && \
107
		             cd ${_DISTDIR} && \
108
		             ${FIND} ${_NODE_DIR} -and -exec ${TOUCH} -h -d 1970-01-01T00:00:00Z {} + && \
109
		             ${FIND} ${_NODE_DIR} -print0 | sort -z | \
110
		                  ${TAR} czf ${_NODE_DIST_FILE} --options gzip:!timestamp --no-recursion --null -T - && \
111
		             ${RM} -rf ${_NODE_DIR}; then \
112
		             return 0; \
113
		        fi; \
114
		        done; \
115
		        ${ECHO_MSG} "=> Fetch failed for ${PORTNAME}"; \
116
		        exit 1; \
117
		fi;
118
119
do-build:
120
	@cd ${WRKSRC} && \
121
		cd ${_NODE_DO_CD} && \
122
		${NPM_ENV} ${NPM_CMD} rebuild ${NPM_BUILD_ARGS}
123
124
do-install:
125
	@${ECHO} "=> Staging ${PKGNAME}"
126
	@${MKDIR} ${STAGEDIR}${NODE_INSTALL_BASE}
127
	@${CP} -r ${WRKSRC} ${STAGEDIR}${NODE_INSTALL_DIR}
128
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name Makefile`
129
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name "*.bat"`
130
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name "*.log"`
131
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name "*.orig"`
132
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name "*.bak"`
133
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name ".git*"`
134
	@${RM} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name npm-shrinkwrap.json`
135
	@${STRIP_CMD} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name "*.o"`
136
	@${STRIP_CMD} `${FIND} ${STAGEDIR}${NODE_INSTALL_DIR} -name "*.node"`
137
138
.endif

Return to bug 204577