Skip to content

Commit

Permalink
Add Swift output support (#142)
Browse files Browse the repository at this point in the history
* rename ios to objc to prep for swift support

* corrected template path

* Finished up swift export

* Fixed tests

* fixed array serialization

* updated generated file

* fixed circleci setup

* ref cocoapods 1.8.4

* Added window property to example app

* addressed comments from Colin K

* updated objc podfile

* removed window reference to get back to known behavior

Co-authored-by: Brandon Sneed <brandon.sneed@segment.com>
  • Loading branch information
bsneed and Brandon Sneed authored Oct 8, 2020
1 parent 59ca69d commit 117bfc2
Show file tree
Hide file tree
Showing 87 changed files with 1,623 additions and 2,324 deletions.
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
name: Install Cocoapods
command: |
cd tests/e2e/ios-objc
pod check --verbose || pod install --deployment
pod check --verbose || pod install --repo-update
- save_cache:
paths:
- tests/e2e/ios-objc/Pods
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:
name: Install Cocoapods
command: |
cd tests/e2e/ios-objc
pod check --verbose || pod install --deployment
pod check --verbose || pod install --repo-update
- save_cache:
paths:
- tests/e2e/ios-objc/Pods
Expand Down Expand Up @@ -192,7 +192,7 @@ jobs:
name: Install Cocoapods
command: |
cd tests/e2e/ios-swift
pod check --verbose || pod install --deployment
pod check --verbose || pod install --repo-update
- save_cache:
paths:
- tests/e2e/ios-swift/Pods
Expand Down Expand Up @@ -223,7 +223,7 @@ jobs:
name: Install Cocoapods
command: |
cd tests/e2e/ios-swift
pod check --verbose || pod install --deployment
pod check --verbose || pod install --repo-update
- save_cache:
paths:
- tests/e2e/ios-swift/Pods
Expand Down
11 changes: 8 additions & 3 deletions src/generators/gen.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JSONSchema7 } from 'json-schema'
import { parse, Schema, getPropertiesSchema, Type } from './ast'
import { javascript } from './javascript'
import { ios } from './ios'
import { objc } from './objc'
import { swift } from './swift'
import { android } from './android'
import { Options, SDK } from './options'
import { Options, SDK, Language } from './options'
import { registerStandardHelpers, generateFromTemplate } from '../templates'
import { Namer, Options as NamerOptions } from './namer'
import stringify from 'json-stable-stringify'
Expand Down Expand Up @@ -157,7 +158,11 @@ export async function gen(trackingPlan: RawTrackingPlan, options: GenOptions): P
if (options.client.sdk === SDK.WEB || options.client.sdk === SDK.NODE) {
return await runGenerator(javascript, parsedTrackingPlan, options)
} else if (options.client.sdk === SDK.IOS) {
return await runGenerator(ios, parsedTrackingPlan, options)
if (options.client.language === Language.SWIFT) {
return await runGenerator(swift, parsedTrackingPlan, options)
} else {
return await runGenerator(objc, parsedTrackingPlan, options)
}
} else if (options.client.sdk === SDK.ANDROID) {
return await runGenerator(android, parsedTrackingPlan, options)
} else {
Expand Down
1 change: 0 additions & 1 deletion src/generators/ios/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/generators/objc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { objc } from './objc'
34 changes: 17 additions & 17 deletions src/generators/ios/ios.ts → src/generators/objc/objc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { Generator, BasePropertyContext, GeneratorClient } from '../gen'
// These contexts are what will be passed to Handlebars to perform rendering.
// Everything in these contexts should be properly sanitized.

interface IOSObjectContext {
// The formatted name for this object, ex: "numAvocados
interface ObjCObjectContext {
// The formatted name for this object, ex: "numAvocados".
name: string
// Set of files that need to be imported in this file.
imports: string[]
}

interface IOSPropertyContext {
interface ObjCPropertyContext {
// The formatted name for this property, ex: "numAvocados".
name: string
// The type of this property. ex: "NSNumber".
Expand All @@ -31,12 +31,12 @@ interface IOSPropertyContext {
importName?: string
}

interface IOSTrackCallContext {
interface ObjCTrackCallContext {
// The formatted function name, ex: "orderCompleted".
functionName: string
}

export const ios: Generator<{}, IOSTrackCallContext, IOSObjectContext, IOSPropertyContext> = {
export const objc: Generator<{}, ObjCTrackCallContext, ObjCObjectContext, ObjCPropertyContext> = {
generatePropertiesObject: false,
namer: {
// See: https://github.com/AnanthaRajuCprojects/Reserved-Key-Words-list-of-various-programming-languages/blob/master/Objective-C%20Reserved%20Words.md
Expand Down Expand Up @@ -99,7 +99,7 @@ export const ios: Generator<{}, IOSTrackCallContext, IOSObjectContext, IOSProper
},
generateObject: async (client, schema, properties, parentPath) => {
const property = defaultPropertyContext(client, schema, 'SERIALIZABLE_DICT', parentPath, true)
let object: IOSObjectContext | undefined = undefined
let object: ObjCObjectContext | undefined = undefined

if (properties.length > 0) {
// If at least one property is set, generate a class that only allows the explicitly
Expand Down Expand Up @@ -132,34 +132,34 @@ export const ios: Generator<{}, IOSTrackCallContext, IOSObjectContext, IOSProper
await Promise.all([
client.generateFile(
'SEGTypewriterAnalytics.h',
'generators/ios/templates/analytics.h.hbs',
'generators/objc/templates/analytics.h.hbs',
context
),
client.generateFile(
'SEGTypewriterAnalytics.m',
'generators/ios/templates/analytics.m.hbs',
'generators/objc/templates/analytics.m.hbs',
context
),
client.generateFile(
'SEGTypewriterUtils.h',
'generators/ios/templates/SEGTypewriterUtils.h.hbs',
'generators/objc/templates/SEGTypewriterUtils.h.hbs',
context
),
client.generateFile(
'SEGTypewriterUtils.m',
'generators/ios/templates/SEGTypewriterUtils.m.hbs',
'generators/objc/templates/SEGTypewriterUtils.m.hbs',
context
),
client.generateFile(
'SEGTypewriterSerializable.h',
'generators/ios/templates/SEGTypewriterSerializable.h.hbs',
'generators/objc/templates/SEGTypewriterSerializable.h.hbs',
context
),
...context.objects.map(o =>
client.generateFile(`${o.name}.h`, 'generators/ios/templates/class.h.hbs', o)
client.generateFile(`${o.name}.h`, 'generators/objc/templates/class.h.hbs', o)
),
...context.objects.map(o =>
client.generateFile(`${o.name}.m`, 'generators/ios/templates/class.m.hbs', o)
client.generateFile(`${o.name}.m`, 'generators/objc/templates/class.m.hbs', o)
),
])
},
Expand All @@ -171,7 +171,7 @@ function defaultPropertyContext(
type: string,
namespace: string,
isPointerType: boolean
): IOSPropertyContext {
): ObjCPropertyContext {
return {
name: client.namer.register(schema.name, namespace, {
transform: camelCase,
Expand All @@ -192,7 +192,7 @@ function defaultPropertyContext(

function generateFunctionSignature(
functionName: string,
properties: (BasePropertyContext & IOSPropertyContext)[],
properties: (BasePropertyContext & ObjCPropertyContext)[],
withOptions: boolean
): string {
let signature = functionName
Expand Down Expand Up @@ -235,7 +235,7 @@ function generateFunctionSignature(
function generateFunctionCall(
caller: string,
functionName: string,
properties: (BasePropertyContext & IOSPropertyContext)[],
properties: (BasePropertyContext & ObjCPropertyContext)[],
extraParameterName?: string,
extraParameterValue?: string
): string {
Expand Down Expand Up @@ -263,7 +263,7 @@ function generateFunctionCall(
}

function generatePropertiesDictionary(
properties: (BasePropertyContext & IOSPropertyContext)[],
properties: (BasePropertyContext & ObjCPropertyContext)[],
prefix?: string
): string {
let out = 'NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];\n'
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/generators/swift/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { swift } from './swift'
Loading

0 comments on commit 117bfc2

Please # to comment.