.github/workflows/check.yml

name: Check

on:
  push:
    branches: [main]
  pull_request:

jobs:
  links:
    runs-on: ${{ fromJSON('["self-hosted", "homelab"]') }}
    steps:
      - uses: actions/checkout@v5
      - uses: lycheeverse/lychee-action@v2
        with:
          args: --offline --include-fragments --no-progress README.md 'skills/**/*.md'

  swift:
    # The newest stable Xcode, which the skills are written against.
    runs-on: ${{ fromJSON('["self-hosted", "mini"]') }}
    env:
      BLOCKS: .build/blocks
    steps:
      - uses: actions/checkout@v5
      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: latest-stable
      - name: The Swift version matches the one the skills and README describe
        run: |
          actual=$(swift --version 2>&1 | sed -n 's/.*Swift version \([0-9]*\.[0-9]*\).*/\1/p' | head -1)
          echo "Swift $actual"
          for doc in README.md skills/swift-best-practices/SKILL.md; do
            stated=$(sed -n 's/^The rules\( below\)\{0,1\} describe Swift \([0-9.]*\) .*/\2/p' "$doc")
            if [ "$stated" != "$actual" ]; then
              echo "::error file=$doc::describes Swift $stated, but CI runs Swift $actual"
              exit 1
            fi
          done

      - name: Each file in lints/check triggers the warning it expects
        run: |
          # Strip the color and hyperlink escapes that swift build adds to diagnostics.
          output=$( (swift build --package-path lints; swift format lint --recursive lints/check) 2>&1 |
            perl -pe 's/\e\]8;;.*?\e\\//g; s/\e\[[0-9;]*m//g')
          failed=0
          for file in lints/check/*.swift; do
            rule=$(sed -n 's/^\/\/ Expect: .*\[\(.*\)\].*/\1/p' "$file")
            if ! grep -F "$(basename "$file")" <<<"$output" | grep -F "[$rule]" > /dev/null; then
              echo "::error file=$file::expected [$rule]"
              failed=1
            fi
          done
          exit $failed

      # A block with `// Sources/<target>/<file>.swift` lines is a package example. Its
      # files go into .github/examples/<doc name>/, which holds the package manifest.
      # The new side of a ```diff block in a skill or reference becomes a script that
      # imports Foundation. Diffs in assets/ are templates, not Swift.
      - name: Extract every Swift block in the skills
        run: |
          mkdir -p "$BLOCKS"
          for doc in $(git ls-files 'skills/*.md' ':!:skills/*/evals/*'); do
            awk -v prefix="$BLOCKS/$(echo "$doc" | tr / _)" \
              -v package=".github/examples/$(basename "$doc" .md)" '
              /^```swift/ { n++; on = 1; block = ""; next }
              /^```/ && on {
                on = 0
                if (block ~ /(^|\n)\/\/ Sources\//) {
                  split(block, lines, "\n")
                  for (i = 1; i < length(lines); i++) {
                    if (lines[i] ~ /^\/\/ Sources\//) {
                      blank = 0
                      file = package "/" substr(lines[i], 4)
                      system("mkdir -p \"$(dirname \"" file "\")\"")
                    } else if (lines[i] == "") blank++
                    else {
                      for (; blank > 0; blank--) print "" > file
                      print lines[i] > file
                    }
                  }
                } else printf "%s", block > (prefix "-" n ".swift")
                next
              }
              on { block = block $0 "\n" }' "$doc"
          done
          mkdir -p "$BLOCKS/diffs"
          for doc in $(git ls-files 'skills/*.md' ':!:skills/*/evals/*' ':!:skills/*/assets/*'); do
            awk -v prefix="$BLOCKS/diffs/$(echo "$doc" | tr / _)" '
              /^```diff/ { n++; on = 1; file = prefix "-" n ".swift"; print "import Foundation" > file; next }
              /^```/ { on = 0 }
              on && /^[ +]/ { print substr($0, 2) > file }
              on && /^$/ { print "" > file }' "$doc"
          done

      - name: Build the package examples
        run: |
          for package in .github/examples/*/; do
            swift build --package-path "$package"
          done

      # -emit-sil runs the data race checks that -typecheck skips. Each block compiles
      # twice, once as library code and once as an app target that is on the main actor
      # by default.
      - name: Compile the blocks with the settings from lints/Package.swift
        run: |
          dev=$(xcode-select -p)
          toolchain=$(dirname "$(dirname "$(xcrun -f swiftc)")")
          failed=0
          for block in "$BLOCKS"/*.swift "$BLOCKS"/diffs/*.swift; do
            mode=-parse-as-library
            grep -q '^let package' "$block" && mode=
            case "$block" in "$BLOCKS"/diffs/*) mode= ;; esac
            for isolation in nonisolated MainActor; do
              if ! xcrun swiftc -emit-sil -o /dev/null -swift-version 6 $mode \
                -default-isolation "$isolation" \
                -enable-upcoming-feature NonisolatedNonsendingByDefault \
                -enable-upcoming-feature InferIsolatedConformances \
                -enable-upcoming-feature ExistentialAny \
                -enable-upcoming-feature InternalImportsByDefault \
                -strict-memory-safety \
                -Werror ExistentialAny -Werror StrictMemorySafety -Werror DeprecatedDeclaration \
                -I "$toolchain/lib/swift/pm/ManifestAPI" -package-description-version 6.2 \
                -plugin-path "$toolchain/lib/swift/host/plugins/testing" \
                -F "$dev/Platforms/MacOSX.platform/Developer/Library/Frameworks" \
                -I "$dev/Platforms/MacOSX.platform/Developer/usr/lib" \
                "$block"; then
                echo "::error::$(basename "$block") does not compile with default isolation $isolation"
                failed=1
              fi
            done
          done
          exit $failed

      - name: Format lint every block with lints/.swift-format
        run: |
          swift format lint --strict --configuration lints/.swift-format \
            "$BLOCKS"/*.swift "$BLOCKS"/diffs/*.swift .github/examples/*/Sources/*/*.swift