Skip to content

Commit

Permalink
Swift plugin template
Browse files Browse the repository at this point in the history
  • Loading branch information
csjones committed Jun 21, 2024
0 parents commit afb7c35
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build

on:
# push:
# branches: [ main ]
# pull_request:
# branches: [ main ]

jobs:
macOS:
name: Test
runs-on: macOS-13
steps:
- uses: actions/checkout@v3
- name: Display tuist commands in macOS
run: swift package template

Linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: docker build .
63 changes: 63 additions & 0 deletions .github/workflows/check-github-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Check GitHub Release

on:
# schedule:
# Daily at 8:20 UTC and 20:20 UTC
# - cron: '20 8,20 * * *'
# # Enables manually running this workflow from the Actions tab
# workflow_dispatch:

jobs:
check:
name: Check GitHub Release
runs-on: ubuntu-latest
env:
BINARY_NAME: lefthook
BINARY_REPO: evilmartians/lefthook
PLUGIN_REPO: 21-DOT-DEV/swift-plugin-template
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SEMANTIC_VERSIONING_REGEX: (v?[0-9]+\.[0-9]+\.[0-9]+)
outputs:
BINARY_NAME: ${{ env.BINARY_NAME }}
BINARY_REPO: ${{ env.BINARY_REPO }}
BINARY_VERSION: ${{ steps.two.outputs.BINARY_VERSION }}
PLUGIN_VERSION: ${{ steps.one.outputs.PLUGIN_VERSION }}
steps:
- name: Get current plugin version
id: one
run: |
PLUGIN_RELEASE=$(gh release list -R ${{ github.repository }} -L 1)
echo "PLUGIN_VERSION=$(echo $PLUGIN_RELEASE | grep -oE "${{ env.SEMANTIC_VERSIONING_REGEX }}" | head -n1)" >> $GITHUB_OUTPUT
- name: Get current binary version
id: two
run: |
BINARY_RELEASE=$(gh release list -R ${{ env.BINARY_REPO }} -L 1)
echo "BINARY_VERSION=$(echo $BINARY_RELEASE | grep -oE "${{ env.SEMANTIC_VERSIONING_REGEX }}" | head -n1)" >> $GITHUB_OUTPUT
- name: Get trimmed binary version
id: three
run: |
# The example binary includes a leading 'v' in the release version number. We drop it on the next line.
echo "TRIMMED_BINARY_VERSION=$(echo ${{ steps.two.outputs.BINARY_VERSION }} | cut -c2-)" >> $GITHUB_OUTPUT
debug:
name: Debug Outputs
needs: check
runs-on: ubuntu-latest
steps:
- name: Print outputs from check job
run: |
echo "${{ needs.check.outputs.BINARY_NAME }}"
echo "${{ needs.check.outputs.BINARY_REPO }}"
echo "${{ needs.check.outputs.BINARY_VERSION }}"
echo "${{ github.repository }}"
echo "${{ needs.check.outputs.PLUGIN_VERSION }}"
echo "${{ needs.check.outputs.TRIMMED_BINARY_VERSION }}"
trigger:
name: Trigger Artifact Publish
needs: check
uses: ./.github/workflows/publish-artifact-bundle.yml
with:
binary_name: ${{ needs.check.outputs.BINARY_NAME }}
binary_repo: ${{ needs.check.outputs.BINARY_REPO }}
binary_version: ${{ needs.check.outputs.BINARY_VERSION }}
plugin_repo: ${{ github.repository }}
if: ${{ needs.check.outputs.PLUGIN_VERSION != needs.check.outputs.TRIMMED_BINARY_VERSION }}
95 changes: 95 additions & 0 deletions .github/workflows/publish-artifact-bundle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Publish Artifact Bundle

on:
# workflow_call:
# inputs:
# binary_name:
# required: true
# type: string
# binary_repo:
# required: true
# type: string
# binary_version:
# required: true
# type: string
# plugin_repo:
# required: true
# type: string

# # Enables manually running this workflow from the Actions tab
# workflow_dispatch:
# inputs:
# binary_name:
# required: true
# type: string
# binary_repo:
# required: true
# type: string
# binary_version:
# required: true
# type: string
# plugin_repo:
# required: true
# type: string

