Skip to content

Commit

Permalink
FEAT: Support local macro pod (trinhngocthuyen#138)
Browse files Browse the repository at this point in the history
* Add a local macro pkg

* FEAT: Support local macro pod
  • Loading branch information
trinhngocthuyen authored Dec 2, 2024
1 parent 4868173 commit 7d6293c
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 5 deletions.
6 changes: 6 additions & 0 deletions examples/EX/SPMPlayground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ struct Foo {
}

import Orcam
import Wizard
import MacroCodableKit

@AllOfCodable // MacroCodableKit
@Init // Orcam
struct FooMacro {
let x: Int

func check() {
let color = #uiColor(0xff0000)
print("color: \(color)")
}
}
36 changes: 36 additions & 0 deletions examples/LocalPackages/ex-macros/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "Wizard",
platforms: [.macOS(.v11), .iOS(.v14), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],
products: [
.library(
name: "Wizard",
targets: ["Wizard"]
),
.executable(
name: "WizardPlayground",
targets: ["WizardPlayground"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
.package(url: "https://github.com/stackotter/swift-macro-toolkit.git", from: "0.3.1"),
],
targets: [
.macro(
name: "WizardImpl",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
.product(name: "MacroToolkit", package: "swift-macro-toolkit"),
]
),
.target(name: "Wizard", dependencies: ["WizardImpl"]),
.executableTarget(name: "WizardPlayground", dependencies: ["Wizard"]),
]
)
4 changes: 4 additions & 0 deletions examples/LocalPackages/ex-macros/Sources/Wizard/Wizard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import UIKit

@freestanding(expression)
public macro uiColor(_ intLiteral: IntegerLiteralType ) -> UIColor = #externalMacro(module: "WizardImpl", type: "HexToUIColorMacro")
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import MacroToolkit

enum HexColorMacroError: Error {
case notFoundHex
}

public struct HexToUIColorMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
guard let arg = node.argumentList.first, let hex = Expr(arg.expression).asIntegerLiteral?.value else {
throw HexColorMacroError.notFoundHex
}
return """
UIColor(
red: CGFloat((\(raw: hex) >> 16) & 0xff) / 255,
green: CGFloat((\(raw: hex) >> 8) & 0xff) / 255,
blue: CGFloat((\(raw: hex) >> 0) & 0xff) / 255,
alpha: CGFloat(1)
)
"""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

@main
struct HexColorMacroPlugin: CompilerPlugin {
let providingMacros: [Macro.Type] = [
HexToUIColorMacro.self,
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import UIKit
import Wizard

let color = #uiColor(0xff0000)
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

install:
sh scripts/prebuilt_macros.sh unzip
bundle exec pod install --verbose
bundle exec pod install --verbose --ansi

spm.fetch:
bundle exec pod spm macro fetch --all --verbose
Expand Down
1 change: 1 addition & 0 deletions examples/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ target "EX" do # rubocop:disable Metrics/BlockLength
:git => "https://github.com/mikhailmaslo/macro-codable-kit",
:tag => "0.3.0",
}
pod "Wizard", :macro => {:path => "LocalPackages/ex-macros"}

spm_pkg "SnapKit",
:url => "https://github.com/SnapKit/SnapKit.git",
Expand Down
5 changes: 5 additions & 0 deletions examples/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ PODS:
- Orcam (0.0.1)
- Services (0.0.1):
- Logger
- Wizard (0.0.1)

DEPENDENCIES:
- CommonUI (from `LocalPods/CommonUI`)
- Logger (from `LocalPods/Logger`)
- MacroCodableKit (from `.spm.pods/macros/MacroCodableKit`)
- Orcam (from `.spm.pods/macros/Orcam`)
- Services (from `LocalPods/Services`)
- Wizard (from `.spm.pods/macros/Wizard`)

EXTERNAL SOURCES:
CommonUI:
Expand All @@ -26,13 +28,16 @@ EXTERNAL SOURCES:
:path: ".spm.pods/macros/Orcam"
Services:
:path: LocalPods/Services
Wizard:
:path: ".spm.pods/macros/Wizard"

SPEC CHECKSUMS:
CommonUI: 90679c43fb06ad1f1de5ff4278ceadb3503a8340
Logger: 43a9cc3dc5f479042eb238d8680aaa0d6afd3ad2
MacroCodableKit: 29a3a48508c673918521ef5db6f1485db2ca75d0
Orcam: 288b7e3fc07e33706ceab2f226e4177e99847143
Services: 5d4b4ce9877cac8bb44481806f754d0dd267a55b
Wizard: 65d0442d19c0c27999a0eea600e3d451caa260e5

PODFILE CHECKSUM: dummy-checksum-to-prevent-merge-conflicts

Expand Down
Binary file modified examples/cache/prebuilt_macros.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions lib/cocoapods-spm/config/spm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ def spm_config
def macro_pods
pod_config.podfile.macro_pods
end

def local_macro_pod?(name)
!local_macro_pod_dir(name).nil?
end

def local_macro_pod_dir(name)
opts = macro_pods.fetch(name, {})
return Path(opts[:podspec]).dirname if opts.key?(:podspec)

Pathname(opts[:path]) if opts.key?(:path)
end
end

attr_accessor :dsl_config, :cli_config
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-spm/def/podfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def platforms
private

def prepare_macro_pod_dir(name, requirement)
link = requirement[:git]
link = requirement[:git] || "N/A"
podspec_content = <<~HEREDOC
Pod::Spec.new do |s|
s.name = "#{name}"
Expand Down
24 changes: 22 additions & 2 deletions lib/cocoapods-spm/macro/fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "cocoapods-spm/helpers/io"
require "cocoapods-spm/macro/metadata"
require_relative "config"

Expand All @@ -16,8 +17,7 @@ def initialize(options = {})
end

def run
UI.puts "Fetching macro #{name}...".magenta
download_macro_source
prepare_macro_source
macro_dir = spm_config.macro_root_dir / name
macro_downloaded_dir = spm_config.macro_downloaded_root_dir / name
FileUtils.copy_entry(
Expand All @@ -29,6 +29,10 @@ def run

private

def local?
local_macro_pod?(name)
end

def generate_metadata
raise "Package.swift not exist in #{macro_downloaded_dir}" \
unless (macro_downloaded_dir / "Package.swift").exist?
Expand All @@ -37,7 +41,23 @@ def generate_metadata
metadata_path.write(raw)
end

def prepare_macro_source
if local?
symlink_local_macro_source
else
download_macro_source
end
end

def symlink_local_macro_source
UI.message "Creating symlink to local macro source: #{name}..."
# For local macro pod, just need to copy local pod dir to downloaded sandbox,
# or create a symlink .spm.pods/macros/.downloaded/FOO -> LocalPods/FOO
IOUtils.symlink(local_macro_pod_dir(name), macro_downloaded_dir)
end

def download_macro_source
UI.puts "Downloading source for macro: #{name}...".magenta
@specs_by_platform ||= @podfile.root_target_definitions.to_h do |definition|
spec = Pod::Spec.from_file(spm_config.macro_root_dir / name / "#{name}.podspec")
[definition.platform, [spec]]
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-spm/macro/pod_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def prebuilder
end

def install_macro_pod!
fetcher.run unless fetcher.metadata_path.exist?
fetcher.run if local_macro_pod?(name) || !fetcher.metadata_path.exist?
prebuilder.run
end
end
Expand Down

0 comments on commit 7d6293c

Please sign in to comment.