Skip to content

Commit

Permalink
Merge pull request square#1926 from kevincianfarini/generate-duration
Browse files Browse the repository at this point in the history
Add gradle task to generate WellKnownTypes
  • Loading branch information
efirestone authored Apr 6, 2021
2 parents 85dc212 + 83ab6ea commit d158ede
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ jobs:

- name: Check test files
run: |
sudo xcode-select -switch "/Applications/Xcode_12.app"
./gradlew clean generateTests
./gradlew -p wire-library/wire-runtime-swift build
if [ ! -z "$(git status --porcelain)" ]; then git diff; echo -e "\nTest files changed. Did you run ./gradlew generateTests?"; exit 1; fi
- name: Install Chrome
Expand Down
2 changes: 1 addition & 1 deletion Wire.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
s.ios.deployment_target = '10.0'
s.osx.deployment_target = '10.15'

s.source_files = 'wire-library/wire-runtime-swift/src/main/swift/*.swift'
s.source_files = 'wire-library/wire-runtime-swift/src/main/swift/**/*.swift'

s.test_spec do |test_spec|
test_spec.ios.deployment_target = '11.0'
Expand Down
58 changes: 55 additions & 3 deletions wire-library/wire-runtime-swift/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,53 @@ dependencies {
wire project(':wire-compiler')
}

task generateSwiftProtos(type: JavaExec) {
def swiftOut = 'src/main/swift/wellknowntypes'
def protoPath = '../wire-schema/src/jvmMain/resources/'

doFirst {
def outFile = file(swiftOut)
outFile.deleteDir()
outFile.mkdir()
}

def includedTypes = ["Duration"].collect {
"google.protobuf.$it"
}

classpath = configurations.wire
main = 'com.squareup.wire.WireCompiler'
args = [
"--proto_path=$protoPath",
"--swift_out=$swiftOut",
"--includes=${includedTypes.join(",")}",
"--excludes=google.protobuf.*"
]

// TODO(kcianfarini) this is a workaround for https://github.com/square/wire/issues/1928
doLast {
def file = [
"EnumOptions.swift",
"EnumValueOptions.swift",
"FieldOptions.swift",
"FileOptions.swift",
"MessageOptions.swift",
"MethodOptions.swift",
"ServiceOptions.swift"
]

def actualFiles = file.collect {
"wire-library/wire-runtime-swift/src/main/swift/wellknowntypes/" + it
}

actualFiles.each {new File(it).delete() }
}
}

task generateTestProtos(type: JavaExec) {
def swiftOut = 'src/test/swift/gen'
def protoPath = 'src/test/proto'

doFirst {
def outFile = file(swiftOut)
outFile.deleteDir()
Expand All @@ -30,14 +75,21 @@ task generateTestProtos(type: JavaExec) {
classpath = configurations.wire
main = 'com.squareup.wire.WireCompiler'
args = [
'--proto_path=src/test/proto',
"--swift_out=$swiftOut"
"--proto_path=$protoPath",
"--swift_out=$swiftOut"
]
}

// TODO make finding test tasks more dynamic
afterEvaluate {
tasks.named('compileTestSwift').configure {
def swiftTasks = ["compileDebugSwift", "compileReleaseSwift"]
swiftTasks.each {task ->
tasks.named(task).configure {
it.dependsOn(generateSwiftProtos)
}
}

tasks.named("compileTestSwift").configure {
it.dependsOn(generateTestProtos)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Code generated by Wire protocol buffer compiler, do not edit.
// Source: google.protobuf.Duration in google/protobuf/duration.proto
import Foundation
import Wire

/**
* A Duration represents a signed, fixed-length span of time represented
* as a count of seconds and fractions of seconds at nanosecond
* resolution. It is independent of any calendar and concepts like "day"
* or "month". It is related to Timestamp in that the difference between
* two Timestamp values is a Duration and it can be added or subtracted
* from a Timestamp. Range is approximately +-10,000 years.
*
* Example 1: Compute Duration from two Timestamps in pseudo code.
*
* Timestamp start = ...;
* Timestamp end = ...;
* Duration duration = ...;
*
* duration.seconds = end.seconds - start.seconds;
* duration.nanos = end.nanos - start.nanos;
*
* if (duration.seconds < 0 && duration.nanos > 0) {
* duration.seconds += 1;
* duration.nanos -= 1000000000;
* } else if (durations.seconds > 0 && duration.nanos < 0) {
* duration.seconds -= 1;
* duration.nanos += 1000000000;
* }
*
* Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
*
* Timestamp start = ...;
* Duration duration = ...;
* Timestamp end = ...;
*
* end.seconds = start.seconds + duration.seconds;
* end.nanos = start.nanos + duration.nanos;
*
* if (end.nanos < 0) {
* end.seconds -= 1;
* end.nanos += 1000000000;
* } else if (end.nanos >= 1000000000) {
* end.seconds += 1;
* end.nanos -= 1000000000;
* }
*/
public struct Duration {

/**
* Signed seconds of the span of time. Must be from -315,576,000,000
* to +315,576,000,000 inclusive.
*/
@JSONString
public var seconds: Int64
/**
* Signed fractions of a second at nanosecond resolution of the span
* of time. Durations less than one second are represented with a 0
* `seconds` field and a positive or negative `nanos` field. For durations
* of one second or more, a non-zero value for the `nanos` field must be
* of the same sign as the `seconds` field. Must be from -999,999,999
* to +999,999,999 inclusive.
*/
public var nanos: Int32
public var unknownFields: Data = .init()

public init(seconds: Int64, nanos: Int32) {
self.seconds = seconds
self.nanos = nanos
}

}

#if !WIRE_REMOVE_EQUATABLE
extension Duration : Equatable {
}
#endif

#if !WIRE_REMOVE_HASHABLE
extension Duration : Hashable {
}
#endif

extension Duration : Proto3Codable {
public init(from reader: ProtoReader) throws {
var seconds: Int64? = nil
var nanos: Int32? = nil

let token = try reader.beginMessage()
while let tag = try reader.nextTag(token: token) {
switch tag {
case 1: seconds = try reader.decode(Int64.self)
case 2: nanos = try reader.decode(Int32.self)
default: try reader.readUnknownField(tag: tag)
}
}
self.unknownFields = try reader.endMessage(token: token)

self.seconds = try Duration.checkIfMissing(seconds, "seconds")
self.nanos = try Duration.checkIfMissing(nanos, "nanos")
}

public func encode(to writer: ProtoWriter) throws {
try writer.encode(tag: 1, value: self.seconds)
try writer.encode(tag: 2, value: self.nanos)
try writer.writeUnknownFields(unknownFields)
}
}

#if !WIRE_REMOVE_CODABLE
extension Duration : Codable {
public enum CodingKeys : String, CodingKey {

case seconds
case nanos

}
}
#endif

0 comments on commit d158ede

Please sign in to comment.