Skip to content

Commit

Permalink
🚧 website bootstrap, codegen script, homebrew formula bootstrap, git …
Browse files Browse the repository at this point in the history
…integration

What's left is writing some documentation and publishing the 2.0.0 version 🚀
  • Loading branch information
maticzav committed Feb 13, 2021
1 parent 996ed85 commit e5b02f1
Show file tree
Hide file tree
Showing 62 changed files with 18,095 additions and 6,865 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test

on:
push: {}
pull_request: {}

jobs:
build:
runs-on: macOS-latest
steps:
- uses: actions/checkout@v2
- name: Resolve
run: swift package resolve
- name: Build
run: swift build
- name: Perform package tests
run: swift test
- name: Integration tests with workflows
run: ./scripts/test.sh
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Swift

/.build
build
/Packages
xcuserdata/

Expand All @@ -14,5 +15,15 @@ Package.resolved

node_modules

package.tgz

responses/*
!responses/.gitkeep

# Yarn

.yarn/cache
.yarn/install-state.gz
.yarn/build-state.yml

tsconfig.tsbuildinfo
8 changes: 8 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-typescript.cjs

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions .yarn/releases/yarn-berry.cjs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"

yarnPath: .yarn/releases/yarn-berry.cjs
40 changes: 40 additions & 0 deletions Formula/SwiftGraphQL.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class SwiftGraphQL < Formula
desc "GraphQL code generator and client written in Swift"
homepage "https://github.com/maticzav/SwiftGraphQL"
url "https://github.com/maticzav/swift-graphql/archive/2.0.0.tar.gz"
sha256 "0e6adb87619b364ffe92d4e196a5811a0a988b02ba483324d14b5a5a83676ea5"
head "https://github.com/maticzav/swift-graphql.git"

depends_on :xcode

def install

# libxml2 has to be included in ISYSTEM_PATH for building one of
# dependencies. It didn't happen automatically before Xcode 9.3
# so homebrew patched environment variable to get it work.
#
# That works fine when you have just Xcode installed, but there
# is also CLT. If it is also installed, ISYSTEM_PATH has
# a reference to CLT libxml2 AND a reference to Xcode default
# toolchain libxml2. That causes build failure with "module redeclared"
# error. So if both Xcode and CLT are installed one reference
# has to be removed.
#
# It's a bug of homebrew but before it's fixed, it's easier
# to provide in-place workaround for now.
# Please remove this once homebrew is patched.

# step 1: capture old value and patch environment
if OS::Mac::Xcode.version >= Version.new("9.3") && !OS::Mac::Xcode.without_clt? then
old_isystem_paths = ENV["HOMEBREW_ISYSTEM_PATHS"]
ENV["HOMEBREW_ISYSTEM_PATHS"] = old_isystem_paths.gsub("/usr/include/libxml2", "")
end

# step 2: usual build
system "make", "install", "PREFIX=#{prefix}"

# step 3: restoring environment to pristine state
ENV["HOMEBREW_ISYSTEM_PATHS"] = old_isystem_paths if defined? old_isystem_paths

end
end
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
targets: ["SwiftGraphQLCodegen"]
),
.executable(
name: "SwiftGraphQLCLI",
name: "swift-graphql",
targets: ["SwiftGraphQLCLI"]
),
/* Utilities */
Expand Down Expand Up @@ -49,6 +49,7 @@ let package = Package(
"SwiftGraphQLCodegen",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"Yams",
"Files"
],
path: "Sources/SwiftGraphQLCLI"
),
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Human: Identifiable {
}

