Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tahabebek committed Nov 7, 2024
1 parent 465c40e commit 205dab1
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 42 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: 'Test Preternatural Build Action'
on:
pull_request:
branches: [ main ]

jobs:
test-swift-package:
runs-on: ghcr.io/cirruslabs/macos-runner:sonoma
steps:
- name: Test Swift Package Basic Functionality
uses: tahabebek/MockPackage/BuildAction@main
with:
path: ${{ github.sha }}
153 changes: 111 additions & 42 deletions preternatural-build/action.yml
Original file line number Diff line number Diff line change
@@ -1,76 +1,145 @@
name: 'Preternatural Build Action'
description: 'Run Preternatural build command on repositories with a specified Xcode version'
inputs:
derived_data_path:
description: 'The path to the derived data folder'
xcode-version:
description: 'Xcode version to use'
required: false
default: 'latest-stable'
platforms:
description: 'Target platforms (array of: iOS, macOS, tvOS, watchOS, visionOS, all)'
required: false
default: '["macOS"]'
xcode-version:
description: 'Xcode version to use'
required: true
configurations:
description: 'Build configurations (array of: debug, release)'
required: false
default: '["debug", "release"]'
derived_data_path:
description: 'The path to the derived data folder'
required: false
default: DerivedData/ProjectBuild

runs:
using: 'composite'
steps:
- name: Setup Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: ${{ inputs.xcode-version }}

- name: Install Preternatural
if: ${{ !env.ACT }} # Skipping when run locally.
if: ${{ !env.ACT }}
shell: bash
run: |
brew tap PreternaturalAI/preternatural
brew install preternatural
- name: Cache derived data
uses: actions/cache@v3
if: ${{ !env.ACT }} # Skipping when run locally.
- name: Generate project cache key
id: cache-key
shell: bash
run: |
if ls *.xcodeproj/project.pbxproj 1> /dev/null 2>&1; then
echo "Found Xcode project"
PROJECT_NAME=$(basename *.xcodeproj .xcodeproj)
HASH=$(shasum *.xcodeproj/project.pbxproj | cut -d' ' -f1)
elif ls *.xcworkspace/contents.xcworkspacedata 1> /dev/null 2>&1; then
echo "Found Xcode workspace"
PROJECT_NAME=$(basename *.xcworkspace .xcworkspace)
HASH=$(shasum *.xcworkspace/contents.xcworkspacedata | cut -d' ' -f1)
elif [ -f "Package.swift" ]; then
echo "Found Swift package"
PROJECT_NAME=$(swift package describe --type json | awk '/^{/,/^}/' 2>/dev/null | jq -r '.name')
if [ -f "Package.resolved" ]; then
HASH=$(shasum Package.resolved | cut -d' ' -f1)
else
HASH=$(shasum Package.swift | cut -d' ' -f1)
fi
else
echo "No recognized project structure found"
PROJECT_NAME="unknown"
HASH=$(date +%s | shasum | cut -d' ' -f1)
fi
echo "project-name=$PROJECT_NAME" >> $GITHUB_OUTPUT
PLATFORMS_KEY=$(echo '${{ inputs.platforms }}' | tr -d '[]"' | sed 's/, /-/g' | sed 's/,/-/g')
echo "platforms-key=$PLATFORMS_KEY" >> $GITHUB_OUTPUT
echo "package-hash=$HASH" >> $GITHUB_OUTPUT
- name: Cache project derived data
id: cache-derived-data
uses: actions/cache@v4
if: ${{ !env.ACT }}
with:
path: ${{ inputs.derived_data_path || '~/Library/Developer/Xcode/DerivedData' }}
key: ${{ runner.os }}-derived-data-xcode-${{ inputs.xcode-version }}
path: ${{ github.workspace }}/${{ inputs.derived_data_path }}
key: ${{ steps.cache-key.outputs.project-name }}-derived-data-v1-${{ runner.os }}-xcode${{ inputs.xcode-version }}-platforms-${{ steps.cache-key.outputs.platforms-key }}-${{ steps.cache-key.outputs.package-hash }}
restore-keys: |
${{ runner.os }}-derived-data-xcode-${{ inputs.xcode-version }}
- name: Setup Xcode
uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: ${{ inputs.xcode-version }}

