SourceKitten
一个可爱的用于与SourceKit交互的框架和命令行工具。
SourceKitten 通过与 sourcekitd.framework
链接和通信,解析 Swift AST,提取 Swift 或 Objective-C 项目的注释文档,获取 Swift 文件的语法数据以及更多功能!
安装
构建 SourceKitten 需要 Xcode 13.3 或更高版本,或 Swift 5.6 工具链或更高版本,具有 Swift 包管理器。
SourceKitten 通常支持先前的 SourceKit 版本。
Homebrew
运行 brew install sourcekitten
。
Swift 包管理器
在项目的根目录中运行 swift build
。
Bazel
将以下内容添加到您的 WORKSPACE
文件中
SOURCEKITTEN_VERSION = "SOME_VERSION"
SOURCEKITTEN_SHA = "SOME_SHA"
http_archive(
name = "com_github_jpsim_sourcekitten",
url = "https://github.com/jpsim/SourceKitten/archive/refs/tags/%s.tar.gz" % (SOURCEKITTEN_VERSION),
sha256 = SOURCEKITTEN_SHA,
strip_prefix = "SourceKitten-%s" % SOURCEKITTEN_VERSION
)
然后运行: bazel run @com_github_jpsim_sourcekitten//:sourcekitten -- -h
Xcode (通过 Make)
在此项目的根目录中运行 make install
打包
从发布标签下载并打开 SourceKitten.pkg
命令行使用
SourceKitten 安装后,您可以从命令行使用它。
$ sourcekitten help
OVERVIEW: An adorable little command line tool for interacting with SourceKit
USAGE: sourcekitten <subcommand>
OPTIONS:
--version Show the version.
-h, --help Show help information.
SUBCOMMANDS:
complete Generate code completion options
doc Print Swift or Objective-C docs as JSON
format Format Swift file
index Index Swift file and print as JSON
module-info Obtain information about a Swift module and print as JSON
request Run a raw SourceKit request
structure Print Swift structure information as JSON
syntax Print Swift syntax information as JSON
version Display the current version of SourceKitten
See 'sourcekitten help <subcommand>' for detailed help.
如何解析 SourceKit?
SourceKitten 将按照以下顺序搜索 SourceKit:
$XCODE_DEFAULT_TOOLCHAIN_OVERRIDE
$TOOLCHAIN_DIR
xcrun -find swift
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
~/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
~/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
在 Linux 上,预计 SourceKit 将位于 /usr/lib/libsourcekitdInProc.so
,或者在 LINUX_SOURCEKIT_LIB_PATH
环境变量中指定。
使用SourceKitten构建的项目
- SwiftLint:强制执行Swift样式和约定的工具。
- Jazzy:Swift & Objective-C的心灵之书。
- Sourcery:Swift的元编程,停止编写样板代码。
- SwiftyMocky:用于生成Mock的框架。
- SourceKittenDaemon:适用于任何文本编辑器的Swift自动补全。
- SourceDocs:命令行工具,可以从行内源代码注释生成Markdown文档。
- Cuckoo:Swift的第一个无样板代码Mock框架。
- IBAnalyzer:无需运行您的应用程序或编写单元测试即可发现常见的xib和Storyboard相关问题。
- Taylor:测量Swift代码指标,并在Xcode、Jenkins和其他CI平台上获取报告。
查看更多
- https://github.com/appsquickly/TyphoonSwift
- https://github.com/banjun/bansan
- https://github.com/Beaver/BeaverCodeGen
- https://github.com/Ben-G/Meet
- https://github.com/dfreemanRIIS/ETAMock
- https://github.com/dostu/SwiftMetric
- https://github.com/draven-archive/MetaKit
- https://github.com/geosor/SwiftVisualizer
- https://github.com/godfreynolan/AgileSwiftTst
- https://github.com/godfreynolan/CodeCraftsman
- https://github.com/ilyapuchka/dipgen
- https://github.com/ilyapuchka/SourceKittenEditorExtension
- https://github.com/interstateone/Unused
- https://github.com/ishkawa/DIKit
- https://github.com/IvanovGeorge/FBAuth
- https://github.com/jmpg93/NavigatorSwift
- https://github.com/jpmartha/Pancake
- https://github.com/jpweber/Kontext
- https://github.com/KenichiroSato/CatDogTube
- https://github.com/klundberg/grift
- https://github.com/kovtun1/DependenciesGraph
- https://github.com/lvsti/Bridgecraft
- https://github.com/maralla/completor-swift
- https://github.com/marcsnts/Shopify-Winter18-Technical
- https://github.com/momentumworks/Formula
- https://github.com/nevil/UNClassDiagram
- https://github.com/norio-nomura/LinuxSupportForXcode
- https://github.com/paulofaria/swift-package-crawler-data
- https://github.com/rajat-explorer/Github-Profiler
- https://github.com/rockbruno/swiftshield
- https://github.com/S2dentik/Enlight
- https://github.com/seanhenry/SwiftMockGeneratorForXcode
- https://github.com/sharplet/swiftags
- https://github.com/siejkowski/Croupier
- https://github.com/SwiftKit/CuckooGenerator
- https://github.com/SwiftKit/Torch
- https://github.com/SwiftTools/SwiftFelisCatus
- https://github.com/swizzlr/lux
- https://github.com/tid-kijyun/XcodeSourceEditorExtension-ProtocolImplementation
- https://github.com/tjarratt/fake4swift
- https://github.com/tkohout/Genie
- https://github.com/tomquist/MagicMirror
- https://github.com/TurfDb/TurfGen
- https://github.com/vadimue/AwesomeWeather
- https://github.com/yonaskolb/Beak
- https://github.com/zenzz/vs-swifter-server
- https://github.com/zenzz/zxxswifter-server
- https://github.com/scribd/Weaver
- https://github.com/Nonchalant/FactoryProvider
完成
运行 sourcekitten complete --file file.swift --offset 123
或 sourcekitten complete --text "0." --offset 2
将打印出在提供的文件/文本中的偏移量代码完成选项
[{
"descriptionKey" : "advancedBy(n: Distance)",
"associatedUSRs" : "s:FSi10advancedByFSiFSiSi s:FPSs21RandomAccessIndexType10advancedByuRq_S__Fq_Fqq_Ss16ForwardIndexType8Distanceq_ s:FPSs16ForwardIndexType10advancedByuRq_S__Fq_Fqq_S_8Distanceq_ s:FPSs10Strideable10advancedByuRq_S__Fq_Fqq_S_6Strideq_ s:FPSs11_Strideable10advancedByuRq_S__Fq_Fqq_S_6Strideq_",
"kind" : "source.lang.swift.decl.function.method.instance",
"sourcetext" : "advancedBy(<#T##n: Distance##Distance#>)",
"context" : "source.codecompletion.context.thisclass",
"typeName" : "Int",
"moduleName" : "Swift",
"name" : "advancedBy(n: Distance)"
},
{
"descriptionKey" : "advancedBy(n: Self.Distance, limit: Self)",
"associatedUSRs" : "s:FeRq_Ss21RandomAccessIndexType_SsS_10advancedByuRq_S__Fq_FTqq_Ss16ForwardIndexType8Distance5limitq__q_",
"kind" : "source.lang.swift.decl.function.method.instance",
"sourcetext" : "advancedBy(<#T##n: Self.Distance##Self.Distance#>, limit: <#T##Self#>)",
"context" : "source.codecompletion.context.superclass",
"typeName" : "Self",
"moduleName" : "Swift",
"name" : "advancedBy(n: Self.Distance, limit: Self)"
},
...
]
要使用iOS SDK,通过 -sdk
和 -target
参数传递 -
sourcekitten complete --text "import UIKit ; UIColor." --offset 22 -- -target arm64-apple-ios9.0 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk
文档
运行sourcekitten doc
会将解析出的所有参数传递给xcodebuild
(或者在--single-file
模式下直接传递给编译器,SourceKit/clang)。
示例用法
sourcekitten doc -- -workspace SourceKitten.xcworkspace -scheme SourceKittenFramework
sourcekitten doc --single-file file.swift -- -j4 file.swift
sourcekitten doc --module-name Alamofire -- -project Alamofire.xcodeproj
sourcekitten doc -- -workspace Haneke.xcworkspace -scheme Haneke
sourcekitten doc --objc Realm/Realm.h -- -x objective-c -isysroot $(xcrun --show-sdk-path) -I $(pwd)
结构
运行sourcekitten structure --file file.swift
或sourcekitten structure --text "struct A { func b() {} }"
将返回一个包含结构信息的JSON数组。
{
"key.substructure" : [
{
"key.kind" : "source.lang.swift.decl.struct",
"key.offset" : 0,
"key.nameoffset" : 7,
"key.namelength" : 1,
"key.bodyoffset" : 10,
"key.bodylength" : 13,
"key.length" : 24,
"key.substructure" : [
{
"key.kind" : "source.lang.swift.decl.function.method.instance",
"key.offset" : 11,
"key.nameoffset" : 16,
"key.namelength" : 3,
"key.bodyoffset" : 21,
"key.bodylength" : 0,
"key.length" : 11,
"key.substructure" : [
],
"key.name" : "b()"
}
],
"key.name" : "A"
}
],
"key.offset" : 0,
"key.diagnostic_stage" : "source.diagnostic.stage.swift.parse",
"key.length" : 24
}
语法
运行sourcekitten syntax --file file.swift
或sourcekitten syntax --text "import Foundation // Hello World"
将返回一个包含语法高亮信息的JSON数组。
[
{
"offset" : 0,
"length" : 6,
"type" : "source.lang.swift.syntaxtype.keyword"
},
{
"offset" : 7,
"length" : 10,
"type" : "source.lang.swift.syntaxtype.identifier"
},
{
"offset" : 18,
"length" : 14,
"type" : "source.lang.swift.syntaxtype.comment"
}
]
请求
运行sourcekitten request --yaml [FILE|TEXT]
将使用给定的yaml执行sourcekit请求。例如
key.request: source.request.cursorinfo
key.sourcefile: "/tmp/foo.swift"
key.offset: 8
key.compilerargs:
- "/tmp/foo.swift"
SourceKittenFramework
大多数《sourcekitten》命令行工具的功能实际上都封装在一个名为 SourceKittenFramework 的框架中。
如果您想在其他工具中使用 SourceKitten 或可能扩展其功能,请查看 SourceKittenFramework 源代码,以查看其 API 是否符合您的需求。
注意:《sourcekitten》完全由 Swift 编写,SourceKittenFramework API 并未设计用于与 Objective-C 进行接口交互。
授权协议
MIT 授权。