From 11cfc055af7f6561dd9341615c924594f1e76f48 Mon Sep 17 00:00:00 2001 From: Massan Date: Sun, 4 May 2025 22:05:27 +0900 Subject: [PATCH 1/3] Add PowerShell script for Windows DevContainer setup (Docker/Podman) --- Script/run_devcontainer_claude_code.ps1 | 139 ++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 Script/run_devcontainer_claude_code.ps1 diff --git a/Script/run_devcontainer_claude_code.ps1 b/Script/run_devcontainer_claude_code.ps1 new file mode 100644 index 00000000..b578a664 --- /dev/null +++ b/Script/run_devcontainer_claude_code.ps1 @@ -0,0 +1,139 @@ +#------------------------------------------------------------------------------ +# Script: run_devcontainer_claude_code.ps1 +# Author: [Your Name or Project Name] +# Description: Automates the setup and connection to a DevContainer environment +# using either Docker or Podman on Windows. +# +# IMPORTANT USAGE REQUIREMENT: +# This script MUST be executed from the ROOT directory of your project. +# It assumes the script file itself is located in a 'Script' subdirectory. +# +# Assumed Project Structure: +# Project/ +# ├── .devcontainer/ +# └── Script/ +# └── run_devcontainer_claude_code.ps1 <-- This script's location +# +# How to Run: +# 1. Open PowerShell. +# 2. Change your current directory to the project root: +# cd c:\path\to\your\Project +# 3. Execute the script, specifying the container backend: +# .\Script\run_devcontainer_claude_code.ps1 -Backend +# +# The -Backend parameter is mandatory and accepts 'docker' or 'podman'. +#------------------------------------------------------------------------------ +[CmdletBinding()] +param( + [Parameter(Mandatory=$true)] + [string]$Backend +) + +# Notify script start +Write-Host "--- DevContainer Startup & Connection Script ---" +Write-Host "Using backend: $($Backend)" + +# Validate the input backend +if ($Backend -notin @('docker', 'podman')) { + Write-Error "Invalid backend specified. Please use 'docker' or 'podman'." + Write-Host "Usage: ." + $MyInvocation.MyCommand.Definition + " -Backend " + exit 1 +} + +# --- Backend-Specific Initialization --- +if ($Backend -eq 'podman') { + Write-Host "--- Podman Backend Initialization ---" + + # --- Step 1a: Initialize Podman machine --- + Write-Host "Initializing Podman machine 'claudeVM'..." + try { + podman machine init claudeVM + Write-Host "Podman machine 'claudeVM' initialized or already exists." + } catch { + Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)" + exit 1 # Exit script on error + } + + # --- Step 1b: Start Podman machine --- + Write-Host "Starting Podman machine 'claudeVM'..." + try { + podman machine start claudeVM -q + Write-Host "Podman machine started or already running." + } catch { + Write-Error "Failed to start Podman machine: $($_.Exception.Message)" + exit 1 + } + + # --- Step 2: Set default connection --- + Write-Host "Setting default Podman connection to 'claudeVM'..." + try { + podman system connection default claudeVM + Write-Host "Default connection set." + } catch { + Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)" + } + +} elseif ($Backend -eq 'docker') { + Write-Host "--- Docker Backend Initialization ---" + + # --- Step 1 & 2: Check Docker Desktop --- + Write-Host "Checking if Docker Desktop is running and docker command is available..." + try { + docker info | Out-Null + Write-Host "Docker Desktop (daemon) is running." + } catch { + Write-Error "Docker Desktop is not running or docker command not found." + Write-Error "Please ensure Docker Desktop is running." + exit 1 + } +} + +# --- Step 3: Bring up DevContainer --- +Write-Host "Bringing up DevContainer in the current folder..." +try { + $devcontainerUpCommand = "devcontainer up --workspace-folder ." + if ($Backend -eq 'podman') { + $devcontainerUpCommand += " --docker-path podman" + } + Invoke-Expression $devcontainerUpCommand + Write-Host "DevContainer startup process completed." +} catch { + Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)" + exit 1 +} + +# --- Step 4: Get DevContainer ID --- +Write-Host "Finding the DevContainer ID..." +$currentFolder = (Get-Location).Path + +$psCommand = "$($Backend) ps --filter ""label=devcontainer.local_folder=$currentFolder"" --format ""{{.ID}}""" + +try { + $containerId = $(Invoke-Expression $psCommand).Trim() +} catch { + Write-Error "Failed to get container ID (Command: $psCommand): $($_.Exception.Message)" + exit 1 +} + +if (-not $containerId) { + Write-Error "Could not find DevContainer ID for the current folder ('$currentFolder')." + Write-Error "Please check if 'devcontainer up' was successful and the container is running." + exit 1 +} +Write-Host "Found container ID: $containerId" + +# --- Step 5 & 6: Execute command and enter interactive shell inside container --- +Write-Host "Executing 'claude' command and then starting zsh session inside container $($containerId)..." +try { + $execCommand = "$($Backend) exec -it $containerId zsh -c 'claude; exec zsh'" + Invoke-Expression $execCommand + + Write-Host "Interactive session ended." + +} catch { + Write-Error "Failed to execute command inside container (Command: $execCommand): $($_.Exception.Message)" + exit 1 +} + +# Notify script completion +Write-Host "--- Script completed ---" \ No newline at end of file From 545d78c331eda8903d0b0777511c816638aac420 Mon Sep 17 00:00:00 2001 From: Masa1984a Date: Thu, 3 Jul 2025 06:29:38 +0900 Subject: [PATCH 2/3] Apply review suggestions: Add ValidateSet and improve documentation --- Script/run_devcontainer_claude_code.ps1 | 60 ++++++++++++------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/Script/run_devcontainer_claude_code.ps1 b/Script/run_devcontainer_claude_code.ps1 index b578a664..587cb127 100644 --- a/Script/run_devcontainer_claude_code.ps1 +++ b/Script/run_devcontainer_claude_code.ps1 @@ -1,31 +1,34 @@ -#------------------------------------------------------------------------------ -# Script: run_devcontainer_claude_code.ps1 -# Author: [Your Name or Project Name] -# Description: Automates the setup and connection to a DevContainer environment -# using either Docker or Podman on Windows. -# -# IMPORTANT USAGE REQUIREMENT: -# This script MUST be executed from the ROOT directory of your project. -# It assumes the script file itself is located in a 'Script' subdirectory. -# -# Assumed Project Structure: -# Project/ -# ├── .devcontainer/ -# └── Script/ -# └── run_devcontainer_claude_code.ps1 <-- This script's location -# -# How to Run: -# 1. Open PowerShell. -# 2. Change your current directory to the project root: -# cd c:\path\to\your\Project -# 3. Execute the script, specifying the container backend: -# .\Script\run_devcontainer_claude_code.ps1 -Backend -# -# The -Backend parameter is mandatory and accepts 'docker' or 'podman'. -#------------------------------------------------------------------------------ +<# +.SYNOPSIS + Automates the setup and connection to a DevContainer environment using either Docker or Podman on Windows. + +.DESCRIPTION + This script automates the process of initializing, starting, and connecting to a DevContainer + using either Docker or Podman as the container backend. It must be executed from the root + directory of your project and assumes the script is located in a 'Script' subdirectory. + +.PARAMETER Backend + Specifies the container backend to use. Valid values are 'docker' or 'podman'. + +.EXAMPLE + .\Script\run_devcontainer_claude_code.ps1 -Backend docker + Uses Docker as the container backend. + +.EXAMPLE + .\Script\run_devcontainer_claude_code.ps1 -Backend podman + Uses Podman as the container backend. + +.NOTES + Project Structure: + Project/ + ├── .devcontainer/ + └── Script/ + └── run_devcontainer_claude_code.ps1 +#> [CmdletBinding()] param( [Parameter(Mandatory=$true)] + [ValidateSet('docker','podman')] [string]$Backend ) @@ -33,13 +36,6 @@ param( Write-Host "--- DevContainer Startup & Connection Script ---" Write-Host "Using backend: $($Backend)" -# Validate the input backend -if ($Backend -notin @('docker', 'podman')) { - Write-Error "Invalid backend specified. Please use 'docker' or 'podman'." - Write-Host "Usage: ." + $MyInvocation.MyCommand.Definition + " -Backend " - exit 1 -} - # --- Backend-Specific Initialization --- if ($Backend -eq 'podman') { Write-Host "--- Podman Backend Initialization ---" From c93c724eeb652b77c5f231a7b3b1744b4ca9f98b Mon Sep 17 00:00:00 2001 From: Kurt Carpenter Date: Wed, 2 Jul 2025 18:06:00 -0700 Subject: [PATCH 3/3] PowerShell parameter improvements --- Script/run_devcontainer_claude_code.ps1 | 30 ++++++++++++------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Script/run_devcontainer_claude_code.ps1 b/Script/run_devcontainer_claude_code.ps1 index 587cb127..117202cf 100644 --- a/Script/run_devcontainer_claude_code.ps1 +++ b/Script/run_devcontainer_claude_code.ps1 @@ -1,4 +1,4 @@ -<# +<# .SYNOPSIS Automates the setup and connection to a DevContainer environment using either Docker or Podman on Windows. @@ -25,6 +25,7 @@ └── Script/ └── run_devcontainer_claude_code.ps1 #> + [CmdletBinding()] param( [Parameter(Mandatory=$true)] @@ -43,7 +44,7 @@ if ($Backend -eq 'podman') { # --- Step 1a: Initialize Podman machine --- Write-Host "Initializing Podman machine 'claudeVM'..." try { - podman machine init claudeVM + & podman machine init claudeVM Write-Host "Podman machine 'claudeVM' initialized or already exists." } catch { Write-Error "Failed to initialize Podman machine: $($_.Exception.Message)" @@ -53,7 +54,7 @@ if ($Backend -eq 'podman') { # --- Step 1b: Start Podman machine --- Write-Host "Starting Podman machine 'claudeVM'..." try { - podman machine start claudeVM -q + & podman machine start claudeVM -q Write-Host "Podman machine started or already running." } catch { Write-Error "Failed to start Podman machine: $($_.Exception.Message)" @@ -63,7 +64,7 @@ if ($Backend -eq 'podman') { # --- Step 2: Set default connection --- Write-Host "Setting default Podman connection to 'claudeVM'..." try { - podman system connection default claudeVM + & podman system connection default claudeVM Write-Host "Default connection set." } catch { Write-Warning "Failed to set default Podman connection (may be already set or machine issue): $($_.Exception.Message)" @@ -87,11 +88,11 @@ if ($Backend -eq 'podman') { # --- Step 3: Bring up DevContainer --- Write-Host "Bringing up DevContainer in the current folder..." try { - $devcontainerUpCommand = "devcontainer up --workspace-folder ." + $arguments = @('up', '--workspace-folder', '.') if ($Backend -eq 'podman') { - $devcontainerUpCommand += " --docker-path podman" + $arguments += '--docker-path', 'podman' } - Invoke-Expression $devcontainerUpCommand + & devcontainer @arguments Write-Host "DevContainer startup process completed." } catch { Write-Error "Failed to bring up DevContainer: $($_.Exception.Message)" @@ -102,12 +103,11 @@ try { Write-Host "Finding the DevContainer ID..." $currentFolder = (Get-Location).Path -$psCommand = "$($Backend) ps --filter ""label=devcontainer.local_folder=$currentFolder"" --format ""{{.ID}}""" - try { - $containerId = $(Invoke-Expression $psCommand).Trim() + $containerId = (& $Backend ps --filter "label=devcontainer.local_folder=$currentFolder" --format '{{.ID}}').Trim() } catch { - Write-Error "Failed to get container ID (Command: $psCommand): $($_.Exception.Message)" + $displayCommand = "$Backend ps --filter `"label=devcontainer.local_folder=$currentFolder`" --format '{{.ID}}'" + Write-Error "Failed to get container ID (Command: $displayCommand): $($_.Exception.Message)" exit 1 } @@ -121,13 +121,11 @@ Write-Host "Found container ID: $containerId" # --- Step 5 & 6: Execute command and enter interactive shell inside container --- Write-Host "Executing 'claude' command and then starting zsh session inside container $($containerId)..." try { - $execCommand = "$($Backend) exec -it $containerId zsh -c 'claude; exec zsh'" - Invoke-Expression $execCommand - + & $Backend exec -it $containerId zsh -c 'claude; exec zsh' Write-Host "Interactive session ended." - } catch { - Write-Error "Failed to execute command inside container (Command: $execCommand): $($_.Exception.Message)" + $displayCommand = "$Backend exec -it $containerId zsh -c 'claude; exec zsh'" + Write-Error "Failed to execute command inside container (Command: $displayCommand): $($_.Exception.Message)" exit 1 }