Archived
1
0
This repository has been archived on 2024-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
code-server/scripts/tasks.bash

267 lines
8.2 KiB
Bash
Raw Normal View History

2019-07-03 02:10:17 +02:00
#!/bin/bash
set -euox pipefail
2019-07-03 02:10:17 +02: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 02:10:17 +02:00
rm -rf "${serverPath}"
mkdir -p "${serverPath}"
2019-07-12 00:12:52 +02:00
cp -r "${rootPath}/src" "${serverPath}"
cp -r "${rootPath}/typings" "${serverPath}"
cp "${rootPath}/main.js" "${serverPath}"
2019-07-03 02:10:17 +02: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-10 23:29:15 +02:00
# Ignore scripts to avoid also installing VS Code dependencies which has
# already been done.
cd "${serverPath}" && yarn --ignore-scripts
2019-07-03 02:10:17 +02:00
rm -r node_modules/@types/node # I keep getting type conflicts
fi
2019-07-12 00:12:52 +02: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 02:10:17 +02:00
}
2019-07-12 00:12:52 +02:00
# Prepend the nbin shim which enables finding files within the binary.
2019-07-05 18:54:04 +02:00
function prepend-loader() {
local filePath="${buildPath}/${1}" ; shift
2019-07-12 00:12:52 +02:00
cat "${rootPath}/scripts/nbin-shim.js" "${filePath}" > "${filePath}.temp"
2019-07-05 18:54:04 +02: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 00:12:52 +02:00
if [[ "${target}" == "darwin" ]] ; then
sed -i "" -e "s:{{ROOT_PATH}}:${buildPath//:/\\:}:g" "${filePath}"
2019-07-12 00:12:52 +02:00
else
sed -i "s:{{ROOT_PATH}}:${buildPath//:/\\:}:g" "${filePath}"
2019-07-12 00:12:52 +02:00
fi
2019-07-05 18:54:04 +02:00
}
2019-07-03 02:10:17 +02:00
# Copy code-server into VS Code then build it.
function build-code-server() {
copy-server
local min=""
export BUILD_SOURCEVERSION
BUILD_SOURCEVERSION=$(node -p "require('${sourcePath}/build/lib/git.js').getVersion('${rootPath}')")
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 02:10:17 +02:00
rm -rf "${buildPath}"
mkdir -p "${buildPath}"
2019-07-03 02:10:17 +02: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 02:10:17 +02:00
2019-08-07 23:18:17 +02:00
local packageJson="{\"codeServerVersion\": \"${codeServerVersion}-vsc${vscodeVersion}\"}"
cp -r "${sourcePath}/.build/extensions" "${buildPath}"
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"
cp -r "${sourcePath}/out-vscode${min}" "${buildPath}/out"
2019-07-11 01:10:39 +02: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 00:12:52 +02:00
2019-07-05 18:54:04 +02:00
prepend-loader "out/vs/server/main.js"
prepend-loader "out/bootstrap-fork.js"
log "Final build: ${buildPath}"
2019-07-03 02:10:17 +02: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 02:10:17 +02:00
if command -v wget &> /dev/null ; then
wget "${url}" --quiet -O - | tar -C "${stagingPath}" -xz
2019-07-03 02:10:17 +02:00
else
curl "${url}" --silent --fail | tar -C "${stagingPath}" -xz
2019-07-03 02:10:17 +02: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 02:10:17 +02:00
exit 1
fi
return 1
2019-07-03 02:10:17 +02:00
fi
return 0
2019-07-03 02:10:17 +02:00
}
# Fully build code-server.
2019-07-03 02:10:17 +02: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
yarn gulp extensions-build-package --max-old-space-size=32384
fi
2019-07-03 02:10:17 +02:00
build-code-server
}
# Package the binary into a tar or zip for release.
2019-07-03 02:10:17 +02:00
function package-task() {
2019-07-11 01:10:39 +02:00
local archivePath="${releasePath}/${binaryName}"
2019-07-03 02:10:17 +02:00
rm -rf "${archivePath}"
mkdir -p "${archivePath}"
2019-07-10 23:29:15 +02:00
cp "${buildPath}/${binaryName}" "${archivePath}/code-server"
2019-07-03 02:10:17 +02:00
cp "${rootPath}/README.md" "${archivePath}"
cp "${sourcePath}/LICENSE.txt" "${archivePath}"
cp "${sourcePath}/ThirdPartyNotices.txt" "${archivePath}"
2019-07-03 02:10:17 +02:00
cd "${releasePath}"
2019-07-12 00:12:52 +02:00
if [[ "${target}" == "darwin" ]] ; then
2019-07-11 01:10:39 +02:00
zip -r "${binaryName}.zip" "${binaryName}"
2019-07-08 17:33:58 +02:00
log "Archive: ${archivePath}.zip"
2019-07-12 00:12:52 +02:00
else
tar -czf "${binaryName}.tar.gz" "${binaryName}"
log "Archive: ${archivePath}.tar.gz"
2019-07-03 02:10:17 +02:00
fi
}
# Bundle built code into a binary.
2019-07-03 02:10:17 +02: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 02:10:17 +02:00
rm node_modules/@coder/nbin
log "Binary: ${buildPath}/${binaryName}"
2019-07-03 02:10:17 +02:00
}
2019-07-12 00:12:52 +02:00
# Check if it looks like we are inside VS Code.
function in-vscode () {
local dir="${1}" ; shift
local maybeVsCode
2019-07-12 00:12:52 +02:00
local dirName
maybeVsCode="$(realpath "${dir}/../../..")"
dirName="$(basename "${maybeVsCode}")"
2019-07-12 00:12:52 +02:00
if [[ "${dirName}" != "vscode" ]] ; then
return 1
fi
if [[ ! -f "${maybeVsCode}/package.json" ]] ; then
2019-07-12 00:12:52 +02:00
return 1
fi
if ! grep '"name": "code-oss-dev"' "${maybeVsCode}/package.json" --quiet ; then
2019-07-12 00:12:52 +02:00
return 1
fi
return 0
}
2019-07-03 02:10:17 +02:00
function main() {
2019-07-12 00:12:52 +02:00
local relativeRootPath
local rootPath
relativeRootPath="$(dirname "${0}")/.."
rootPath="$(realpath "${relativeRootPath}")"
2019-07-03 02:10:17 +02:00
local task="${1}" ; shift
2019-07-12 00:12:52 +02: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 00:12:52 +02:00
fi
2019-07-03 02:10:17 +02: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 02:10:17 +02:00
# If we're inside a VS Code directory, assume we want to develop. In that case
2019-07-10 23:29:15 +02:00
# we should set an OUT directory and not build in this directory.
2019-07-12 00:12:52 +02:00
if in-vscode "${outPath}" ; then
log "Set the OUT environment variable to something outside of VS Code" "error"
exit 1
2019-07-10 23:29:15 +02: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 02:10:17 +02:00
local codeServerVersion="${1}" ; shift
local ci="${CI:-}"
local minify="${MINIFY:-}"
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
2019-07-03 02:10:17 +02:00
fi
local binaryName="code-server${codeServerVersion}-vsc${vscodeVersion}-${target}-${arch}"
local buildPath="${stagingPath}/${binaryName}-built"
2019-07-03 02:10:17 +02:00
"${task}-task" "$@"
}
main "$@"