compiler-driven, structured, type-safe source generation. never use gyb again!

Overview

factory
2022-09-10-a

ci build status

factory is a structured, type-safe source generation tool. It is intended to be a replacement for (and improvement over) the gyb tool!

factory is powered by swift-syntax, so it evolves with the toolchain. You cannot run it with the 5.6.2 release toolchain, because the 5.6.2 toolchain is too old and does not support the API factory needs to transform your sources safely.

The current toolchain pin is:

swift-DEVELOPMENT-SNAPSHOT-2022-09-10-a

Swift files generated by factory are still backwards compatible, in fact one of the main use cases of factory is back-deployment.

Platforms

SPF officially supports Linux and macOS.

Please note that on macOS, SPM sets the wrong @rpath by default, so you will need to manually add a symlink to lib_InternalSwiftSyntaxParser.dylib inside your build artifacts directory.

Overview

factory was designed with the following goals in mind:

  1. Template files should look like normal .swift files, so highlighters and IDEs don’t freak out.

  2. Templates should be safe, and prohibit arbitrary string splicing or token substitutions.

  3. Templates should work well with documentation comments.

  4. Template syntax should be minimal, purely additive, and source generation tooling should accept and return vanilla .swift sources unchanged.

  5. Template users should be able to use as much or as little templating as they like, and using templating on one declaration should not increase the cognitive burden of the rest of the code in the file.

  6. Templating systems should nudge users towards using the least amount of templating necessary for their use-case.

  7. Template sources should be self-explanatory, and understandable by developers who have never heard of swift-package-factory.

  8. Template users should be able to stop using a templating system, and be able to assume responsibility for maintaining generated .swift files at any time.

In a nutshell:

Basic.spf

extension Int
{
    @matrix(__ordinal__: [i, j, k], __value__: [0, 1, 2])
    @inlinable public 
    var __ordinal__:Int 
    {
        __value__
    }

    @basis 
    let cases:[Never] = [a, b]

    enum Cases:Int
    {
        @matrix(__case__: cases)
        case __case__
    }

    @matrix(__case__: cases)
    public static 
    var __case__:Self 
    {
        Cases.__case__.rawValue
    }
}

Basic.spf.swift

extension Int
{
    @inlinable public 
    var i:Int 
    {
        0
    }
    @inlinable public 
    var j:Int 
    {
        1
    }
    @inlinable public 
    var k:Int 
    {
        2
    }

    enum Cases:Int
    {
        case a
        case b
    }

    public static 
    var a:Self 
    {
        Cases.a.rawValue
    }

    public static 
    var b:Self 
    {
        Cases.b.rawValue
    }
}

Getting started

Check out the Examples directory to learn how to use SPF!

Features

factory extends the Swift language with three attributes:

  1. @basis

    Defines a sequence of tokens to iterate over when generating declarations from a template. It can be applied to a let binding, and it must be initialized with an array literal.

    The declaration it is attached to will be removed from the generated .swift code, along with any associated comments and doccomments.

  2. @matrix

    Replicates the declaration it is attached to, along with any associated comments and doccomments. It takes @basis bindings or and/or inline array literals as arguments, with the name of the argument becoming the name of the loop variable. If more than one basis is given, @matrix zips them, and will discard trailing basis elements if their lengths differ.

    @matrix can be applied to an associatedtype, actor, class, case, enum, extension, func, import, init, operator, precedence group, protocol, struct, subscript, typealias, let, or var declaration.

  3. @retro

    Downgrades a protocol with primary associated types to a protocol without any, and gates the two variants by #if swift(>=5.7). It can be applied to a protocol with at least one primary associatedtype.

    @retro copies any comments and doccomments attached to the original protocol, and includes them in the generated #if blocks.

You might also like...
A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety
A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety

A Protocol-Oriented NotificationCenter which is type safe, thread safe and with memory safety. Type Safe No more userInfo dictionary and Downcasting,

