Skip to content

Commit

Permalink
Merge pull request #27 from Rallista/feat/swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthetechie authored Mar 2, 2024
2 parents 8a9320d + f1f3201 commit 1eb1618
Show file tree
Hide file tree
Showing 50 changed files with 559 additions and 524 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release Maplibre-SwiftUI
name: Release

on:
push:
Expand Down
21 changes: 17 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test maplibre-swiftui-dsl-playground
name: Test

on:
push:
Expand All @@ -7,6 +7,19 @@ on:
branches: [ main ]

jobs:
format-lint:
runs-on: macos-14

steps:
- name: Install tools
run: brew install swiftformat

- name: Checkout maplibre-swiftui-dsl-playground
uses: actions/checkout@v3

- name: Check format
run: swiftformat . --lint

test:
runs-on: macos-14
strategy:
Expand All @@ -20,15 +33,15 @@ jobs:
]

steps:
- name: Install xcbeautify
- name: Install tools
run: brew install xcbeautify

- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: '15.0'

- name: Checkout maplibre-swiftui-dsl-playground
uses: actions/checkout@v3

- name: Test ${{ matrix.scheme }} on ${{ matrix.destination }}
run: xcodebuild -scheme ${{ matrix.scheme }} test -skipMacroValidation -destination '${{ matrix.destination }}' | xcbeautify && exit ${PIPESTATUS[0]}
15 changes: 15 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# file options

--exclude .build

# format options

--header ""
--indent 4
--importgrouping testable-bottom
--maxwidth 120
--swiftversion 5.9

# rules

--enable isEmpty
25 changes: 14 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport
import PackageDescription

let package = Package(
name: "MapLibreSwiftUI",
Expand All @@ -13,10 +13,12 @@ let package = Package(
products: [
.library(
name: "MapLibreSwiftUI",
targets: ["MapLibreSwiftUI"]),
targets: ["MapLibreSwiftUI"]
),
.library(
name: "MapLibreSwiftDSL",
targets: ["MapLibreSwiftDSL"]),
targets: ["MapLibreSwiftDSL"]
),
],
dependencies: [
.package(url: "https://github.com/maplibre/maplibre-gl-native-distribution.git", from: "6.1.0"),
Expand All @@ -32,37 +34,38 @@ let package = Package(
.target(name: "InternalUtils"),
.target(name: "MapLibreSwiftDSL"),
.product(name: "MapLibre", package: "maplibre-gl-native-distribution"),
.product(name: "Mockable", package: "Mockable")
.product(name: "Mockable", package: "Mockable"),
],
swiftSettings: [
.define("MOCKING", .when(configuration: .debug))
]),
.define("MOCKING", .when(configuration: .debug)),
]
),
.target(
name: "MapLibreSwiftDSL",
dependencies: [
.target(name: "InternalUtils"),
.product(name: "MapLibre", package: "maplibre-gl-native-distribution"),
.product(name: "MapLibreSwiftMacros", package: "maplibre-swift-macros")
.product(name: "MapLibreSwiftMacros", package: "maplibre-swift-macros"),
]
),
.target(
name: "InternalUtils"
),

// MARK: Tests

