Skip to content

asam139/swift-syntax-bazel-integration-example

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

Swift Syntax Bazel Integration Example

An example how to integrate Macros using SwiftSyntax into Bazel projects
Explore the docs »

Report Bug · Request Feature

Table of Contents
  1. Repository
  2. Getting Started
  3. Usage
  4. Contributing
  5. License
  6. Contact
  7. Acknowledgments

About

An example how to integrate Macros using SwiftSyntax into Bazel projects.

Introduction

SwiftSyntax serves as a versatile tool in the realm of Swift development, offering a comprehensive set of libraries for parsing, inspecting, generating, and modifying Swift source code. Initially developed by Apple, it has evolved into an open-source project with active contributions from a diverse community. The library's documentation is readily available on swiftpackageindex, complemented by numerous articles in the GitHub readme. Notably, SwiftSyntax forms the backbone for essential tools like the Swift parser and swift-format.

A key aspect of SwiftSyntax's functionality lies in its ability to create an Abstract Syntax Tree (AST) of Swift source code. This AST serves as a high-level, secure, and efficient API, enabling developers to interact with their code in a structured manner. Exploring the Swift AST becomes more accessible through tools such as the Swift AST Explorer.

The landscape of Swift development witnessed a significant enhancement with the introduction of Swift Macros in the WWDC 2023 release of Swift 5.9. Leveraging SwiftSyntax as its foundation library, Swift Macros offer a novel approach to extending Swift by introducing new expressions, facilitating the creation of expressive libraries, and streamlining the elimination of unnecessary boilerplate code.

An intriguing dimension brought forth by Swift Macros is their role in extending the compiler with custom compile-time checks. While some may argue that similar outcomes can be achieved through reusable methods, Swift Macros stand out due to their unique features, making them an exceptional tool in the developer's toolkit.

Motivation

Swift Macros were introduced as a feature bundled within Swift Packages. This approach enhances shareability. However, it also tightens the reliance on seamless integration between Xcode and the Swift Package Manager (SPM), which, from my experience and that of others, can be less than ideal in large projects with numerous dependencies.

Given the suboptimal experience offered by Apple’s ecosystem, which precludes optimization opportunities, developers uses external depencencies managers, especially Cocoapods. Besides they uses build systems as Bazel to automate tasks, compile and execute tests in a deterministic environment.

For such reason the objective of this repository was to check the viability together pros/const of define and integrate Swift Macros into Bazel environment.

Structure

The repository defines the following parts:

  • MacrosPlugin:

    • Depends of SwiftSyntax library
      • SwiftCompilerPlugin
      • SwiftSyntaxBuilder
      • SwiftSyntaxMacros
    • Contains macros implementation
    • Define which macros are visible to the compiler
  • Macros:

    • Depends of MacrosPlugin
    • Public macro declaration which indicates:
      • Macro name
      • Type which contains the implementation of the expansion that our macro actually performs
      • Module where the Type is defined, in our case MacrosPlugin
  • iOS Example App:

    • Depends of Macros
    • Define examples using the macros

Built With

Bazel SwiftSyntax

Getting Started

To get a local copy up and running follow these simple example steps.

Prerequisites

Installation

  1. Clone the repo
    git clone https://github.com/asam139/swift-syntax-bazel-integration-example.git
  2. Run example app
    bazel run //MacrosApp:xcodeproj

Usage

Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources.

Example 1

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

(back to top)

Contact

Saúl Moreno Abril- @93sauu

Project Link: Swift Syntax Bazel Integration Example

Acknowledgments

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published