build-lin.sh 7.45 KB
Newer Older
Stéphane Veyret's avatar
Stéphane Veyret committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

if [[ -z "${1}" ]]; then
	echo "Fatal: no version given to build script"
	exit 1
fi
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
	source "$NVM_DIR/nvm.sh"
else
	echo "Fatal: could not load nvm"
	exit 1
fi

14
15
16
17
18
19
20
21
22
23
# ---------
# Functions
# ---------

# Copy nw.js compiled module released library to node libraries.
# -
# Parameters:
# 1. Module name.
nw_copy() {
	[[ -z ${1} ]] && exit 1
24
25
26
27
28
29
30
31
	from_folder=lib/binding/Release/node-webkit-v${NW_VERSION}-linux-x64
	if [[ ! -z ${2} ]]; then
	  from_folder=${2}
	fi
	local dest=lib/binding/Release/node-v${ADDON_VERSION}-linux-x64
	mkdir -p ${dest}
	cp ${from_folder}/${1}.node \
		${dest}/${1}.node || exit 1
32
33
34
35
36
37
38
39
}

# Copy nw.js compiled module library to node libraries, prefixing with node_.
# -
# Parameters:
# 1. Module name.
nw_copy_node() {
	[[ -z ${1} ]] && exit 1
40
41
	local dest=lib/binding/node-v${ADDON_VERSION}-linux-x64/
	mkdir -p ${dest}
42
	cp lib/binding/node-webkit-v${NW_VERSION}-linux-x64/node_${1}.node \
43
		${dest}/node_${1}.node || exit 1
44
45
46
47
48
49
50
51
52
53
54
55
}

# Compile the module with nw.js.
# -
# Parameters:
# 1. Module name.
# 2. Action to be done to module after compilation, if needed.
nw_compile() {
	[[ -z ${1} ]] && exit 1
	cd ${1} || exit 1
	node-pre-gyp --runtime=node-webkit --target=${NW_VERSION} configure || exit 1
	node-pre-gyp --runtime=node-webkit --target=${NW_VERSION} build || exit 1
56
	[[ -z ${2} ]] || ${2} ${1} ${3}
57
58
59
60
61
62
63
64
65
66
67
68
69
	cd ..
}

# Create description.
# -
# Parameters:
# 1. Initial file name.
# 2. Building type (either “desktop” or “server”).
# 3. Category (OS, distribution).
create_desc() {
	cat >"${1}".desc <<-EOF
	{
	  "version": "${DUNITER_TAG}",
Stéphane Veyret's avatar
Stéphane Veyret committed
70
	  "job": "${CI_JOB_ID}",
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	  "type": "${2^}",
	  "category": "${3}",
	  "arch": "x64"
	}
	EOF
}

# Desktop specific building phase.
# -
# Parameters:
# 1. Building directory.
build_extra_desktop() {
	cp -r "${ROOT}/release/extra/desktop/"* "${1}" || exit 1
}

# Server specific building phase.
# -
# Parameters:
# 1. Building directory.
build_extra_server() {
	mkdir -p "${1}/lib/systemd/system" || exit 1
	cp "${ROOT}/release/extra/systemd/duniter.service" "${1}/lib/systemd/system" || exit 1
}

