Skip to content

Commit

Permalink
CONFIG: Updates to build (#83)
Browse files Browse the repository at this point in the history
* CONFIG: Updates to build

* Fixed workflow

* Fixed security alert

* Update documentation and README
  • Loading branch information
adamayoung authored Apr 10, 2023
1 parent a81647f commit 764e59e
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 46 deletions.
49 changes: 16 additions & 33 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,36 @@ on:
pull_request:
branches:
- main

env:
DEVELOPER_DIR: /Applications/Xcode_14.0.app/Contents/Developer
workflow_dispatch:

concurrency:
group: "ci-${{ github.head_ref || github.run_id }}"
cancel-in-progress: true

jobs:
lint:
name: Lint
build-test:
name: Build and Test
runs-on: macos-12
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Cache SPM
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: .build
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
${{ runner.os }}-spm-
- name: Lint
run: swift package plugin swiftlint --strict --reporter github-actions-logging

- name: swiftlint
run: swiftlint --strict --reporter github-actions-logging
- name: Build
run: swift build --build-tests

- name: Test
run: swift test --skip-build --parallel

codeScan:
name: Code Scan
Expand All @@ -41,7 +45,7 @@ jobs:
security-events: write
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Scan
uses: MobSF/mobsfscan@main
Expand All @@ -53,36 +57,15 @@ jobs:
with:
sarif_file: results.sarif

build-test:
name: Build and Test
runs-on: macos-12
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Cache SPM
uses: actions/cache@v2
with:
path: .build
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
${{ runner.os }}-spm-
- name: Build
run: swift build --build-tests

- name: Test
run: swift test --skip-build --parallel

build-documentation:
name: Build Documentation
runs-on: macos-12
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Cache SPM
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: .build
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ permissions:
pages: write
id-token: write

env:
DEVELOPER_DIR: /Applications/Xcode_14.0.app/Contents/Developer

concurrency:
group: "documentation"
cancel-in-progress: true
Expand Down
11 changes: 10 additions & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-plugin.git",
"state" : {
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6",
"revision" : "9b1258905c21fc1b97bf03d1b4ca12c4ec4e5fda",
"version" : "1.2.0"
}
},
{
"identity" : "swift-docc-symbolkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-docc-symbolkit",
"state" : {
"revision" : "b45d1f2ed151d057b54504d653e0da5552844e34",
"version" : "1.0.0"
}
}
Expand Down
38 changes: 36 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// swift-tools-version:5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "TMDb",

defaultLocalization: "en",

platforms: [
.macOS(.v12), .iOS(.v15), .tvOS(.v15), .watchOS(.v8)
],
Expand All @@ -14,26 +17,57 @@ let package = Package(
],

dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.0.0")
.package(url: "https://github.com/apple/swift-docc-plugin.git", from: "1.2.0")
],

targets: [
.target(
name: "TMDb"
name: "TMDb",
plugins: [
.plugin(name: "SwiftLintPlugin")
]
),
.testTarget(
name: "TMDbTests",
dependencies: ["TMDb"],
resources: [
.process("Resources")
],
plugins: [
.plugin(name: "SwiftLintPlugin")
]
),
.testTarget(
name: "TMDbIntegrationTests",
dependencies: ["TMDb"],
resources: [
.process("Resources")
],
plugins: [
.plugin(name: "SwiftLintPlugin")
]
),

.plugin(
name: "SwiftLintPlugin",
capability: .buildTool(),
dependencies: ["SwiftLintBinary"]
),
.plugin(
name: "SwiftLint",
capability: .command(
intent: .custom(
verb: "swiftlint",
description: "SwiftLint."
)
),
dependencies: ["SwiftLintBinary"]
),

.binaryTarget(
name: "SwiftLintBinary",
url: "https://github.com/realm/SwiftLint/releases/download/0.51.0/SwiftLintBinary-macos.artifactbundle.zip",
checksum: "9fbfdf1c2a248469cfbe17a158c5fbf96ac1b606fbcfef4b800993e7accf43ae"
)
]
)
38 changes: 38 additions & 0 deletions Plugins/SwiftLint/SwiftLintCommandPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation
import PackagePlugin

