前言
这是做项目瘦身的时候翻到的一个好用的工具:perihery。支持 OC,不仅能筛选出未使用的类,而且 struct
、protocol
、function
、property
、enum
、typealias
、associatedType
这些场景中,你没有使用的方法,没有使用的参数都能给你筛选出来。
使用方法
- 使用 homebrew 安装 periphery
如果没有安装 homebrew,使用下面语句安装,如果已安装,跳过:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
然后使用 homebrew 安装 periphery:
brew install peripheryapp/periphery/periphery
- 在你的项目中引入 periphery 库
打开项目的 Podfile,添加:
pod 'Periphery'
执行 pod install
。
- 扫描项目
进入项目的根目录,执行:
periphery --setup
首次执行只要先 setup, setup 会有一些选项需要选择:
Welcome to Periphery!
This guided setup will help you select the appropriate configuration for your project.
* Inspecting project...
Select build targets to analyze:
? Delimit choices with a single space, e.g: 1 2 3, or 'all' to select all options
1 SwiftLoseWeight
> all
? Delimit choices with a single space, e.g: 1 2 3
1 SwiftLoseWeight
> 1
Assume Objective-C accessible declarations are in use?
? Declarations exposed to the Objective-C runtime explicitly with @objc, or implicitly by inheriting NSObject will be assumed to be in use. You may want to choose 'Yes' here if your project contains a mix of Swift & Objective-C.
(Y)es/(N)o > Y
Assume all 'public' declarations are in use?
? You should choose 'Yes' here if your public interfaces are not used by any selected build target, as may be the case for a framework/library project.
(Y)es/(N)o > Y
Save configuration to .periphery.yml?
(Y)es/(N)o > Y
可以参照着进行修改,setup
之后会执行 scan
指令,以下是我测试的一个 Demo 的输出:
Swift 代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
test()
testStruct()
}
}
class TestObject {
func testUsedMethod<T>(_ t: T, name: String) {}
func testUnUsedMethod() {}
}
extension TestObject {
func testUnUsedMethodInExtension() {}
}
struct TestStruct1 {
var objectInStruct = 0
func structMethod_1(_ object: String) {}
private func structMethod_2() {}
}
struct TestStruct2 {
var objectInStruct = 0
private func structMethod_1(_ object: String) {}
private func structMethod_2() {}
}
func test() {
let object = TestObject()
object.testUsedMethod(1, name: "rui")
}
func testStruct() {
var struct_1 = TestStruct1()
struct_1.objectInStruct = 1
struct_1.structMethod_1("amazing")
}
命令行输出(xxx是你项目的根目录的全路径):
xxx/ViewController.swift:19:30: warning: Parameter 't' is unused
xxx/ViewController.swift:19:36: warning: Parameter 'name' is unused
xxx/ViewController.swift:20:10: warning: Function 'testUnUsedMethod()' is unused
xxx/ViewController.swift:24:10: warning: Function 'testUnUsedMethodInExtension()' is unused
xxx/ViewController.swift:28:9: warning: Property 'objectInStruct' is assigned, but never used
xxx/ViewController.swift:29:27: warning: Parameter 'object' is unused
xxx/ViewController.swift:30:18: warning: Function 'structMethod_2()' is unused
xxx/ViewController.swift:33:8: warning: Struct 'TestStruct2' is unused
可以看到,不光没有使用到的方法,连没有使用到的参数都会被筛选出来,就算是定义在 extension 中,但是如果没有被使用也一样会被筛选出来,但是不管如何,筛选出来的类还是要自己根据业务去看一下实际上到底有没有使用,二次确认后再进行删除。
如果你的项目是通过 .xcworkspace
来跑的(一般都是),还需要多指定一些参数,如:
periphery scan --workspace MyApp.xcworkspace --schemes MyApp --targets MyApp
其他
在 Podfile
中添加 pod periphery
,执行 pod install
。
cd
到项目根目录,执行:
periphery scan --workspace MyApp.xcworkspace --schemes NewSS --targets MyApp
等待执行,第一次执行会比较久,执行完看命令行即可,如果你使用的是 iterm2,可能会出现输出太多导致无法全部查看的问题,可以打开 iterm2 的设置,将下图所示的地方勾上,这样就可以完整显示了。
转载请注明 出处。