# Debian package building.
# -
# Parameters:
# 1. Building type (either “desktop” or “server”).
# 2. Debian package name.
build_deb_pack() {
	rm -rf "${RELEASES}/duniter-x64"
	mkdir "${RELEASES}/duniter-x64" || exit 1
	cp -r "${ROOT}/release/extra/debian/package/"* "${RELEASES}/duniter-x64" || exit 1
	build_extra_${1} "${RELEASES}/duniter-x64"
	mkdir -p "${RELEASES}/duniter-x64/opt/duniter/" || exit 1
	chmod 755 "${RELEASES}/duniter-x64/DEBIAN/"post* || exit 1
	chmod 755 "${RELEASES}/duniter-x64/DEBIAN/"pre* || exit 1
	sed -i "s/Version:.*/Version:${DUNITER_DEB_VER}/g" "${RELEASES}/duniter-x64/DEBIAN/control" || exit 1

	cd "${RELEASES}/${1}_/"
	zip -qr "${RELEASES}/duniter-x64/opt/duniter/duniter.zip" * || exit 1

	sed -i "s/Package: .*/Package: ${2}/g" "${RELEASES}/duniter-x64/DEBIAN/control" || exit 1

	cd "${RELEASES}"
	fakeroot dpkg-deb --build duniter-x64 || exit 1
	mv duniter-x64.deb "${BIN}/duniter-${1}-${DUNITER_TAG}-linux-x64.deb" || exit 1
	create_desc "${BIN}/duniter-${1}-${DUNITER_TAG}-linux-x64.deb" "${1}" "Linux (Ubuntu/Debian)"
}

Stéphane Veyret's avatar
Stéphane Veyret committed
121
122
123
124
# -----------
# Prepare
# -----------

125
NODE_VERSION=9.4.0
Stéphane Veyret's avatar
Stéphane Veyret committed
126
127
128
NVER="v${NODE_VERSION}"
DUNITER_TAG="v${1}"
DUNITER_DEB_VER=" ${1}"
129
130
ADDON_VERSION=59
NW_VERSION=0.28.0
Stéphane Veyret's avatar
Stéphane Veyret committed
131
132
133
NW_RELEASE="v${NW_VERSION}"
NW="nwjs-${NW_RELEASE}-linux-x64"
NW_GZ="${NW}.tar.gz"
134
DUNITER_UI_VER="1.7.x"
Stéphane Veyret's avatar
Stéphane Veyret committed
135
136
137

nvm install ${NVER} || exit 1
nvm use ${NVER} || exit 1
138
139
npm install -g node-pre-gyp || exit 1
npm install -g nw-gyp || exit 1
Stéphane Veyret's avatar
Stéphane Veyret committed
140
141
142
143
144
145
146
147
148
149
150
151
152

# -----------
# Folders
# -----------

ROOT="${PWD}"
WORK_NAME=work
WORK="${ROOT}/${WORK_NAME}"
DOWNLOADS="${WORK}/downloads"
RELEASES="${WORK}/releases"
BIN="${WORK}/bin"

mkdir -p "${DOWNLOADS}" "${RELEASES}" "${BIN}" || exit 1
153
rm -rf "${BIN}/"*.{deb,tar.gz}{,.desc} # Clean up
Stéphane Veyret's avatar
Stéphane Veyret committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

# -----------
# Downloads
# -----------

cd "${DOWNLOADS}"
curl -O https://dl.nwjs.io/${NW_RELEASE}/${NW_GZ} || exit 1
tar xzf ${NW_GZ} || exit 1
rm ${NW_GZ}
curl -O http://nodejs.org/dist/${NVER}/node-${NVER}-linux-x64.tar.gz || exit 1
tar xzf node-${NVER}-linux-x64.tar.gz || exit 1
rm node-${NVER}-linux-x64.tar.gz

# -----------
# Releases
# -----------

# Prepare sources
mkdir -p "${RELEASES}/duniter" || exit 1
cp -r $(find "${ROOT}" -mindepth 1 -maxdepth 1 ! -name "${WORK_NAME}") "${RELEASES}/duniter" || exit 1
cd "${RELEASES}/duniter"
rm -Rf .gitignore .git || exit 1 # Remove git files

# Build
echo ">> VM: building modules..."
179
yarn || exit 1
Stéphane Veyret's avatar
Stéphane Veyret committed
180

181
182
183
# Patch leveldown
cp $"${ROOT}/release/resources/leveldown-fix.json" "${RELEASES}/duniter/node_modules/leveldown/package.json" || exit 1

Stéphane Veyret's avatar
Stéphane Veyret committed
184
# Duniter UI
185
yarn add "duniter-ui@${DUNITER_UI_VER}" || exit 1
Cédric Moreau's avatar
Cédric Moreau committed
186
yarn --production || exit 1
Stéphane Veyret's avatar
Stéphane Veyret committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

