Skip to content

Commit

Permalink
[Setup] Non Apple platforms (#13)
Browse files Browse the repository at this point in the history
This PR contains the work done to address the issue #12, with regards to provide basic support of non Apple platforms.

To provide further details about the work done:
- [x] flattened the folder structure, especially now that the idea to filter folders based on platform is being discarded;
- [x] implemented precompiler processors to filter out platform-specific source code;
- [x] updated the `Package` file to provide basic support for non-Apple platforms;
- [x] added and also improved some targets to the `Makefile` file to smooth the current development workflows;
- [x] updated the `.gitignore` file with references to the `.vscode` folder and the `.env` file;
- [x] updated the Swift tools version to v5.7.

Co-authored-by: Javier Cicchelli <[email protected]>
Reviewed-on: https://repo.rock-n-code.com/rock-n-code/swift-libs/pulls/13
  • Loading branch information
Javier Cicchelli and mr-rock committed Apr 28, 2023
1 parent 4a1fab2 commit c4d09cd
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 75 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.DS_Store
/.build
/.vscode
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
.env
87 changes: 87 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# --- IMPORTS ---

# Imports environment variables.
environment_vars ?= .env

include $(environment_vars)
export $(shell sed 's/=.*//' $(environment_vars))

# --- ARGUMENTS ---

override docker?=${CLI_USE_DOCKER}
override tag?=${DOCKER_IMAGE_TAG}
override platform?=${DOCKER_IMAGE_PLATFORM}
override config?=${SWIFT_BUILD_CONFIGURATION}
override clean?=${DOCKER_IMAGE_CLEAN}

# --- DEPENDENCIES ---

outdated: ## List the package dependencies that can be updated.
@swift package update --dry-run

update: ## Update the package dependencies defined in the project.
@swift package update

# --- DEVELOPMENT ---

build: ## Build this package with Swift either locally or in a Docker image.
ifeq ($(docker),yes)
@-docker run \
--rm \
--volume ${PWD}:${DOCKER_VOLUME_TARGET} \
--workdir ${DOCKER_VOLUME_TARGET} \
--platform ${platform} \
${DOCKER_IMAGE_NAME}:${tag} \
swift build --configuration ${config}
ifeq ($(clean),yes)
@docker rmi ${DOCKER_IMAGE_NAME}:${tag}
endif
else
@swift build --configuration ${config}
endif

# --- TESTING ---

test: ## Test this package with Swift either locally or in a Docker image.
ifeq ($(docker),yes)
@-docker run \
--rm \
--volume ${PWD}:${DOCKER_VOLUME_TARGET} \
--workdir ${DOCKER_VOLUME_TARGET} \
--platform ${platform} \
${DOCKER_IMAGE_NAME}:${tag} \
swift test --configuration ${config}
ifeq ($(clean),yes)
@docker rmi ${DOCKER_IMAGE_NAME}:${tag}
endif
else
@swift test --configuration ${config}
endif

# --- HOUSE-KEEPING ---

clean: ## Clean the build artifacts of the package.
@swift package clean

reset: ## Reset the build folder of the package.
@swift package reset

flush-images: ## Flush all outstanding Swift docker images.
@docker images \
--all | grep ${DOCKER_IMAGE_NAME} | awk '{print $$3}' | xargs docker rmi --force

# --- MISCELLANEOUS ---

xcode: ## Open this package in Xcode.
@open -a Xcode Package.swift

# --- HELP ---

# Outputs the documentation for each of the defined tasks when `help` is called.
# Reference: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help

help: ## Prints the written documentation for all the defined tasks.
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help
164 changes: 90 additions & 74 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.5
// swift-tools-version: 5.7
//
// This source file is part of the SwiftLibs open source project
//
Expand All @@ -11,14 +11,86 @@

import PackageDescription

private var excludePlatforms: [String] = [.PlatformFolder.iOS]
// MARK: - Variables

#if os(iOS)
excludePlatforms = []
private var targetsLibrary: [String] = [
.Target.communications,
.Target.coordination,
.Target.core,
.Target.dependencies,
]

private var targetsPackage: [Target] = [
.target(
name: .Target.communications,
dependencies: []
),
.target(
name: .Target.coordination,
dependencies: []
),
.target(
name: .Target.core,
dependencies: []
),
.target(
name: .Target.dependencies,
dependencies: []
),
.testTarget(
name: "CommunicationsTests",
dependencies: [
.init(stringLiteral: .Target.communications)
],
path: "Tests/Communications"
),
.testTarget(
name: "CoordinationTests",
dependencies: [
.init(stringLiteral: .Target.coordination)
],
path: "Tests/Coordination"
),
.testTarget(
name: "CoreTests",
dependencies: [
.init(stringLiteral: .Target.core)
],
path: "Tests/Core"
),
.testTarget(
name: "DependenciesTests",
dependencies: [
.init(stringLiteral: .Target.dependencies)
],
path: "Tests/Dependencies"
),
]

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
targetsLibrary.append(.Target.persistence)
targetsPackage.append(contentsOf: [
.target(
name: .Target.persistence,
dependencies: []
),
.testTarget(
name: "PersistenceTests",
dependencies: [
.init(stringLiteral: .Target.persistence)
],
path: "Tests/Persistence",
resources: [
.process("Resources")
]
),
])
#endif

// MARK: - Package

let package = Package(
name: "SwiftLibs",
name: .Package.name,
platforms: [
.iOS(.v15),
.macOS(.v12),
Expand All @@ -27,82 +99,26 @@ let package = Package(
],
products: [
.library(
name: "SwiftLibs",
targets: [
"Communications",
"Coordination",
"Core",
"Dependencies",
"Persistence"
]
name: .Package.name,
targets: targetsLibrary
),
],
dependencies: [],
targets: [
.target(
name: "Communications",
dependencies: []
),
.target(
name: "Coordination",
dependencies: [],
exclude: excludePlatforms
),
.target(
name: "Core",
dependencies: []
),
.target(
name: "Dependencies",
dependencies: []
),
.target(
name: "Persistence",
dependencies: []
),
.testTarget(
name: "CommunicationsTests",
dependencies: [
"Communications"
],
path: "Tests/Communications"
),
.testTarget(
name: "CoordinationTests",
dependencies: [
"Coordination"
],
path: "Tests/Coordination",
exclude: excludePlatforms
),
.testTarget(
name: "CoreTests",
dependencies: [
"Core"
],
path: "Tests/Core"
),
.testTarget(
name: "DependenciesTests",
dependencies: [
"Dependencies"
],
path: "Tests/Dependencies"
),
.testTarget(
name: "PersistenceTests",
dependencies: [
"Persistence"
],
path: "Tests/Persistence"
),
]
targets: targetsPackage
)

// MARK: - String+Constants

private extension String {
enum PlatformFolder {
static let iOS = "Platform/iOS"
enum Package {
static let name = "SwiftLibs"
}

enum Target {
static let communications = "Communications"
static let coordination = "Coordination"
static let core = "Core"
static let dependencies = "Dependencies"
static let persistence = "Persistence"
}
}
2 changes: 2 additions & 0 deletions Sources/Communications/Classes/MockURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
import Foundation

/// This class overrides the `URLProtocol` protocol used by the `URLSession` to handle the loading of protocol-specific URL data so it is possible to mock URL response for testing purposes.
Expand Down Expand Up @@ -116,3 +117,4 @@ public struct MockURLResponse {
}

}
#endif
2 changes: 2 additions & 0 deletions Sources/Communications/Use Cases/MakeURLRequestUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
import Foundation

/// This use case generate a url request out of a given endpoint.
Expand Down Expand Up @@ -55,3 +56,4 @@ public struct MakeURLRequestUseCase {
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if os(iOS)
import UIKit

/// This is a base class for the `NavigationRouter` concrete router implementations.
Expand Down Expand Up @@ -70,3 +71,4 @@ extension BaseNavigationRouter: UINavigationControllerDelegate {
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if os(iOS)
import UIKit

/// This class is responsible for showing view controllers modally, as it is a concrete implementation of the `Router` protocol.
Expand Down Expand Up @@ -88,3 +89,4 @@ private extension ModalNavigationRouter {
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if os(iOS)
import UIKit

/// This class is responsible for pushing view controllers into a navigation controller, as it is a concrete implementation of the `Router` protocol.
Expand Down Expand Up @@ -66,3 +67,4 @@ extension PushNavigationRouter: Router {
}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if os(iOS)
import UIKit

/// This class is responsible for populating the window of an application.
Expand Down Expand Up @@ -45,3 +46,4 @@ public class WindowRouter: Router {
}

}
#endif
2 changes: 2 additions & 0 deletions Sources/Persistence/Classes/Fetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if canImport(Combine) && canImport(CoreData)
import Combine
import CoreData

Expand Down Expand Up @@ -167,3 +168,4 @@ public enum Change: Hashable {
case section(SectionUpdate)
case object(ObjectUpdate)
}
#endif
2 changes: 2 additions & 0 deletions Sources/Persistence/Protocols/Service.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#if canImport(CoreData)
import CoreData

public protocol Service {
Expand Down Expand Up @@ -38,3 +39,4 @@ public protocol Service {
func save(childContext context: NSManagedObjectContext) throws

}
#endif
Loading

0 comments on commit c4d09cd

Please sign in to comment.