Skip to content

Commit

Permalink
feat: swift implementation (#34)
Browse files Browse the repository at this point in the history
* chore(swift): init

* chore(swift): plugin done

* chore: make class, variables and plugin getter public

* chore: log level enum represent int

* chore: test asserted

* chore: podspec points to main

* chore(swift): add ci
  • Loading branch information
cbrzn authored Aug 18, 2023
1 parent 5bafb2a commit 41731dc
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/logger-plugin-swift-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: logger-swift-ci

on:
pull_request:
types: [closed]
branches:
- main
paths:
- "logger/implementations/swift/**"

jobs:
ci:
name: swift-logger-ci
runs-on: macos-latest
timeout-minutes: 60
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Build Pod
run: pod spec lint --allow-warnings --verbose

- name: Build Package
run: swift build
working-directory: ./logger/implementations/swift

- name: Run Tests
run: swift test
working-directory: ./logger/implementations/swift
15 changes: 15 additions & 0 deletions LoggerPlugin.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Pod::Spec.new do |s|
s.name = 'LoggerPlugin'
s.version = '0.0.1'
s.summary = 'Logger plugin'
s.homepage = 'https://github.com/polywrap/logging'
s.license = 'MIT'
s.author = { 'Cesar' => '[email protected]' }

s.source_files = 'logger/implementations/swift/Source/**/*.swift'
s.swift_version = "5.0"
s.ios.deployment_target = '14.0'
s.source = { :git => "https://github.com/polywrap/logging.git", :branch => 'main' }
s.static_framework = true
s.dependency 'PolywrapClient', '~> 0.0.7'
end
12 changes: 12 additions & 0 deletions logger/implementations/swift/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
!Source/wrap
node_modules
yarn.lock
41 changes: 41 additions & 0 deletions logger/implementations/swift/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"pins" : [
{
"identity" : "asyncobjects",
"kind" : "remoteSourceControl",
"location" : "https://github.com/SwiftyLab/AsyncObjects.git",
"state" : {
"revision" : "93aacfba87d738ac94fb50c2a6768c43ec9d42eb",
"version" : "2.1.0"
}
},
{
"identity" : "messagepacker",
"kind" : "remoteSourceControl",
"location" : "https://github.com/hirotakan/MessagePacker.git",
"state" : {
"revision" : "4d8346c6bc579347e4df0429493760691c5aeca2",
"version" : "0.4.7"
}
},
{
"identity" : "swift-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/polywrap/swift-client",
"state" : {
"branch" : "main",
"revision" : "488ecdb35461ba38de5bb9144ca2b37609c75caa"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version" : "1.0.4"
}
}
],
"version" : 2
}
37 changes: 37 additions & 0 deletions logger/implementations/swift/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "LoggerPlugin",
platforms: [.iOS(.v15), .macOS(.v11)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LoggerPlugin",
targets: ["LoggerPlugin"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/polywrap/swift-client", branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "LoggerPlugin",
dependencies: [
.product(name: "PolywrapClient", package: "swift-client"),
],
path: "Source"
),
.testTarget(
name: "LoggerPluginTests",
dependencies: ["LoggerPlugin"],
path: "Tests"
),
],
swiftLanguageVersions: [.v5]
)
50 changes: 50 additions & 0 deletions logger/implementations/swift/Source/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import OSLog
import PolywrapClient

public typealias LogFunc = (LogLevel, String) -> Void

extension Logger {
/// Using your bundle identifier is a great way to ensure a unique identifier.
private static var subsystem = Bundle.main.bundleIdentifier!

/// Logs the view cycles like a view that appeared.
static let viewCycle = Logger(subsystem: subsystem, category: "viewcycle")

/// All logs related to tracking and analytics.
static let statistics = Logger(subsystem: subsystem, category: "statistics")
}

public class LoggerPlugin: Plugin {
public var methodsMap: [String : PluginMethod] = [:]
public let config: LogFunc?

public init(config: LogFunc?) {
self.config = config
}

public func log(_ args: ArgsLog, _ env: VoidCodable?, _ invoker: Invoker) -> Bool {
if let logFunc = config {
logFunc(args.level, args.message)
return true
}

switch args.level {
case .DEBUG:
Logger.viewCycle.debug("\(args.message)")
case .WARN:
Logger.viewCycle.warning("\(args.message)")
case .ERROR:
Logger.viewCycle.error("\(args.message)")
case .INFO:
Logger.viewCycle.info("\(args.message)")
}

return true
}
}

public func getLoggerPlugin(_ logFunc: LogFunc?) -> LoggerPlugin {
var plugin = LoggerPlugin(config: logFunc)
plugin.addMethod(name: "log", closure: plugin.log)
return plugin
}
15 changes: 15 additions & 0 deletions logger/implementations/swift/Source/wrap/Module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// NOTE: This is an auto-generated file.
// All modifications will be overwritten.

import PolywrapClient
import Foundation

public struct ArgsLog: Codable {
var level: LogLevel
var message: String
}


public protocol Plugin: PluginModule {
func log(_ args: ArgsLog, _ env: VoidCodable?, _ invoker: Invoker) throws -> Bool
}
46 changes: 46 additions & 0 deletions logger/implementations/swift/Source/wrap/Types.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// NOTE: This is an auto-generated file.
// All modifications will be overwritten.

import PolywrapClient
import Foundation

// Env START //


// Env END //

// Objects START //


// Objects END //

// Enums START //

public enum LogLevel: Int, Codable {
case DEBUG = 0
case INFO
case WARN
case ERROR
}


// Enums END //

// Imported objects START //


// Imported objects END //

// Imported envs START //


// Imported envs END //

// Imported enums START //


// Imported enums END //

// Imported modules START //

// Imported Modules END //
22 changes: 22 additions & 0 deletions logger/implementations/swift/Tests/LoggerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import XCTest
@testable import LoggerPlugin
@testable import PolywrapClient


func getClient() throws -> (PolywrapClient, Uri) {
let uri = try Uri("plugin/logger")
let loggerPlugin = getLoggerPlugin(nil)
let package = PluginPackage(loggerPlugin)
let builder = BuilderConfig().addPackage(uri, package)
return (builder.build(), uri)
}

final class LoggerPluginTests: XCTestCase {
func testLog() throws {
let (client, uri) = try getClient()
let args = ArgsLog(level: LogLevel.INFO, message: "Hallo!")

let response: Bool = try client.invoke(uri: uri, method: "log", args: args)
XCTAssert(response)
}
}
10 changes: 10 additions & 0 deletions logger/implementations/swift/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@polywrap/logger-swift",
"description": "Polywrap Logger Plugin in swift",
"scripts": {
"codegen": "polywrap codegen -g ./Source/wrap"
},
"devDependencies": {
"polywrap": "0.11.1"
}
}
1 change: 1 addition & 0 deletions logger/implementations/swift/polywrap.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import * from "wrapscan.io/polywrap/[email protected]"
7 changes: 7 additions & 0 deletions logger/implementations/swift/polywrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
format: 0.3.0
project:
type: plugin/swift
name: Logger
source:
module: ./Package.swift
schema: ./polywrap.graphql

0 comments on commit 41731dc

Please sign in to comment.