rm -rf release coverage test # Non production folders
cp -r "${RELEASES}/duniter" "${RELEASES}/desktop_" || exit 1
cp -r "${RELEASES}/duniter" "${RELEASES}/server_" || exit 1

# -------------------------------------
# Build Desktop version against nw.js
# -------------------------------------

echo "${NW_RELEASE}"

# FIX: bug of nw.js, we need to patch first.
# TODO: remove this patch once a correct version of Nw.js is out (NodeJS 8 or 9 if the above modules are compliant)
cd "${RELEASES}/desktop_/node_modules/wotb"
node-pre-gyp --runtime=node-webkit --target=$NW_VERSION configure \
  || echo "This failure is expected"

cd "${RELEASES}/desktop_/node_modules/"
nw_compile wotb nw_copy
nw_compile naclb nw_copy
207
nw_compile leveldown nw_copy "build/Release/"
Stéphane Veyret's avatar
Stéphane Veyret committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
nw_compile scryptb nw_copy
nw_compile sqlite3 nw_copy_node

# Unused binaries
cd "${RELEASES}/desktop_/"
rm -rf node_modules/sqlite3/build

# --------------------------------
# Embed nw.js in desktop version
# --------------------------------

# Install Nw.js
mkdir -p "${RELEASES}/desktop_release" || exit 1
cp -r "${DOWNLOADS}/${NW}/"* "${RELEASES}/desktop_release/" || exit 1
# Embed Node.js with Nw.js to make Duniter modules installable
cp -r "${DOWNLOADS}/node-${NVER}-linux-x64/lib" "${RELEASES}/desktop_release/" || exit 1
cp -r "${DOWNLOADS}/node-${NVER}-linux-x64/include" "${RELEASES}/desktop_release/" || exit 1
cp -r "${DOWNLOADS}/node-${NVER}-linux-x64/bin" "${RELEASES}/desktop_release/" || exit 1
# Add some specific files for GUI
cp "${RELEASES}/desktop_/gui/"* "${RELEASES}/desktop_release/" || exit 1
# Add Duniter sources
cp -R "${RELEASES}/desktop_/"* "${RELEASES}/desktop_release/" || exit 1
# Insert Nw specific fields while they do not exist (1.3.3)
sed -i "s/\"main\": \"index.js\",/\"main\": \"index.html\",/" "${RELEASES}/desktop_release/package.json" || exit 1
# Add links for Node.js + NPM
cd "${RELEASES}/desktop_release/bin"
ln -s "../lib/node_modules/npm/bin/npm-cli.js" "./npm" -f || exit 1
cd ..
ln -s "./bin/node" "node" -f || exit 1
ln -s "./bin/npm" "npm" -f || exit 1
#sed -i "s/\"node-main\": \"\.\.\/sources\/bin\/duniter\",/\"node-main\": \".\/bin\/duniter\",/" "$RELEASES/desktop_release/package.json"
rm -rf "${RELEASES}/desktop_"
mv "${RELEASES}/desktop_release" "${RELEASES}/desktop_"

# ---------------------------------
# Embed node.js in server version
# ---------------------------------

cp -r "${DOWNLOADS}/node-${NVER}-linux-x64" "${RELEASES}/server_/node" || exit 1

# ---------------
# Build .tar.gz
# ---------------

cd "${RELEASES}/desktop_"
tar czf "${BIN}/duniter-desktop-${DUNITER_TAG}-linux-x64.tar.gz" * || exit 1
254
create_desc "${BIN}/duniter-desktop-${DUNITER_TAG}-linux-x64.tar.gz" "Desktop" "Linux (generic)"
Stéphane Veyret's avatar
Stéphane Veyret committed
255
256
257
258
259
260
261

# -----------------------
# Build Debian packages
# -----------------------

build_deb_pack desktop duniter-desktop
build_deb_pack server duniter