.testTarget(
name: "MapLibreSwiftUITests",
dependencies: [
"MapLibreSwiftUI",
.product(name: "MockableTest", package: "Mockable"),
.product(name: "SnapshotTesting", package: "swift-snapshot-testing")
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
]
),
.testTarget(
name: "MapLibreSwiftDSLTests",
dependencies: [
"MapLibreSwiftDSL"
"MapLibreSwiftDSL",
]
),
]
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ Then, for each target add either the DSL (for just the DSL) or both (for the Swi
Check out the (super basic) [previews at the bottom of MapView.swift](Sources/MapLibreSwiftUI/MapView.swift)
or more detailed [Examples](Sources/MapLibreSwiftUI/Examples) to see how it works in practice.

## Developer Quick Start

This project uses [`swiftformat`](https://github.com/nicklockwood/SwiftFormat) to automatically handle basic swift formatting
as well as to lint and ensure conformance in PRs. Check out the swiftformat [Install Guide](https://github.com/nicklockwood/SwiftFormat?tab=readme-ov-file#how-do-i-install-it)
to add swiftformat to your machine.

Once installed, you can autoformat code using the command:

```sh
swiftformat .
```

Swiftformat can occasionally poorly resolve a formatting issue (e.g. when you've already line-broken a large comment). Issues like this are typically easy to manually correct.

## Structure

This package is structured into a few targets. `InternalUtils` is pretty much what it says. `MapLibreSwiftDSL` and
Expand Down Expand Up @@ -85,4 +99,4 @@ things like fleshing out the expresison API helpers (which will be highly repeti
check out the issue tracker, as these sholud be noted there.

TODO: Look at PointFree's Snapshot testing that generates images of SwiftUI views. Also look at inline snapshotting.
https://github.com/pointfreeco/swift-snapshot-testing
https://github.com/pointfreeco/swift-snapshot-testing
4 changes: 2 additions & 2 deletions Sources/InternalUtils/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public func addSourceIfNecessary(_ source: MLNSource, to mlnStyle: MLNStyle) ->
}
}

extension UIImage{
public extension UIImage {
/// Computes a SHA256 hash of the image data.
///
/// This is used internally to generate identifiers for images that can be used in the MapLibre GL
/// style which uniquely identify `UIImage`s to the renderer.
public func sha256() -> String{
func sha256() -> String {
if let imageData = cgImage?.dataProvider?.data as? Data {
return imageData.digest.hexString
}
Expand Down
29 changes: 14 additions & 15 deletions Sources/MapLibreSwiftDSL/Enums.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MapLibre
import InternalUtils
import MapLibre

// This file exists for convenience until / unless
// this is merged into the MapLibre Native Swift module OR Swift gains the
Expand All @@ -11,13 +11,12 @@ public enum LineCap {
case square
}


extension LineCap: MLNRawRepresentable {
public var mlnRawValue: MLNLineCap {
switch self {
case .butt: return .butt
case .round: return .round
case .square: return .square
case .butt: .butt
case .round: .round
case .square: .square
}
}
}
Expand All @@ -31,9 +30,9 @@ public enum LineJoin {
extension LineJoin: MLNRawRepresentable {
public var mlnRawValue: MLNLineJoin {
switch self {
case .bevel: return .bevel
case .miter: return .miter
case .round: return .round
case .bevel: .bevel
case .miter: .miter
case .round: .round
}
}
}
Expand All @@ -52,19 +51,19 @@ extension MLNVariableExpression {
var nsExpression: NSExpression {
switch self {
case .featureAccumulated:
return .featureAccumulatedVariable
.featureAccumulatedVariable
case .featureAttributes:
return .featureAttributesVariable
.featureAttributesVariable
case .featureIdentifier:
return .featureIdentifierVariable
.featureIdentifierVariable
case .geometryType:
return .geometryTypeVariable
.geometryTypeVariable
case .heatmapDensity:
return .heatmapDensityVariable
.heatmapDensityVariable
case .lineProgress:
return .lineProgressVariable
.lineProgressVariable
case .zoomLevel:
return .zoomLevelVariable
.zoomLevelVariable
}
}
}
16 changes: 10 additions & 6 deletions Sources/MapLibreSwiftDSL/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import Foundation
import MapLibre


// TODO: Parameters and stops need nicer interfaces
// TODO: Expression should be able to accept other expressions like variable getters. Probably should be a protocol?
public func interpolatingExpression(expression: MLNVariableExpression, curveType: MLNExpressionInterpolationMode, parameters: NSExpression?, stops: NSExpression) -> NSExpression {
return NSExpression(forMLNInterpolating: expression.nsExpression,
curveType: curveType,
parameters: parameters,
stops: stops)
public func interpolatingExpression(
expression: MLNVariableExpression,
curveType: MLNExpressionInterpolationMode,
parameters: NSExpression?,
stops: NSExpression
) -> NSExpression {
NSExpression(forMLNInterpolating: expression.nsExpression,
curveType: curveType,
parameters: parameters,
stops: stops)
}
10 changes: 5 additions & 5 deletions Sources/MapLibreSwiftDSL/MapLibre Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Various quality-of-life extensions to MapLibre APIs.

// Various quality-of-life extensions to MapLibre APIs.
import MapLibre

// TODO: Upstream this?
extension MLNPolyline {
public extension MLNPolyline {
/// Constructs a polyline (aka LineString) from a list of coordinates.
public convenience init(coordinates: [CLLocationCoordinate2D]) {
convenience init(coordinates: [CLLocationCoordinate2D]) {
self.init(coordinates: coordinates, count: UInt(coordinates.count))
}
}

extension MLNPointFeature {
public convenience init(coordinate: CLLocationCoordinate2D, configure: ((MLNPointFeature) -> Void)? = nil) {
public extension MLNPointFeature {
convenience init(coordinate: CLLocationCoordinate2D, configure: ((MLNPointFeature) -> Void)? = nil) {
self.init()
self.coordinate = coordinate

Expand Down
42 changes: 21 additions & 21 deletions Sources/MapLibreSwiftDSL/MapViewContentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ import Foundation
@resultBuilder
public enum MapViewContentBuilder: DefaultResultBuilder {
public static func buildExpression(_ expression: StyleLayerDefinition) -> [StyleLayerDefinition] {
return [expression]
[expression]
}

public static func buildExpression(_ expression: [StyleLayerDefinition]) -> [StyleLayerDefinition] {
return expression
expression
}
public static func buildExpression(_ expression: Void) -> [StyleLayerDefinition] {
return []

public static func buildExpression(_: Void) -> [StyleLayerDefinition] {
[]
}

public static func buildBlock(_ components: [StyleLayerDefinition]...) -> [StyleLayerDefinition] {
return components.flatMap { $0 }
components.flatMap { $0 }
}

public static func buildArray(_ components: [StyleLayerDefinition]) -> [StyleLayerDefinition] {
return components
components
}

public static func buildArray(_ components: [[StyleLayerDefinition]]) -> [StyleLayerDefinition] {
return components.flatMap { $0 }
components.flatMap { $0 }
}

public static func buildEither(first components: [StyleLayerDefinition]) -> [StyleLayerDefinition] {
return components
components
}

public static func buildEither(second components: [StyleLayerDefinition]) -> [StyleLayerDefinition] {
return components
components
}

public static func buildOptional(_ components: [StyleLayerDefinition]?) -> [StyleLayerDefinition] {
return components ?? []
components ?? []
}

// MARK: Custom Handler for StyleLayerCollection type.

public static func buildExpression(_ styleCollection: StyleLayerCollection) -> [StyleLayerDefinition] {
return styleCollection.layers
styleCollection.layers
}
}
Loading

0 comments on commit 1eb1618

Please sign in to comment.