Files
moeka-project/.github/actions/setup-swiftlint/action.yml
T

135 lines
4.8 KiB
YAML

name: 'Setup SwiftLint'
description: 'Download and install SwiftLint binary to /usr/bin'
branding:
icon: 'code'
color: 'blue'
inputs:
version:
description: 'SwiftLint version (e.g., 0.58.2). If not specified, the latest version will be used'
required: false
default: 'latest'
runs:
using: 'composite'
steps:
- name: Detect OS and architecture
id: os
shell: bash
run: |
ARCH=$(uname -m)
if [[ "$ARCH" == "x86_64" ]]; then
ARCH_SUFFIX="amd64"
elif [[ "$ARCH" == "aarch64" ]] || [[ "$ARCH" == "arm64" ]]; then
ARCH_SUFFIX="arm64"
else
echo "Error: Unsupported architecture $ARCH"
exit 1
fi
if [[ "$RUNNER_OS" == "macOS" ]]; then
echo "platform=macos" >> $GITHUB_OUTPUT
echo "archive_name=portable_swiftlint.zip" >> $GITHUB_OUTPUT
elif [[ "$RUNNER_OS" == "Linux" ]]; then
echo "platform=linux" >> $GITHUB_OUTPUT
echo "archive_name=swiftlint_linux.zip" >> $GITHUB_OUTPUT
else
echo "Error: Unsupported OS $RUNNER_OS"
exit 1
fi
echo "arch=$ARCH_SUFFIX" >> $GITHUB_OUTPUT
- name: Resolve version
id: version
shell: bash
run: |
if [[ "${{ inputs.version }}" == "latest" ]]; then
# Fetch the latest release tag using a more robust method
CURL_ARGS=(-s)
if [[ -n "${{ github.token }}" ]]; then
CURL_ARGS+=(-H "Authorization: Bearer ${{ github.token }}")
fi
CURL_ARGS+=(-H "Accept: application/vnd.github+json")
CURL_ARGS+=(-H "X-GitHub-Api-Version: 2022-11-28")
CURL_ARGS+=("https://api.github.com/repos/realm/SwiftLint/releases/latest")
API_RESPONSE=$(curl "${CURL_ARGS[@]}")
# Try using jq if available (most GitHub Actions runners have it)
if command -v jq >/dev/null 2>&1; then
VERSION=$(echo "$API_RESPONSE" | jq -r '.tag_name // empty')
else
# Fallback: parse JSON manually with sed, being very specific about the tag_name field
VERSION=$(echo "$API_RESPONSE" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1)
fi
# Validate the version looks correct (should contain version numbers)
if [[ -z "$VERSION" ]] || [[ "$VERSION" == "null" ]] || [[ ! "$VERSION" =~ ^[0-9] ]]; then
echo "Error: Failed to get latest version or got invalid version: '$VERSION'"
echo "API Response (first 50 lines):"
echo "$API_RESPONSE" | head -50
exit 1
fi
echo "Latest version: $VERSION"
else
VERSION="${{ inputs.version }}"
# Remove 'v' prefix if user provided it, since SwiftLint tags don't use it
VERSION="${VERSION#v}"
echo "Using specified version: $VERSION"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Download and install SwiftLint
shell: bash
run: |
set -e
PLATFORM="${{ steps.os.outputs.platform }}"
VERSION="${{ steps.version.outputs.version }}"
ARCH="${{ steps.os.outputs.arch }}"
ARCHIVE_NAME="${{ steps.os.outputs.archive_name }}"
if [[ "$PLATFORM" == "linux" ]]; then
ARCHIVE_NAME="${ARCHIVE_NAME%.zip}_${ARCH}.zip"
fi
DOWNLOAD_URL="https://github.com/realm/SwiftLint/releases/download/${VERSION}/${ARCHIVE_NAME}"
echo "Downloading SwiftLint from $DOWNLOAD_URL..."
if ! curl -L -f -o swiftlint.zip "$DOWNLOAD_URL"; then
ARCHIVE_NAME_FALLBACK="${ARCHIVE_NAME%.zip}.zip"
DOWNLOAD_URL="https://github.com/realm/SwiftLint/releases/download/${VERSION}/${ARCHIVE_NAME_FALLBACK}"
echo "Trying fallback URL: $DOWNLOAD_URL"
if ! curl -L -f -o swiftlint.zip "$DOWNLOAD_URL"; then
echo "Error: Download failed, please check if the version is correct"
exit 1
fi
fi
echo "Extracting..."
EXTRACT_DIR=$(mktemp -d)
unzip -q -o swiftlint.zip -d "$EXTRACT_DIR"
if [[ -f "$EXTRACT_DIR/swiftlint" ]]; then
BINARY_PATH="$EXTRACT_DIR/swiftlint"
elif [[ -f "$EXTRACT_DIR/swiftlint_linux/swiftlint" ]]; then
BINARY_PATH="$EXTRACT_DIR/swiftlint_linux/swiftlint"
elif [[ -f "$EXTRACT_DIR/portable_swiftlint/swiftlint" ]]; then
BINARY_PATH="$EXTRACT_DIR/portable_swiftlint/swiftlint"
else
echo "Error: Could not find swiftlint binary"
ls -la "$EXTRACT_DIR"
exit 1
fi
chmod +x "$BINARY_PATH"
sudo mv "$BINARY_PATH" /usr/bin/swiftlint
if ! swiftlint version; then
echo "Error: SwiftLint installation verification failed"
exit 1
fi
rm -rf swiftlint.zip "$EXTRACT_DIR" 2>/dev/null || true