tasks.bash 8.1 KB
Newer Older
A
Asher 已提交
1
#!/bin/bash
A
Asher 已提交
2
set -euox pipefail
A
Asher 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15

function log() {
	local message="${1}" ; shift
	local level="${1:-info}"
	if [[ "${level}" == "error" ]] ; then
		>&2 echo "${message}"
	else
		echo "${message}"
	fi
}

# Copy code-server into VS Code along with its dependencies.
function copy-server() {
A
Asher 已提交
16
	local serverPath="${sourcePath}/src/vs/server"
A
Asher 已提交
17 18 19
	rm -rf "${serverPath}"
	mkdir -p "${serverPath}"

A
Asher 已提交
20 21 22
	cp -r "${rootPath}/src" "${serverPath}"
	cp -r "${rootPath}/typings" "${serverPath}"
	cp "${rootPath}/main.js" "${serverPath}"
A
Asher 已提交
23 24 25 26 27 28
	cp "${rootPath}/package.json" "${serverPath}"
	cp "${rootPath}/yarn.lock" "${serverPath}"

	if [[ -d "${rootPath}/node_modules" ]] ; then
		cp -r "${rootPath}/node_modules" "${serverPath}"
	else
A
Asher 已提交
29 30
		# Ignore scripts to avoid also installing VS Code dependencies which has
		# already been done.
A
Asher 已提交
31
		cd "${serverPath}" && yarn --ignore-scripts
A
Asher 已提交
32 33
		rm -r node_modules/@types/node # I keep getting type conflicts
	fi
A
Asher 已提交
34 35 36 37 38

	# TODO: Duplicate identifier issue. There must be a better way to fix this.
	if [[ "${target}" == "darwin" ]] ; then
		rm "${serverPath}/node_modules/fsevents/node_modules/safe-buffer/index.d.ts"
	fi
A
Asher 已提交
39 40
}

A
Asher 已提交
41
# Prepend the nbin shim which enables finding files within the binary.
A
Asher 已提交
42
function prepend-loader() {
A
Asher 已提交
43
	local filePath="${buildPath}/${1}" ; shift
A
Asher 已提交
44
	cat "${rootPath}/scripts/nbin-shim.js" "${filePath}" > "${filePath}.temp"
A
Asher 已提交
45 46 47 48
	mv "${filePath}.temp" "${filePath}"
	# Using : as the delimiter so the escaping here is easier to read.
	# ${parameter/pattern/string}, so the pattern is /: (if the pattern starts
	# with / it matches all instances) and the string is \\: (results in \:).
A
Asher 已提交
49
	if [[ "${target}" == "darwin" ]] ; then
A
Asher 已提交
50
		sed -i "" -e "s:{{ROOT_PATH}}:${buildPath//:/\\:}:g" "${filePath}"
A
Asher 已提交
51
	else
A
Asher 已提交
52
		sed -i "s:{{ROOT_PATH}}:${buildPath//:/\\:}:g" "${filePath}"
A
Asher 已提交
53
	fi
A
Asher 已提交
54 55
}

A
Asher 已提交
56 57 58
# Copy code-server into VS Code then build it.
function build-code-server() {
	copy-server
A
Asher 已提交
59 60 61 62 63 64 65
	local min=""
	if [[ -n "${minify}" ]] ; then
		min="-min"
		yarn gulp minify-vscode --max-old-space-size=32384
	else
		yarn gulp optimize-vscode --max-old-space-size=32384
	fi
A
Asher 已提交
66

A
Asher 已提交
67 68
	rm -rf "${buildPath}"
	mkdir -p "${buildPath}"
A
Asher 已提交
69

A
Asher 已提交
70 71 72 73
	# Rebuild to make sure native modules work on the target system.
	cp "${sourcePath}/remote/"{package.json,yarn.lock,.yarnrc} "${buildPath}"
	cd "${buildPath}" && yarn --production --force --build-from-source
	rm "${buildPath}/"{package.json,yarn.lock,.yarnrc}
A
Asher 已提交
74

75
	local packageJson="{\"codeServerVersion\": \"${codeServerVersion}\"}"
A
Asher 已提交
76
	cp -r "${sourcePath}/.build/extensions" "${buildPath}"
77 78
	node "${rootPath}/scripts/merge.js" "${sourcePath}/package.json" "${rootPath}/scripts/package.json" "${buildPath}/package.json" "${packageJson}"
	node "${rootPath}/scripts/merge.js" "${sourcePath}/.build/product.json" "${rootPath}/scripts/product.json" "${buildPath}/product.json"
A
Asher 已提交
79
	cp -r "${sourcePath}/out-vscode${min}" "${buildPath}/out"
A
Asher 已提交
80

A
Asher 已提交
81 82 83 84
	# Only keep production dependencies for the server.
	cp "${rootPath}/"{package.json,yarn.lock} "${buildPath}/out/vs/server"
	cd "${buildPath}/out/vs/server" && yarn --production --ignore-scripts
	rm "${buildPath}/out/vs/server/"{package.json,yarn.lock}
A
Asher 已提交
85

A
Asher 已提交
86 87 88
	prepend-loader "out/vs/server/main.js"
	prepend-loader "out/bootstrap-fork.js"

A
Asher 已提交
89
	log "Final build: ${buildPath}"
A
Asher 已提交
90 91
}