// Create a selection.
let human = Selection<Human, Objects.Human> {
let human = Selection.Human {
Human(
id: try $0.id(),
name: try $0.name(),
Expand All @@ -43,7 +43,7 @@ let human = Selection<Human, Objects.Human> {
}

// Construct a query.
let query = Selection<[Human], Operations.Query> {
let query = Selection.Query {
try $0.humans(human.list)
}

Expand Down
4 changes: 0 additions & 4 deletions Sources/GraphQLAST/README.md

This file was deleted.

7 changes: 0 additions & 7 deletions Sources/SwiftGraphQL/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions Sources/SwiftGraphQLCLI/Config.swift

This file was deleted.

82 changes: 78 additions & 4 deletions Sources/SwiftGraphQLCLI/main.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,96 @@
import ArgumentParser
import Files
import Foundation
import SwiftGraphQLCodegen
import Yams

SwiftGraphQLCLI.main()

// MARK: - CLI

struct SwiftGraphQLCLI: ParsableCommand {
// MARK: - Parameters

@Argument(help: "GraphQL server endpoint.")
var endpoint: String

@Option(help: "Relative path to your YML config file.")
@Option(help: "Relative path from CWD to your YML config file.")
var config: String?

@Option(name: .shortAndLong, help: "Relative path to the output file.")
@Option(name: .shortAndLong, help: "Relative path from CWD to the output file.")
var output: String?

// MARK: - Main

mutating func run() throws {}
func run() throws {
// Make sure we get a valid endpoint to fetch.
guard let url = URL(string: endpoint) else {
SwiftGraphQLCLI.exit(withError: SwiftGraphQLGeneratorError.endpoint)
}

// Load configuration if config path is present, otherwise use default.
let config: Config

if let configPath = self.config {
let raw = try Folder.current.file(at: configPath).read()
config = try Config(from: raw)
} else {
config = Config()
}

// Generate the code.
let generator = GraphQLCodegen(scalars: config.scalars)
let code = try generator.generate(from: url)

// Write to target file or stdout.
if let outputPath = self.output {
try Folder.current.file(at: outputPath).write(code)
} else {
FileHandle.standardOutput.write(code.data(using: .utf8)!)
}

// The end
}
}

SwiftGraphQLCLI.main()
// MARK: - Configuraiton

/*
swiftgraphql.yml
```yml
scalars:
Date: DateTime
```
*/

struct Config: Codable, Equatable {
/// Key-Value dictionary of scalar mappings.
let scalars: ScalarMap

// MARK: - Initializers

/// Creates an empty configuration instance.
init() {
scalars = ScalarMap()
}

/// Creates a new config instance from given parameters.
init(scalars: ScalarMap) {
self.scalars = scalars
}

/// Tries to decode the configuration from a string.
init(from data: Data) throws {
let decoder = YAMLDecoder()
self = try decoder.decode(Config.self, from: data)
}
}



// MARK: - Errors

enum SwiftGraphQLGeneratorError: String, Error {
case endpoint = "Invalid endpoint!"
}
14 changes: 1 addition & 13 deletions Sources/SwiftGraphQLCodegen/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,9 @@ public struct GraphQLCodegen {

// MARK: - Methods

/// Generates a target GraphQL Swift file.
/// Generates a target SwiftGraphQL Selection file.
///
/// - parameter target: Output file path.
/// - parameter from: GraphQL server endpoint.
///
/// - note: This function does not create a target file. You should make sure file exists beforehand.
public func generate(
_ target: URL,
from schemaURL: URL
) throws {
let code: String = try generate(from: schemaURL)
try code.write(to: target, atomically: true, encoding: .utf8)
}

/// Generates the API and returns it to handler.
public func generate(from endpoint: URL) throws -> String {
let schema = try Schema(from: endpoint)
let code = try generate(schema: schema)
Expand Down
16 changes: 0 additions & 16 deletions Sources/SwiftGraphQLCodegen/README.md

This file was deleted.

5 changes: 0 additions & 5 deletions examples/StarWars/Codegen/.gitignore

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions examples/StarWars/Codegen/Package.swift

This file was deleted.

27 changes: 0 additions & 27 deletions examples/StarWars/Codegen/Sources/Codegen/main.swift

This file was deleted.

Loading

0 comments on commit e5b02f1

Please sign in to comment.