@main
struct SwiftLintCommandPlugin: CommandPlugin {

func performCommand(context: PluginContext, arguments: [String]) throws {
let swiftlintTool = try context.tool(named: "swiftlint")
let swiftlintExecutableURL = URL(fileURLWithPath: swiftlintTool.path.string)
var swiftlintArguments = [
"lint",
"--cache-path",
context.pluginWorkDirectory.string
]

var argumentExtractor = ArgumentExtractor(arguments)

if let configFile = argumentExtractor.extractOption(named: "config").first {
swiftlintArguments.append(contentsOf: ["--config", configFile])
}

if let reporter = argumentExtractor.extractOption(named: "reporter").first {
swiftlintArguments.append(contentsOf: ["--reporter", reporter])
}

if argumentExtractor.extractFlag(named: "strict") > 0 {
swiftlintArguments.append("--strict")
}

let process = try Process.run(swiftlintExecutableURL, arguments: swiftlintArguments)
process.waitUntilExit()

if process.terminationStatus != 0 {
Diagnostics.error("'swiftlint' failed")
}
}

}
39 changes: 39 additions & 0 deletions Plugins/SwiftLintPlugin/Path+Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Darwin
import Foundation
import PackagePlugin

extension Path {

/// Scans the receiver, then all of its parents looking for a configuration file with the name ".swiftlint.yml".
///
/// - returns: Path to the configuration file, or nil if one cannot be found.
func firstConfigurationFileInParentDirectories() -> Path? {
let defaultConfigurationFileName = ".swiftlint.yml"
let proposedDirectory = sequence(
first: self,
next: { path in
guard path.stem.count > 1 else {
// Check we're not at the root of this filesystem, as `removingLastComponent()`
// will continually return the root from itself.
return nil
}

return path.removingLastComponent()
}
).first { path in
let potentialConfigurationFile = path.appending(subpath: defaultConfigurationFileName)
return potentialConfigurationFile.isAccessible()
}
return proposedDirectory?.appending(subpath: defaultConfigurationFileName)
}

/// Safe way to check if the file is accessible from within the current process sandbox.
private func isAccessible() -> Bool {
let result = string.withCString { pointer in
access(pointer, R_OK)
}

return result == 0
}

}
76 changes: 76 additions & 0 deletions Plugins/SwiftLintPlugin/SwiftLintPlugin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Foundation
import PackagePlugin

@main
struct SwiftLintPlugin: BuildToolPlugin {

func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
guard let sourceTarget = target as? SourceModuleTarget else {
return []
}
return createBuildCommands(
inputFiles: sourceTarget.sourceFiles(withSuffix: "swift").map(\.path),
packageDirectory: context.package.directory,
workingDirectory: context.pluginWorkDirectory,
tool: try context.tool(named: "swiftlint")
)
}

private func createBuildCommands(
inputFiles: [Path],
packageDirectory: Path,
workingDirectory: Path,
tool: PluginContext.Tool
) -> [Command] {
if inputFiles.isEmpty {
// Don't lint anything if there are no Swift source files in this target
return []
}

var arguments = [
"lint",
"--quiet",
"--cache-path", "\(workingDirectory)"
]

// Manually look for configuration files, to avoid issues when the plugin does not execute our tool from the
// package source directory.
if let configuration = packageDirectory.firstConfigurationFileInParentDirectories() {
arguments.append(contentsOf: ["--config", "\(configuration.string)"])
}
arguments += inputFiles.map(\.string)

// We are not producing output files and this is needed only to not include cache files into bundle
let outputFilesDirectory = workingDirectory.appending("Output")

return [
.prebuildCommand(
displayName: "SwiftLint",
executable: tool.path,
arguments: arguments,
outputFilesDirectory: outputFilesDirectory
)
]
}

}

#if canImport(XcodeProjectPlugin)
import XcodeProjectPlugin

extension SwiftLintPlugin: XcodeBuildToolPlugin {

func createBuildCommands(context: XcodePluginContext, target: XcodeTarget) throws -> [Command] {
let inputFilePaths = target.inputFiles
.filter { $0.type == .source && $0.path.extension == "swift" }
.map(\.path)
return createBuildCommands(
inputFiles: inputFilePaths,
packageDirectory: context.xcodeProject.directory,
workingDirectory: context.pluginWorkDirectory,
tool: try context.tool(named: "swiftlint")
)
}

}
#endif
Loading

0 comments on commit 764e59e

Please sign in to comment.