Skip to content

Add extensions support for vscode-web #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions vscode-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags: [helper, ide, vscode, web]

# VS Code Web

Automatically install [Visual Studio Code Server](https://code.visualstudio.com/docs/remote/vscode-server) in a workspace using the [VS Code CLI](https://code.visualstudio.com/docs/editor/command-line) and create an app to access it via the dashboard.
Automatically install [Visual Studio Code Server](https://code.visualstudio.com/docs/remote/vscode-server) in a workspace and create an app to access it via the dashboard.

```tf
module "vscode-web" {
Expand All @@ -31,8 +31,20 @@ module "vscode-web" {
source = "registry.coder.com/modules/vscode-web/coder"
version = "1.0.3"
agent_id = coder_agent.example.id
install_dir = "/home/coder/.vscode-web"
install_prefix = "/home/coder/.vscode-web"
folder = "/home/coder"
accept_license = true
}
```

### Install Extensions

```tf
module "vscode-web" {
source = "registry.coder.com/modules/vscode-web/coder"
version = "1.0.2"
agent_id = coder_agent.example.id
extensions = ["github.copilot", "ms-python.python", "ms-toolsai.jupyter"]
accept_license = true
}
```
63 changes: 0 additions & 63 deletions vscode-web/main.test.ts

This file was deleted.

28 changes: 23 additions & 5 deletions vscode-web/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,48 @@ variable "log_path" {
default = "/tmp/vscode-web.log"
}

variable "install_dir" {
variable "install_prefix" {
type = string
description = "The directory to install VS Code CLI"
default = "/tmp/vscode-cli"
description = "The prefix to install vscode-web to."
default = "/tmp/vscode-web"
}

variable "extensions" {
type = list(string)
description = "A list of extensions to install."
default = []
}

variable "accept_license" {
type = bool
description = "Accept the VS Code license. https://code.visualstudio.com/license"
description = "Accept the VS Code Server license. https://code.visualstudio.com/license/server"
default = false
validation {
condition = var.accept_license == true
error_message = "You must accept the VS Code license agreement by setting accept_license=true."
}
}

variable "telemetry_level" {
type = string
description = "Set the telemetry level for VS Code Web."
default = "error"
validation {
condition = var.telemetry_level == "off" || var.telemetry_level == "crash" || var.telemetry_level == "error" || var.telemetry_level == "all"
error_message = "Incorrect value. Please set either 'off', 'crash', 'error', or 'all'."
}
}

resource "coder_script" "vscode-web" {
agent_id = var.agent_id
display_name = "VS Code Web"
icon = "/icon/code.svg"
script = templatefile("${path.module}/run.sh", {
PORT : var.port,
LOG_PATH : var.log_path,
INSTALL_DIR : var.install_dir,
INSTALL_PREFIX : var.install_prefix,
EXTENSIONS : join(",", var.extensions),
TELEMETRY_LEVEL : var.telemetry_level,
})
run_on_start = true
}
Expand Down
48 changes: 38 additions & 10 deletions vscode-web/run.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
#!/usr/bin/env sh
#!/usr/bin/env bash

BOLD='\033[0;1m'
EXTENSIONS=("${EXTENSIONS}")

# Create install directory if it doesn't exist
mkdir -p ${INSTALL_DIR}
# Create install prefix
mkdir -p ${INSTALL_PREFIX}

printf "$${BOLD}Installing vscode-cli!\n"
printf "$${BOLD}Installing Microsoft Visual Studio Code Server!\n"

# Download and extract code-cli tarball
output=$(curl -Lk 'https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64' --output vscode_cli.tar.gz && tar -xf vscode_cli.tar.gz -C ${INSTALL_DIR} && rm vscode_cli.tar.gz)
# Download and extract vscode-server
ARCH=$(uname -m)
case "$ARCH" in
x86_64) ARCH="x64" ;;
aarch64) ARCH="arm64" ;;
*)
echo "Unsupported architecture"
exit 1
;;
esac

HASH=$(curl https://update.code.visualstudio.com/api/commits/stable/server-linux-$ARCH-web | cut -d '"' -f 2)
output=$(curl -sL https://vscode.download.prss.microsoft.com/dbazure/download/stable/$HASH/vscode-server-linux-$ARCH-web.tar.gz | tar -xz -C ${INSTALL_PREFIX} --strip-components 1)

if [ $? -ne 0 ]; then
echo "Failed to install vscode-cli: $output"
echo "Failed to install Microsoft Visual Studio Code Server: $output"
exit 1
fi
printf "🥳 vscode-cli has been installed.\n\n"
printf "$${BOLD}Microsoft Visual Studio Code Server has been installed.\n"

VSCODE_SERVER="${INSTALL_PREFIX}/bin/code-server"

# Install each extension...
IFS=',' read -r -a EXTENSIONLIST <<< "$${EXTENSIONS}"
for extension in "$${EXTENSIONLIST[@]}"; do
if [ -z "$extension" ]; then
continue
fi
printf "🧩 Installing extension $${CODE}$extension$${RESET}...\n"
output=$($VSCODE_SERVER --install-extension "$extension" --force)
if [ $? -ne 0 ]; then
echo "Failed to install extension: $extension: $output"
exit 1
fi
Comment on lines +34 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given:

EXTENSIONS="foo,bar,baz"

I would suggest the following posix way, so we don't need to rely on bash/arrays.

Suggested change
IFS=',' read -r -a EXTENSIONLIST <<< "$${EXTENSIONS}"
for extension in "$${EXTENSIONLIST[@]}"; do
if [ -z "$extension" ]; then
continue
fi
printf "🧩 Installing extension $${CODE}$extension$${RESET}...\n"
output=$($VSCODE_SERVER --install-extension "$extension" --force)
if [ $? -ne 0 ]; then
echo "Failed to install extension: $extension: $output"
exit 1
fi
extension=
while [ "$${extension}" != "$${EXTENSIONS}" ]; do
extension=$${EXTENSIONS%%,*}
EXTENSIONS=$${EXTENSIONS#*,}
if [ -z "$${extension}" ]; then
continue
fi
printf "🧩 Installing extension $${CODE}$extension$${RESET}...\n"
output=$("$${VSCODE_SERVER}" --install-extension "$${extension}" --force)
if [ $? -ne 0 ]; then
echo "Failed to install extension: $${extension}: $${output}"
exit 1
fi

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is borrowed from code-server, and I did not try to improve it as code-server is our top downloaded module and well tested. But I agree and we can update.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafredri, any second thoughts? Otherwise I should also update code-server module.

done

echo "👷 Running ${INSTALL_DIR}/bin/code serve-web --port ${PORT} --without-connection-token --accept-server-license-terms in the background..."
echo "👷 Running ${INSTALL_PREFIX}/bin/code-server serve-local --port ${PORT} --accept-server-license-terms serve-local --without-connection-token --telemetry-level ${TELEMETRY_LEVEL} in the background..."
echo "Check logs at ${LOG_PATH}!"
${INSTALL_DIR}/code serve-web --port ${PORT} --without-connection-token --accept-server-license-terms > ${LOG_PATH} 2>&1 &
"${INSTALL_PREFIX}/bin/code-server" serve-local --port "${PORT}" --accept-server-license-terms serve-local --without-connection-token --telemetry-level "${TELEMETRY_LEVEL}" > "${LOG_PATH}" 2>&1 &