-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Adding CLI command for Operation Manifest (#3152)
- Loading branch information
1 parent
e95cec8
commit a47a8af
Showing
14 changed files
with
551 additions
and
60 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
61 changes: 61 additions & 0 deletions
61
Sources/CodegenCLI/Commands/GenerateOperationManifest.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,61 @@ | ||
import Foundation | ||
import ArgumentParser | ||
import ApolloCodegenLib | ||
|
||
public struct GenerateOperationManifest: ParsableCommand { | ||
|
||
// MARK: - Configuration | ||
|
||
public static var configuration = CommandConfiguration( | ||
abstract: "Generate Persisted Queries operation manifest based on a code generation configuration." | ||
) | ||
|
||
@OptionGroup var inputs: InputOptions | ||
|
||
// MARK: - Implementation | ||
|
||
public init() { } | ||
|
||
public func run() throws { | ||
try _run() | ||
} | ||
|
||
func _run( | ||
fileManager: FileManager = .default, | ||
codegenProvider: CodegenProvider.Type = ApolloCodegen.self, | ||
logger: LogLevelSetter.Type = CodegenLogger.self | ||
) throws { | ||
logger.SetLoggingLevel(verbose: inputs.verbose) | ||
|
||
try checkForCLIVersionMismatch( | ||
with: inputs | ||
) | ||
|
||
switch (inputs.string, inputs.path) { | ||
case let (.some(string), _): | ||
try generateManifest( | ||
data: try string.asData(), | ||
codegenProvider: codegenProvider | ||
) | ||
case let (nil, path): | ||
try generateManifest( | ||
data: try fileManager.unwrappedContents(atPath: path), | ||
codegenProvider: codegenProvider | ||
) | ||
} | ||
} | ||
|
||
private func generateManifest( | ||
data: Data, | ||
codegenProvider: CodegenProvider.Type | ||
) throws { | ||
let configuration = try JSONDecoder().decode(ApolloCodegenConfiguration.self, from: data) | ||
|
||
try codegenProvider.generateOperationManifest( | ||
with: configuration, | ||
withRootURL: rootOutputURL(for: inputs), | ||
fileManager: .default | ||
) | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
Sources/CodegenCLI/Extensions/ParsableCommand+Apollo.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,45 @@ | ||
import Foundation | ||
import ArgumentParser | ||
import ApolloCodegenLib | ||
|
||
extension ParsableCommand { | ||
func rootOutputURL(for inputOptions: InputOptions) -> URL? { | ||
if inputOptions.string != nil { return nil } | ||
let rootURL = URL(fileURLWithPath: inputOptions.path).deletingLastPathComponent() | ||
if rootURL.path == FileManager.default.currentDirectoryPath { return nil } | ||
return rootURL | ||
} | ||
|
||
func checkForCLIVersionMismatch( | ||
with inputs: InputOptions, | ||
ignoreVersionMismatch: Bool = false | ||
) throws { | ||
if case let .versionMismatch(cliVersion, apolloVersion) = | ||
try VersionChecker.matchCLIVersionToApolloVersion(projectRootURL: rootOutputURL(for: inputs)) { | ||
let errorMessage = """ | ||
Apollo Version Mismatch | ||
We've detected that the version of the Apollo Codegen CLI does not match the version of the | ||
Apollo library used in your project. This may lead to incompatible generated objects. | ||
Please update your version of the Codegen CLI by following the instructions at: | ||
https://www.apollographql.com/docs/ios/code-generation/codegen-cli/#installation | ||
CLI version: \(cliVersion) | ||
Apollo version: \(apolloVersion) | ||
""" | ||
|
||
if inputs.ignoreVersionMismatch { | ||
print(""" | ||
Warning: \(errorMessage) | ||
""") | ||
} else { | ||
|
||
throw Error(errorDescription: """ | ||
Error: \(errorMessage) | ||
To ignore this error during code generation, use the argument: --ignore-version-mismatch. | ||
""") | ||
} | ||
} | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
Sources/CodegenCLI/Extensions/ParsableCommand+RootOutputURL.swift
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.