Hey everyone, I'd like to work on the customizable package templates project.
Abstract
One of the fastest ways to create a swift project is to use a swift package init
command in swiftPM commandline tool. User can then select from a handful of hardcoded templates to start with. Because the templates are hardcoded into swiftPM’s source code, they are very limited in terms of variety.
Users have many different needs for such templates, so it’s best to figure out a way to customize these templates and share them with others.
The goals are to 1.define a format for templates, 2.design an API for their customization, and 3.make initialization of a project(with templates) interactive.
Here is my full proposal: proposal
I draw inspiration from this post: Update to swift package init templates
Design Ideas
Introducing three commands:
swift package init --template <path/to/your/template/file>
---- initialize a project with template file
swift package init
---- initialize a project interactively
swift package template <path/to/your/project>
---- create a template file using existing projects
In my opinion, the project can be split into 3 parts: Use(user-friendly CLI), Share(file format) and Customize(API).
[Use (user-friendly CLI) ]
For a user-friendly CLI, I think frontend people know it best. In Vite(a frontend tool for initialize a react or vue project), the CLI is very user-friendly.
Using arrow-keys and return to guide the initialization of the project has always been interactive, intuitive and easy-to-use to me.
With the current version of swiftPM, if we enter the swift package init command, a library package is created by default.
It would be better if after entering this command, we could interactively select the type of package we want to create.
[Share (file format) ]
As for sharing, I think a .stmpl
(i.e. swift template, or anything you want it to be) format could be introduced. A program template consists mainly of directories and text files, and I think the xml
format would be better for very large pure code templates.
With the .stmpl file in hand, we can initialize a project, with a customized myTemplate.stmpl, fast and easy, using --template
option like this:
Or just swift package init:
And because .stmpl file itself contains interactable information, we can select a set of dependencies to include, and pick between including tests or not.
[Customize (API) ]
Now, for users who want to create their own templates, it’s good to have an API to empower that. Instead of an API in the form of functions, I think we can create a Template.swift just like Package.swift, where users configure attributes(for example, include specific directories by default, interactability) of a template, just like Package.swift.
With a simple swift package template </path/to/your/project> command, swiftPM reads the configurations users make, including which directories to include, which directories can be included interatively and which dependencies to include(essencially writing Package.swift file). Users can then create their customized templates(.stmpl), and share them with people.
In Template.swift, for example, you can define:
- [name] Name a template using name attribute
- [testDirectory] Select a directory for testing, specify its relative path using “.path: /Your/Path”. Use “.toggle” to make including it interactable like this:
- [directories] In directories array, determine whether to include a directory by default using “defaultInclude:”, again, use “toggle:” to let user include it interactively
- [dependencies] In dependencies array, use “url:” to specify which dependencies to include, use “defaultInclude” to determine include it by default, use “toggle:” to let user include this dependency interactively.
Current progress
I started with the simplest and implemented the interactive swift package init
command (Very rough implementation, the code structure will be optimized in the future)
You can watch a video demo here:
Sweet swift forum, please give me feedback
Please do not hesitate to give me some advice on how to name the commands, the file format, the properties that Template.swift should support, and the features that you would like to see added. Thank you very much for your participation and for seeing the end of the article.