Skip to content

Conversation

butschster
Copy link
Collaborator

@butschster butschster commented Apr 4, 2025

This PR implements platform and architecture detection for the self-update command, allowing it to download the appropriate binary based on the user's system.

Changes

  • Created a new BinaryNameBuilder class to generate platform-specific binary names (e.g., ctx-1.22.0-darwin-arm64)

How It Works

  1. The system detects the current platform (linux, darwin, windows) and architecture (amd64, arm64)
  2. It first attempts to download the platform-specific binary (e.g., ctx-1.22.0-linux-amd64)
  3. If that fails, it automatically falls back to the generic binary format (e.g., ctx or ctx.phar)
  4. All existing functionality is preserved for environments where detection isn't possible

Fix: Self-update command fails with "Text file busy" error

When executing the self-update command, the application was trying to overwrite the currently running PHAR file, which resulted in the following error:

Warning: file_put_contents(/usr/local/bin/ctx): Failed to open stream: Text file busy in phar:///usr/local/bin/ctx/src/Lib/Files.php on line 41

I've implemented a robust binary updating system that handles the "Text file busy" scenario:

  1. Created a BinaryUpdater class that:

    • First attempts a direct update (which works if the file isn't busy)
    • Falls back to platform-specific update strategies if direct update fails
  2. Implemented platform-specific update strategies:

    • UnixUpdateStrategy - Creates a bash script that runs after the process exits
    • WindowsUpdateStrategy - Creates a batch script for Windows environments
  3. Updated SelfUpdateCommand to use the new BinaryUpdater system

The update process now:

  1. Downloads the new version to a temporary file
  2. Tries to directly update the binary (may work in some cases)
  3. If that fails, creates and executes a background script that:
    • Waits for the parent process to exit
    • Tries multiple times to replace the binary
    • Provides appropriate feedback to the user
    • Cleans up temporary files

This approach ensures that the update completes successfully even when the binary is currently in use, and it works correctly across different platforms.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 18 changed files in this pull request and generated no comments.

Files not reviewed (17)
  • build-binary.sh: Language not supported
  • src/Console/SelfUpdateCommand.php: Language not supported
  • src/Lib/BinaryUpdater/BinaryUpdater.php: Language not supported
  • src/Lib/BinaryUpdater/Strategy/UnixUpdateStrategy.php: Language not supported
  • src/Lib/BinaryUpdater/Strategy/UpdateStrategyInterface.php: Language not supported
  • src/Lib/BinaryUpdater/Strategy/WindowsUpdateStrategy.php: Language not supported
  • src/Lib/BinaryUpdater/UpdaterFactory.php: Language not supported
  • src/Lib/GithubClient/Architecture.php: Language not supported
  • src/Lib/GithubClient/BinaryNameBuilder.php: Language not supported
  • src/Lib/GithubClient/GithubClient.php: Language not supported
  • src/Lib/GithubClient/GithubClientInterface.php: Language not supported
  • src/Lib/GithubClient/Model/GithubRepository.php: Language not supported
  • src/Lib/GithubClient/Model/Release.php: Language not supported
  • src/Lib/GithubClient/Platform.php: Language not supported
  • src/Lib/GithubClient/ReleaseManager.php: Language not supported
  • src/Source/Github/GithubFinder.php: Language not supported
  • src/Source/Github/GithubSourceFetcher.php: Language not supported

@butschster butschster merged commit 52d2604 into main Apr 5, 2025
8 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in Context Generator Apr 5, 2025
@butschster butschster deleted the issue/114 branch April 5, 2025 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

Archived in project

1 participant