Skip to content

Commit

Permalink
[#97] Rename CRApplication{Delegate} to `Criollo.Application{Delega…
Browse files Browse the repository at this point in the history
…te}` and refine `CRApplicationMain`
  • Loading branch information
thecatalinstan committed Sep 17, 2022
1 parent 327d5b6 commit 1d25169
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 57 deletions.
28 changes: 21 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand All @@ -20,9 +33,6 @@ let package = Package(
dependencies: [
.product(name: "CocoaAsyncSocket", package: "CocoaAsyncSocket")
],
exclude: [
"../../Criollo.podspec"
],
publicHeadersPath: "Headers",
cSettings: [
.headerSearchPath("."),
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion Sources/Criollo/CRApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void CRHandleSignal(int sig) {
signal(sig, CRHandleSignal);
}

int CRApplicationMain(int argc, const char * argv[], id<CRApplicationDelegate> delegate) {
int CRApplicationMain(int argc, char *argv[], id<CRApplicationDelegate> delegate) {
signal(SIGTERM, CRHandleSignal);
signal(SIGINT, CRHandleSignal);
signal(SIGQUIT, CRHandleSignal);
Expand Down
72 changes: 38 additions & 34 deletions Sources/Criollo/Headers/Criollo/CRApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,56 @@

#import <Foundation/Foundation.h>

/**
* 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
Expand All @@ -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<CRApplicationDelegate> delegate);
FOUNDATION_EXTERN __kindof CRApplication * _Null_unspecified CRApp;
FOUNDATION_EXTERN int CRApplicationMain(int argc, char * _Nullable argv[_Nonnull], id<CRApplicationDelegate> delegate) NS_REFINED_FOR_SWIFT;

NS_SWIFT_NAME(Application)
@interface CRApplication : NSObject

@property (nonatomic, readonly, weak) id<CRApplicationDelegate> delegate;
@property (class, nonatomic, readonly, strong) CRApplication *sharedApplication;

@property (class, nonatomic, readonly) CRApplication *sharedApplication;

- (instancetype)initWithDelegate:(id<CRApplicationDelegate> _Nullable)delegate NS_DESIGNATED_INITIALIZER;

Expand Down
2 changes: 1 addition & 1 deletion Sources/CriolloDemoObjectiveC/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

14 changes: 10 additions & 4 deletions Sources/CriolloDemoSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@
// 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
res.send("Hello world!")
}
server.startListening()
}

}
10 changes: 0 additions & 10 deletions Sources/CriolloDemoSwift/main.swift

This file was deleted.

17 changes: 17 additions & 0 deletions Sources/CriolloSwift/Criollo.swift
Original file line number Diff line number Diff line change
@@ -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."])
}
}
}

0 comments on commit 1d25169

Please sign in to comment.