${{ steps.cache-key.outputs.project-name }}-derived-data-v1-${{ runner.os }}-xcode${{ inputs.xcode-version }}-platforms-
- name: Debug Cache Status
if: ${{ !env.ACT }}
shell: bash
run: |
echo "==== Cache Status ===="
echo "Project Cache:"
echo "Cache key components:"
echo "- Runner OS: ${{ runner.os }}"
echo "- Xcode Version: ${{ inputs.xcode-version }}"
echo "- Platforms: ${{ inputs.platforms }}"
echo "- Package.resolved hash: ${{ hashFiles('Package.resolved') }}"
echo "- Package.swift hash: ${{ hashFiles('Package.swift') }}"
PROJECT_DERIVED_DATA=${{ github.workspace }}/${{ inputs.derived_data_path }}
echo "Project derived data path: $PROJECT_DERIVED_DATA"
if [ "${{ steps.cache-derived-data.outputs.cache-hit }}" == "true" ]; then
echo "Project cache: HIT"
if [ -d "$PROJECT_DERIVED_DATA" ]; then
echo "Directory exists and contains:"
find "$PROJECT_DERIVED_DATA" -type d -maxdepth 2
echo "Total size:"
du -sh "$PROJECT_DERIVED_DATA"
else
echo "WARNING: Project cache hit but directory doesn't exist!"
fi
else
echo "Project cache: MISS"
fi
echo "=================================="
- name: Execute Preternatural build command
id: build
shell: bash
run: |
defaults write com.apple.dt.Xcode IDESkipMacroFingerprintValidation -bool YES
COMMAND="preternatural build"
if [ -n "${{ inputs.derived_data_path }}" ]; then
COMMAND="$COMMAND --derived-data-path '${{ inputs.derived_data_path }}'"
fi
PLATFORMS=$(echo '${{ inputs.platforms }}' | tr -d '[]' | sed 's/, /,/g')
CONFIGURATIONS=$(echo '${{ inputs.configurations }}' | tr -d '[]' | sed 's/, /,/g')
DERIVED_DATA_PATH=${{ github.workspace }}/${{ inputs.derived_data_path }}
# Handle platforms array
PLATFORMS="${{ join(fromJSON(inputs.platforms), ',') }}"
if [ -n "$PLATFORMS" ]; then
COMMAND="$COMMAND --platforms '$PLATFORMS'"
echo "Processed values:"
echo "PLATFORMS: $PLATFORMS"
echo "CONFIGURATIONS: $CONFIGURATIONS"
echo "DERIVED_DATA_PATH: $DERIVED_DATA_PATH"
cmd="preternatural build"
if [[ -n "\${DERIVED_DATA_PATH}" ]]; then
cmd="\$cmd --derived-data-path \$(printf %q "\${DERIVED_DATA_PATH}")"
fi
# Handle configurations array
CONFIGS="${{ join(fromJSON(inputs.configurations), ',') }}"
if [ -n "$CONFIGS" ]; then
COMMAND="$COMMAND --configurations '$CONFIGS'"
if [[ -n "\${PLATFORMS}" ]]; then
cmd="\$cmd --platforms \${PLATFORMS}"
fi
echo "Executing command: $COMMAND"
eval $COMMAND
if [[ -n "\${CONFIGURATIONS}" ]]; then
cmd="\$cmd --configurations \${CONFIGURATIONS}"
fi
echo "Executing command: $cmd"
defaults write com.apple.dt.Xcode IDESkipMacroFingerprintValidation -bool YES
eval $cmd
- name: Find and copy all xcactivity logs
if: failure()
shell: bash
run: |
if [ -n "${{ inputs.derived_data_path }}" ]; then
DERIVED_DATA_PATH="${{ inputs.derived_data_path }}"
else
DERIVED_DATA_PATH="$HOME/Library/Developer/Xcode/DerivedData"
fi
DERIVED_DATA_PATH=${{ github.workspace }}/${{ inputs.derived_data_path }}
echo "Searching for logs in: $DERIVED_DATA_PATH"
mkdir -p ./artifacts
Expand Down Expand Up @@ -106,23 +175,23 @@ runs:
mkdir -p ./json_logs
for log in ./artifacts/*.xcactivitylog; do
json_file="./json_logs/$(basename "$log" .xcactivitylog).json"
xclogparser parse --file "$log" --output "$json_file"
xclogparser parse --file "$log" --reporter json --output "$json_file"
echo "Contents of $json_file:"
cat "$json_file"
echo "--------------------------------"
done
- name: Upload xcactivity logs (JSON)
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: xcactivity-logs-json
path: ./json_logs/*.json
if-no-files-found: error

- name: Upload xcactivity logs (Raw)
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: xcactivity-logs-raw
path: ./artifacts/*.xcactivitylog
Expand Down

0 comments on commit 205dab1

Please sign in to comment.