A
Asher 已提交
92 93 94 95
# Download and extract a tar from a URL with either curl or wget depending on
# which is available.
function download-tar() {
	local url="${1}" ; shift
A
Asher 已提交
96
	if command -v wget &> /dev/null ; then
A
Asher 已提交
97
		wget "${url}" --quiet -O - | tar -C "${stagingPath}" -xz
A
Asher 已提交
98
	else
A
Asher 已提交
99
		curl "${url}" --silent --fail | tar -C "${stagingPath}" -xz
A
Asher 已提交
100 101 102
	fi
}

A
Asher 已提交
103 104 105 106 107 108 109 110 111
# Download a pre-built package. If it doesn't exist and we are in the CI, exit.
# Otherwise the return will be whether it existed or not. The pre-built package
# is provided to reduce CI build time.
function download-pre-built() {
	local archiveName="${1}" ; shift
	local url="https://codesrv-ci.cdr.sh/${archiveName}"
	if ! download-tar "${url}" ; then
		if [[ -n "${ci}" ]] ; then
			log "${url} does not exist" "error"
A
Asher 已提交
112 113
			exit 1
		fi
A
Asher 已提交
114
		return 1
A
Asher 已提交
115
	fi
A
Asher 已提交
116
	return 0
A
Asher 已提交
117 118
}

A
Asher 已提交
119
# Fully build code-server.
A
Asher 已提交
120
function build-task() {
A
Asher 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	mkdir -p "${stagingPath}"
	if [[ ! -d "${sourcePath}" ]] ; then
		if ! download-pre-built "vscode-${vscodeVersion}.tar.gz" ; then
			git clone https://github.com/microsoft/vscode  --quiet \
				--branch "${vscodeVersion}" --single-branch --depth=1 \
				"${sourcePath}"
		fi
	fi
	cd "${sourcePath}"
	git reset --hard && git clean -fd
	git apply "${rootPath}/scripts/vscode.patch"
	if [[ ! -d "${sourcePath}/node_modules" ]] ; then
		if [[ -n "${ci}" ]] ; then
			log "Pre-built VS Code ${vscodeVersion} has no node_modules" "error"
			exit 1
		fi
		yarn
	fi
	if [[ ! -d "${sourcePath}/.build/extensions" ]] ; then
		if [[ -n "${ci}" ]] ; then
			log "Pre-built VS Code ${vscodeVersion} has no built extensions" "error"
			exit 1
		fi
		yarn gulp extensions-build-package --max-old-space-size=32384
	fi
A
Asher 已提交
146 147 148
	build-code-server
}

A
Asher 已提交
149
# Package the binary into a tar or zip for release.
A
Asher 已提交
150
function package-task() {
A
Asher 已提交
151
	local archivePath="${releasePath}/${binaryName}"
A
Asher 已提交
152 153 154
	rm -rf "${archivePath}"
	mkdir -p "${archivePath}"

A
Asher 已提交
155
	cp "${buildPath}/${binaryName}" "${archivePath}/code-server"
A
Asher 已提交
156
	cp "${rootPath}/README.md" "${archivePath}"
A
Asher 已提交
157 158
	cp "${sourcePath}/LICENSE.txt" "${archivePath}"
	cp "${sourcePath}/ThirdPartyNotices.txt" "${archivePath}"
A
Asher 已提交
159 160

	cd "${releasePath}"
A
Asher 已提交
161
	if [[ "${target}" == "darwin" ]] ; then
A
Asher 已提交
162
		zip -r "${binaryName}.zip" "${binaryName}"
A
Asher 已提交
163
		log "Archive: ${archivePath}.zip"
A
Asher 已提交
164 165 166
	else
		tar -czf "${binaryName}.tar.gz" "${binaryName}"
		log "Archive: ${archivePath}.tar.gz"
A
Asher 已提交
167 168 169
	fi
}

