Dynamic Swift is a wrapper application for working with SwiftPM projects. The primary purpose of this application is to allow developers to write code in swift that generates code (much like how other projects may use Python, Perl, or some other scripting language to generate code), this way its all in the same language.
For installation instructions click here For configuration instructions click here
Notes:
This project has a minimum requirement of Swift 4.0
When adding new dswift files manually through Xcode, add a new Swift file making sure to set the proper target. After the file is created then re-name the extension to .dswift. Xcode won't allow you to set the target afterwards if the file extension is not a known compilable file.
The other way to add missing dswift files to the Xcode Project is to re-generate the project file with the following command: dswift package generate-xcodeproj
Dynamic Swift files work much like Active Server Pages (ASP) and Java Server Pages (JSP) files. Anything not in a <%... %> block is treated as text that is put directly to the output file. Supported bocks:
Dynamic Static Swift files are JSON files that instruct the dswift application to load the contents of a specified file into the project as a static variable
{
"file": "{Relative path to file to load}",
"namespace": "{Optional namespace path for extension}",
"modifier": "{access modifier. public or internal}",
"name": "{Name to give new struct object}",
"type": "{load type. binary or text or text(iana character set name)}"
}
Code in the generator file but outside the generator class
<%!!
...
%>
Code in the generator class but outside the generator method
<%!
...
%>
Code within the generator method
<%
...
%>
Outputs the value resulting from the content of the block
<%=...%>
Include the content of a file into a dswift file
<%@include file="./file.to.include.dswiftInclude" onlyOnce="true" quiet="false" %>
Include the content of a folder in the building of the dswift file
<%@include folder="./folder.to.include/" quiet="false" extensionMapping="txt:swift" %>
Include a GitHub Package in the building of a dswift file
<%@include package="https://github.com/TheAngryDarling/SwiftCodeTimer.git" from="1.0.1" packageName="CodeTimer" %>
Tells dswift that this dswift file references another file so dswift should check it for modifications to determin if the dswift file should be rebuilt
<%@reference file="./file.to.include.dswiftInclude" %>
Tells dswift that this dswift file references another folder so dswift should check it for modifications to determin if the dswift file should be rebuilt
<%@reference folder="./folder.to.include/" %>
/// Example DSwift Include File A.dswiftInclude
public class IncludeClass {
...
}
/// Example DSwift Include File B.dswiftInclude
print("This is an include method")
/// Example File:
import Foundation
public class Example {
<%@include file="Include File A.dswiftInclude" onlyOnce="true" %>
<%@include file="Include File A.dswiftInclude" %>
<% for i in 0..<5 {%>
public func testFunc<%=i%>() {
<%@include file="Include File B.dswiftInclude" quiet="true" %>
print("This is function #<%=i%>")
}
<%}%>
}
// Output file:
// This file was dynamically generated from '{file name}' by Dynamic Swift. Please do not modify directly.
import Foundation
public class Example {
/* *** Begin Included 'Include File A.dswiftInclude' *** */
public class IncludeClass {
...
}
/* *** End Include 'Include File A.dswiftInclude' *** */
/* *** Begin Included Include File A.dswiftInclude' *** */
/* *** Already included elsewhere *** */
/* *** End Include 'Include File A.dswiftInclude' *** */
public func testFunc0() {
print("This is an include method")
print("This is function #0")
}
public func testFunc1() {
print("This is an include method")
print("This is function #1")
}
public func testFunc2() {
print("This is an include method")
print("This is function #2")
}
public func testFunc3() {
print("This is an include method")
print("This is function #3")
}
public func testFunc4() {
print("This is an include method")
print("This is function #4")
}
}
{
"file": "{Relative path to file to load}",
"namespace": "{Optional namespace path for extension}",
"modifier": "{access modifier. public or internal}",
"name": "{Name to give new struct object}",
"type": "{load type. binary or text or text(iana character set name)}"
}
dswift-file (no namespace)
{
"file": "string.file",
"modifier": "public",
"name": "Strings",
"type": "text"
}
generated file (no namespace)
public struct Strings {
private init() { }
private static let string: String = """
...
"""
public static let encoding: String.Encoding = String.Encoding(rawValue: 4)
public static var data: Data { return Strings.string.data(using: encoding)! }
}
dswift-file (with namespace)
{
"file": "string.file",
"namespace": "ClassName1.ClassName2",
"modifier": "public",
"name": "Strings",
"type": "text"
}
generated file (with namespace)
public extension ClassName1.ClassName2 {
struct Strings {
private init() { }
private static let string: String = """
...
"""
public static let encoding: String.Encoding = String.Encoding(rawValue: 4)
public static var data: Data { return Strings.string.data(using: encoding)! }
}
}
dswift-file (no namespace)
{
"file": "binary.file",
"modifier": "public",
"name": "Binary",
"type": "binary"
}
generated file (no namespace)
public struct Binary {
private init() { }
private static let _value: [Int8] = [
...
]
public static var data: Data { return Data(bytes: Binary._value) }
}
dswift-file (with namespace)
{
"file": "binary.file",
"namespace": "ClassName1.ClassName2",
"modifier": "public",
"name": "Binary",
"type": "binary"
}
generated file (with namespace)
public extension ClassName1.ClassName2 {
struct Binary {
private init() { }
private static let _value: [Int8] = [
...
]
public static var data: Data { return Data(bytes: Binary._value) }
}
}
Dynamic Swift ships with completion scripts for both Bash and ZSH. These files should be generated in order to use them.
Use the following commands to install the Bash completions to ~/.dswift-package-complete.bash
and automatically load them using your ~/.bash_profile
file.
dswift package generate-completion-script bash > ~/.dswift-package-complete.bash
echo -e "source ~/.dswift-package-complete.bash\n" >> ~/.dbash_profile
source ~/.dswift-package-complete.bash
Alternatively, add the following commands to your ~/.bash_profile
file to directly load completions:
# Source Dynamic Swift completion
if [ -n "`which dswift`" ]; then
eval "`dswift package generate-completion-script bash`"
fi
Use the following commands to install the ZSH completions to ~/.zsh/_swift
. You can chose a different folder, but the filename should be _swift
. This will also add ~/.zsh
to your $fpath
using your ~/.zshrc
file.
mkdir ~/.zsh
swift package package generate-completion-script zsh > ~/.zsh/_dswift
echo -e "fpath=(~/.zsh \$fpath)\n" >> ~/.zshrc
compinit
This project is licensed under Apache License v2.0 - see the LICENSE.md file for details
link |
Stars: 5 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics