Browse Source

Templates are now compiled directly into the protoc-gen-swiftgrpc plugin.

Tim Burks 9 years ago
parent
commit
38b73fa52e

+ 4 - 2
Plugin/Makefile

@@ -2,7 +2,9 @@
 default:	build
 
 build:  clear
-	swift build
+	swift build # This run is to build the template encoder
+	.build/debug/TemplateEncoder > Sources/protoc-gen-swiftgrpc/templates.swift
+	swift build # This run is to build the plugin
 	cp .build/debug/protoc-gen-swiftgrpc .
 	cp .build/debug/protoc-gen-swift .
 
@@ -19,4 +21,4 @@ clear :
 	rm -f echo.client.pb.swift echo.server.pb.swift swiftgrpc.log
 
 clean : clear
-	rm -rf protoc-gen-swiftgrpc Packages .build
+	rm -rf protoc-gen-swiftgrpc Packages .build protoc-gen-swift

+ 12 - 5
Plugin/Package.swift

@@ -15,9 +15,16 @@
 import PackageDescription
 
 let package = Package(
-    name: "protoc-gen-swiftgrpc",
-    dependencies: [
-        .Package(url: "https://github.com/apple/swift-protobuf.git", Version(0,9,24)),
-        .Package(url: "https://github.com/timburks/Stencil.git", Version(9,9,9)) // temporary fork
-    ]
+  name: "SwiftGRPCPlugin",
+  targets: [
+    Target(name: "protoc-gen-swiftgrpc",
+           dependencies: [
+            "TemplateEncoder",
+            ]),
+    Target(name: "TemplateEncoder")
+  ],
+  dependencies: [
+    .Package(url: "https://github.com/apple/swift-protobuf.git", Version(0,9,24)),
+    .Package(url: "https://github.com/timburks/Stencil.git", Version(9,9,9)) // temporary fork
+  ]
 )

+ 3 - 3
Plugin/README.md

@@ -11,6 +11,6 @@ the Makefile:
 
 	protoc ../Examples/Echo/echo.proto --proto_path=../Examples/Echo --swiftgrpc_out=. 
 
-The plugin uses template files in the [swiftgrpc.templates](swiftgrpc.templates) 
-directory. This directory currently must be in the same location 
-as the `protoc-gen-swiftgrpc` plugin executable.
+The plugin uses template files in the [Templates](Templates) directory. 
+These files are compiled into the `protoc-gen-swiftgrpc` plugin executable.
+

+ 40 - 0
Plugin/Sources/TemplateEncoder/main.swift

@@ -0,0 +1,40 @@
+// Copyright 2016 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import Foundation
+
+let TEMPLATES = "Templates"
+
+var s = ""
+s += "// GENERATED: DO NOT EDIT\n"
+s += "//\n"
+s += "// This file contains base64 encodings of templates used for Swift GRPC code generation.\n"
+s += "//\n"
+s += "func loadTemplates() -> [String:String] {\n"
+s += "  var templates : [String:String] = [:]\n"
+
+let filenames = try FileManager.default.contentsOfDirectory(atPath:TEMPLATES)
+for filename in filenames {
+  if filename.hasSuffix(".swift") {
+    let fileURL = URL(fileURLWithPath:TEMPLATES + "/" + filename)
+    let filedata = try Data(contentsOf:fileURL)
+    let encoding = filedata.base64EncodedString()
+    s += "\n"
+    s += "  templates[\"" + filename + "\"] = \"" + encoding + "\"\n"
+  }
+}
+
+s += "  return templates\n"
+s += "}\n"
+print(s)

+ 51 - 0
Plugin/Sources/protoc-gen-swiftgrpc/InternalLoader.swift

