Skip to content
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

Streamline the autoformat workflow to enable version pinning for all platforms #571

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .github/autoformat.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EVO_CLANGFORMAT_VERSION=19.1.2
EVO_STYLUA_VERSION=v0.20.0
71 changes: 48 additions & 23 deletions .github/workflows/autoformat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,66 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- os: ubuntu-latest
stylua: stylua-linux.zip
exe: ""
- os: macos-latest
stylua: stylua-macos.zip
exe: ""
- os: windows-latest
stylua: stylua-win64.zip
exe: ".exe"
defaults:
run:
shell: bash

steps:
# MSYS needs to be available before running any git commands
- name: Install clang-format (via MSYS)
if: runner.os == 'Windows'
uses: msys2/setup-msys2@v2
with:
install: git mingw-w64-x86_64-clang

- name: Disable autocrlf # Messes up everything on Windows since the formatter applies \n
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Check out Git repository
uses: actions/checkout@v4
with:
fetch-depth: 1
submodules: false

- name: Set up environment
run: |
set -a
source .github/autoformat.env
cat .github/autoformat.env >> $GITHUB_ENV
echo $(pwd) >> $GITHUB_PATH
- name: Set up StyLua
uses: JohnnyMorganz/[email protected]
- name: Cache clang-format binary
id: cache-clang-format
uses: actions/cache@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --check . --verbose
version: v0.20.0

# The preinstalled version is positively antique; replace with the latest and greatest (to match MSYS)
- name: Install clang-format (via APT)
if: runner.os == 'Linux'
run: ./deps/install-clang-format.sh

- name: Install clang-format (via Homebrew)
if: runner.os == 'macOS'
run: brew install clang-format

path: clang-format${{ matrix.exe }}
key: clang-format-${{ runner.os }}-${{ env.EVO_CLANGFORMAT_VERSION }}
restore-keys: |
clang-format-${{ runner.os }}-${{ env.EVO_CLANGFORMAT_VERSION }}
- name: Install clang-format
if: steps.cache-clang-format.outputs.cache-hit != 'true'
run: deps/get-clang-format.sh

- name: Verify clang-format version
run: which clang-format && clang-format --version

- name: Install stylua
run: |
curl --location --output ${{ matrix.stylua }} https://github.com/JohnnyMorganz/StyLua/releases/download/${{ env.EVO_STYLUA_VERSION }}/${{ matrix.stylua }}
unzip ${{ matrix.stylua }}
chmod +x stylua
- name: Verify stylua version
run: which stylua && stylua --version

- name: Run autoformat
run: ./autoformat.sh

- name: Check for inconsistent formatting
run: git --no-pager diff --exit-code -b . #The -b is for inconsistent newlines, which we ignore (for now)
run: git --no-pager diff --exit-code -b .
21 changes: 8 additions & 13 deletions autoformat.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# See deps/install-clang-format.sh for the required version (should always match)
REQUIRED_CLANG_FORMAT_VERSION="17"
CLANG_FORMAT="clang-format-$REQUIRED_CLANG_FORMAT_VERSION"

# The MSYS version may lag behind somewhat, so a fallback option is needed
if ! command -v $CLANG_FORMAT &> /dev/null
then
echo "clang-format-$REQUIRED_CLANG_FORMAT_VERSION not found. Using the default clang-format instead"
echo
CLANG_FORMAT="clang-format"
fi
#!/bin/bash
set -aeuo pipefail

STYLUA="stylua"
CLANG_FORMAT="clang-format"
echo "Installed formatters:"
echo

echo "* " $(stylua --version)
echo "* " $($CLANG_FORMAT --version)
echo $(which $STYLUA)
echo $($STYLUA --version)
echo $(which $CLANG_FORMAT)
echo $($CLANG_FORMAT --version)
echo

echo "Formatting Lua sources ..."
Expand Down
50 changes: 50 additions & 0 deletions deps/get-clang-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
set -aeuo pipefail
source .github/autoformat.env

GITHUB_ORGANIZATION="llvm"
GITHUB_REPOSITORY="llvm-project"
REQUIRED_LLVM_VERSION=$EVO_CLANGFORMAT_VERSION # Pinned via ENV file to avoid headaches in CI runs
LLVMORG_SUFFIX="llvmorg-$REQUIRED_LLVM_VERSION"
GITHUB_BASE_URL="https://github.com/$GITHUB_ORGANIZATION/$GITHUB_REPOSITORY/releases/download/$LLVMORG_SUFFIX"

echo "Downloading clang-format release for version $REQUIRED_LLVM_VERSION"

PLATFORM=$(uname)
echo "Detected platform: $PLATFORM"

case $PLATFORM in
MINGW64_NT*|CYGWIN_NT*|MSYS_NT*)
LLVM_ASSET_NAME="LLVM-$REQUIRED_LLVM_VERSION-Windows-X64"
CLANG_FORMAT_EXECUTABLE="clang-format.exe"
;;
Linux)
LLVM_ASSET_NAME="LLVM-$REQUIRED_LLVM_VERSION-Linux-X64"
CLANG_FORMAT_EXECUTABLE="clang-format"
;;
Darwin)
LLVM_ASSET_NAME="LLVM-$REQUIRED_LLVM_VERSION-macOS-ARM64"
CLANG_FORMAT_EXECUTABLE="clang-format"
;;
*)
echo "Unsupported platform: $PLATFORM"
exit 1
;;
esac

LLVM_RELEASE_ASSET="$LLVM_ASSET_NAME.tar.xz"
TARBALL_DOWNLOAD_DIR=$(pwd)/deps
TARBALL_DOWNLOAD_FILE="$TARBALL_DOWNLOAD_DIR/$LLVM_RELEASE_ASSET"

LLVM_RELEASE_URL="$GITHUB_BASE_URL/$LLVM_RELEASE_ASSET"
echo "Fetching $LLVM_RELEASE_URL ..."
curl --location --output "$TARBALL_DOWNLOAD_FILE" "$LLVM_RELEASE_URL"

CLANG_FORMAT_PATH="$LLVM_ASSET_NAME/bin/$CLANG_FORMAT_EXECUTABLE"
echo "Unpacking $CLANG_FORMAT_PATH ..."
tar --strip-components=2 -xvf "$TARBALL_DOWNLOAD_FILE" "$CLANG_FORMAT_PATH"

chmod +x "./$CLANG_FORMAT_EXECUTABLE"

echo "Cleanup: Removing $TARBALL_DOWNLOAD_FILE ..."
rm $TARBALL_DOWNLOAD_FILE
27 changes: 0 additions & 27 deletions deps/install-clang-format.sh

This file was deleted.

Loading