From e64da92eba2b017e0dab307b4a04dcf60ad34c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Will=20=E4=BF=9D=E5=93=A5?= <88981+doggy8088@users.noreply.github.com> Date: Tue, 22 Apr 2025 00:48:05 +0800 Subject: [PATCH] Completing PowerShell script development can better support the Windows operating system. --- codex-cli/.gitignore | 2 + codex-cli/scripts/build_container.ps1 | 24 ++++++++++++ codex-cli/scripts/run_in_container.ps1 | 51 ++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 codex-cli/.gitignore create mode 100644 codex-cli/scripts/build_container.ps1 create mode 100644 codex-cli/scripts/run_in_container.ps1 diff --git a/codex-cli/.gitignore b/codex-cli/.gitignore new file mode 100644 index 000000000..bd92f33ae --- /dev/null +++ b/codex-cli/.gitignore @@ -0,0 +1,2 @@ +Dockerfile.windows +package-lock.json \ No newline at end of file diff --git a/codex-cli/scripts/build_container.ps1 b/codex-cli/scripts/build_container.ps1 new file mode 100644 index 000000000..450428040 --- /dev/null +++ b/codex-cli/scripts/build_container.ps1 @@ -0,0 +1,24 @@ +#!/usr/bin/env pwsh +# PowerShell 7.5 +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +# Get the script's directory and navigate to the parent directory. +$scriptDir = Split-Path $MyInvocation.MyCommand.Definition -Parent +Push-Location (Join-Path $scriptDir '..') +try { + & npm install + & npm run build + Remove-Item ./dist/openai-codex-*.tgz -Recurse -Force -ErrorAction Ignore + & npm pack --pack-destination ./dist + Move-Item ./dist/openai-codex-*.tgz ./dist/codex.tgz -Force + + # Dockerfile Patch to Resolve Permission Issues on Windows + Copy-Item -Path ./Dockerfile -Destination ./Dockerfile.windows -Force + echo 'RUN git config --global --add safe.directory /app' >> ./Dockerfile.windows + + & docker build -t codex -f './Dockerfile.windows' . +} +finally { + Pop-Location +} diff --git a/codex-cli/scripts/run_in_container.ps1 b/codex-cli/scripts/run_in_container.ps1 new file mode 100644 index 000000000..b8d6e0b53 --- /dev/null +++ b/codex-cli/scripts/run_in_container.ps1 @@ -0,0 +1,51 @@ +#!/usr/bin/env pwsh +# PowerShell 7.5 +<# +Usage: + .\run_in_container.ps1 [-WorkDir ] -- +Example: + .\run_in_container.ps1 -WorkDir G:\Projects\demo1 -- "explain this codebase to me" + .\run_in_container.ps1 +#> + +param( + [string]$WorkDir = (Get-Location).ProviderPath, + [Parameter(ValueFromRemainingArguments)] [string[]]$Args +) + +$WorkDir = (Get-Item $WorkDir).FullName +$containerName = 'codex_' + ($WorkDir -replace '[\\/]', '_' -replace '[^0-9A-Za-z_-]', '') + +# Check whether OPENAI_API_KEY has been set in environment variables. +if (-not $env:OPENAI_API_KEY) { + Write-Error 'An OPENAI_API_KEY environment variable must be provided.' + exit 1 +} + +function Cleanup { + docker rm -f $containerName 2> $null +} + +function Quote-BashArg { + param([string]$Value) + return "'$($Value -replace "'", "''\'''")'" +} + +try { + Cleanup # Remove old container + + docker run --name $containerName -d ` + -e OPENAI_API_KEY ` + --cap-add=NET_ADMIN --cap-add=NET_RAW ` + -v "${WorkDir}:/app" ` + codex sleep infinity | Out-Null + + # docker exec $containerName bash -c 'sudo /usr/local/bin/init_firewall.sh' + + $quoted = ($Args | ForEach-Object { Quote-BashArg $_ }) -join ' ' + + docker exec -it $containerName bash -c "cd '/app' && codex --full-auto $quoted" +} +finally { + Register-EngineEvent PowerShell.Exiting -Action { Cleanup } | Out-Null +}