diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index b7d9506..a3273f2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -20,6 +20,7 @@ jobs: - gcc # skipping zephyr-dev due to hard requirement of an existing west-workspace # - zephyr-dev + - prek baseImage: - debian:latest - ubuntu:latest @@ -46,6 +47,7 @@ jobs: - ceedling # - gcc - zephyr-dev + - prek steps: - uses: actions/checkout@v4 diff --git a/src/prek/README.md b/src/prek/README.md new file mode 100644 index 0000000..7278b53 --- /dev/null +++ b/src/prek/README.md @@ -0,0 +1,22 @@ + +# prek (Pre-Commit Check Tool) + +prek + +## Example Usage + +```json +"features": { + "ghcr.io/DojoFive/embedops-features/prek:1": {} +} +``` + +## Options + +| Options Id | Description | Type | Default Value | +|-----|-----|-----|-----| +| prek-version | Select or enter the prek Version | string | latest | + +--- + +_Note: This file was auto-generated from the [devcontainer-feature.json](https://github.com/DojoFive/embedops-features/blob/main/src/zephyr-dev/devcontainer-feature.json). Add additional notes to a `NOTES.md`._ diff --git a/src/prek/devcontainer-feature.json b/src/prek/devcontainer-feature.json new file mode 100644 index 0000000..957fcb3 --- /dev/null +++ b/src/prek/devcontainer-feature.json @@ -0,0 +1,21 @@ +{ + "name": "prek", + "id": "prek", + "version": "1.0.0", + "description": "prek", + "options": { + "prek-version": { + "type": "string", + "proposals": [ + "latest", + "v0.2.22" + ], + "default": "latest", + "description": "Select or enter the Prek Version" + }, + }, + "installsAfter": [ + "ghcr.io/devcontainers/features/common-utils", + "ghcr.io/devcontainers/features/docker-in-docker" + ] +} \ No newline at end of file diff --git a/src/prek/install.sh b/src/prek/install.sh new file mode 100644 index 0000000..a4bf340 --- /dev/null +++ b/src/prek/install.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash +set -x + +export DEBIAN_FRONTEND=noninteractive +# Load system info, including OS type +. /etc/os-release + +# Determine base Linux distribution +if [[ "${ID}" = "debian" || "${ID_LIKE}" = "debian" ]]; then + ADJUSTED_ID="debian" +elif [ "${ID}" = "alpine" ]; then + ADJUSTED_ID="alpine" +else + echo "Linux distro ${ID} not supported." + exit 1 +fi + +if type apt-get > /dev/null 2>&1; then + INSTALL_CMD=apt-get +elif type apk > /dev/null 2>&1; then + INSTALL_CMD=apk +elif type microdnf > /dev/null 2>&1; then + INSTALL_CMD=microdnf +elif type dnf > /dev/null 2>&1; then + INSTALL_CMD=dnf +elif type yum > /dev/null 2>&1; then + INSTALL_CMD=yum +else + echo "(Error) Unable to find a supported package manager." + exit 1 +fi + +pkg_mgr_update() { + if [ ${INSTALL_CMD} = "apt-get" ]; then + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then + echo "Running apt-get update..." + ${INSTALL_CMD} update -y + fi + elif [ ${INSTALL_CMD} = "apk" ]; then + if [ "$(find /var/cache/apk/* | wc -l)" = "0" ]; then + echo "Running apk update..." + ${INSTALL_CMD} update + fi + fi +} + +check_packages() { + if [ ${INSTALL_CMD} = "apt-get" ]; then + if ! dpkg -s "$@" > /dev/null 2>&1; then + pkg_mgr_update + ${INSTALL_CMD} -y install --no-install-recommends "$@" + fi + elif [ ${INSTALL_CMD} = "apk" ]; then + ${INSTALL_CMD} add \ + --no-cache \ + "$@" + else + echo "Linux distro ${ID} not supported." + exit 1 + fi +} + +clean_up() { + case $ADJUSTED_ID in + debian) + rm -rf /var/lib/apt/lists/* + ;; + alpine) + rm -rf /var/cache/apk/* + ;; + esac +} + +install_dependencies() { + echo "Installing dependencies for ${ADJUSTED_ID}..." + + check_packages curl jq ca-certificates +} + +install_prek() { + echo "Installing prek..." + + # Check if PREK_VERSION is set, otherwise exit + if [[ -z "${PREK_VERSION}" ]]; then + echo "Error: PREK_VERSION is not set. Please provide the version as a feature variable." + exit 1 + fi + + if [[ "${PREK_VERSION}" = "latest" ]]; then + PREK_VERSION=$(\ + curl -Lf -H "Accept: application/vnd.github+json" "https://api.github.com/repos/j178/prek/releases?per_page=1" | jq -r '.[0].tag_name'\ + ) + fi + + local SDK_URL="https://github.com/j178/prek/releases/download/${PREK_VERSION}/prek-installer.sh" + export PREK_INSTALL_DIR=/usr/bin + + curl --proto '=https' --tlsv1.2 -LsSfo prek-installer.sh ${SDK_URL} || { echo "Error: Failed to get prek installer."; exit 1; } + sh prek-installer.sh || { echo "Error: Failed to install prek."; exit 1; } + + rm prek-installer.sh +} + +main() { + echo "Starting prek installation process..." + pkg_mgr_update + install_dependencies + install_prek + + echo "prek installation process completed successfully." + clean_up +} + +main \ No newline at end of file diff --git a/test/prek/alpine.sh b/test/prek/alpine.sh new file mode 100644 index 0000000..455a938 --- /dev/null +++ b/test/prek/alpine.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +# This test file will be executed against an auto-generated devcontainer.json that +# includes the 'hello' Feature with no options. +# +# For more information, see: https://github.com/devcontainers/cli/blob/main/docs/features/test.md +# + +set -e + +# Optional: Import test library bundled with the devcontainer CLI +# See https://github.com/devcontainers/cli/blob/HEAD/docs/features/test.md#dev-container-features-test-lib +# Provides the 'check' and 'reportResults' commands. +source dev-container-features-test-lib + +# Feature-specific tests +# The 'check' command comes from the dev-container-features-test-lib. Syntax is... +# check