Skip to content

Commit

Permalink
Merge branch 'segmentio:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 authored Mar 28, 2024
2 parents 0ea77cd + 7eeb2ab commit 886547d
Show file tree
Hide file tree
Showing 24 changed files with 1,422 additions and 647 deletions.
170 changes: 93 additions & 77 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,135 @@ name: Swift

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
cancel_previous:
runs-on: ubuntu-latest
steps:
- uses: styfle/cancel-workflow-action@0.9.1
with:
workflow_id: ${{ github.event.workflow.id }}
- uses: styfle/cancel-workflow-action@0.12.0
with:
workflow_id: ${{ github.event.workflow.id }}

build_and_test_spm_mac:
needs: cancel_previous
runs-on: macos-latest
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: Build
run: swift build
- name: Run tests
run: swift test
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: Build
run: swift build
- name: Run tests
run: swift test

build_and_test_spm_linux:
needs: cancel_previous
runs-on: ubuntu-latest
steps:
- uses: fwal/setup-swift@v1.21.0
with:
swift-version: "5.7.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: Build
run: swift build
- name: Run tests
run: swift test --enable-test-discovery
- uses: sersoft-gmbh/swifty-linux-action@v3
with:
release-version: "5.9.2"
github-token: ${{secrets.GITHUB_TOKEN}}
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: Build
run: swift build
- name: Run tests
run: swift test --enable-test-discovery

build_and_test_ios:
needs: cancel_previous
runs-on: macos-latest
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: xcodebuild -scheme Segment test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 13'

- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: xcodebuild -scheme Segment test -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 15'

build_and_test_tvos:
needs: cancel_previous
runs-on: macos-latest
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: xcodebuild -scheme Segment test -sdk appletvsimulator -destination 'platform=tvOS Simulator,name=Apple TV'
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: xcodebuild -scheme Segment test -sdk appletvsimulator -destination 'platform=tvOS Simulator,name=Apple TV'

build_and_test_watchos:
needs: cancel_previous
runs-on: macos-latest
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: xcodebuild -scheme Segment test -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch Series 9 (45mm)'

build_and_test_visionos:
needs: cancel_previous
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: xcodebuild -scheme Segment test -sdk watchsimulator -destination 'platform=watchOS Simulator,name=Apple Watch Series 8 (45mm)'

- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- run: defaults write com.apple.dt.Xcode AllowUnsupportedVisionOSHost -bool YES
- run: defaults write com.apple.CoreSimulator AllowUnsupportedVisionOSHost -bool YES
- run: xcodebuild -downloadPlatform visionOS
- run: echo - skip until apple fixes this - xcodebuild -scheme Segment test -sdk xrsimulator -destination 'platform=visionOS Simulator,name=Apple Vision Pro'
- run: xcodebuild -scheme Segment -sdk xrsimulator -destination 'platform=visionOS Simulator,name=Apple Vision Pro'

build_and_test_examples:
needs: cancel_previous
runs-on: macos-latest
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: latest-stable
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.5.3
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: build for ios simulator
run: |
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: "15.2"
- uses: actions/checkout@v2
- uses: webfactory/ssh-agent@v0.8.0
with:
ssh-private-key: ${{ secrets.SOVRAN_SSH_KEY }}
- name: build for ios simulator
run: |
cd Examples/apps/BasicExample
xcodebuild -workspace "BasicExample.xcworkspace" -scheme "BasicExample" -sdk iphonesimulator
- name: build for ios simulator
run: |
- name: build for ios simulator
run: |
cd Examples/apps/ObjCExample
xcodebuild -workspace "ObjCExample.xcworkspace" -scheme "ObjCExample" -sdk iphonesimulator
- name: build for ios simulator
run: |
- name: build for ios simulator
run: |
cd Examples/apps/SegmentUIKitExample
xcodebuild -workspace "SegmentUIKitExample.xcworkspace" -scheme "SegmentUIKitExample" -sdk iphonesimulator
- name: build for ios simulator
run: |
- name: build for ios simulator
run: |
cd Examples/apps/SegmentWeatherWidget
xcodebuild -workspace "SegmentWeatherWidget.xcworkspace" -scheme "SegmentWeatherWidget" -sdk iphonesimulator
- name: build for mac catalyst
run: |
- name: build for mac catalyst
run: |
cd Examples/apps/SegmentUIKitExample
xcodebuild -workspace "SegmentUIKitExample.xcworkspace" -scheme "SegmentUIKitExample" -destination 'platform=macOS,variant=Mac Catalyst'
7 changes: 4 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.7
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -9,7 +9,8 @@ let package = Package(
.macOS("10.15"),
.iOS("13.0"),
.tvOS("11.0"),
.watchOS("7.1")
.watchOS("7.1"),
.visionOS("1.0")
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
Expand All @@ -32,7 +33,7 @@ let package = Package(
.product(name: "Sovran", package: "sovran-swift"),
.product(name: "JSONSafeEncoder", package: "jsonsafeencoder-swift")
],
exclude: ["PrivacyInfo.xcprivacy"]),
resources: [.process("Resources")]),
.testTarget(
name: "Segment-Tests",
dependencies: ["Segment"]),
Expand Down
41 changes: 0 additions & 41 deletions Package@swift-5.9.swift

