fix(vscode-desktop-core): update extension extraction and download methods, and add module dir with logging paths for troubleshooting

This commit is contained in:
DevelopmentCats 2025-10-17 14:57:34 -05:00
parent e336fa8978
commit c5a76ab005
2 changed files with 138 additions and 66 deletions

View File

@ -117,37 +117,41 @@ describe("vscode-desktop-core extension script logic", async () => {
{ {
protocol: "vscode", protocol: "vscode",
name: "VS Code", name: "VS Code",
expectedUrls: ["marketplace.visualstudio.com"], expectedUrls: [
"marketplace.visualstudio.com/_apis/public/gallery/vscode/",
],
marketplace: "Microsoft", marketplace: "Microsoft",
}, },
{ {
protocol: "vscode-insiders", protocol: "vscode-insiders",
name: "VS Code Insiders", name: "VS Code Insiders",
expectedUrls: ["marketplace.visualstudio.com"], expectedUrls: [
"marketplace.visualstudio.com/_apis/public/gallery/vscode/",
],
marketplace: "Microsoft", marketplace: "Microsoft",
}, },
{ {
protocol: "vscodium", protocol: "vscodium",
name: "VSCodium", name: "VSCodium",
expectedUrls: ["open-vsx.org"], expectedUrls: ["open-vsx.org/api/"],
marketplace: "Open VSX", marketplace: "Open VSX",
}, },
{ {
protocol: "cursor", protocol: "cursor",
name: "Cursor", name: "Cursor",
expectedUrls: ["open-vsx.org"], expectedUrls: ["open-vsx.org/api/"],
marketplace: "Open VSX", marketplace: "Open VSX",
}, },
{ {
protocol: "windsurf", protocol: "windsurf",
name: "WindSurf", name: "WindSurf",
expectedUrls: ["open-vsx.org"], expectedUrls: ["open-vsx.org/api/"],
marketplace: "Open VSX", marketplace: "Open VSX",
}, },
{ {
protocol: "kiro", protocol: "kiro",
name: "Kiro", name: "Kiro",
expectedUrls: ["open-vsx.org"], expectedUrls: ["open-vsx.org/api/"],
marketplace: "Open VSX", marketplace: "Open VSX",
}, },
]; ];
@ -186,15 +190,15 @@ describe("vscode-desktop-core extension script logic", async () => {
// Verify extension ID is present // Verify extension ID is present
expect(scriptContent).toContain("ms-vscode.hexeditor"); expect(scriptContent).toContain("ms-vscode.hexeditor");
// Verify the case statement includes the IDE protocol // Verify the case statement includes the IDE protocol (Terraform substitutes the variable)
expect(scriptContent).toContain(`case "${ide.protocol}" in`); expect(scriptContent).toContain(`case "${ide.protocol}" in`);
// Verify that the correct case branch exists for the IDE // Verify that the correct case branch exists for the IDE
if (ide.marketplace === "Microsoft") { if (ide.marketplace === "Microsoft") {
expect(scriptContent).toContain(`"vscode"|"vscode-insiders"`); expect(scriptContent).toContain(`"vscode" | "vscode-insiders"`);
} else { } else {
expect(scriptContent).toContain( expect(scriptContent).toContain(
`"vscodium"|"cursor"|"windsurf"|"kiro"`, `"vscodium" | "cursor" | "windsurf" | "kiro"`,
); );
} }
@ -206,11 +210,11 @@ describe("vscode-desktop-core extension script logic", async () => {
// Verify the script uses the correct case branch for this IDE // Verify the script uses the correct case branch for this IDE
if (ide.marketplace === "Microsoft") { if (ide.marketplace === "Microsoft") {
expect(scriptContent).toContain( expect(scriptContent).toContain(
"# Microsoft IDEs: Use Visual Studio Marketplace", "# Microsoft IDEs: Use the VS Code API to get metadata",
); );
} else { } else {
expect(scriptContent).toContain( expect(scriptContent).toContain(
"# Non-Microsoft IDEs: Use Open VSX Registry", "# Non-Microsoft IDEs: Use Open VSX Registry metadata endpoint",
); );
} }
}); });
@ -223,7 +227,7 @@ describe("vscode-desktop-core extension script logic", async () => {
const variables = { const variables = {
...defaultVariables, ...defaultVariables,
extensions_urls: extensions_urls:
'["https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-vscode/vsextensions/hexeditor/latest/vspackage"]', '["https://marketplace.visualstudio.com/_apis/public/gallery/vscode/ms-vscode/hexeditor/latest"]',
extensions_dir: extensionsDir, extensions_dir: extensionsDir,
}; };

View File

@ -1,5 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck disable=SC2269 # Terraform template variables
# shellcheck disable=SC2034 # Color variables used in Terraform templates
# shellcheck disable=SC2059 # printf format strings with Terraform variables
set -euo pipefail set -euo pipefail
# Variables from Terraform template # Variables from Terraform template
@ -40,9 +44,11 @@ generate_extension_url() {
return 1 return 1
fi fi
# Extract publisher and extension name (simple approach) # Extract publisher and extension name
local publisher=$(echo "$extension_id" | cut -d'.' -f1) local publisher
local name=$(echo "$extension_id" | cut -d'.' -f2-) publisher=$(echo "$extension_id" | cut -d'.' -f1)
local name
name=$(echo "$extension_id" | cut -d'.' -f2-)
if [[ -z "$publisher" ]] || [[ -z "$name" ]]; then if [[ -z "$publisher" ]] || [[ -z "$name" ]]; then
printf "$${RED}❌ Invalid extension ID format: $extension_id$${RESET}\n" >&2 printf "$${RED}❌ Invalid extension ID format: $extension_id$${RESET}\n" >&2
@ -52,16 +58,16 @@ generate_extension_url() {
# Generate URL based on IDE type # Generate URL based on IDE type
case "${IDE_TYPE}" in case "${IDE_TYPE}" in
"vscode" | "vscode-insiders") "vscode" | "vscode-insiders")
# Microsoft IDEs: Use Visual Studio Marketplace # Microsoft IDEs: Use the VS Code API to get metadata
printf "https://marketplace.visualstudio.com/_apis/public/gallery/publishers/%s/vsextensions/%s/latest/vspackage" "$publisher" "$name" printf "https://marketplace.visualstudio.com/_apis/public/gallery/vscode/%s/%s/latest" "$publisher" "$name"
;; ;;
"vscodium" | "cursor" | "windsurf" | "kiro") "vscodium" | "cursor" | "windsurf" | "kiro")
# Non-Microsoft IDEs: Use Open VSX Registry # Non-Microsoft IDEs: Use Open VSX Registry metadata endpoint
printf "https://open-vsx.org/api/%s/%s/latest/file/%s.%s-latest.vsix" "$publisher" "$name" "$publisher" "$name" printf "https://open-vsx.org/api/%s/%s/latest" "$publisher" "$name"
;; ;;
*) *)
# Default: Use Open VSX Registry for unknown IDEs # Default: Use Open VSX Registry for unknown IDEs
printf "https://open-vsx.org/api/%s/%s/latest/file/%s.%s-latest.vsix" "$publisher" "$name" "$publisher" "$name" printf "https://open-vsx.org/api/%s/%s/latest" "$publisher" "$name"
;; ;;
esac esac
} }
@ -70,7 +76,9 @@ generate_extension_url() {
download_and_install_extension() { download_and_install_extension() {
local target_dir="$1" local target_dir="$1"
local extension_id="$2" local extension_id="$2"
local url="$3" local metadata_url="$3"
local temp_dir="$4"
local log_file="$5"
# Check if already installed (idempotency) # Check if already installed (idempotency)
if is_extension_installed "$target_dir" "$extension_id"; then if is_extension_installed "$target_dir" "$extension_id"; then
@ -80,51 +88,84 @@ download_and_install_extension() {
printf "$${BOLD}📦 Installing extension $${CODE}$extension_id$${RESET}...\n" printf "$${BOLD}📦 Installing extension $${CODE}$extension_id$${RESET}...\n"
# Create temp directory # Use dedicated temp directory for this extension
local temp_dir=$(mktemp -d) local extension_temp_dir
extension_temp_dir="$temp_dir/$extension_id-$(date +%s)"
local download_file="$temp_dir/$extension_id.vsix" local download_file="$temp_dir/$extension_id.vsix"
# Download with timeout # First, get the metadata JSON
if timeout 30 curl -fsSL "$url" -o "$download_file" 2> /dev/null; then local metadata_response
# Verify the download is a valid file if metadata_response=$(timeout 30 curl -fsSL "$metadata_url" 2>&1); then
if file "$download_file" 2> /dev/null | grep -q "Zip archive"; then # Extract the download URL from JSON (handle both VS Code and Open VSX)
# Create target directory local download_url
mkdir -p "$target_dir" if [[ "${IDE_TYPE}" == "vscode" || "${IDE_TYPE}" == "vscode-insiders" ]]; then
local extract_dir="$target_dir/$extension_id" # VS Code format
download_url=$(echo "$metadata_response" | jq -r '.versions[0].files[] | select(.assetType == "Microsoft.VisualStudio.Services.VSIXPackage") | .source' 2> /dev/null)
else
# Open VSX format
download_url=$(echo "$metadata_response" | jq -r '.files.download // .downloads.universal // empty' 2> /dev/null)
fi
# Remove existing incomplete installation if [[ -n "$download_url" && "$download_url" != "null" ]]; then
if [ -d "$extract_dir" ]; then # Download the actual .vsix file
rm -rf "$extract_dir" if timeout 30 curl -fsSL "$download_url" -o "$download_file" 2>&1; then
fi # Verify the download is a valid file
if file "$download_file" 2> /dev/null | grep -q "Zip archive"; then
# Create target directory
mkdir -p "$target_dir"
local extract_dir="$target_dir/$extension_id"
mkdir -p "$extract_dir" # Remove existing incomplete installation
if [ -d "$extract_dir" ]; then
rm -rf "$extract_dir"
fi
# Extract extension mkdir -p "$extract_dir"
if unzip -q "$download_file" -d "$extract_dir" 2> /dev/null; then
if [ -f "$extract_dir/package.json" ]; then # Extract extension
printf "$${GREEN}✅ Successfully installed $${CODE}$extension_id$${RESET}\n" if unzip -q "$download_file" -d "$extract_dir" 2> /dev/null; then
rm -rf "$temp_dir" if [ -f "$extract_dir/package.json" ]; then
return 0 printf "$${GREEN}✅ Successfully installed $${CODE}$extension_id$${RESET}\n"
# Log success
echo "$(date): Successfully installed $extension_id" >> "$log_file"
rm -rf "$extension_temp_dir"
return 0
else
printf "$${RED}❌ Invalid extension package$${RESET}\n"
echo "$(date): Invalid extension package for $extension_id" >> "$log_file"
rm -rf "$extract_dir"
rm -rf "$extension_temp_dir"
return 1
fi
else
printf "$${RED}❌ Failed to extract extension$${RESET}\n"
echo "$(date): Failed to extract $extension_id" >> "$log_file"
rm -rf "$extract_dir"
rm -rf "$extension_temp_dir"
return 1
fi
else else
printf "$${RED}❌ Invalid extension package$${RESET}\n" printf "$${RED}❌ Invalid file format$${RESET}\n"
rm -rf "$extract_dir" echo "$(date): Invalid file format for $extension_id" >> "$log_file"
rm -rf "$temp_dir" rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
else else
printf "$${RED}❌ Failed to extract extension$${RESET}\n" printf "$${RED}❌ Download failed$${RESET}\n"
rm -rf "$extract_dir" echo "$(date): Download failed for $extension_id from $download_url" >> "$log_file"
rm -rf "$temp_dir" rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
else else
printf "$${RED}❌ Invalid file format$${RESET}\n" printf "$${RED}❌ Could not extract download URL from metadata$${RESET}\n"
rm -rf "$temp_dir" echo "$(date): Could not extract download URL for $extension_id" >> "$log_file"
rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
else else
printf "$${RED}❌ Download failed$${RESET}\n" printf "$${RED}❌ Failed to fetch extension metadata$${RESET}\n"
rm -rf "$temp_dir" echo "$(date): Failed to fetch metadata for $extension_id from $metadata_url" >> "$log_file"
rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
} }
@ -133,8 +174,11 @@ download_and_install_extension() {
install_extension_from_url() { install_extension_from_url() {
local url="$1" local url="$1"
local target_dir="$2" local target_dir="$2"
local temp_dir="$3"
local log_file="$4"
local extension_name=$(basename "$url" | sed 's/\.vsix$$//') local extension_name
extension_name=$(basename "$url" | sed 's/\.vsix$$//')
local extension_id="$extension_name" local extension_id="$extension_name"
printf "$${BOLD}📦 Installing extension from URL: $${CODE}$extension_name$${RESET}...\n" printf "$${BOLD}📦 Installing extension from URL: $${CODE}$extension_name$${RESET}...\n"
@ -144,11 +188,12 @@ install_extension_from_url() {
return 0 return 0
fi fi
# Create temp directory # Use dedicated temp directory
local temp_dir=$(mktemp -d) local extension_temp_dir
extension_temp_dir="$temp_dir/$extension_id-$(date +%s)"
local download_file="$temp_dir/$extension_id.vsix" local download_file="$temp_dir/$extension_id.vsix"
if timeout 30 curl -fsSL "$url" -o "$download_file" 2> /dev/null; then if timeout 30 curl -fsSL "$url" -o "$download_file" 2>&1; then
# Create target directory # Create target directory
mkdir -p "$target_dir" mkdir -p "$target_dir"
local extract_dir="$target_dir/$extension_id" local extract_dir="$target_dir/$extension_id"
@ -163,23 +208,27 @@ install_extension_from_url() {
if unzip -q "$download_file" -d "$extract_dir" 2> /dev/null; then if unzip -q "$download_file" -d "$extract_dir" 2> /dev/null; then
if [ -f "$extract_dir/package.json" ]; then if [ -f "$extract_dir/package.json" ]; then
printf "$${GREEN}✅ Successfully installed $${CODE}$extension_id$${RESET}\n" printf "$${GREEN}✅ Successfully installed $${CODE}$extension_id$${RESET}\n"
rm -rf "$temp_dir" echo "$(date): Successfully installed $extension_id from URL" >> "$log_file"
rm -rf "$extension_temp_dir"
return 0 return 0
else else
printf "$${RED}❌ Invalid extension package$${RESET}\n" printf "$${RED}❌ Invalid extension package$${RESET}\n"
echo "$(date): Invalid extension package for $extension_id from URL" >> "$log_file"
rm -rf "$extract_dir" rm -rf "$extract_dir"
rm -rf "$temp_dir" rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
else else
printf "$${RED}❌ Failed to extract extension$${RESET}\n" printf "$${RED}❌ Failed to extract extension$${RESET}\n"
echo "$(date): Failed to extract $extension_id from URL" >> "$log_file"
rm -rf "$extract_dir" rm -rf "$extract_dir"
rm -rf "$temp_dir" rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
else else
printf "$${RED}❌ Failed to download extension from URL$${RESET}\n" printf "$${RED}❌ Failed to download extension from URL$${RESET}\n"
rm -rf "$temp_dir" echo "$(date): Failed to download $extension_id from URL: $url" >> "$log_file"
rm -rf "$extension_temp_dir"
return 1 return 1
fi fi
} }
@ -188,6 +237,8 @@ install_extension_from_url() {
install_extensions_from_urls() { install_extensions_from_urls() {
local urls="$1" local urls="$1"
local target_dir="$2" local target_dir="$2"
local temp_dir="$3"
local log_file="$4"
if [[ -z "$urls" ]]; then if [[ -z "$urls" ]]; then
return 0 return 0
@ -200,7 +251,7 @@ install_extensions_from_urls() {
# Trim whitespace # Trim whitespace
url=$(echo "$url" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') url=$(echo "$url" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ -n "$url" ]; then if [ -n "$url" ]; then
install_extension_from_url "$url" "$target_dir" install_extension_from_url "$url" "$target_dir" "$temp_dir" "$log_file"
fi fi
done done
} }
@ -209,6 +260,8 @@ install_extensions_from_urls() {
install_extensions_from_ids() { install_extensions_from_ids() {
local extensions="$1" local extensions="$1"
local target_dir="$2" local target_dir="$2"
local temp_dir="$3"
local log_file="$4"
if [[ -z "$extensions" ]]; then if [[ -z "$extensions" ]]; then
return 0 return 0
@ -221,12 +274,13 @@ install_extensions_from_ids() {
# Trim whitespace # Trim whitespace
extension_id=$(echo "$extension_id" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') extension_id=$(echo "$extension_id" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ -n "$extension_id" ]; then if [ -n "$extension_id" ]; then
local extension_url local metadata_url
extension_url=$(generate_extension_url "$extension_id") metadata_url=$(generate_extension_url "$extension_id")
if [ -n "$extension_url" ]; then if [ -n "$metadata_url" ]; then
download_and_install_extension "$target_dir" "$extension_id" "$extension_url" download_and_install_extension "$target_dir" "$extension_id" "$metadata_url" "$temp_dir" "$log_file"
else else
printf "$${RED}❌ Invalid extension ID: $extension_id$${RESET}\n" printf "$${RED}❌ Invalid extension ID: $extension_id$${RESET}\n"
echo "$(date): Invalid extension ID: $extension_id" >> "$log_file"
fi fi
fi fi
done done
@ -244,6 +298,18 @@ main() {
fi fi
done done
# Create dedicated module directory structure
local module_dir="$HOME/.vscode-desktop-core"
local temp_dir="$module_dir/tmp"
local logs_dir="$module_dir/logs"
mkdir -p "$temp_dir" "$logs_dir"
# Set up logging
local log_file
log_file="$logs_dir/extension-installation-$(date +%Y%m%d-%H%M%S).log"
printf "$${BOLD}📝 Logging to: $${CODE}$log_file$${RESET}\n"
# Expand tilde in extensions directory path # Expand tilde in extensions directory path
local extensions_dir="${EXTENSIONS_DIR}" local extensions_dir="${EXTENSIONS_DIR}"
if [ "$${extensions_dir#\~}" != "$extensions_dir" ]; then if [ "$${extensions_dir#\~}" != "$extensions_dir" ]; then
@ -261,15 +327,17 @@ main() {
# Install extensions from URLs (airgapped scenario) # Install extensions from URLs (airgapped scenario)
if [ -n "${EXTENSIONS_URLS}" ]; then if [ -n "${EXTENSIONS_URLS}" ]; then
install_extensions_from_urls "${EXTENSIONS_URLS}" "$extensions_dir" install_extensions_from_urls "${EXTENSIONS_URLS}" "$extensions_dir" "$temp_dir" "$log_file"
fi fi
# Install extensions from extension IDs (normal scenario) # Install extensions from extension IDs (normal scenario)
if [[ -n "${EXTENSIONS}" ]]; then if [[ -n "${EXTENSIONS}" ]]; then
install_extensions_from_ids "${EXTENSIONS}" "$extensions_dir" install_extensions_from_ids "${EXTENSIONS}" "$extensions_dir" "$temp_dir" "$log_file"
fi fi
printf "$${BOLD}$${GREEN}✨ Extension installation completed for $${CODE}${IDE_TYPE}$${RESET}$${BOLD}$${GREEN}!$${RESET}\n" printf "$${BOLD}$${GREEN}✨ Extension installation completed for $${CODE}${IDE_TYPE}$${RESET}$${BOLD}$${GREEN}!$${RESET}\n"
printf "$${BOLD}📁 Extensions installed to: $${CODE}$extensions_dir$${RESET}\n"
printf "$${BOLD}📝 Log file: $${CODE}$log_file$${RESET}\n"
} }
# Script execution entry point # Script execution entry point