A
Asher 已提交
170
# Bundle built code into a binary.
A
Asher 已提交
171 172 173 174 175 176
function binary-task() {
	# I had trouble getting VS Code to build with the @coder/nbin dependency due
	# to the types it installs (tons of conflicts), so for now it's a global
	# dependency.
	cd "${rootPath}"
	npm link @coder/nbin
A
Asher 已提交
177
	node "${rootPath}/scripts/nbin.js" "${buildPath}" "${target}" "${binaryName}"
A
Asher 已提交
178
	rm node_modules/@coder/nbin
A
Asher 已提交
179
	log "Binary: ${buildPath}/${binaryName}"
A
Asher 已提交
180 181
}

A
Asher 已提交
182 183 184
# Check if it looks like we are inside VS Code.
function in-vscode () {
	local dir="${1}" ; shift
A
Asher 已提交
185
	local maybeVsCode
A
Asher 已提交
186
	local dirName
A
Asher 已提交
187 188
	maybeVsCode="$(realpath "${dir}/../../..")"
	dirName="$(basename "${maybeVsCode}")"
A
Asher 已提交
189 190 191
	if [[ "${dirName}" != "vscode" ]] ; then
		return 1
	fi
A
Asher 已提交
192
	if [[ ! -f "${maybeVsCode}/package.json" ]] ; then
A
Asher 已提交
193 194
		return 1
	fi
A
Asher 已提交
195
	if ! grep '"name": "code-oss-dev"' "${maybeVsCode}/package.json" --quiet ; then
A
Asher 已提交
196 197 198 199 200
		return 1
	fi
	return 0
}

A
Asher 已提交
201
function main() {
A
Asher 已提交
202 203 204 205 206
	local relativeRootPath
	local rootPath
	relativeRootPath="$(dirname "${0}")/.."
	rootPath="$(realpath "${relativeRootPath}")"

A
Asher 已提交
207
	local task="${1}" ; shift
A
Asher 已提交
208
	if [[ "${task}" == "ensure-in-vscode" ]] ; then
A
Asher 已提交
209 210 211 212 213
		if ! in-vscode "${rootPath}"; then
			log "Not in VS Code" "error"
			exit 1
		fi
		exit 0
A
Asher 已提交
214 215
	fi

A
Asher 已提交
216 217 218 219
	# This lets you build in a separate directory since building within this
	# directory while developing makes it hard to keep developing since compiling
	# will compile everything in the build directory as well.
	local outPath="${OUT:-${rootPath}}"
A
Asher 已提交
220 221
	local releasePath="${outPath}/release"
	local stagingPath="${outPath}/build"
A
Asher 已提交
222

A
Asher 已提交
223
	# If we're inside a VS Code directory, assume we want to develop. In that case
A
Asher 已提交
224
	# we should set an OUT directory and not build in this directory.
A
Asher 已提交
225 226 227
	if in-vscode "${outPath}" ; then
		log "Set the OUT environment variable to something outside of VS Code" "error"
		exit 1
A
Asher 已提交
228 229
	fi

A
Asher 已提交
230 231 232 233 234 235 236 237 238 239 240 241
	local vscodeVersion="${1}" ; shift
	local sourceName="vscode-${vscodeVersion}-source"
	local sourcePath="${stagingPath}/${sourceName}"

	if [[ "${task}" == "package-prebuilt" ]] ; then
		local archiveName="vscode-${vscodeVersion}.tar.gz"
		cd "${stagingPath}"
		git reset --hard && git clean -xfd -e '.build/extensions' -e 'node_modules'
		tar -czf "${archiveName}" "${sourceName}"
		mkdir -p "${releasePath}" && mv -f "${archiveName}" "${releasePath}"
		exit 0
	fi
A
Asher 已提交
242

A
Asher 已提交
243
	local codeServerVersion="${1}" ; shift
A
Asher 已提交
244 245
	local ci="${CI:-}"
	local minify="${MINIFY:-}"
A
Asher 已提交
246 247 248 249 250 251 252 253 254 255
	local arch
	arch=$(uname -m)
	local target="${1:-}"
	if [[ -z "${target}" ]] ; then
		local ostype="${OSTYPE:-}"
		if [[ "${ostype}" == "darwin"* ]] ; then
			target="darwin"
		else
			target="linux"
		fi
A
Asher 已提交
256
	fi
A
Asher 已提交
257 258 259
	local binaryName="code-server${codeServerVersion}-vsc${vscodeVersion}-${target}-${arch}"
	local buildPath="${stagingPath}/${binaryName}-built"

A
Asher 已提交
260 261 262 263
	"${task}-task" "$@"
}

main "$@"