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

Move Code Generation From Bash to Swift Scripts #940

Merged
merged 99 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
1002835
start building out the bit of the codegen lib that can call into the …
designatednerd Sep 26, 2019
c52e100
poke at getting things to run a bit better
designatednerd Sep 27, 2019
b8dab0c
make sure current directory URL gets set properly
designatednerd Sep 30, 2019
3ca77a4
actually get codegen running
designatednerd Sep 30, 2019
adcf085
Clean up a bunch of stuff on codegen and add documentation
designatednerd Sep 30, 2019
025cb59
Set valid arch for the codegen target
designatednerd Sep 30, 2019
4d90b5d
clarify which errors are the apollo codegen and which are the project…
designatednerd Sep 30, 2019
a4db4aa
build locally with build folder and hard-coded framework link
designatednerd Sep 30, 2019
91d3710
Add codegen lib to swift package specifications
designatednerd Sep 30, 2019
47123a2
clean up indentation
designatednerd Sep 30, 2019
7b45639
use OSX, not OSXApplicationExtension in if/else fence
designatednerd Sep 30, 2019
320f386
allow basher to take an optional URL
designatednerd Oct 4, 2019
a260036
add folder deletion helper to file manager
designatednerd Oct 4, 2019
4a7a0a7
Move apollo-specific stuff into a CLI and set up extraction in swift
designatednerd Oct 4, 2019
76b5fc1
add a schema downloader and options for the CLI
designatednerd Oct 4, 2019
56b6163
start adding tests for codegen
designatednerd Oct 7, 2019
56f3991
Better detail on logs
designatednerd Oct 8, 2019
900f8b0
better grabbing of the SHA256 of the file
designatednerd Oct 8, 2019
e3d8d59
More error handling and tests around CLI extraction
designatednerd Oct 8, 2019
af5ab40
add tests to validate creation of options and arguments for codegen a…
designatednerd Oct 8, 2019
fd02a6c
fix docs on main codegen
designatednerd Oct 9, 2019
3cef5e2
Add ability to automatically create folders for output when needed
designatednerd Oct 9, 2019
6322b24
fix alignment and error output for basher
designatednerd Oct 9, 2019
c717dd8
add tests of outputting individual vs single files
designatednerd Oct 9, 2019
779516b
auto-create output folder for schema download if needed
designatednerd Oct 9, 2019
fb08d38
fix up error description
designatednerd Oct 9, 2019
c43fe89
Add a whole mess of documentation
designatednerd Oct 9, 2019
0eac850
Update SHASUM in CLI extractor
designatednerd Oct 10, 2019
63e35d8
set up tests to run off main lib scheme
designatednerd Oct 10, 2019
58f250f
Update shasum in CLI extractor
designatednerd Oct 21, 2019
344f662
add codegen tests to Circle tests
designatednerd Oct 21, 2019
be4464e
switch to single options initializer
designatednerd Oct 21, 2019
2b9750e
bail on shasum validation if the file doesn't exist
designatednerd Oct 21, 2019
74d5603
move codegen tests into tests group
designatednerd Oct 21, 2019
8b3a17a
generate code coverage when testing code lib
designatednerd Oct 21, 2019
9d1e217
Test extracting with missing shasum file
designatednerd Oct 21, 2019
84116f3
add codegen package to sources
designatednerd Oct 22, 2019
faa3bdc
use swift build and swift run to run the codgen target
designatednerd Oct 22, 2019
96e5b40
pull file path helper into a separate object
designatednerd Oct 22, 2019
d242f53
add and test CLI downloader
designatednerd Oct 22, 2019
cf2cb24
Move codegen test stuff into an xcconfig file
designatednerd Dec 9, 2019
8045654
add and internally test convenience methods to access the script dire…
designatednerd Dec 11, 2019
97b8d1a
share codegen scheme
designatednerd Jan 16, 2020
b8f4203
add support for adjustable timeout
designatednerd Jan 16, 2020
7b3554b
Add download timeout to convenience initializer
designatednerd Jan 16, 2020
667883a
add debug log to try and figure out infinite loop
designatednerd Jan 16, 2020
a98be0b
welp that'd certainly lead to an infinite loop 🤦‍♀️
designatednerd Jan 16, 2020
155663f
get rid of script directory finder since we'll recommend adding a sep…
designatednerd Feb 3, 2020
4ca6901
fix alignment
designatednerd Feb 3, 2020
2522413
move codegen into its own separate executable
designatednerd Feb 3, 2020
845f524
remove now unused package manager class
designatednerd Feb 3, 2020
b108a10
scripts folder url -> cli folder url
designatednerd Feb 3, 2020
59f4a8b
get rid of old codegen target
designatednerd Feb 3, 2020
0967584
get rid of linux test helper
designatednerd Feb 3, 2020
d827899
add documentation
designatednerd Feb 3, 2020
d5cb93f
add support for swift multiline string literal suppression flag
designatednerd Feb 4, 2020
c0cf99a
not sure why these weren't committed before...
designatednerd Feb 4, 2020
d000774
get codegen able to generate off multiple targets
designatednerd Feb 4, 2020
f2834f6
remove unused environment param, clean up docs
designatednerd Feb 4, 2020
5e098cc
better naming
designatednerd Feb 4, 2020
0d82b98
auto-generate docs for codegen lib
designatednerd Feb 4, 2020
9dac417
simplify relative apollo path
designatednerd Feb 4, 2020
090744a
add folder structure but ignore contents of `ApolloCLI` folder.
designatednerd Feb 4, 2020
89b7dbc
kill off header comments
designatednerd Feb 6, 2020
b1b53e5
fix up and test schema downloading
designatednerd Feb 6, 2020
5ae8f7d
better error name for codegen error since it's nested
designatednerd Feb 6, 2020
8679037
Don't need a local URL to run the schema downloader from
designatednerd Feb 6, 2020
c81ed59
improve error message for codegen
designatednerd Feb 6, 2020
7f2de68
update CLI version download URL and hash
designatednerd Feb 7, 2020
da8986e
add test for graphql format download
designatednerd Feb 7, 2020
c78efa0
those properties don't need to be public
designatednerd Feb 7, 2020
4d71b0f
add debug string convertible to print args for options objects
designatednerd Feb 7, 2020
e1144fa
Make it easier and clearer to download schema in SDL
designatednerd Feb 7, 2020
5baf370
better setup for logging results
designatednerd Feb 7, 2020
8dcc30d
fix bad merge
designatednerd Feb 7, 2020
e07f9b2
start re-adding docs
designatednerd Feb 11, 2020
054a3bd
update CLI version in swift codegen
designatednerd Feb 13, 2020
0081550
add schema download target to codegen
designatednerd Feb 13, 2020
af5470f
add inline docs to codegen script
designatednerd Feb 14, 2020
70850ca
add more swift scripting docs
designatednerd Feb 14, 2020
9d2555c
fix link to codegen options documentation
designatednerd Feb 17, 2020
e501aa5
add redirect to tutorial introduction from /tutorial
designatednerd Feb 17, 2020
32bedfb
much better schema download docs, add docs on creating a query
designatednerd Feb 17, 2020
be049d4
flesh out the last of the instructions
designatednerd Feb 17, 2020
f21a79d
fix link
designatednerd Feb 17, 2020
72b1d98
fix title casing, fix anchor link
designatednerd Feb 17, 2020
b85ed13
Add links to swift scripting instructions from installation and schem…
designatednerd Feb 17, 2020
6f4d55f
Add file finder so we can get a URL out of the script file
designatednerd Feb 17, 2020
cc55b25
Use the file finder instead of a magic sourceroot var
designatednerd Feb 17, 2020
238d06f
update docs to use way simpler method to get source root
designatednerd Feb 17, 2020
af93d09
Add troubleshooting note
designatednerd Feb 17, 2020
ccb4fb4
shorten wait description
designatednerd Feb 18, 2020
74e68a3
clarify codegen options convenience initializer
designatednerd Feb 18, 2020
d92bbf2
fix doc generation command for codegen lib
designatednerd Feb 18, 2020
eb9625d
update codegen lib docs
designatednerd Feb 18, 2020
4463403
Edits to Swift scripting article
Feb 19, 2020
ff1a65d
Incorporate edits from designatednerd
Feb 19, 2020
dd7b83c
Remove blockquote from notice
Feb 19, 2020
d0cdf75
Merge pull request #1027 from apollographql/sb/codegen-edits
designatednerd Feb 19, 2020
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
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ jobs:
steps:
- common_test_steps

