How krzysztofzablocki/inject works
Project Type and Purpose
• This is an open‑source Swift package (library) designed to add “hot‑reload” capabilities to iOS/macOS apps (UIKit, AppKit, SwiftUI) by wrapping and simplifying the use of the external InjectionIII tool.
• It’s not a full‑stack application but rather a developer‑workflow helper that plugs into existing projects via SwiftPM or CocoaPods and runtime linker flags.
File Structure Analysis
• Top‑level artifacts:
• Package.swift / InjectHotReload.podspec → SPM & CocoaPods integration points
• .swiftpm / Xcode workspace files → Build integration scaffolding
• Sources/Inject → Core library module
• Under Sources/Inject:
• InjectConfiguration.swift → runtime settings (e.g., animation)
• Integrations/KitFrameworks.swift → UIKit/AppKit support
• Integrations/Hosts.swift → ViewControllerHost & ViewHost abstractions for state cleanup
• Integrations/SwiftUI.swift → SwiftUI modifiers (.enableInjection(), @ObserveInjection)
README Insights
• Describes how Inject is a “thin wrapper” around InjectionIII, adding no‑op behavior in production builds.
• Provides step‑by‑step integration instructions for SwiftPM, CocoaPods, Xcode build flags, and per‑developer setup.
• Demonstrates two major integration patterns: SwiftUI and imperative UI (UIKit/AppKit) via host wrappers and injection hooks.
• Emphasizes that the only external service/tool is the InjectionIII macOS app, which compiles & pushes code into the running process over a debug IPC channel.
Main Components
a. Developer’s iOS/macOS App
• Your app target with code that does import Inject, adds @ObserveInjection, calls .enableInjection() or wraps views/controllers with Inject.ViewHost/ViewControllerHost.
b. Inject Library (client side)
• InjectConfiguration
• Hosts (ViewHost, ViewControllerHost)
• Framework integrations (UIKit/AppKit)
• SwiftUI extensions
c. Build System
• Swift Package Manager or CocoaPods (package resolution, linking)
• Xcode build flags (-Xlinker -interposable, -weak_framework SwiftUI for iOS 12)
d. External Tool
• InjectionIII macOS app (monitors file changes, recompiles code delta, communicates over IPC/dlopen)
e. Runtime Flow
• InjectionIII → dynamic code injection into the running app → Inject’s runtime hooks trigger view/controller reload.
Relationships & Data Flows
• At Build Time:
• App target links against Inject library via SPM/CocoaPods.
• Compiler and linker flags ensure symbols are interposable.
• At Runtime:
1. InjectionIII watches source files & recompiles deltas.
2. It injects compiled code into the running app via debugger hooks/dlopen.
3. Inject library intercepts that event (via @autoclosure reload hooks).
4. Hosts unwrap and reinstantiate views/controllers, or SwiftUI view bodies recompute with .enableInjection().
Architectural Patterns & Principles
• Thin wrapper/adaptor pattern: Inject adapts the InjectionIII engine into a developer‑friendly API.
• Host/Container pattern: separates mutable host layers from immutable view/controller logic.
• No‑op in production: uses conditional compilation and interposable symbols stripped by LLVM.