From 1d251692d959cbb4fe4a61ce65ae815066d49b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ca=CC=86ta=CC=86lin=20Stan?= Date: Wed, 14 Sep 2022 08:46:33 +0300 Subject: [PATCH] [#97] Rename `CRApplication{Delegate}` to `Criollo.Application{Delegate}` and refine `CRApplicationMain` --- Package.swift | 28 ++++++-- Sources/Criollo/CRApplication.m | 2 +- .../Criollo/Headers/Criollo/CRApplication.h | 72 ++++++++++--------- Sources/CriolloDemoObjectiveC/main.m | 2 +- Sources/CriolloDemoSwift/AppDelegate.swift | 14 ++-- Sources/CriolloDemoSwift/main.swift | 10 --- Sources/CriolloSwift/Criollo.swift | 17 +++++ 7 files changed, 88 insertions(+), 57 deletions(-) delete mode 100644 Sources/CriolloDemoSwift/main.swift create mode 100644 Sources/CriolloSwift/Criollo.swift diff --git a/Package.swift b/Package.swift index 62227d8..34d7984 100644 --- a/Package.swift +++ b/Package.swift @@ -7,9 +7,22 @@ let package = Package( name: "Criollo", platforms: [.iOS(.v9), .macOS(.v10_10), .tvOS(.v9)], products: [ - .library(name: "Criollo", targets: ["Criollo"]), - .executable(name: "CriolloDemoSwift", targets: ["CriolloDemoSwift"]), - .executable(name: "CriolloDemoObjectiveC", targets: ["CriolloDemoObjectiveC"]), + .library( + name: "Criollo", + targets: ["Criollo"] + ), + .library( + name: "CriolloSwift", + targets: ["CriolloSwift"] + ), + .executable( + name: "CriolloDemoSwift", + targets: ["CriolloDemoSwift"] + ), + .executable( + name: "CriolloDemoObjectiveC", + targets: ["CriolloDemoObjectiveC"] + ), ], dependencies: [ .package(name:"CocoaAsyncSocket", url: "https://github.com/robbiehanson/CocoaAsyncSocket", .upToNextMinor(from: "7.6.5")), @@ -20,9 +33,6 @@ let package = Package( dependencies: [ .product(name: "CocoaAsyncSocket", package: "CocoaAsyncSocket") ], - exclude: [ - "../../Criollo.podspec" - ], publicHeadersPath: "Headers", cSettings: [ .headerSearchPath("."), @@ -43,9 +53,13 @@ let package = Package( .headerSearchPath("../../Sources/Criollo/Routing"), ] ), + .target( + name: "CriolloSwift", + dependencies: ["Criollo"] + ), .executableTarget( name: "CriolloDemoSwift", - dependencies: ["Criollo"] + dependencies: ["CriolloSwift"] ), .executableTarget( name: "CriolloDemoObjectiveC", diff --git a/Sources/Criollo/CRApplication.m b/Sources/Criollo/CRApplication.m index 3dfd208..9862299 100644 --- a/Sources/Criollo/CRApplication.m +++ b/Sources/Criollo/CRApplication.m @@ -39,7 +39,7 @@ void CRHandleSignal(int sig) { signal(sig, CRHandleSignal); } -int CRApplicationMain(int argc, const char * argv[], id delegate) { +int CRApplicationMain(int argc, char *argv[], id delegate) { signal(SIGTERM, CRHandleSignal); signal(SIGINT, CRHandleSignal); signal(SIGQUIT, CRHandleSignal); diff --git a/Sources/Criollo/Headers/Criollo/CRApplication.h b/Sources/Criollo/Headers/Criollo/CRApplication.h index bd88f8f..a90a74f 100644 --- a/Sources/Criollo/Headers/Criollo/CRApplication.h +++ b/Sources/Criollo/Headers/Criollo/CRApplication.h @@ -7,54 +7,56 @@ #import -/** - * These constants define wether a Criollo app should terminate or not and are - * are used by the CRApplicationDelegate method `applicationShouldTerminate`. - * - * @see https://developer.apple.com/reference/appkit/nsapplicationterminatereply - */ -typedef NS_ENUM(NSUInteger, CRApplicationTerminateReply) { +/// These constants define wether a Criollo app should terminate or not and are +/// are used by the @c CRApplicationDelegate method @c -applicationShouldTerminate: +/// +/// @see https://developer.apple.com/reference/appkit/nsapplicationterminatereply + +typedef NS_ENUM(NSUInteger, CRApplicationTerminateReply) { CRTerminateCancel = 0, CRTerminateNow = 1, CRTerminateLater = 2 -}; +} NS_SWIFT_NAME(Application.TerminateReply); @class CRApplication; NS_ASSUME_NONNULL_BEGIN -/** - * The CRApplicationDelegate protocol defines the methods that may be implemented - * by delegates of CRApplication objects. It is mean to mimic the behavior of - * NSApplicationDelegate. - * - * @see https://developer.apple.com/reference/appkit/nsapplicationdelegate - */ +/// The @c CRApplicationDelegate protocol defines the methods that may be +/// implemented by delegates of @c CRApplication objects. It is meant to mimic +/// the behavior of @c NSApplicationDelegate. +/// +/// @see https://developer.apple.com/reference/appkit/nsapplicationdelegate +NS_SWIFT_NAME(ApplicationDelegate) @protocol CRApplicationDelegate @required -/** - * Sent by the default notification center after the application has been launched - * and initialized but before it has received its first event. - * - * @param notification A notification named CRApplicationDidFinishLaunchingNotification. - * Calling the `object` method of this notification returns the CRApplication - * object itself. - */ + +/// Sent by the default notification center after the application has been launched +/// and initialized but before it has received its first event. +/// +/// @param notification A notification named @c CRApplicationDidFinishLaunchingNotification. +/// +/// Calling the @c object method of this notification returns the @c CRApplication +/// object itself. - (void)applicationDidFinishLaunching:(NSNotification *)notification; @optional -/** - * Sent by the default notification center immediately before the application - * object is initialized. - * - * @param notification A notification named CRApplicationWillFinishLaunchingNotification. - * Calling the `object` method of this notification returns the CRApplication - * object itself. - */ +/// Sent by the default notification center immediately before the application +/// object is initialized. +/// +/// @param notification A notification named CRApplicationWillFinishLaunchingNotification. +/// +/// Calling the `object` method of this notification returns the CRApplication +/// object itself. - (void)applicationWillFinishLaunching:(NSNotification *)notification; +/// <#Description#> +/// @param sender <#sender description#> - (CRApplicationTerminateReply)applicationShouldTerminate:(CRApplication *)sender; + +/// <#Description#> +/// @param notification <#notification description#> - (void)applicationWillTerminate:(NSNotification *)notification; @end @@ -63,13 +65,15 @@ FOUNDATION_EXPORT NSNotificationName const CRApplicationWillFinishLaunchingNotif FOUNDATION_EXPORT NSNotificationName const CRApplicationDidFinishLaunchingNotification; FOUNDATION_EXPORT NSNotificationName const CRApplicationWillTerminateNotification; -FOUNDATION_EXPORT id CRApp; -FOUNDATION_EXPORT int CRApplicationMain(int argc, const char * _Nullable argv[_Nullable], id delegate); +FOUNDATION_EXTERN __kindof CRApplication * _Null_unspecified CRApp; +FOUNDATION_EXTERN int CRApplicationMain(int argc, char * _Nullable argv[_Nonnull], id delegate) NS_REFINED_FOR_SWIFT; +NS_SWIFT_NAME(Application) @interface CRApplication : NSObject @property (nonatomic, readonly, weak) id delegate; -@property (class, nonatomic, readonly, strong) CRApplication *sharedApplication; + +@property (class, nonatomic, readonly) CRApplication *sharedApplication; - (instancetype)initWithDelegate:(id _Nullable)delegate NS_DESIGNATED_INITIALIZER; diff --git a/Sources/CriolloDemoObjectiveC/main.m b/Sources/CriolloDemoObjectiveC/main.m index 93f84cc..8697638 100644 --- a/Sources/CriolloDemoObjectiveC/main.m +++ b/Sources/CriolloDemoObjectiveC/main.m @@ -9,7 +9,7 @@ #import "AppDelegate.h" -int main(int argc, const char * argv[]) { +int main(int argc, char * argv[]) { return CRApplicationMain(argc, argv, [AppDelegate new]); } diff --git a/Sources/CriolloDemoSwift/AppDelegate.swift b/Sources/CriolloDemoSwift/AppDelegate.swift index c19010d..042236d 100644 --- a/Sources/CriolloDemoSwift/AppDelegate.swift +++ b/Sources/CriolloDemoSwift/AppDelegate.swift @@ -5,13 +5,18 @@ // Created by Cătălin Stan on 15/09/2022. // -import Criollo +import CriolloSwift + +@main +class AppDelegate: ApplicationDelegate { -class AppDelegate: CRApplicationDelegate { - private lazy var server = CRHTTPServer() - // MARK: - CRApplicationDelegate + public static func main() throws { + try Criollo.applicationMain(AppDelegate()) + } + + // MARK: - ApplicationDelegate func applicationDidFinishLaunching(_ notification: Notification) { server.add { _, res, _ in @@ -19,4 +24,5 @@ class AppDelegate: CRApplicationDelegate { } server.startListening() } + } diff --git a/Sources/CriolloDemoSwift/main.swift b/Sources/CriolloDemoSwift/main.swift deleted file mode 100644 index 063bed8..0000000 --- a/Sources/CriolloDemoSwift/main.swift +++ /dev/null @@ -1,10 +0,0 @@ -// -// main.swift -// -// -// Created by Cătălin Stan on 15/09/2022. -// - -import Criollo - -exit(CRApplicationMain(CommandLine.argc, nil, AppDelegate())) diff --git a/Sources/CriolloSwift/Criollo.swift b/Sources/CriolloSwift/Criollo.swift new file mode 100644 index 0000000..835cf69 --- /dev/null +++ b/Sources/CriolloSwift/Criollo.swift @@ -0,0 +1,17 @@ +// +// Application.swift +// +// +// Created by Cătălin Stan on 16/09/2022. +// + +@_exported import Criollo + +public class Criollo { + public static func applicationMain(_ delegate: ApplicationDelegate) throws { + let res = __CRApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, delegate) + if res != EXIT_SUCCESS { + throw NSError(domain: NSPOSIXErrorDomain, code: Int(res), userInfo: [NSLocalizedDescriptionKey : strerror(res) ?? "The application returned an unknown error code."]) + } + } +}