Skip to content

Commit

Permalink
aider: Add Playwright support and cleanup logic (#6)
Browse files Browse the repository at this point in the history
This pull request enables Aider's web scraping functionality by
installing Playwright and its browser dependencies. Users can enable or
disable Playwright install and select which browsers they want to
install, if any.

## Changes

1. **Playwright Support**:
   - Introduced Playwright installation logic in `install.sh`.
- Enabled configuration of Playwright and browser selection via
`devcontainer-feature.json`.
2. **Improved Installation Script**:
   - Refactored logic to improve modularity and maintainability.
   - Implemented OS-specific and user-detection logic.
3. **Testing**:
- Expanded `scenarios.json` to include tests for Playwright and browser
installs.
   - Added new scripts:
- `without_playwright.sh` to test that Playwright install can be
disabled.
- `with_playwright_browsers.sh` to validate the successful installation
of specific browsers.
4. **Cache Management**:
- Added a `clean_up` function and Debian/Ubuntu detection to purge apt
lists and pipx cache.
5. **Changelog Introduction**:
- Added a `CHANGELOG.md` following the [Keep a
Changelog](http://keepachangelog.com/en/1.0.0/) format for better
documentation and release tracking.
  • Loading branch information
ivy authored Dec 20, 2024
1 parent e16c1f7 commit 069a8ce
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 15 deletions.
21 changes: 21 additions & 0 deletions src/aider/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Support for installing dependencies like Playwright and browser dependencies to enable Aider's web scraping functionality.
- Logic to clean up caches and other temporary files after installation to optimize performance and reduce disk usage.

## [1.0.0] - 2024-12-18

### Added

- Initial implementation of Aider devcontainer feature with support for Debian and Ubuntu.

[Unreleased]: https://github.com/ivy/devcontainer-features/commits/main/src/aider
[1.0.0]: https://github.com/ivy/devcontainer-features/commits/main/src/aider?since=2024-12-18&until=2024-12-19
22 changes: 22 additions & 0 deletions src/aider/devcontainer-feature.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@
"0.69.1"
],
"description": "Select an Aider version to install."
},
"installPlaywright": {
"type": "boolean",
"default": true,
"description": "Install Playwright for the best web scraping."
},
"installPlaywrightBrowsers": {
"type": "string",
"default": "chromium",
"proposals": [
"chromium",
"chromium-headless-shell",
"chrome",
"chrome-beta",
"msedge",
"msedge-beta",
"msedge-dev",
"bidi-chromium",
"firefox",
"webkit"
],
"description": "Select the browsers to install for Playwright (comma-delimit for multiple)."
}
},
"dependsOn": {
Expand Down
94 changes: 79 additions & 15 deletions src/aider/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,35 @@ set -o pipefail
# Version of Aider to install.
AIDER_VERSION="${VERSION:-latest}"

# Whether to install Playwright for web scraping.
INSTALLPLAYWRIGHT="${INSTALLPLAYWRIGHT:-true}"

# Browsers to install for Playwright.
INSTALLPLAYWRIGHTBROWSERS="${INSTALLPLAYWRIGHTBROWSERS:-chromium}"

# Username to install Aider for.
USERNAME="${USERNAME:-"${_REMOTE_USER:-automatic}"}"

# Detect the Linux distribution ID. Adjust to account for derivatives.
detect_adjusted_id() {
if [ ! -r /etc/os-release ]; then
echo "WARN: Unable to detect the OS release." >&2
return
fi

# shellcheck disable=SC1091
source /etc/os-release

case "${ID:-unknown}" in
debian|ubuntu)
ADJUSTED_ID=debian
;;
*)
ADJUSTED_ID="${ID:-unknown}"
;;
esac
}