CodegenLibmacOS_current:
macos:
xcode: << pipeline.parameters.xcode_version >>
environment:
DESTINATION: platform=macOS,arch=x86_64
CIRCLE_XCODE_SCHEME: ApolloCodegenLib
CIRCLE_XCODE_SDK: << pipeline.parameters.macos_sdk >>
steps:
- common_test_steps

CocoaPodsTrunk:
macos:
xcode: << pipeline.parameters.xcode_version >>
Expand Down Expand Up @@ -195,6 +205,8 @@ workflows:
name: ApolloWebSocket iOS << pipeline.parameters.ios_current_version >>
- WebSocketiOS_previous:
name: ApolloWebSocket iOS << pipeline.parameters.ios_previous_version >>
- CodegenLibmacOS_current:
name: Swift Code Generation
- CocoaPodsTrunk:
name: Push Podspec to CocoaPods Trunk
requires:
Expand All @@ -208,6 +220,7 @@ workflows:
- ApolloWebSocket macOS << pipeline.parameters.macos_version >>
- ApolloWebSocket iOS << pipeline.parameters.ios_current_version >>
- ApolloWebSocket iOS << pipeline.parameters.ios_previous_version >>
- Swift Code Generation
filters:
# Only build semver tags
tags:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ node_modules/
package-lock.json
scripts/apollo
scripts/apollo.tar.gz
Codegen/ApolloCLI
294 changes: 291 additions & 3 deletions Apollo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions Apollo.xcodeproj/xcshareddata/xcschemes/ApolloCodegenLib.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1100"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9B7B6F46233C26D100F32205"
BuildableName = "ApolloCodegenLib.framework"
BlueprintName = "ApolloCodegenLib"
ReferencedContainer = "container:Apollo.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "NO"
codeCoverageEnabled = "YES"
onlyGenerateCoverageForSpecifiedTargets = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9B7B6F46233C26D100F32205"
BuildableName = "ApolloCodegenLib.framework"
BlueprintName = "ApolloCodegenLib"
ReferencedContainer = "container:Apollo.xcodeproj">
</BuildableReference>
</MacroExpansion>
<EnvironmentVariables>
<EnvironmentVariable
key = "SRCROOT"
value = "$SRCROOT"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9B7B6F46233C26D100F32205"
BuildableName = "ApolloCodegenLib.framework"
BlueprintName = "ApolloCodegenLib"
ReferencedContainer = "container:Apollo.xcodeproj">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9BAEEBFB234BB8FD00808306"
BuildableName = "ApolloCodegenTests.xctest"
BlueprintName = "ApolloCodegenTests"
ReferencedContainer = "container:Apollo.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9B7B6F46233C26D100F32205"
BuildableName = "ApolloCodegenLib.framework"
BlueprintName = "ApolloCodegenLib"
ReferencedContainer = "container:Apollo.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
1 change: 1 addition & 0 deletions Codegen/ApolloCLI/.keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is here to preserve folder structure in git.
43 changes: 43 additions & 0 deletions Codegen/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"object": {
"pins": [
{
"package": "SQLite.swift",
"repositoryURL": "https://github.com/stephencelis/SQLite.swift.git",
"state": {
"branch": null,
"revision": "0a9893ec030501a3956bee572d6b4fdd3ae158a1",
"version": "0.12.2"
}
},
{
"package": "Starscream",
"repositoryURL": "https://github.com/daltoniam/Starscream",
"state": {
"branch": null,
"revision": "e6b65c6d9077ea48b4a7bdda8994a1d3c6969c8d",
"version": "3.1.1"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
}
},
{
"package": "swift-tools-support-core",
"repositoryURL": "https://github.com/apple/swift-tools-support-core",
"state": {
"branch": null,
"revision": "693aba4c4c9dcc4767cc853a0dd38bf90ad8c258",
"version": "0.0.1"
}
}
]
},
"version": 1
}
22 changes: 22 additions & 0 deletions Codegen/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Codegen",
dependencies: [
.package(path: ".."),
.package(url: "https://github.com/apple/swift-tools-support-core", from: "0.0.1")
],
targets: [
.target(
name: "Codegen",
dependencies: ["ApolloCodegenLib", "SwiftToolsSupport-auto"]),
.target(name: "SchemaDownload",
dependencies: ["ApolloCodegenLib"]),
.testTarget(
name: "CodegenTests",
dependencies: ["Codegen"]),
]
)
3 changes: 3 additions & 0 deletions Codegen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Codegen

