一个将元程序引入Swift的工具,允许你生成Swift的代码

网友投稿 654 2022-10-29

一个将元程序引入Swift的工具,允许你生成Swift的代码

一个将元程序引入Swift的工具,允许你生成Swift的代码

Sourcery is a code generator for Swift language, built on top of Apple's own SourceKit. It extends the language abstractions to allow you to generate boilerplate code automatically.

It's used in over 30,000 projects on both iOS and macOS and it powers some of the most popular and critically-acclaimed apps you have used. Its massive community adoption was one of the factors that pushed Apple to implement derived Equality and automatic Codable conformance. Sourcery is maintained by a growing community of contributors.

Try Sourcery for your next project or add it to an existing one -- you'll save a lot of time and be happy you did!

TL;DR

Sourcery allows you to get rid of repetitive tasks. An example might be implementing Equatable, without Sourcery you need to implement stuff like this:

extension Person: Equatable { static func ==(lhs: Person, rhs: Person) -> Bool { guard lhs.firstName == rhs.firstName else { return false } guard lhs.lastName == rhs.lastName else { return false } guard lhs.birthDate == rhs.birthDate else { return false } return true }}

This is trivial code but imagine doing this across ten types. Across fifty. How many structs and classes are in your project?

Sourcery removes the need to write this code. And if you refactor or add properties, the equality code will be automatically updated for you, eliminating possible human errors.

Sourcery automation can be applied to many more domains, e.g.

Equality & HashingEnum cases & CountsLensesMocks & StubsLinuxMainDecoratorsJSON codingNSCoding and Codable

It's trivial to write new templates to remove boilerplate that is specific to your projects.

How To Get Started

There are plenty of tutorials for different uses of Sourcery, and you can always ask for help in our Swift Forum Category.

The Magic of Sourcery is a great starting tutorialGenerating Swift Code for iOS deals with JSON handling codeHow To Automate Swift Boilerplate with Sourcery generates conversions to dictionariesCodable Enums implements Codable support for EnumerationsBuilding an API client with Sourcery builds API client leveraging Sourcery annotationsMetaprogramming in Swift is a video from CocoaHeads where Krzysztof introduces Sourcery

Installation

Binary form Download the latest release with the prebuilt binary from release tab. Unzip the archive into the desired destination and run bin/sourcery Homebrew brew install sourcery CocoaPods Add pod 'Sourcery' to your Podfile and run pod update Sourcery. This will download the latest release binary and will put it in your project's CocoaPods path so you will run it with $PODS_ROOT/Sourcery/bin/sourcery Mint Run mint run krzysztofzablocki/Sourcery. Building from source Download the latest release source code from the release tab or clone the repository and build Sourcery manually. Building with Swift Package Manager Run swift build -c release in the root folder. This will create a .build/release folder and will put the binary there. Move the whole .build/release folder to your desired destination and run with path_to_release_folder/sourceryNote: JS templates are not supported when building with SPM yet. Building with Xcode Open Sourcery.xcworkspace and build with Sourcery-Release scheme. This will create Sourcery.app in the Derived Data folder. You can copy it to your desired destination and run with path_to_sourcery_app/Sourcery.app/Contents/MacOS/Sourcery

Documentation

Full documentation for the latest release is available here.

Usage

Sourcery is a command line tool; you can either run it manually or in a custom build phase using the following command:

$ ./sourcery --sources --templates --output

Note: this command differs depending on how you installed Sourcery (see Installing)

Command line options

--sources - Path to a source swift files or directories. You can provide multiple paths using multiple --sources option.--templates - Path to templates. File or Directory. You can provide multiple paths using multiple --templates options.--force-parse - File extensions of Sourcery generated file you want to parse. You can provide multiple extension using multiple --force-parse options. (i.e. file-arse.swift will be parsed even if generated by Sourcery if --force-parse toparse). Useful when trying to implement a multiple phases generation.--output [default: current path] - Path to output. File or Directory.--config [default: current path] - Path to config file. File or Directory. See Configuration file.--args - Additional arguments to pass to templates. Each argument can have an explicit value or will have implicit true value. Arguments should be separated with , without spaces (i.e. --args arg1=value,arg2). Arguments are accessible in templates via argument.name--watch [default: false] - Watch both code and template folders for changes and regenerate automatically.--verbose [default: false] - Turn on verbose logging--quiet [default: false] - Turn off any logging, only emit errors--disableCache [default: false] - Turn off caching of parsed data--prune [default: false] - Prune empty generated files--version - Display the current version of Sourcery--help - Display help information

Configuration file

Instead of CLI arguments you can use a .sourcery.yml configuration file:

sources: - - templates: - - force-parse: - - output: args: :

Read more about this configuration file here.

Contributing

Contributions to Sourcery are welcomed and encouraged!

It is easy to get involved. Please see the Contributing guide for more details.

A list of contributors is available through GitHub.

To clarify what is expected of our community, Sourcery has adopted the code of conduct defined by the Contributor Covenant. This document is used across many open source communities, and articulates my values well. For more, see the Code of Conduct.

License

Sourcery is available under the MIT license. See LICENSE for more information.

Attributions

This tool is powered by

SourceKitten by JP SimardStencil and few other libs by Kyle Fuller

Thank you! to:

Mariusz Ostrowski for creating the logo.Artsy Eidolon team, because we use their codebase as a stub data for performance testing the parser.Olivier Halligon for showing me his setup scripts for CLI tools which are powering our rakefile.

Other Libraries / Tools

If you want to generate code for asset related data like .xib, .storyboards etc. use SwiftGen. SwiftGen and Sourcery are complementary tools.

Make sure to check my other libraries and tools, especially:

KZPlayground - Powerful playgrounds for Swift and Objective-CKZFileWatchers - Daemon for observing local and remote file changes, used for building other developer tools (Sourcery uses it)

You can follow me on Twitter for news/updates about other projects I am creating.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Zookeeper+Kafka集群搭建
下一篇:xlog 是一个日志记录器用于net/context感知的HTTP应用程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~