# Detect the username to use for the installation. This code is adapted from the
# official devcontainer Python feature.
detect_username() {
Expand Down Expand Up @@ -49,29 +75,67 @@ detect_username() {
fi
}

as_user() {
# HACK(ivy): PS1=true works around an edge case where pipx isn't added
# to the PATH. This happens with the `mcr.microsoft.com/devcontainers/base:ubuntu`
# image which includes a check at the top of /etc/bash.bashrc which
# returns in non-interactive shells.
if [ "$USERNAME" = root ]; then
bash -c "PS1=true; . /etc/bash.bashrc || true; $1"
else
su - "$USERNAME" bash -c "PS1=true; . /etc/bash.bashrc || true; $1"
fi
}

# Install Aider using pipx.
install_aider() {
if [ "$AIDER_VERSION" = latest ]; then
as_user 'pipx install aider-chat'
else
as_user "pipx install aider-chat==${AIDER_VERSION}"
fi
}

install_playwright() {
local browsers

if [ "$INSTALLPLAYWRIGHT" != true ]; then
return
fi

as_user 'pipx inject --include-apps --include-deps aider-chat playwright'

if [ -n "$INSTALLPLAYWRIGHTBROWSERS" ]; then
# Split $INSTALLPLAYWRIGHTBROWSERS by comma into an array and bind it to $browsers.
IFS=, read -r -a browsers <<< "$INSTALLPLAYWRIGHTBROWSERS"

echo "Installing Playwright browsers: $INSTALLPLAYWRIGHTBROWSERS..."
as_user "playwright install --with-deps ${browsers[*]}"
fi
}

# Clean up caches and temporary files.
clean_up() {
# Clean up pipx cache.
as_user 'pipx runpip aider-chat cache purge'

if [ "$ADJUSTED_ID" = debian ]; then
rm -fr /var/lib/apt/lists/*
fi
}

# Main entrypoint
main() {
if [ "$(id -u)" -ne 0 ]; then
echo 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' >&2
exit 1
fi

detect_adjusted_id
detect_username
if [ "$AIDER_VERSION" = latest ]; then
echo "Installing latest Aider..."
# NOTE(ivy): PS1=true works around an edge case where pipx isn't added
# to the PATH. This happens with the `mcr.microsoft.com/devcontainers/base:ubuntu`
# image which includes a check at the top of /etc/bash.bashrc which
# returns in non-interactive shells.
su - "$USERNAME" bash -c "PS1=true; . /etc/bash.bashrc || true; pipx install aider-chat"
else
echo "Installing Aider version $AIDER_VERSION..."
# NOTE(ivy): PS1=true works around an edge case where pipx isn't added
# to the PATH. This happens with the `mcr.microsoft.com/devcontainers/base:ubuntu`
# image which includes a check at the top of /etc/bash.bashrc which
# returns in non-interactive shells.
su - "$USERNAME" bash -c "PS1=true; . /etc/bash.bashrc || true; pipx install aider-chat==${AIDER_VERSION}"
fi
install_aider
install_playwright
clean_up

echo "Aider has been installed!"
}
Expand Down
16 changes: 16 additions & 0 deletions test/aider/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,21 @@
"version": "0.69.0"
}
}
},
"without_playwright": {
"image": "mcr.microsoft.com/devcontainers/base:debian",
"features": {
"aider": {
"installPlaywright": false
}
}
},
"with_playwright_browsers": {
"image": "mcr.microsoft.com/devcontainers/base:debian",
"features": {
"aider": {
"installPlaywrightBrowsers": "chromium,firefox"
}
}
}
}
1 change: 1 addition & 0 deletions test/aider/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ source dev-container-features-test-lib
# The 'check' command comes from the dev-container-features-test-lib. Syntax is...
# check <LABEL> <cmd> [args...]
check "validate aider installed" command -v aider
check "validate playwright installed" command -v playwright

# Report result
# If any of the checks above exited with a non-zero exit code, the test will fail.
Expand Down
20 changes: 20 additions & 0 deletions test/aider/with_playwright_browsers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# This test file will be executed against one of the scenarios devcontainer.json test that
# includes the 'color' feature with "favorite": "gold" option.

set -e

# Optional: Import test library bundled with the devcontainer CLI
# shellcheck disable=SC1091
source dev-container-features-test-lib

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib.
# shellcheck disable=SC2016
check "chromium is installed" bash -c 'find ~/.cache/ms-playwright -maxdepth 1 -type d | grep -E chromium'
check "firefox is installed" bash -c 'find ~/.cache/ms-playwright -maxdepth 1 -type d | grep -E firefox'

# Report result
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults
19 changes: 19 additions & 0 deletions test/aider/without_playwright.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# This test file will be executed against one of the scenarios devcontainer.json test that
# includes the 'color' feature with "favorite": "gold" option.

set -e

# Optional: Import test library bundled with the devcontainer CLI
# shellcheck disable=SC1091
source dev-container-features-test-lib

# Feature-specific tests
# The 'check' command comes from the dev-container-features-test-lib.
# shellcheck disable=SC2016
check "playwright is not installed" bash -c '! command -v playwright'

# Report result
# If any of the checks above exited with a non-zero exit code, the test will fail.
reportResults

0 comments on commit 069a8ce

Please sign in to comment.