A description of this package.
84 changes: 84 additions & 0 deletions Codegen/Sources/Codegen/ArgumentSetup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Foundation
import ApolloCodegenLib
import TSCUtility

enum Target {
case starWars
case gitHub

init?(name: String) {
switch name {
case "StarWars":
self = .starWars
case "GitHub":
self = .gitHub
default:
return nil
}
}

func targetRootURL(fromSourceRoot sourceRootURL: Foundation.URL) -> Foundation.URL {
switch self {
case .gitHub:
return sourceRootURL
.appendingPathComponent("Tests")
.appendingPathComponent("GitHubAPI")
case .starWars:
return sourceRootURL
.appendingPathComponent("Tests")
.appendingPathComponent("StarWarsAPI")
}
}

func options(fromSourceRoot sourceRootURL: Foundation.URL) -> ApolloCodegenOptions {
let targetRootURL = self.targetRootURL(fromSourceRoot: sourceRootURL)
switch self {
case .starWars:
return ApolloCodegenOptions(targetRootURL: targetRootURL)
case .gitHub:
let json = targetRootURL.appendingPathComponent("schema.json")
let outputFileURL = targetRootURL.appendingPathComponent("API.swift")
let operationIDsURL = targetRootURL.appendingPathComponent("operationIDs.json")

return ApolloCodegenOptions(mergeInFieldsFromFragmentSpreads: true,
operationIDsURL: operationIDsURL,
outputFormat: .singleFile(atFileURL: outputFileURL),
suppressSwiftMultilineStringLiterals: true,
urlToSchemaFile: json)
}
}
}

