Skip to content

Commit

Permalink
Merge branch 'Async'
Browse files Browse the repository at this point in the history
  • Loading branch information
Darktt committed Jun 19, 2024
2 parents 65b3dff + 6c9141e commit 75907f3
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
15 changes: 15 additions & 0 deletions Examples/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "b40d38ec82a51719a4021b58c01e2ff1caa54d6fc147fa6f51c4209545b5408f",
"pins" : [
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"branch" : "main",
"revision" : "7c499d4add2030af79636e4f3b61a311abd4cb92"
}
}
],
"version" : 3
}
33 changes: 33 additions & 0 deletions Examples/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Examples",
platforms: [
.macOS(.v12),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
.macCatalyst(.v13)
],
products: [
.executable(
name: "Examples",
targets: ["Examples"]
),
],
dependencies: [
.package(name: "QueueMacro", path: "../"),
],
targets: [
.executableTarget(
name: "Examples",
dependencies: [
.product(name: "QueueMacro", package: "QueueMacro"),
],
path: "Sources"
)
]
)
19 changes: 19 additions & 0 deletions Examples/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// File.swift
//
//
// Created by Eden on 2024/4/12.
//

import Foundation
import QueueMacro

#Async({

print("Hello, World!!!!!")
})

#Async(queue: .main, {

print("Hello, World!!!!!")
})
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"originHash" : "9a228cda5010c957182ce3f99bdfa677f46c965cb2013b18fc1fa19c749ebb13",
"originHash" : "8d6f4d1809205be3e50b265765e2dc0502b7b6ee3ec04074563be74fd300acf4",
"pins" : [
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"branch" : "main",
"revision" : "031b2e3ad22c74677ad1ff6a867fa2e9ffb2e510"
"revision" : "6c459f2728572a64c4b29ce3140daa90c3c6e7d1"
}
}
],
Expand Down
8 changes: 7 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import CompilerPluginSupport

let package = Package(
name: "QueueMacro",
platforms: [.macOS(.v12), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
platforms: [
.macOS(.v12),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
.macCatalyst(.v13)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
Expand Down
14 changes: 14 additions & 0 deletions Sources/Macros/Async/AsyncError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// AsyncError.swift
//
//
// Created by Eden on 2024/4/11.
//

import Foundation

public
enum AsyncError: Error
{
case missingArgument(String)
}
38 changes: 38 additions & 0 deletions Sources/Macros/Async/AsyncMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// AsyncMacro.swift
//
//
// Created by Eden on 2024/4/11.
//

import Foundation
import SwiftSyntax
import SwiftSyntaxMacros

public
struct AsyncMacro: ExpressionMacro
{
public static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax
{
guard let queue = node.arguments.first?.expression else {

throw AsyncError.missingArgument("queue")
}

if node.arguments.count == 2, let execution = node.arguments.last {

return """
DispatchQueue\(queue).async(execute: \(execution))
"""
}

if let execution = node.trailingClosure {

return """
DispatchQueue\(queue).async \(execution)
"""
}

throw AsyncError.missingArgument("execution")
}
}
4 changes: 2 additions & 2 deletions Sources/Macros/MainQueue/MainQueueMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
//

import Foundation
import SwiftSyntaxMacros
import SwiftSyntax
import SwiftSyntaxMacros

public
struct MainQueueMacro: ExpressionMacro
Expand All @@ -21,7 +21,7 @@ struct MainQueueMacro: ExpressionMacro

if let execution = node.trailingClosure {

return "DispatchQueue.main.async(execute: \(execution))"
return "DispatchQueue.main.async \(execution)"
}

throw MainQueueError.missingArgument("execution")
Expand Down
3 changes: 2 additions & 1 deletion Sources/Macros/Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SwiftSyntaxMacros
struct Plugins: CompilerPlugin
{
let providingMacros: [Macro.Type] = [
MainQueueMacro.self
AsyncMacro.self,
MainQueueMacro.self,
]
}
9 changes: 8 additions & 1 deletion Sources/QueueMacro/QueueMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@

import Foundation

// Swift AST Explorer: https://swift-ast-explorer.com

@freestanding(expression)
public
macro Async(queue: DispatchQueue = .main, _ execution: @escaping @Sendable @convention(block) () -> Void) = #externalMacro(module: "Macros", type: "AsyncMacro")

@freestanding(expression)
public macro MainQueue(_ execution: @escaping () -> Void) = #externalMacro(module: "Macros", type: "MainQueueMacro")
public
macro MainQueue(_ execution: @escaping () -> Void) = #externalMacro(module: "Macros", type: "MainQueueMacro")
24 changes: 24 additions & 0 deletions Tests/QueueMacroTests/QueueMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,28 @@ final class QueueMacroTests: XCTestCase
}
}
}

func testAsync()
{
let expectation = self.expectation(description: "Async")

DispatchQueue.global(qos: .default).async {

#Async(queue: .main, {

XCTAssertTrue(Thread.isMainThread)
expectation.fulfill()
})
}

self.waitForExpectations(timeout: 1.0)
}

func testA()
{
#Async(queue: .main) {

XCTAssertTrue(Thread.isMainThread)
}
}
}

0 comments on commit 75907f3

Please sign in to comment.