From 10b0f10bbf7b9e12d660e4f690238b463237c35a Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Sun, 10 Dec 2023 17:54:00 -0800 Subject: [PATCH] use a python venv (#173) * use a python venv adheres to PEP668 and addresses #171 * use powershell to manage py venv in windows runners * leverage steps.if with duplicated scripts should support thee OS's native shell * Add warning to README about debian only support --- README.md | 5 +++ action.yml | 122 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 88 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 3fda4212..4f8c1301 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ A Github Action for linting C/C++ code integrating clang-tidy and clang-format to collect feedback provided in the form of annotations, thread comments, and step summary. +> [!WARNING] +> We only support Linux runners using a Debian based Linux OS (like Ubuntu and many others). +> +> MacOS and Windows runners are supported as well. + ## What's New v2 diff --git a/action.yml b/action.yml index 02a7cfac..bb70c266 100644 --- a/action.yml +++ b/action.yml @@ -102,58 +102,102 @@ inputs: outputs: checks-failed: description: An integer that can be used as a boolean value to indicate if any checks failed by clang-tidy and clang-format. - value: ${{ steps.cpp-linter.outputs.checks-failed }} + value: ${{ steps.cpp-linter-unix.outputs.checks-failed || steps.cpp-linter-windows.outputs.checks-failed }} clang-tidy-checks-failed: description: An integer that can be used as a boolean value to indicate if any checks failed by clang-tidy only. - value: ${{ steps.cpp-linter.outputs.clang-tidy-checks-failed }} + value: ${{ steps.cpp-linter-unix.outputs.clang-tidy-checks-failed || steps.cpp-linter-windows.outputs.clang-tidy-checks-failed }} clang-format-checks-failed: description: An integer that can be used as a boolean value to indicate if any checks failed by clang-format only. - value: ${{ steps.cpp-linter.outputs.clang-format-checks-failed }} + value: ${{ steps.cpp-linter-unix.outputs.clang-format-checks-failed || steps.cpp-linter-windows.outputs.clang-format-checks-failed }} runs: using: "composite" steps: - - name: Install action dependencies + - name: Install python + uses: actions/setup-python@v4 + id: setup-python + with: + python-version: '3.11' + update-environment: false + + - name: Install Linux clang dependencies + if: runner.os == 'Linux' shell: bash run: | - if [[ "${{runner.os}}" == "Linux" ]]; then - sudo apt-get update - # First try installing from default Ubuntu repositories before trying LLVM script - if ! sudo apt-get install -y clang-format-${{ inputs.version }} clang-tidy-${{ inputs.version }}; then - # This LLVM script will add the relevant LLVM PPA: https://apt.llvm.org/ - wget https://apt.llvm.org/llvm.sh -O $GITHUB_ACTION_PATH/llvm_install.sh - chmod +x $GITHUB_ACTION_PATH/llvm_install.sh - if sudo $GITHUB_ACTION_PATH/llvm_install.sh ${{ inputs.version }}; then - sudo apt-get install -y clang-format-${{ inputs.version }} clang-tidy-${{ inputs.version }} - fi + sudo apt-get update + # First try installing from default Ubuntu repositories before trying LLVM script + if ! sudo apt-get install -y clang-format-${{ inputs.version }} clang-tidy-${{ inputs.version }}; then + # This LLVM script will add the relevant LLVM PPA: https://apt.llvm.org/ + wget https://apt.llvm.org/llvm.sh -O $GITHUB_ACTION_PATH/llvm_install.sh + chmod +x $GITHUB_ACTION_PATH/llvm_install.sh + if sudo $GITHUB_ACTION_PATH/llvm_install.sh ${{ inputs.version }}; then + sudo apt-get install -y clang-format-${{ inputs.version }} clang-tidy-${{ inputs.version }} fi fi - if [[ "${{runner.os}}" == "macOS" ]]; then - python3 -m venv "$GITHUB_ACTION_PATH/venv" - source "$GITHUB_ACTION_PATH/venv/bin/activate" - fi - python3 -m pip install -r "$GITHUB_ACTION_PATH/requirements.txt" + + - name: Setup python venv (Unix) + if: runner.os == 'Linux' || runner.os == 'macOS' + shell: bash + run: | + ${{ steps.setup-python.outputs.python-path }} -m venv "$GITHUB_ACTION_PATH/venv" + source "$GITHUB_ACTION_PATH/venv/bin/activate" + pip install -r "$GITHUB_ACTION_PATH/requirements.txt" clang-tools -i ${{ inputs.version }} -b - - name: Run cpp-linter - id: cpp-linter + - name: Run cpp-linter (Unix) + id: cpp-linter-unix + if: runner.os == 'Linux' || runner.os == 'macOS' shell: bash run: | - if [[ "${{runner.os}}" == "macOS" ]]; then - source "$GITHUB_ACTION_PATH/venv/bin/activate" - fi + source "$GITHUB_ACTION_PATH/venv/bin/activate" + cpp-linter \ - --style="${{ inputs.style }}" \ - --extensions=${{ inputs.extensions }} \ - --tidy-checks="${{ inputs.tidy-checks }}" \ - --repo-root=${{ inputs.repo-root }} \ - --version=${{ inputs.version }} \ - --verbosity=${{ inputs.verbosity }} \ - --lines-changed-only=${{ inputs.lines-changed-only }} \ - --files-changed-only=${{ inputs.files-changed-only }} \ - --thread-comments=${{ inputs.thread-comments }} \ - --no-lgtm=${{ inputs.no-lgtm }} \ - --step-summary=${{ inputs.step-summary }} \ - --ignore="${{ inputs.ignore }}" \ - --database=${{ inputs.database }} \ - --file-annotations=${{ inputs.file-annotations }} \ - --extra-arg="${{ inputs.extra-args }}" + --style="${{ inputs.style }}" \ + --extensions=${{ inputs.extensions }} \ + --tidy-checks="${{ inputs.tidy-checks }}" \ + --repo-root=${{ inputs.repo-root }} \ + --version=${{ inputs.version }} \ + --verbosity=${{ inputs.verbosity }} \ + --lines-changed-only=${{ inputs.lines-changed-only }} \ + --files-changed-only=${{ inputs.files-changed-only }} \ + --thread-comments=${{ inputs.thread-comments }} \ + --no-lgtm=${{ inputs.no-lgtm }} \ + --step-summary=${{ inputs.step-summary }} \ + --ignore="${{ inputs.ignore }}" \ + --database=${{ inputs.database }} \ + --file-annotations=${{ inputs.file-annotations }} \ + --extra-arg="${{ inputs.extra-args }}" + + - name: Setup python venv (Windows) + if: runner.os == 'Windows' + shell: pwsh + run: | + ${{ steps.setup-python.outputs.python-path }} -m venv "$env:GITHUB_ACTION_PATH/venv" + Invoke-Expression -Command "$env:GITHUB_ACTION_PATH/venv/Scripts/Activate.ps1" + pip install -r "$env:GITHUB_ACTION_PATH/requirements.txt" + clang-tools -i ${{ inputs.version }} -b + + - name: Run cpp-linter (Windows) + id: cpp-linter-windows + if: runner.os == 'Windows' + shell: pwsh + run: | + Invoke-Expression -Command "$env:GITHUB_ACTION_PATH/venv/Scripts/Activate.ps1" + + $app = 'cpp-linter' + + ' --style="${{ inputs.style }}"' + + ' --extensions=${{ inputs.extensions }}' + + ' --tidy-checks="${{ inputs.tidy-checks }}"' + + ' --repo-root=${{ inputs.repo-root }}' + + ' --version=${{ inputs.version }}' + + ' --verbosity=${{ inputs.verbosity }}' + + ' --lines-changed-only=${{ inputs.lines-changed-only }}' + + ' --files-changed-only=${{ inputs.files-changed-only }}' + + ' --thread-comments=${{ inputs.thread-comments }}' + + ' --no-lgtm=${{ inputs.no-lgtm }}' + + ' --step-summary=${{ inputs.step-summary }}' + + ' --ignore="${{ inputs.ignore }}"' + + ' --database=${{ inputs.database }}' + + ' --file-annotations=${{ inputs.file-annotations }}' + + ' --extra-arg="${{ inputs.extra-args }}"' + + Invoke-Expression -Command $app