Skip to content

Commit

Permalink
Merge pull request #17 from SwiftPackageIndex/add-validator
Browse files Browse the repository at this point in the history
Add spi-manifest-validate executable
  • Loading branch information
finestructure authored Oct 19, 2022
2 parents 01d43f5 + a8d0cec commit 2f086bf
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.vscode/
.swiftpm/
12 changes: 4 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ let package = Package(
name: "SPIManifest",
platforms: [.macOS(.v10_15)],
products: [
.executable(name: "spi-manifest-validate", targets: ["Validator"]),
.library(name: "SPIManifest", targets: ["SPIManifest"]),
],
dependencies: [
.package(url: "https://github.com/jpsim/Yams.git", "4.0.0"..<"6.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "SPIManifest",
dependencies: ["Yams"]),
.testTarget(
name: "SPIManifestTests",
dependencies: ["SPIManifest"]),
.executableTarget(name: "Validator", dependencies: ["SPIManifest"]),
.target(name: "SPIManifest", dependencies: ["Yams"]),
.testTarget(name: "SPIManifestTests", dependencies: ["SPIManifest"]),
]
)
37 changes: 37 additions & 0 deletions Sources/SPIManifest/File.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2020-2022 Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


public enum ManifestError: Error {
case fileTooLarge(size: Int)
case decodingError(String)
case invalidPath(path: String)
case noData
}


extension ManifestError: CustomStringConvertible {
public var description: String {
switch self {
case .fileTooLarge(size: let size):
return "File is too large: \(size) bytes"
case let .decodingError(message):
return "The file could not be decoded: \(message)"
case let .invalidPath(path: path):
return "Invalid path: \(path)."
case .noData:
return "The file contains no data."
}
}
}
56 changes: 48 additions & 8 deletions Sources/SPIManifest/Manifest.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Dave Verwer, Sven A. Schmidt, and other contributors.
// Copyright 2020-2022 Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -98,15 +98,55 @@ extension Manifest {
let path = directory.hasSuffix("/")
? "\(directory)\(fileName)"
: "\(directory)/\(fileName)"
guard
Current.fileManager.fileExists(path),
let data = Current.fileManager.contents(path),
data.count <= maxByteSize,
var manifest = try? YAMLDecoder().decode(Self.self, from: data)
else { return nil }
return try? load(at: path)
}

Self.fixPlatforms(manifest: &manifest)
public static func load(at path: String) throws -> Self {
guard Current.fileManager.fileExists(atPath: path) else {
throw ManifestError.invalidPath(path: path)
}

guard let data = Current.fileManager.contents(path) else {
throw ManifestError.noData
}

guard data.count <= maxByteSize else {
throw ManifestError.fileTooLarge(size: data.count)
}

var manifest: Self
do {
manifest = try YAMLDecoder().decode(Self.self, from: data)
} catch let error as DecodingError {
switch error {
case let .typeMismatch(_, context):
throw ManifestError.decodingError("""
Error at path '\(context.codingPath.map(\.stringValue).joined(separator: "."))': \(context.debugDescription).
""")

case let .valueNotFound(_, context):
throw ManifestError.decodingError("""
Error at path '\(context.codingPath.map(\.stringValue).joined(separator: "."))': \(context.debugDescription).
""")

case let .keyNotFound(key, _):
throw ManifestError.decodingError("""
Key not found: '\(key.stringValue)'.
""")

case let .dataCorrupted(context):
throw ManifestError.decodingError("""
Data corrupted: '\(context.debugDescription)'.
""")

@unknown default:
throw ManifestError.decodingError("\(error)")
}
} catch {
throw ManifestError.decodingError("\(error)")
}

Self.fixPlatforms(manifest: &manifest)
return manifest
}

Expand Down
27 changes: 27 additions & 0 deletions Sources/Validator/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if os(macOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif

import SPIManifest


func main() {
guard CommandLine.arguments.count == 2,
let path = CommandLine.arguments.last else {
print("Usage: spi-manifest-validate <.spi.yml file>")
exit(1)
}

do {
_ = try SPIManifest.Manifest.load(at: path)
} catch {
print("🔴 \(error)")
exit(2)
}

print("✅ The file is valid.")
}

main()

0 comments on commit 2f086bf

Please sign in to comment.