jobs:
publish:
name: Publish Artifact Bundle
runs-on: macOS-13
env:
PLUGIN_VERSION: ${{ inputs.binary_version }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
with:
path: ${{ inputs.binary_name }}
- name: Get latest binaries
run: |
gh release download ${{ inputs.binary_version }} \
-R "${{ inputs.binary_repo }}" \
-p '*MacOS*64' \
-p '*Linux*64' \
-p '*Windows*64.exe' \
-p '*.deb' \
-p '*.rpm' \
-D '${{ inputs.binary_name }}/Resources/template.artifactbundle'
- name: Unzip Tuist
run: |
7z x "${{ inputs.binary_name }}/Resources/tuist.zip" -o"${{ inputs.binary_name }}/Resources/template.artifactbundle"
- name: Update GitHub Env Vars
run: |
# The example binary includes a leading 'v' in the release version number. We drop it on the next line.
echo "PLUGIN_VERSION=$(echo $PLUGIN_VERSION | cut -c2-)" >> $GITHUB_ENV
echo "ARTIFACT_NAME=${{ inputs.binary_name }}.artifactbundle.zip" >> $GITHUB_ENV
echo "ARTIFACT_PATH=${{ inputs.binary_name }}/Resources/template.artifactbundle" >> $GITHUB_ENV
echo "ESCAPED_PLUGIN_REPO=$(echo "${{ inputs.plugin_repo }}" | sed 's/\//\\\//g')" >> $GITHUB_ENV
- name: Set info.json version
run: |
sed -i '' "s/<VERSION>/${{ env.PLUGIN_VERSION }}/g" ${{ env.ARTIFACT_PATH }}/info.json
sed -i '' "s/<TEMPLATE>/${{ inputs.binary_name }}/g" ${{ env.ARTIFACT_PATH }}/info.json
- name: Add executable permissions
run: |
# The example binaries for MacOS need the executable permission added.
chmod +x "${{ env.ARTIFACT_PATH }}/${{ inputs.binary_name }}_${{ env.PLUGIN_VERSION }}_MacOS_arm64"
chmod +x "${{ env.ARTIFACT_PATH }}/${{ inputs.binary_name }}_${{ env.PLUGIN_VERSION }}_MacOS_x86_64"
- name: Zip Artifact Bundle
run: |
(cd "${{ env.ARTIFACT_PATH }}" && 7z a -tzip -mx=9 "${{ env.ARTIFACT_NAME }}" *)
- name: Reset Git Repo
run: |
(cd ${{ inputs.binary_name }} && git reset --hard)
- name: Update Package.swift
run: |
sed -i '' "s/checksum: \".*\"/checksum: \"$(shasum -a 256 '${{ env.ARTIFACT_PATH }}/${{ env.ARTIFACT_NAME }}' | sed 's/ .*//')\"/g" ${{ inputs.binary_name }}/Package.swift
sed -i '' -E "s/\/[0-9]+\.[0-9]+\.[0-9]+\//\/${{ env.PLUGIN_VERSION }}\//" ${{ inputs.binary_name }}/Package.swift
sed -i '' -E "s/(https:\/\/github.com\/${{ env.ESCAPED_PLUGIN_REPO }}.git\", exact: \")([0-9]+\.[0-9]+\.[0-9]+)/\1${{ env.PLUGIN_VERSION }}/" ${{ inputs.binary_name }}/README.md
- name: Push Changes to GitHub
run: |
(cd ${{ inputs.binary_name }} && git commit -am "Updating ${{ inputs.binary_name }} to ${{ env.PLUGIN_VERSION }}" && git push origin)
- name: Create GitHub Release
run: |
echo $(cd ${{ inputs.binary_name }} && git rev-parse HEAD)
gh release create ${{ env.PLUGIN_VERSION }} \
-R ${{ inputs.plugin_repo }} \
--target $(cd ${{ inputs.binary_name }} && git rev-parse HEAD) \
"${{ env.ARTIFACT_PATH }}/${{ env.ARTIFACT_NAME }}"
90 changes: 90 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
.swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Use an official Swift runtime image
FROM swift:5.8.1

# Copies the root directory of the repository into the image's filesystem at `/Linux`
ADD . /Linux

# Set the working directory to `/Linux`
WORKDIR /Linux

# List available plugins in Linux
RUN swift package plugin template
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 GigaBitcoin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// swift-tools-version:5.6

import PackageDescription

let package = Package(
name: "template",
products: [
.plugin(
name: "template",
targets: [
"TemplatePlugin"
]
)
],
dependencies: [],
targets: [
.plugin(
name: "TemplatePlugin",
capability: .command(
intent: .custom(
verb: "template",
description: "Execute commands defined by template."
)
),
dependencies: ["template"]
),
// .binaryTarget(name: "template", path: "template.artifactbundle.zip"),
.binaryTarget(
name: "template",
url: "https://github.com/GigaBitcoin/template-plugin/releases/download/0.0.1/template.artifactbundle.zip",
checksum: "42e1e7a4f7d7586ec6d13b3e03cce5612ac237244cc3cb1e6de7c49416d04520"
),
],
swiftLanguageVersions: [.v5]
)
41 changes: 41 additions & 0 deletions Plugins/TemplatePlugin/TemplatePlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// TemplatePlugin.swift
// GigaBitcoin/template-plugin
//
// Copyright (c) 2023 GigaBitcoin LLC
// Distributed under the MIT software license
//
// See the accompanying file LICENSE for information
//

import Foundation
import PackagePlugin

@main
struct TemplatePlugin: CommandPlugin {
func performCommand(
context: PackagePlugin.PluginContext,
arguments: [String]
) async throws {
let binary = try context.tool(named: "template")
let process = Process()

process.executableURL = URL(filePath: binary.path.string)
process.arguments = arguments

try process.run()
process.waitUntilExit()

// Check whether the `template` invocation was successful.
guard process.terminationReason == .exit && process.terminationStatus == 0 else {
Diagnostics.error("""
'template' invocation failed with a nonzero exit code: '\(process.terminationStatus)'.
See 'swift package plugin template --help' for details.
"""
)

return
}
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# template-plugin
GitHub template for Swift plugins

Goal: Wrap pre-built binaries into an artifact bundle for use in a Swift plugin
Loading

0 comments on commit afb7c35

Please sign in to comment.