Skip to content

aider: Add Playwright support and cleanup logic #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
86 changes: 76 additions & 10 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,30 +75,70 @@ detect_username() {
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
as_user() {
if [ "$USERNAME" = root ]; then
"$@"
else
su - "$USERNAME" bash -c "PS1=true; . /etc/bash.bashrc || true; $1"
fi
}

detect_username
# Install Aider using pipx.
install_aider() {
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"
as_user '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}"
as_user "pipx install aider-chat==${AIDER_VERSION}"
fi
}

install_playwright() {
if [ "$INSTALLPLAYWRIGHT" != true ]; then
return
fi

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

if [ -n "$INSTALLPLAYWRIGHTBROWSERS" ]; then
echo "Installing Playwright browsers: $INSTALLPLAYWRIGHTBROWSERS..."
as_user "playwright install --with-deps $INSTALLPLAYWRIGHTBROWSERS"
fi
}

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

# Clean up Playwright cache (browser packages).
as_user 'rm -fr ~/.cache/ms-playwright'

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
install_aider
install_playwright
clean_up

echo "Aider has been installed!"
}

Expand Down
Loading