简陋的swift carthage copy-frameworks 辅助脚本代码

这里是一个简单的Swift脚本,用于辅助carthage copy-frameworks命令:

import Foundation

let fileManager = FileManager.default
let carthageBuildPath = "/Users/username/Carthage/Build"
let carthageCachePath = "/Users/username/Library/Caches/org.carthage.CarthageKit"
let projectPath = "/Users/username/ProjectFolder"

// 1. 删除过期的构建
let carthageBuildFiles = try fileManager.contentsOfDirectory(atPath: carthageBuildPath)
for file in carthageBuildFiles {
    let fullPath = carthageBuildPath + "/" + file
    if  fileManager.fileExists(atPath: fullPath) {
        do {
            let attributes = try fileManager.attributesOfItem(atPath: fullPath)
            let modifiedDate = attributes[.modificationDate] as! Date
            let daysAgo = Date().timeIntervalSince(modifiedDate) / (24 * 60 * 60)
            if daysAgo > 7 {   // 过期超过7天
                try fileManager.removeItem(atPath: fullPath)
            }
        } catch {
            print(error)
        }
    }
}

// 2. 复制框架
let carthageCacheFiles = try fileManager.contentsOfDirectory(atPath: carthageCachePath)
for file in carthageCacheFiles {
    if file.hasSuffix(".framework.zip") || file.hasSuffix(".dSYM.zip") {
        let fullPath = carthageCachePath + "/" + file
        let destination = projectPath + "/Carthage/Build/" + file
        do {
            try fileManager.copyItem(atPath: fullPath, toPath: destination)
        } catch {
            print(error)
        }
    }
}

// 3. 解压缩框架
let carthageBuildFiles = try fileManager.contentsOfDirectory(atPath: carthageBuildPath) 
for file in carthageBuildFiles {
    if file.hasSuffix(".framework.zip") {
        let fullPath = carthageBuildPath + "/" + file
        let destination = carthageBuildPath + "/" + file.replacingOccurrences(of: ".zip", with: "")
        do {
            try fileManager.unzipItem(at: URL(fileURLWithPath: fullPath), to: URL(fileURLWithPath: destination))
        } catch {
            print(error)
        } 
    }
}

这个脚本会自动完成以下三个步骤:

1. 删除Carthage/Build文件夹过期的构建产物(超过7天)

2. 从Carthage/Library/Caches文件夹复制.framework.zip和.dSYM.zip文件到Carthage/Build文件夹

3. 解压Build文件夹中的.framework.zip文件这样,您就无需手动运行carthage copy-frameworks命令,该脚本会自动完成framework的管理和同步。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
评论 抢沙发

请登录后发表评论