Type-safe CAAnimation wrapper. It makes preventing to set wrong type values.
Type-safe CAAnimation wrapper. It makes preventing to set wrong type values.

TheAnimation TheAnimation is Type-safe CAAnimation wrapper. Introduction For example, if you want to animate backgroundColor with CABasicAnimation, yo

TopicEventBus is Easy to use, type safe way of implementing Publish–subscribe design pattern.
TopicEventBus is Easy to use, type safe way of implementing Publish–subscribe design pattern.

TopicEventBus Publish–subscribe design pattern implementation framework, with ability to publish events by topic. (NotificationCenter extended alterna

SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.
SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.

SwiftGen is a tool to automatically generate Swift code for resources of your projects (like images, localised strings, etc), to make them type-safe to use.

Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.

Venice provides structured concurrency and CSP for Swift. Features Coroutines Coroutine cancelation Coroutine groups Channels Receive-only chan

🏗 MondrianLayout - describing structured layout for AutoLayout
🏗 MondrianLayout - describing structured layout for AutoLayout

🏗 A DSL based layout builder for AutoLayout

StatusBarOverlay will automatically show a
StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again. It supports apps which hide the status bar and The Notch

StatusBarOverlay StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again. It support

Log every incoming notification to view them again later, also includes attachments and advanced settings to configure

Vē Natively integrated notification logger Installation Add this repository to your package manager

This little app aims to help teach me how to implement more then one API in one single application in a reusable and properly structured manner.

LilAPI App News & Weather This little API app combines two of Jordan Singers Lil Software API's into one app. The goal with this app was to learn how

A structured vertical/horizontal stack layout
A structured vertical/horizontal stack layout

EEStackLayout A vertical stackview which takes subviews with different widths and adds them to it's rows with paddings, spacings etc. Installation Coc

AsyncOperators brings some features of RxSwift/Combine to Structured Concurrency

AsyncOperators brings some features of RxSwift/Combine to Structured Concurrency, such as combineLatest and distinctUntilChanged.

Reliable Server Side Swift ✭ Make Apache great again!
Reliable Server Side Swift ✭ Make Apache great again!

mod_swift mod_swift is a technology demo which shows how to write native modules for the Apache Web Server in the Swift 3 programming language. The de

StatusBarOverlay will automatically show a
StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again

StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again. It supports apps which hide the status bar and The Notch

Easier sharing of structured data between iOS applications and share extensions
Easier sharing of structured data between iOS applications and share extensions

XExtensionItem XExtensionItem is a tiny library allowing for easier sharing of structured data between iOS applications and share extensions. It is ta

SwiftyOpenGraph - A swift library that retrieves structured open graph data from webpages.

SwiftyOpenGraph Usage Initialization Base Properties Types Installation License Usage Initialization You use SwiftyOpenGraph by initializing OpenGraph

JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die
JSONNeverDie - Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die. Also JSONNeverDie is a very important part of Pitaya.

Swiftbot on slack. Inspired by kishikawakatsumi/swift-compiler-discord-bot
Swiftbot on slack. Inspired by kishikawakatsumi/swift-compiler-discord-bot

Swiftbot Swiftbot on slack. Inspired by kishikawakatsumi/swift-compiler-discord-bot Usage $ swiftbot --token xoxb-xxxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxx

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

A compiler, assembler and emulator for the QCPU 2 architecture

QCPU CLI A CLI for compiling Q-code, assembling extended QCPU 2 assembly and emulating machine code. Tags @PAGE upper lower @HEADER label argum

Comments
  • Project fails to compile in Xcode 14 on Ventura

    Project fails to compile in Xcode 14 on Ventura

    Cool project! As a heavy SourceGen user I'm excited about this project! Good job!

    The project fails to compile with Xcode 14 beta 5 on Ventura, any ideas?

    Screenshot 2022-08-18 at 21 00 37 bug 
    opened by Sajjon 1
  • @retro is not being stripped if it is the only attribute

    @retro is not being stripped if it is the only attribute

    failing example:

    @retro protocol ExampleProtocol<Element, Index>
    {
        associatedtype Element 
        associatedtype Index:Strideable
    
        init()
    }
    
    opened by kelvin13 0
  • filenames differ only in case

    filenames differ only in case

    SPF has a library named Factory and an executable tool named factory. unfortunately, this fucks up macOS usage, because macOS has a case-insensitive filesystem!

    related: #3

    bug 
    opened by kelvin13 0
  • doccomment on first line gets stuck to end of previous declaration

    doccomment on first line gets stuck to end of previous declaration

    this:

    /// I am a back-deployed protocol with primary associated types.
    @matrix(__Protocol__: [EvolvingProtocolA, EvolvingProtocolB])
    protocol __Protocol__<Element, Index>
    {
        associatedtype Element 
        associatedtype Index:Strideable
    
        init()
    }
    

    turns into

    /// I am a back-deployed protocol with primary associated types.
    protocol EvolvingProtocolA<Element, Index>
    {
        associatedtype Element 
        associatedtype Index:Strideable
    
        init()
    }/// I am a back-deployed protocol with primary associated types.
    protocol EvolvingProtocolB<Element, Index>
    {
        associatedtype Element 
        associatedtype Index:Strideable
    
        init()
    }
    
    

    which loses the doccomment

    bug 
    opened by kelvin13 0
Releases(swift-DEVELOPMENT-SNAPSHOT-2022-09-24-a)
Owner
taylorswift
kope with klossy
taylorswift
An elegant, flexible, type-safe dependency resolution framework for Swift

Guise is an elegant, flexible, type-safe dependency resolution framework for Swift. Flexible dependency resolution, with optional caching Elegant, str

null 52 Jun 6, 2022
Needle - Compile-time safe Swift dependency injection framework

Needle is a dependency injection (DI) system for Swift. Unlike other DI frameworks, such as Cleanse, Swinject, Needle encourages hierarchical DI struc

Uber Open Source 1.4k Sep 20, 2022
Kraken - Simple Dependency Injection container for Swift. Use protocols to resolve dependencies with easy-to-use syntax!

Kraken Photo courtesy of www.krakenstudios.blogspot.com Introduction Kraken is a simple Dependency Injection Container. It's aimed to be as simple as

Syed Sabir Salman-Al-Musawi 1 Oct 9, 2020
Deli is an easy-to-use Dependency Injection Container that creates DI containers

Deli is an easy-to-use Dependency Injection Container that creates DI containers with all required registrations and corresponding factories.

Jungwon An 134 Aug 10, 2022
🗂 Never use NSSearchPathForDirectoriesInDomains again

AppFolder AppFolder is a lightweight framework that lets you design a friendly, strongly-typed representation of a directories inside your app's conta

Oleg Dreyman 934 Sep 19, 2022
Count It, Never lose the count again

Count It Never lose the count again. Dead simple App with Apple Watch integration that lets you count anything. Laps while exercising. How many beers

Chris Jimenez 62 Feb 14, 2022
Type-safe networking abstraction layer that associates request type with response type.

APIKit APIKit is a type-safe networking abstraction layer that associates request type with response type. // SearchRepositoriesRequest conforms to Re

Yosuke Ishikawa 1.9k Sep 22, 2022
💾 🔜📱 Type-safe data-driven CollectionView, TableView Framework. (We can also use ASCollectionNode)

⚠️ The latest updates is this PR. It changes the difference algorithm to DifferenceKit. DataSources ?? ?? ?? Type-safe data-driven List-UI Framework.

Muukii 560 Sep 15, 2022
MemoryCache - type-safe, thread-safe memory cache class in Swift

MemoryCache is a memory cache class in swift. The MemoryCache class incorporates LRU policies, which ensure that a cache doesn’t

Yusuke Morishita 74 Aug 31, 2022
Modern thread-safe and type-safe key-value observing for Swift and Objective-C

Now Archived and Forked PMKVObserver will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork o

Postmates Inc. 708 Jun 29, 2022