This file was deleted.

30 changes: 15 additions & 15 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ public class Analytics {
/// - configuration: The configuration to use
public init(configuration: Configuration) {
if Self.isActiveWriteKey(configuration.values.writeKey) {
// If you're hitting this in testing, it could be a memory leak, or something async is still running
// and holding a reference. You can use XCTest.waitUntilFinished(...) to wait for things to complete.
fatalError("Cannot initialize multiple instances of Analytics with the same write key")
} else {
Self.addActiveWriteKey(configuration.values.writeKey)
}

store = Store()
storage = Storage(store: self.store, writeKey: configuration.values.writeKey)
storage = Storage(
store: self.store,
writeKey: configuration.values.writeKey,
storageMode: configuration.values.storageMode,
operatingMode: configuration.values.operatingMode
)
timeline = Timeline()

// provide our default state
Expand Down Expand Up @@ -330,32 +337,25 @@ extension Analytics {
}
}

if let files = storage.read(Storage.Constants.events) {
if files.count > 0 {
return true
}
}

return false
return storage.dataStore.hasData
}

/// Provides a list of finished, but unsent events.
public var pendingUploads: [URL]? {
return storage.read(Storage.Constants.events)
return storage.read(Storage.Constants.events)?.dataFiles
}

/// Purge all pending event upload files.
public func purgeStorage() {
if let files = pendingUploads {
for file in files {
purgeStorage(fileURL: file)
}
}
storage.dataStore.reset()
}

/// Purge a single event upload file.
public func purgeStorage(fileURL: URL) {
try? FileManager.default.removeItem(at: fileURL)
guard let dataFiles = storage.read(Storage.Constants.events)?.dataFiles else { return }
if dataFiles.contains(fileURL) {
try? FileManager.default.removeItem(at: fileURL)
}
}

/// Wait until the Analytics object has completed startup.
Expand Down
20 changes: 20 additions & 0 deletions Sources/Segment/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ public enum OperatingMode {
static internal let defaultQueue = DispatchQueue(label: "com.segment.operatingModeQueue", qos: .utility)
}

// MARK: - Storage Mode
/// Specifies the storage mode to be used for events
public enum StorageMode {
/// Store events to disk (default).
case disk
/// Store events to disk in the given a directory URL.
case diskAtURL(URL)
/// Store events to memory and specify a max count before they roll off.
case memory(Int)
/// Some custom, user-defined storage mechanism conforming to `DataStore`.
case custom(any DataStore)
}

// MARK: - Internal Configuration

public class Configuration {
Expand All @@ -42,6 +55,7 @@ public class Configuration {
var flushQueue: DispatchQueue = OperatingMode.defaultQueue
var userAgent: String? = nil
var jsonNonConformingNumberStrategy: JSONSafeEncoder.NonConformingFloatEncodingStrategy = .zero
var storageMode: StorageMode = .disk
}

internal var values: Values
Expand Down Expand Up @@ -233,6 +247,12 @@ public extension Configuration {
JSON.jsonNonConformingNumberStrategy = values.jsonNonConformingNumberStrategy
return self
}

@discardableResult
func storageMode(_ mode: StorageMode) -> Configuration {
values.storageMode = mode
return self
}
}

extension Analytics {
Expand Down
7 changes: 1 addition & 6 deletions Sources/Segment/Plugins/DestinationMetadataPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ import Foundation
public class DestinationMetadataPlugin: Plugin {
public let type: PluginType = PluginType.enrichment
public weak var analytics: Analytics?
private var analyticsSettings: Settings? = nil

public func update(settings: Settings, type: UpdateType) {
analyticsSettings = settings
}


public func execute<T: RawEvent>(event: T?) -> T? {
guard var modified = event else {
return event
Expand Down
Loading

0 comments on commit 886547d

Please sign in to comment.