@@ -0,0 +1,51 @@
+// Copyright 2016 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+import Stencil
+import Foundation
+
+// A class for loading Stencil templates from compiled-in representations
+
+public class InternalLoader: Loader {
+  private var templates: [String:String]
+
+  public init() {
+    self.templates = loadTemplates()
+  }
+
+  public func loadTemplate(name: String, environment: Environment) throws -> Template {
+    if let encoding = templates[name],
+      let data = Data(base64Encoded: encoding, options:[]),
+      let template = String(data:data, encoding:.utf8) {
+      return environment.templateClass.init(templateString: template,
+                                            environment: environment,
+                                            name: name)
+    } else {
+      throw TemplateDoesNotExist(templateNames: [name], loader: self)
+    }
+  }
+
+  public func loadTemplate(names: [String], environment: Environment) throws -> Template {
+    for name in names {
+      if let encoding = templates[name],
+        let data = Data(base64Encoded: encoding, options:[]),
+        let template = String(data:data, encoding:.utf8) {
+        return environment.templateClass.init(templateString: template,
+                                              environment: environment,
+                                              name: name)
+      }
+    }
+    throw TemplateDoesNotExist(templateNames: names, loader: self)
+  }
+}

+ 0 - 0
Plugin/Sources/io.swift → Plugin/Sources/protoc-gen-swiftgrpc/io.swift


+ 1 - 11
Plugin/Sources/main.swift → Plugin/Sources/protoc-gen-swiftgrpc/main.swift

@@ -125,17 +125,7 @@ func Log(_ message : String) {
 
 func main() throws {
 
-  // we expect our templates to be in the same directory as this executable.
-  var path = "./protoc-gen-swiftgrpc"
-  if let executablePath = Bundle.main.executablePath {
-    path = executablePath
-  }
-  let executableURL = URL(fileURLWithPath: path)
-  let executableDir : String = executableURL.deletingLastPathComponent().path
-  let templatePath = Path(executableDir + "/swiftgrpc.templates/")
-
   // initialize template engine and add custom filters
-  let fileSystemLoader = FileSystemLoader(paths: [templatePath])
   let ext = Extension()
   ext.registerFilter("call") { (value: Any?, arguments: [Any?]) in
     return try packageServiceMethodName(arguments) + "Call"
@@ -173,7 +163,7 @@ func main() throws {
     }
     throw TemplateSyntaxError("message: invalid argument \(value)")
   }
-  let templateEnvironment = Environment(loader: fileSystemLoader,
+  let templateEnvironment = Environment(loader: InternalLoader(),
                                         extensions:[ext])
 
   // initialize responses

File diff suppressed because it is too large
+ 7 - 0
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift


+ 0 - 0
Plugin/swiftgrpc.templates/client-call-bidistreaming.swift → Plugin/Templates/client-call-bidistreaming.swift


+ 0 - 0
Plugin/swiftgrpc.templates/client-call-clientstreaming.swift → Plugin/Templates/client-call-clientstreaming.swift


+ 0 - 0
Plugin/swiftgrpc.templates/client-call-serverstreaming.swift → Plugin/Templates/client-call-serverstreaming.swift


+ 0 - 0
Plugin/swiftgrpc.templates/client-call-unary.swift → Plugin/Templates/client-call-unary.swift


+ 0 - 0
Plugin/swiftgrpc.templates/client.pb.swift → Plugin/Templates/client.pb.swift


+ 0 - 0
Plugin/swiftgrpc.templates/server-session-bidistreaming.swift → Plugin/Templates/server-session-bidistreaming.swift


+ 0 - 0
Plugin/swiftgrpc.templates/server-session-clientstreaming.swift → Plugin/Templates/server-session-clientstreaming.swift


+ 0 - 0
Plugin/swiftgrpc.templates/server-session-serverstreaming.swift → Plugin/Templates/server-session-serverstreaming.swift


+ 0 - 0
Plugin/swiftgrpc.templates/server-session-unary.swift → Plugin/Templates/server-session-unary.swift


+ 0 - 0
Plugin/swiftgrpc.templates/server.pb.swift → Plugin/Templates/server.pb.swift


Some files were not shown because too many files changed in this diff