struct ArgumentSetup {

enum ArgumentError: Error, LocalizedError {
case targetNotProvided(args: [String])

var errorDescription: String? {
switch self {
case .targetNotProvided(let args):
return "No valid was provided to generate code for. Args were: \(args)"
}
}
}

static func parse(arguments: [String] = Array(ProcessInfo.processInfo.arguments.dropFirst())) throws -> Target {

let parser = ArgumentParser(usage: "Codegen <options>", overview: "This is what this tool is for")

let target: OptionArgument<String> = parser.add(option: "--target",
shortName: "-t",
kind: String.self,
usage: "The target to generate code for")
let parsedArguments = try parser.parse(arguments)


if
let targetName = parsedArguments.get(target),
let target = Target(name: targetName) {
return target
} else {
throw ArgumentError.targetNotProvided(args: arguments)
}
}
}
39 changes: 39 additions & 0 deletions Codegen/Sources/Codegen/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Foundation
import ApolloCodegenLib
import TSCUtility

// Grab the parent folder of this file on the filesystem
let parentFolderOfScriptFile = FileFinder.findParentFolder()

// Use that to calculate the source root
let sourceRootURL = parentFolderOfScriptFile
.deletingLastPathComponent() // Sources
.deletingLastPathComponent() // Codegen
.deletingLastPathComponent() // apollo-ios

// In a typical app, you'll only need to do this for one target, so you'd
// set these up directly within this file. Here, we're using more than one
// target, so we're using an arg parser to figure out which one to build,
// and an enum to hold related options.
let target = try ArgumentSetup.parse()
let targetURL = target.targetRootURL(fromSourceRoot: sourceRootURL)
let options = target.options(fromSourceRoot: sourceRootURL)

// This more necessary if you're using a sub-folder, but make sure
// there's actually a place to write out what you're doing.
try FileManager.default.apollo_createFolderIfNeeded(at: targetURL)

// Calculate where you want to download the CLI folder.
let cliFolderURL = sourceRootURL
.appendingPathComponent("Codegen")
.appendingPathComponent("ApolloCLI")

do {
// Actually attempt to generate code.
try ApolloCodegen.run(from: targetURL,
with: cliFolderURL,
options: options)
} catch {
// This makes the error message in Xcode a lot more legible.
exit(1)
}
32 changes: 32 additions & 0 deletions Codegen/Sources/SchemaDownload/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import ApolloCodegenLib

// Grab the parent folder of this file on the filesystem
let parentFolderOfScriptFile = FileFinder.findParentFolder()

// Use that to calculate the source root
let sourceRootURL = parentFolderOfScriptFile
.deletingLastPathComponent() // Sources
.deletingLastPathComponent() // Codegen
.deletingLastPathComponent() // apollo-ios

let cliFolderURL = sourceRootURL
.appendingPathComponent("Codegen")
.appendingPathComponent("ApolloCLI")

let endpoint = URL(string: "http://localhost:8080/graphql")!

let output = sourceRootURL
.appendingPathComponent("Tests")
.appendingPathComponent("StarWarsAPI")

let options = ApolloSchemaOptions(schemaFileName: "downloaded_schema",
endpointURL: endpoint,
outputFolderURL: output)

do {
try ApolloSchemaDownloader.run(with: cliFolderURL,
options: options)
} catch {
exit(1)
}
Loading