Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apollo custom extension to replace apollo_ prefix #1183

Merged
merged 4 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
9FF90A6F1DDDEB420034C3B6 /* InputValueEncodingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A6A1DDDEB420034C3B6 /* InputValueEncodingTests.swift */; };
9FF90A711DDDEB420034C3B6 /* ReadFieldValueTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A6B1DDDEB420034C3B6 /* ReadFieldValueTests.swift */; };
9FF90A731DDDEB420034C3B6 /* ParseQueryResponseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FF90A6C1DDDEB420034C3B6 /* ParseQueryResponseTests.swift */; };
AE1CFBD0245EBA25002C8CEE /* ApolloExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE1CFBCE245EB998002C8CEE /* ApolloExtension.swift */; };
C3279FC72345234D00224790 /* TestCustomRequestCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3279FC52345233000224790 /* TestCustomRequestCreator.swift */; };
C338DF1722DD9DE9006AF33E /* RequestCreatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C338DF1622DD9DE9006AF33E /* RequestCreatorTests.swift */; };
C35D43C222DDD4AC00BCBABE /* b.txt in Resources */ = {isa = PBXBuildFile; fileRef = C35D43BE22DDD3C100BCBABE /* b.txt */; };
Expand Down Expand Up @@ -572,6 +573,7 @@
9FF90A6A1DDDEB420034C3B6 /* InputValueEncodingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputValueEncodingTests.swift; sourceTree = "<group>"; };
9FF90A6B1DDDEB420034C3B6 /* ReadFieldValueTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReadFieldValueTests.swift; sourceTree = "<group>"; };
9FF90A6C1DDDEB420034C3B6 /* ParseQueryResponseTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseQueryResponseTests.swift; sourceTree = "<group>"; };
AE1CFBCE245EB998002C8CEE /* ApolloExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApolloExtension.swift; sourceTree = "<group>"; };
C304EBD322DDC7B200748F72 /* a.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = a.txt; sourceTree = "<group>"; };
C3279FC52345233000224790 /* TestCustomRequestCreator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestCustomRequestCreator.swift; sourceTree = "<group>"; };
C338DF1622DD9DE9006AF33E /* RequestCreatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestCreatorTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -769,6 +771,7 @@
9BD681302405F676000874CB /* Output */,
9B8788702405F0150008789E /* Parsing */,
9BD681342405F6D1000874CB /* SchemaDownload */,
AE1CFBCE245EB998002C8CEE /* ApolloExtension.swift */,
9B518C8A235F8B05004C426D /* ApolloFilePathHelper.swift */,
9BD6812E2405F665000874CB /* JSON.swift */,
9BAEEBF22346DDAD00808306 /* CodegenLogger.swift */,
Expand Down Expand Up @@ -1830,6 +1833,7 @@
9BD6812B2405F410000874CB /* ASTFragment.swift in Sources */,
9B68F04A24130D6500E97318 /* EnumGenerator.swift in Sources */,
9B7B6F69233C2C0C00F32205 /* FileManager+Apollo.swift in Sources */,
AE1CFBD0245EBA25002C8CEE /* ApolloExtension.swift in Sources */,
9BE74D3D23FB4A8E006D354F /* FileFinder.swift in Sources */,
9B7B6F59233C287200F32205 /* ApolloCodegen.swift in Sources */,
9BD6813E2405FAC8000874CB /* ASTField.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/ASTVariableType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ASTForthcomingVariableType: Codable {
case .LIST:
return "[\(inner)]?"
case .NON_NULL:
return try inner.apollo_droppingSuffix("?")
return try inner.apollo.droppingSuffix("?")
case .ENUM,
.INPUT_OBJECT,
.INTERFACE,
Expand Down
6 changes: 3 additions & 3 deletions Sources/ApolloCodegenLib/ApolloCodegen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class ApolloCodegen {
public static func run(from folder: URL,
with cliFolderURL: URL,
options: ApolloCodegenOptions) throws -> String {
guard FileManager.default.apollo_folderExists(at: folder) else {
guard FileManager.default.apollo.folderExists(at: folder) else {
throw CodegenError.folderDoesNotExist(folder)
}

switch options.outputFormat {
case .multipleFiles(let folderURL):
try FileManager.default.apollo_createFolderIfNeeded(at: folderURL)
try FileManager.default.apollo.createFolderIfNeeded(at: folderURL)
case .singleFile(let fileURL):
try FileManager.default.apollo_createContainingFolderIfNeeded(for: fileURL)
try FileManager.default.apollo.createContainingFolderIfNeeded(for: fileURL)
}

let cli = try ApolloCLI.createCLI(cliFolderURL: cliFolderURL, timeout: options.downloadTimeout)
Expand Down
21 changes: 21 additions & 0 deletions Sources/ApolloCodegenLib/ApolloExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

public struct ApolloExtension<Base> {
public let base: Base
}

public protocol ApolloCompatible {
associatedtype Base
var apollo: ApolloExtension<Base> { get }
static var apollo: ApolloExtension<Base>.Type { get }
}

extension ApolloCompatible {
public var apollo: ApolloExtension<Self> {
ApolloExtension(base: self)
}

public static var apollo: ApolloExtension<Self>.Type {
ApolloExtension<Self>.self
}
}
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/ApolloSchemaDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct ApolloSchemaDownloader {
@discardableResult
public static func run(with cliFolderURL: URL,
options: ApolloSchemaOptions) throws -> String {
try FileManager.default.apollo_createContainingFolderIfNeeded(for: options.outputURL)
try FileManager.default.apollo.createContainingFolderIfNeeded(for: options.outputURL)

let cli = try ApolloCLI.createCLI(cliFolderURL: cliFolderURL, timeout: options.downloadTimeout)
return try cli.runApollo(with: options.arguments)
Expand Down
8 changes: 4 additions & 4 deletions Sources/ApolloCodegenLib/CLIDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct CLIDownloader {
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
static func downloadIfNeeded(cliFolderURL: URL, timeout: Double) throws {
let zipFileURL = ApolloFilePathHelper.zipFileURL(fromCLIFolder: cliFolderURL)
guard !FileManager.default.apollo_fileExists(at: zipFileURL) else {
guard !FileManager.default.apollo.fileExists(at: zipFileURL) else {
CodegenLogger.log("Zip file with the CLI is already downloaded!")
return
}
Expand All @@ -54,9 +54,9 @@ struct CLIDownloader {
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
static func forceRedownload(cliFolderURL: URL, timeout: Double) throws {
let zipFileURL = ApolloFilePathHelper.zipFileURL(fromCLIFolder: cliFolderURL)
try FileManager.default.apollo_deleteFile(at: zipFileURL)
try FileManager.default.apollo.deleteFile(at: zipFileURL)
let apolloFolderURL = ApolloFilePathHelper.apolloFolderURL(fromCLIFolder: cliFolderURL)
try FileManager.default.apollo_deleteFolder(at: apolloFolderURL)
try FileManager.default.apollo.deleteFolder(at: apolloFolderURL)

try self.download(to: zipFileURL, timeout: timeout)
}
Expand All @@ -67,7 +67,7 @@ struct CLIDownloader {
/// - zipFileURL: The URL where downloaded data should be saved.
/// - timeout: The maximum time to wait before indicating that the download timed out, in seconds.
private static func download(to zipFileURL: URL, timeout: Double) throws {
try FileManager.default.apollo_createContainingFolderIfNeeded(for: zipFileURL)
try FileManager.default.apollo.createContainingFolderIfNeeded(for: zipFileURL)

CodegenLogger.log("Downloading zip file with the CLI...")
let semaphore = DispatchSemaphore(value: 0)
Expand Down
14 changes: 7 additions & 7 deletions Sources/ApolloCodegenLib/CLIExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ struct CLIExtractor {
static func extractCLIIfNeeded(from cliFolderURL: URL, expectedSHASUM: String = CLIExtractor.expectedSHASUM) throws -> URL {
let apolloFolderURL = ApolloFilePathHelper.apolloFolderURL(fromCLIFolder: cliFolderURL)

guard FileManager.default.apollo_folderExists(at: apolloFolderURL) else {
guard FileManager.default.apollo.folderExists(at: apolloFolderURL) else {
CodegenLogger.log("Apollo folder doesn't exist, extracting CLI from zip file.")
return try self.extractCLIFromZip(cliFolderURL: cliFolderURL)
}

guard try self.validateSHASUMInExtractedFile(apolloFolderURL: apolloFolderURL, expected: expectedSHASUM) else {
CodegenLogger.log("SHASUM of extracted zip does not match expected, deleting existing folder and re-extracting.")
try FileManager.default.apollo_deleteFolder(at: apolloFolderURL)
try FileManager.default.apollo.deleteFolder(at: apolloFolderURL)
return try self.extractCLIFromZip(cliFolderURL: cliFolderURL)
}

let binaryFolderURL = ApolloFilePathHelper.binaryFolderURL(fromApollo: apolloFolderURL)
let binaryURL = ApolloFilePathHelper.binaryURL(fromBinaryFolder: binaryFolderURL)
guard FileManager.default.apollo_fileExists(at: binaryURL) else {
guard FileManager.default.apollo.fileExists(at: binaryURL) else {
CodegenLogger.log("There was a valid `.shasum` file, but no binary at the expected path. Deleting existing apollo folder and re-extracting.", logLevel: .warning)
try FileManager.default.apollo_deleteFolder(at: apolloFolderURL)
try FileManager.default.apollo.deleteFolder(at: apolloFolderURL)
return try self.extractCLIFromZip(cliFolderURL: cliFolderURL, expectedSHASUM: expectedSHASUM)
}

Expand All @@ -65,7 +65,7 @@ struct CLIExtractor {
/// - Returns: true if the shasums match, false if not.
static func validateSHASUMInExtractedFile(apolloFolderURL: URL, expected: String = CLIExtractor.expectedSHASUM) throws -> Bool {
let shasumFileURL = ApolloFilePathHelper.shasumFileURL(fromApollo: apolloFolderURL)
guard FileManager.default.apollo_fileExists(at: shasumFileURL) else {
guard FileManager.default.apollo.fileExists(at: shasumFileURL) else {
return false
}

Expand Down Expand Up @@ -104,7 +104,7 @@ struct CLIExtractor {
let apolloFolderURL = ApolloFilePathHelper.apolloFolderURL(fromCLIFolder: cliFolderURL)
let binaryFolderURL = ApolloFilePathHelper.binaryFolderURL(fromApollo: apolloFolderURL)

guard FileManager.default.apollo_folderExists(at: binaryFolderURL) else {
guard FileManager.default.apollo.folderExists(at: binaryFolderURL) else {
throw CLIExtractorError.noBinaryFolderAfterUnzipping(atURL: binaryFolderURL)
}

Expand All @@ -118,7 +118,7 @@ struct CLIExtractor {
/// - Parameter zipFileURL: The url to the zip file containing the Apollo CLI.
/// - Parameter expected: The expected SHASUM. Defaults to the real expected SHASUM. This parameter exists mostly for testing.
static func validateZipFileSHASUM(at zipFileURL: URL, expected: String = CLIExtractor.expectedSHASUM) throws {
let shasum = try FileManager.default.apollo_shasum(at: zipFileURL)
let shasum = try FileManager.default.apollo.shasum(at: zipFileURL)
print("SHASUM: \(shasum)")
guard shasum == expected else {
throw CLIExtractorError.zipFileHasInvalidSHASUM(expectedSHASUM: expected, gotSHASUM: shasum)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/CodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class CodeGenerator<Decoder: FlexibleDecoder> {
private func createFileWithOutput(_ output: String,
named name: String,
inFolder folderURL: URL) throws {
try FileManager.default.apollo_createFolderIfNeeded(at: folderURL)
try FileManager.default.apollo.createFolderIfNeeded(at: folderURL)
let fileURL = folderURL
.appendingPathComponent(name)
.appendingPathExtension("swift")
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/CodegenLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct CodegenLogger {
}

var standardOutput = FileHandle.standardOutput
print("[\(logLevel.name) - ApolloCodegenLib:\(file.apollo_lastPathComponent):\(line)] - \(logString())", to: &standardOutput)
print("[\(logLevel.name) - ApolloCodegenLib:\(file.apollo.lastPathComponent):\(line)] - \(logString())", to: &standardOutput)
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/ApolloCodegenLib/EnumGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class EnumGenerator {

init(astEnumValue: ASTEnumValue) {
self.name = astEnumValue.name
self.nameVariableDeclaration = astEnumValue.name.apollo_sanitizedVariableDeclaration
self.nameUsage = astEnumValue.name.apollo_sanitizedVariableUsage
self.nameVariableDeclaration = astEnumValue.name.apollo.sanitizedVariableDeclaration
self.nameUsage = astEnumValue.name.apollo.sanitizedVariableUsage
self.description = astEnumValue.description
self.isDeprecated = astEnumValue.isDeprecated
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloCodegenLib/FileFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public struct FileFinder {

public static func findParentFolder(from filePath: StaticString = #file) -> URL {
self.findParentFolder(from: filePath.apollo_toString)
self.findParentFolder(from: filePath.apollo.toString)
}

public static func findParentFolder(from filePath: String) -> URL {
Expand Down
58 changes: 27 additions & 31 deletions Sources/ApolloCodegenLib/FileManager+Apollo.swift
Original file line number Diff line number Diff line change
@@ -1,95 +1,91 @@
import Foundation
import CommonCrypto

public extension FileManager {
extension FileManager: ApolloCompatible {}

extension ApolloExtension where Base == FileManager {

/// Checks if a file exists (and is not a folder) at the given path
///
///
/// - Parameter path: The path to check
/// - Returns: `true` if there is something at the path and it is a file, not a folder.
func apollo_fileExists(at path: String) -> Bool {
public func fileExists(at path: String) -> Bool {
var isFolder = ObjCBool(false)
let exists = self.fileExists(atPath: path, isDirectory: &isFolder)

let exists = base.fileExists(atPath: path, isDirectory: &isFolder)
return exists && !isFolder.boolValue
}

/// Checks if a file exists (and is not a folder) at the given URL
///
/// - Parameter url: The URL to check
/// - Returns: `true` if there is something at the URL and it is a file, not a folder.
func apollo_fileExists(at url: URL) -> Bool {
return self.apollo_fileExists(at: url.path)
public func fileExists(at url: URL) -> Bool {
return fileExists(at: url.path)
}

/// Checks if a folder exists (and is not a file) at the given path.
///
/// - Parameter path: The path to check
/// - Returns: `true` if there is something at the path and it is a folder, not a file.
func apollo_folderExists(at path: String) -> Bool {
public func folderExists(at path: String) -> Bool {
var isFolder = ObjCBool(false)
let exists = self.fileExists(atPath: path, isDirectory: &isFolder)

let exists = base.fileExists(atPath: path, isDirectory: &isFolder)
return exists && isFolder.boolValue
}

/// Checks if a folder exists (and is not a file) at the given URL.
///
/// - Parameter url: The URL to check
/// - Returns: `true` if there is something at the URL and it is a folder, not a file.
func apollo_folderExists(at url: URL) -> Bool {
return self.apollo_folderExists(at: url.path)
public func folderExists(at url: URL) -> Bool {
return folderExists(at: url.path)
}

/// Checks if a folder exists then attempts to delete it if it's there.
///
///
/// - Parameter url: The URL to delete the folder for
func apollo_deleteFolder(at url: URL) throws {
guard apollo_folderExists(at: url) else {
public func deleteFolder(at url: URL) throws {
guard folderExists(at: url) else {
// Nothing to delete!
return
}

try self.removeItem(at: url)
try base.removeItem(at: url)
}

/// Checks if a file exists then attempts to delete it if it's there.
///
/// - Parameter url: The URL to delete the file for
func apollo_deleteFile(at url: URL) throws {
guard apollo_fileExists(at: url) else {
public func deleteFile(at url: URL) throws {
guard fileExists(at: url) else {
// Nothing to delete!
return
}

try self.removeItem(at: url)
try base.removeItem(at: url)
}

/// Creates the containing folder (including all intermediate directories) for the given file URL if necessary.
///
/// - Parameter fileURL: The URL of the file to create a containing folder for if necessary.
func apollo_createContainingFolderIfNeeded(for fileURL: URL) throws {
public func createContainingFolderIfNeeded(for fileURL: URL) throws {
let parent = fileURL.deletingLastPathComponent()
try self.apollo_createFolderIfNeeded(at: parent)
try createFolderIfNeeded(at: parent)
}

/// Creates the folder (including all intermediate directories) for the given URL if necessary.
///
/// - Parameter url: The URL of the folder to create if necessary.
func apollo_createFolderIfNeeded(at url: URL) throws {
guard !self.apollo_folderExists(at: url) else {
public func createFolderIfNeeded(at url: URL) throws {
guard !folderExists(at: url) else {
// Folder already exists, nothing more to do here.
return
}

try self.createDirectory(atPath: url.path,
withIntermediateDirectories: true)
try base.createDirectory(atPath: url.path, withIntermediateDirectories: true)
}

/// Calculates the SHASUM (ie, SHA256 hash) of the given file
///
/// - Parameter fileURL: The file to calculate the SHASUM for.
func apollo_shasum(at fileURL: URL) throws -> String {
public func shasum(at fileURL: URL) throws -> String {
let file = try FileHandle(forReadingFrom: fileURL)
defer {
file.closeFile()
Expand Down
4 changes: 2 additions & 2 deletions Sources/ApolloCodegenLib/OptionalBoolean.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation

extension Optional where Wrapped == Bool {

var apollo_boolValue: Bool {
/// It returns false if it is called on `nil`
var boolValue: Bool {
switch self {
case .none:
return false
Expand Down
13 changes: 7 additions & 6 deletions Sources/ApolloCodegenLib/StaticString+Apollo.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Foundation

extension StaticString {

var apollo_lastPathComponent: String {
return (self.apollo_toString as NSString).lastPathComponent
extension StaticString: ApolloCompatible {}

extension ApolloExtension where Base == StaticString {
var lastPathComponent: String {
return (toString as NSString).lastPathComponent
}

var apollo_toString: String {
return self.withUTF8Buffer {
var toString: String {
return base.withUTF8Buffer {
String(decoding: $0, as: UTF8.self)
}
}
Expand Down
Loading