code-server/scripts/tasks.bash

282 lines
8.6 KiB
Bash
Raw Normal View History

2019-07-03 08:10:17 +08:00
#!/bin/bash
set -euox pipefail
2019-07-03 08:10:17 +08:00
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() {
local serverPath="${sourcePath}/src/vs/server"
2019-07-03 08:10:17 +08:00
rm -rf "${serverPath}"
mkdir -p "${serverPath}"
2019-07-12 06:12:52 +08:00
cp -r "${rootPath}/src" "${serverPath}"
cp -r "${rootPath}/typings" "${serverPath}"
cp "${rootPath}/main.js" "${serverPath}"
2019-07-03 08:10:17 +08:00
cp "${rootPath}/package.json" "${serverPath}"
cp "${rootPath}/yarn.lock" "${serverPath}"
if [[ -d "${rootPath}/node_modules" ]] ; then
cp -r "${rootPath}/node_modules" "${serverPath}"
else
2019-07-11 05:29:15 +08:00
# Ignore scripts to avoid also installing VS Code dependencies which has
# already been done.
cd "${serverPath}" && yarn --ignore-scripts
2019-07-03 08:10:17 +08:00
rm -r node_modules/@types/node # I keep getting type conflicts
fi
2019-07-12 06:12:52 +08:00
# 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
2019-07-03 08:10:17 +08:00
}
2019-07-12 06:12:52 +08:00
# Prepend the nbin shim which enables finding files within the binary.
2019-07-06 00:54:04 +08:00
function prepend-loader() {
local filePath="${buildPath}/${1}" ; shift
2019-07-12 06:12:52 +08:00
cat "${rootPath}/scripts/nbin-shim.js" "${filePath}" > "${filePath}.temp"
2019-07-06 00:54:04 +08:00
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 \:).
2019-07-12 06:12:52 +08:00
if [[ "${target}" == "darwin" ]] ; then
sed -i "" -e "s:{{ROOT_PATH}}:${buildPath//:/\\:}:g" "${filePath}"
2019-07-12 06:12:52 +08:00
else
sed -i "s:{{ROOT_PATH}}:${buildPath//:/\\:}:g" "${filePath}"
2019-07-12 06:12:52 +08:00
fi
2019-07-06 00:54:04 +08:00
}
2019-07-03 08:10:17 +08:00
# Copy code-server into VS Code then build it.
function build-code-server() {
copy-server
cd "${sourcePath}" && yarn gulp compile-build --max-old-space-size=32384
2019-08-10 07:50:05 +08:00
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
2019-07-03 08:10:17 +08:00
rm -rf "${buildPath}"
mkdir -p "${buildPath}"
2019-07-03 08:10:17 +08:00
# 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}
2019-07-03 08:10:17 +08:00
cp -r "${sourcePath}/.build/extensions" "${buildPath}"
cp -r "${sourcePath}/out-vscode${min}" "${buildPath}/out"
2019-08-10 07:50:05 +08:00
node "${rootPath}/scripts/build-json.js" "${sourcePath}" "${buildPath}" "${vscodeVersion}" "${codeServerVersion}"
2019-07-11 07:10:39 +08:00
# 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}
2019-07-12 06:12:52 +08:00
2019-08-15 05:20:17 +08:00
# onigasm 2.2.2 has a bug that makes it broken for PHP files so use 2.2.1.
# https://github.com/NeekSandhu/onigasm/issues/17
local onigasmPath="${buildPath}/node_modules/onigasm-umd"
rm -rf "${onigasmPath}"
git clone "https://github.com/alexandrudima/onigasm-umd" "${onigasmPath}"
cd "${onigasmPath}" && yarn && yarn add --dev onigasm@2.2.1 && yarn package
mkdir "${onigasmPath}-temp"
mv "${onigasmPath}/"{release,LICENSE} "${onigasmPath}-temp"
rm -rf "${onigasmPath}"
mv "${onigasmPath}-temp" "${onigasmPath}"
2019-07-06 00:54:04 +08:00
prepend-loader "out/vs/server/main.js"
prepend-loader "out/bootstrap-fork.js"
prepend-loader "extensions/node_modules/typescript/lib/tsserver.js"
2019-07-06 00:54:04 +08:00
log "Final build: ${buildPath}"
2019-07-03 08:10:17 +08:00
}
# 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
2019-07-03 08:10:17 +08:00
if command -v wget &> /dev/null ; then
wget "${url}" --quiet -O - | tar -C "${stagingPath}" -xz
2019-07-03 08:10:17 +08:00
else
curl "${url}" --silent --fail | tar -C "${stagingPath}" -xz
2019-07-03 08:10:17 +08:00
fi
}
# 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"
2019-07-03 08:10:17 +08:00
exit 1
fi
return 1
2019-07-03 08:10:17 +08:00
fi
return 0
2019-07-03 08:10:17 +08:00
}
# Fully build code-server.
2019-07-03 08:10:17 +08:00
function build-task() {
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
2019-08-10 07:50:05 +08:00
yarn gulp compile-extensions-build --max-old-space-size=32384
fi
2019-07-03 08:10:17 +08:00
build-code-server
}
# Package the binary into a tar or zip for release.
2019-07-03 08:10:17 +08:00
function package-task() {
2019-07-11 07:10:39 +08:00
local archivePath="${releasePath}/${binaryName}"
2019-07-03 08:10:17 +08:00
rm -rf "${archivePath}"
mkdir -p "${archivePath}"
2019-07-11 05:29:15 +08:00
cp "${buildPath}/${binaryName}" "${archivePath}/code-server"
2019-07-03 08:10:17 +08:00
cp "${rootPath}/README.md" "${archivePath}"
cp "${sourcePath}/LICENSE.txt" "${archivePath}"
cp "${sourcePath}/ThirdPartyNotices.txt" "${archivePath}"
2019-07-03 08:10:17 +08:00
cd "${releasePath}"
2019-07-12 06:12:52 +08:00
if [[ "${target}" == "darwin" ]] ; then
2019-07-11 07:10:39 +08:00
zip -r "${binaryName}.zip" "${binaryName}"
2019-07-08 23:33:58 +08:00
log "Archive: ${archivePath}.zip"
2019-07-12 06:12:52 +08:00
else
tar -czf "${binaryName}.tar.gz" "${binaryName}"
log "Archive: ${archivePath}.tar.gz"
2019-07-03 08:10:17 +08:00
fi
}
# Bundle built code into a binary.
2019-07-03 08:10:17 +08:00
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
node "${rootPath}/scripts/nbin.js" "${buildPath}" "${target}" "${binaryName}"
2019-07-03 08:10:17 +08:00
rm node_modules/@coder/nbin
log "Binary: ${buildPath}/${binaryName}"
2019-07-03 08:10:17 +08:00
}
2019-07-12 06:12:52 +08:00
# Check if it looks like we are inside VS Code.
function in-vscode () {
local dir="${1}" ; shift
local maybeVsCode
2019-07-12 06:12:52 +08:00
local dirName
maybeVsCode="$(cd "${dir}/../../.." ; pwd -P)"
dirName="$(basename "${maybeVsCode}")"
2019-07-12 06:12:52 +08:00
if [[ "${dirName}" != "vscode" ]] ; then
return 1
fi
if [[ ! -f "${maybeVsCode}/package.json" ]] ; then
2019-07-12 06:12:52 +08:00
return 1
fi
if ! grep '"name": "code-oss-dev"' "${maybeVsCode}/package.json" -q ; then
2019-07-12 06:12:52 +08:00
return 1
fi
return 0
}
2019-07-03 08:10:17 +08:00
function main() {
2019-07-12 06:12:52 +08:00
local rootPath
rootPath="$(cd "$(dirname "${0}/..")" ; pwd -P)"
2019-07-12 06:12:52 +08:00
2019-07-03 08:10:17 +08:00
local task="${1}" ; shift
2019-07-12 06:12:52 +08:00
if [[ "${task}" == "ensure-in-vscode" ]] ; then
if ! in-vscode "${rootPath}"; then
log "Not in VS Code" "error"
exit 1
fi
exit 0
2019-07-12 06:12:52 +08:00
fi
2019-07-03 08:10:17 +08:00
# 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}}"
local releasePath="${outPath}/release"
local stagingPath="${outPath}/build"
2019-07-03 08:10:17 +08:00
# If we're inside a VS Code directory, assume we want to develop. In that case
2019-07-11 05:29:15 +08:00
# we should set an OUT directory and not build in this directory.
2019-07-12 06:12:52 +08:00
if in-vscode "${outPath}" ; then
log "Set the OUT environment variable to something outside of VS Code" "error"
exit 1
2019-07-11 05:29:15 +08:00
fi
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 "${sourcePath}"
git reset --hard && git clean -xfd -e '.build/extensions' -e 'node_modules'
cd "${stagingPath}"
tar -czf "${archiveName}" "${sourceName}"
mkdir -p "${releasePath}" && mv -f "${archiveName}" "${releasePath}"
exit 0
fi
2019-07-03 08:10:17 +08:00
local codeServerVersion="${1}" ; shift
local ci="${CI:-}"
local minify="${MINIFY:-}"
local arch
arch=$(uname -m)
local target="linux"
local ostype="${OSTYPE:-}"
if [[ "${ostype}" == "darwin"* ]] ; then
target="darwin"
else
# On Alpine there seems no way to get the version except to use an invalid
# command which will output the version to stderr and exit with 1.
local output
output=$(ldd --version 2>&1 || :)
if [[ "${output}" == "musl"* ]] ; then
target="alpine"
fi
2019-07-03 08:10:17 +08:00
fi
local binaryName="code-server${codeServerVersion}-vsc${vscodeVersion}-${target}-${arch}"
local buildPath="${stagingPath}/${binaryName}-built"
2019-07-03 08:10:17 +08:00
"${task}-task" "$@"
}
main "$@"