generated from Apodini/Template-Repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1138a9e
commit 7de65b4
Showing
19 changed files
with
482 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,42 @@ | ||
// swift-tools-version:5.5 | ||
|
||
// | ||
// This source file is part of the Apodini open source project | ||
// | ||
// SPDX-FileCopyrightText: 2021 Paul Schmiedmayer and the project authors (see CONTRIBUTORS.md) <[email protected]> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
|
||
let package = Package( | ||
name: "ApodiniTemplate", | ||
platforms: [ | ||
.macOS(.v11) | ||
], | ||
name: "ApodiniDocumentExport", | ||
products: [ | ||
.library(name: "ApodiniTemplate", targets: ["ApodiniTemplate"]) | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "ApodiniDocumentExport", | ||
targets: ["ApodiniDocumentExport"]), | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMinor(from: "0.4.4")), | ||
.package(url: "https://github.com/kylef/PathKit.git", from: "1.0.1"), | ||
.package(url: "https://github.com/omochi/FineJSON.git", from: "1.14.0"), | ||
.package(url: "https://github.com/jpsim/Yams.git", from: "4.0.0") | ||
], | ||
targets: [ | ||
.target(name: "ApodiniTemplate"), | ||
// 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: "ApodiniDocumentExport", | ||
dependencies: [ | ||
.product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
.product(name: "PathKit", package: "PathKit"), | ||
.product(name: "FineJSON", package: "FineJSON"), | ||
.product(name: "Yams", package: "Yams") | ||
]), | ||
.testTarget( | ||
name: "ApodiniTemplateTests", | ||
name: "ApodiniDocumentExportTests", | ||
dependencies: [ | ||
.target(name: "ApodiniTemplate") | ||
] | ||
) | ||
"ApodiniDocumentExport" | ||
], | ||
resources: [ | ||
.process("document.json") | ||
]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ``ApodiniDocumentExport`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Foundation | ||
import ArgumentParser | ||
|
||
/// A typealias for ``OutputFormat`` | ||
public typealias FileFormat = OutputFormat | ||
|
||
extension OutputFormat: ExpressibleByArgument {} | ||
|
||
// MARK: - ExportOptions | ||
/// A protocol that defines export options for `ApodiniMigrator` items | ||
public protocol ExportOptions: ParsableArguments { | ||
/// Optional directory path to export an item | ||
var directory: String? { get set } | ||
/// Optional endpoint path to expose an item | ||
var endpoint: String? { get set } | ||
/// Format of the item to be exported / exposed, either `json` or `yaml` | ||
var format: FileFormat { get set } | ||
} | ||
|
||
extension ExportOptions { | ||
init(directory: String? = nil, endpoint: String? = nil, format: FileFormat) { | ||
self.init() | ||
self.directory = directory | ||
self.endpoint = endpoint | ||
self.format = format | ||
} | ||
} | ||
|
||
public extension ExportOptions { | ||
/// A convenient static function for initializing an ``ExportOptions`` instance | ||
/// - Parameters: | ||
/// - path: A path to a local directory used to export an item | ||
/// - format: Format of the item to be exported, either `json` or `yaml`. Defaults to `.json` | ||
static func directory(_ path: String, format: FileFormat = .json) -> Self { | ||
.init(directory: path, format: format) | ||
} | ||
|
||
/// A convenient static function for initializing an ``ExportOptions`` instance | ||
/// - Parameters: | ||
/// - path: An endpoint path of the web service used to expose an item | ||
/// - format: Format of the item to be exposed, either `json` or `yaml`. Defaults to `.json` | ||
static func endpoint(_ path: String, format: FileFormat = .json) -> Self { | ||
.init(endpoint: path, format: format) | ||
} | ||
|
||
/// A convenient static function for initializing an ``ExportOptions`` instance | ||
/// - Parameters: | ||
/// - directory: A path to a local directory used to export an item | ||
/// - endpoint: An endpoint path of the web service used to expose an item | ||
/// - format: Format of the item to be exposed, either `json` or `yaml`. Defaults to `.json` | ||
static func paths(directory: String, endpoint: String, format: FileFormat = .json) -> Self { | ||
.init(directory: directory, endpoint: endpoint, format: format) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Sources/ApodiniDocumentExport/Extensions/Decodable+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Foundation | ||
import PathKit | ||
@_implementationOnly import Yams | ||
|
||
public extension Decodable { | ||
/// Initializes self from data | ||
static func decode(from data: Data) throws -> Self { | ||
try JSONDecoder().decode(Self.self, from: data) | ||
} | ||
|
||
/// Initializes self from string | ||
static func decode(from string: String) throws -> Self { | ||
try decode(from: string.data()) | ||
} | ||
|
||
/// Initializes self from the content of path | ||
static func decode(from path: Path) throws -> Self { | ||
guard path.is(.json) || path.is(.yaml) else { | ||
throw DecodingError.dataCorrupted( | ||
.init( | ||
codingPath: [], | ||
debugDescription: "`ApodiniMigrator` only supports decoding of files in either json or yaml format" | ||
) | ||
) | ||
} | ||
let data = try path.read() as Data | ||
if path.is(.yaml) { | ||
return try YAMLDecoder().decode(from: data) | ||
} | ||
return try decode(from: data) | ||
} | ||
} |
Oops, something went wrong.