From 0b26bbebfabd2529b78f0fb30eb85ad2775e0225 Mon Sep 17 00:00:00 2001 From: Ivy Evans Date: Fri, 20 Dec 2024 06:42:20 +0000 Subject: [PATCH 1/5] aider: Add Playwright support and cleanup logic - Added a changelog (`CHANGELOG.md`) using the Keep a Changelog format. - Introduced support for Playwright installation and browser selection in `devcontainer-feature.json`. - Updated `install.sh` to include: - Installation of Playwright and specified browsers. - Cleanup of caches and temporary files for improved efficiency. - Refactored logic for user detection and OS-specific adjustments. - Improved modularity and maintainability of the installation script. --- src/aider/CHANGELOG.md | 21 +++++++ src/aider/devcontainer-feature.json | 22 ++++++++ src/aider/install.sh | 86 +++++++++++++++++++++++++---- 3 files changed, 119 insertions(+), 10 deletions(-) create mode 100644 src/aider/CHANGELOG.md diff --git a/src/aider/CHANGELOG.md b/src/aider/CHANGELOG.md new file mode 100644 index 0000000..522060c --- /dev/null +++ b/src/aider/CHANGELOG.md @@ -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 diff --git a/src/aider/devcontainer-feature.json b/src/aider/devcontainer-feature.json index 389eef6..aafd45c 100644 --- a/src/aider/devcontainer-feature.json +++ b/src/aider/devcontainer-feature.json @@ -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": { diff --git a/src/aider/install.sh b/src/aider/install.sh index 55c8362..1ce3eee 100644 --- a/src/aider/install.sh +++ b/src/aider/install.sh @@ -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() { @@ -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!" } From f2a5d9d140329c1dd0d744f4bc974ebe6a2a58b8 Mon Sep 17 00:00:00 2001 From: Ivy Evans Date: Fri, 20 Dec 2024 06:51:18 +0000 Subject: [PATCH 2/5] aider: Fix non-root installation - Adjusted `as_user` function to ensure `pipx` commands work reliably by consistently sourcing the user's bash configuration with `PS1=true`. - Replaced redundant comments with a single, consolidated explanation of the `PS1=true` workaround. - Addressed a bug from the previous refactoring that caused inconsistent behavior in non-root installations. This change ensures reliable execution of Aider installation in non-interactive environments. --- src/aider/install.sh | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/aider/install.sh b/src/aider/install.sh index 1ce3eee..7c10b72 100644 --- a/src/aider/install.sh +++ b/src/aider/install.sh @@ -76,8 +76,12 @@ detect_username() { } 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 @@ -86,16 +90,8 @@ as_user() { # Install Aider using pipx. install_aider() { if [ "$AIDER_VERSION" = latest ]; then - # 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. as_user 'pipx install aider-chat' else - # 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. as_user "pipx install aider-chat==${AIDER_VERSION}" fi } From 87ebba0ceee7e06ad38af7be25246be32d1d4283 Mon Sep 17 00:00:00 2001 From: Ivy Evans Date: Fri, 20 Dec 2024 07:16:36 +0000 Subject: [PATCH 3/5] aider: Don't clean up Playwright cache This path is actually where browsers are downloaded and stored, so it shouldn't be removed. --- src/aider/install.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/aider/install.sh b/src/aider/install.sh index 7c10b72..e20ee12 100644 --- a/src/aider/install.sh +++ b/src/aider/install.sh @@ -114,9 +114,6 @@ 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 From 4caf606fc392af8f87be7fe41dc652b3f7ade3b3 Mon Sep 17 00:00:00 2001 From: Ivy Evans Date: Fri, 20 Dec 2024 07:21:19 +0000 Subject: [PATCH 4/5] aider: Add tests for Playwright install --- test/aider/scenarios.json | 8 ++++++++ test/aider/test.sh | 1 + test/aider/without_playwright.sh | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 test/aider/without_playwright.sh diff --git a/test/aider/scenarios.json b/test/aider/scenarios.json index 4883b76..20ca959 100644 --- a/test/aider/scenarios.json +++ b/test/aider/scenarios.json @@ -16,5 +16,13 @@ "version": "0.69.0" } } + }, + "without_playwright": { + "image": "mcr.microsoft.com/devcontainers/base:debian", + "features": { + "aider": { + "installPlaywright": false + } + } } } diff --git a/test/aider/test.sh b/test/aider/test.sh index 35fec20..2dee510 100644 --- a/test/aider/test.sh +++ b/test/aider/test.sh @@ -42,6 +42,7 @@ source dev-container-features-test-lib # The 'check' command comes from the dev-container-features-test-lib. Syntax is... # check