在运行时获得特定CocoaPod版本的最佳方法是什么?

时间:2022-08-27 23:14:20

I'm currently trying to create a way to log the current version of my Pod at runtime, there are a few ways that come to mind, but I wanted to ask here to make sure i'm not missing something obvious.

我目前正在尝试创建一种方法来在运行时记录我的Pod的当前版本,有一些方法会让我想到,但是我想在这里问一下,以确保我没有遗漏什么明显的东西。

What I've done so far:

到目前为止我所做的:

  • Found out that Cocoapods generates an myPod-umbrella.h header file that exports the following :

    发现可可粉可以产生伞。h头文件,导出以下内容:

    FOUNDATION_EXPORT double myPodVersionNumber;
    FOUNDATION_EXPORT const unsigned char myPodVersionString[];
    

    They Only myPodVersionNumber seems to be accessible, and it always has 1.0 for some reason, is there a way to get that working right since i have a feeling that this is the proper way but i have misconfigured it.

    他们只有myPodVersionNumber看起来是可以访问的,它总是有1。0,出于某种原因,有办法让它正常工作吗,因为我觉得这是正确的方法,但是我配置错了。

  • Attempt to get a hold of the built framework's Info.plist and read the version there, but this seems to be a bit problematic, i have no guarantee what a developer will end up doing with the pod and might end up with a different location of the bundle, or even have it unaccessible to the project.

    尝试获得构建框架的信息。plist和读取那里的版本,但是这似乎有点问题,我不能保证开发人员最终会对pod做什么,并且可能最终会得到捆绑包的不同位置,甚至项目无法访问它。

  • Create a hardcoded property with the version number, this obviously works, but it adds lots of room to error and does not really feel like the right way to implement this, but if there is no other way to get around CocoaPods, i might just have to do that.

    创建一个带有版本号的硬编码属性,这显然是可行的,但是它增加了很多出错的空间,并不是真正想要实现它的方法,但是如果没有其他方法可以绕过CocoaPods,我可能只需要这么做。

  • Have a Build step that will read the PodSpec and generate a simple class that contains metadata about the Pod, feels a bit better than the previous point, but still feels a bit overkill for what i'm looking for.

    有一个构建步骤,它将读取PodSpec并生成一个简单的类,其中包含关于Pod的元数据,感觉比前一点好一点,但仍然觉得我在寻找的东西有点过头了。

Does anyone have a better idea or can point me in the right direction ?

谁有更好的主意,或者能给我指明正确的方向?

What i'm trying to achieve is to be able to run something like this

我想要实现的是能够运行这样的东西

print("Current version: \(myPod.version)")

print(“当前版本:\(myPod.version)”)

and have it log it out properly in the console

让它在控制台正确地登出

#Current version: 1.2.0

当前版本号:1.2.0

1 个解决方案

#1


2  

What about using URLForResource? Prints nicely at runtime with the print statement you asked for.

使用URLForResource呢?在运行时与您所要求的print语句进行良好的打印。

在运行时获得特定CocoaPod版本的最佳方法是什么?

This version prints the entire lock file to the console.

这个版本将整个锁文件打印到控制台。

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSBundle.mainBundle().URLForResource("/lockfilefolder/Podfile", withExtension: "lock")!
    let data = try! String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    print(data)   
}

/* Prints */
// PODS:
// - Firebase/Analytics (3.3.0):
// - FirebaseAnalytics (= 3.2.1)
// - Firebase/Auth (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - FirebaseAuth (= 3.0.3)
// - Firebase/Core (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - Firebase/Database (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - FirebaseDatabase (= 3.0.2)
// TL;DR

This next version prints specific line numbers. By using componentsSeparatedByString("-") I am able to remove the - character before the pod name so it looks cleaner. This works because lock files use - on every line in the list of pod names. Notice we are using pathForResource not URLForResource here.

下一个版本打印特定的行号。通过使用componentsseparation bystring(“-”),我能够在pod名称之前删除-字符,使其看起来更干净。这是因为在pod名称列表的每一行上都使用了锁文件。注意,这里我们使用的是pathForResource而不是URLForResource。

    do {
        if let path = NSBundle.mainBundle().pathForResource("/lockfilefolder/Podfile", ofType: "lock"){
            let data = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
            let lockFileData = data.componentsSeparatedByString("-")
            print("Current version: \(lockFileData[6])")
        }
    } catch let err as NSError {
        print(err)
    }

/* Prints */
// Current version: - Firebase/Core (3.3.0):

This next version we print two lines. We are using the data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) method. It gets verbose to remove the - in this case therefore it's not worth it.

下一个版本我们打印两行。我们使用的是data. componentsdisatedbycharactersinset (NSCharacterSet.newlineCharacterSet())方法。删除-在这种情况下会变得冗长,因此不值得这样做。

let url = NSBundle.mainBundle().URLForResource("/lockfilefolder/Podfile", withExtension: "lock")!
    let data = try! String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    let lockFileData = data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
    print(lockFileData[72])
    print(lockFileData[6])

/* Prints */
// COCOAPODS: 0.39.0
// - Firebase/Core (3.3.0):

#1


2  

What about using URLForResource? Prints nicely at runtime with the print statement you asked for.

使用URLForResource呢?在运行时与您所要求的print语句进行良好的打印。

在运行时获得特定CocoaPod版本的最佳方法是什么?

This version prints the entire lock file to the console.

这个版本将整个锁文件打印到控制台。

override func viewDidLoad() {
    super.viewDidLoad()

    let url = NSBundle.mainBundle().URLForResource("/lockfilefolder/Podfile", withExtension: "lock")!
    let data = try! String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    print(data)   
}

/* Prints */
// PODS:
// - Firebase/Analytics (3.3.0):
// - FirebaseAnalytics (= 3.2.1)
// - Firebase/Auth (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - FirebaseAuth (= 3.0.3)
// - Firebase/Core (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - Firebase/Database (3.3.0):
// - Firebase/Analytics (= 3.3.0)
// - FirebaseDatabase (= 3.0.2)
// TL;DR

This next version prints specific line numbers. By using componentsSeparatedByString("-") I am able to remove the - character before the pod name so it looks cleaner. This works because lock files use - on every line in the list of pod names. Notice we are using pathForResource not URLForResource here.

下一个版本打印特定的行号。通过使用componentsseparation bystring(“-”),我能够在pod名称之前删除-字符,使其看起来更干净。这是因为在pod名称列表的每一行上都使用了锁文件。注意,这里我们使用的是pathForResource而不是URLForResource。

    do {
        if let path = NSBundle.mainBundle().pathForResource("/lockfilefolder/Podfile", ofType: "lock"){
            let data = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
            let lockFileData = data.componentsSeparatedByString("-")
            print("Current version: \(lockFileData[6])")
        }
    } catch let err as NSError {
        print(err)
    }

/* Prints */
// Current version: - Firebase/Core (3.3.0):

This next version we print two lines. We are using the data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) method. It gets verbose to remove the - in this case therefore it's not worth it.

下一个版本我们打印两行。我们使用的是data. componentsdisatedbycharactersinset (NSCharacterSet.newlineCharacterSet())方法。删除-在这种情况下会变得冗长,因此不值得这样做。

let url = NSBundle.mainBundle().URLForResource("/lockfilefolder/Podfile", withExtension: "lock")!
    let data = try! String(contentsOfURL: url, encoding: NSUTF8StringEncoding)
    let lockFileData = data.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet())
    print(lockFileData[72])
    print(lockFileData[6])

/* Prints */
// COCOAPODS: 0.39.0
// - Firebase/Core (3.3.0):