Help us understand the problem. What is going on with this article?

Swiftで開発環境の情報を取得する

概要

  • 下記の記事を読んで、開発環境を書こうというモチベーションが起きました。
  • ただ毎回バージョンを確認するのが手間なので、自動で情報を取得できればと思いちょんプロを書きました。
    • (皆さん何かしらの方法で既に楽してそうではあります)
  • バッジの形式をカスタムしたい場合はfunc badgeText(withLabel label: String, message: String, color: String = "brightgreen") -> Stringを変更してください。

呼び出し例

guard let environmentInfo = EnvironmentInfoController.shared.getEnvironmentInfoString() else { return }
print(environmentInfo)
  • 出力結果

macOS: Version 10.15.4 (Build 19E287) Swift: 5.0.2 Xcode: 11.6

![macOS: Version 10.15.4 (Build 19E287)](https://img.shields.io/badge/macOS-Version 10.15.4 (Build 19E287)-brightgreen) ![Swift: 5.0.2](https://img.shields.io/badge/Swift-5.0.2-brightgreen) ![Xcode: 11.6](https://img.shields.io/badge/Xcode-11.6-brightgreen)

開発環境の取得クラス

import Foundation

struct EnvironmentInfoController {
    static let shared = EnvironmentInfoController()

    func getEnvironmentInfoString() -> String? {

        guard let swiftVersion = getSwiftVersion() else { return nil }
        guard let osVersion    = getOsVersion()    else { return nil }
        guard let xcodeVersion = getXcodeVersion() else { return nil }

        let environmentText = String(format: "%@ %@ %@",
                                     badgeText(withLabel: "macOS", message: osVersion),
                                     badgeText(withLabel: "Swift", message: swiftVersion),
                                     badgeText(withLabel: "Xcode", message: xcodeVersion)
        )

        /*
        // シンプルにテキストで表示したい場合
        var environmentText = ""
        environmentText.append("OS: macOS \(osVersion)")
        environmentText.append("\nSwift: \(swiftVersion)")
        environmentText.append("\nXcode: \(xcodeVersion)")
        */

        return environmentText
    }

    // MARK: - Helper Methods

    func badgeText(withLabel label: String, message: String, color: String = "brightgreen") -> String {
        return "![\(label): \(message)](https://img.shields.io/badge/\(label)-\(message)-\(color))"
    }

    // MARK: - Get EnvironmentInfo Methods

    private func getOsVersion() -> String? {
        let osVersion = ProcessInfo.processInfo.operatingSystemVersionString  // e.g. "Version 10.15.4 (Build 19E287)"
        return osVersion

        //        let osVersion = ProcessInfo.processInfo.operatingSystemVersion
        //        return "\(osVersion.majorVersion).\(osVersion.minorVersion).\(osVersion.patchVersion)"
    }

    private func getSwiftVersion() -> String? {
        #if swift(>=5.49)
        return "5.4.9"
        #elseif swift(>=5.48)
        return "5.4.8"
        #elseif swift(>=5.47)
        return "5.4.7"
        #elseif swift(>=5.46)
        return "5.4.6"
        #elseif swift(>=5.45)
        return "5.4.5"
        #elseif swift(>=5.44)
        return "5.4.4"
        #elseif swift(>=5.43)
        return "5.4.3"
        #elseif swift(>=5.42)
        return "5.4.2"
        #elseif swift(>=5.41)
        return "5.4.1"
        #elseif swift(>=5.40)
        return "5.4.0"
        #elseif swift(>=5.39)
        return "5.3.9"
        #elseif swift(>=5.38)
        return "5.3.8"
        #elseif swift(>=5.37)
        return "5.3.7"
        #elseif swift(>=5.36)
        return "5.3.6"
        #elseif swift(>=5.35)
        return "5.3.5"
        #elseif swift(>=5.34)
        return "5.3.4"
        #elseif swift(>=5.33)
        return "5.3.3"
        #elseif swift(>=5.32)
        return "5.3.2"
        #elseif swift(>=5.31)
        return "5.3.1"
        #elseif swift(>=5.30)
        return "5.3.0"
        #elseif swift(>=5.29)
        return "5.2.9"
        #elseif swift(>=5.28)
        return "5.2.8"
        #elseif swift(>=5.27)
        return "5.2.7"
        #elseif swift(>=5.26)
        return "5.2.6"
        #elseif swift(>=5.25)
        return "5.2.5"
        #elseif swift(>=5.24)
        return "5.2.4"
        #elseif swift(>=5.23)
        return "5.2.3"
        #elseif swift(>=5.22)
        return "5.2.2"
        #elseif swift(>=5.21)
        return "5.2.1"
        #elseif swift(>=5.20)
        return "5.2.0"
        #elseif swift(>=5.19)
        return "5.1.9"
        #elseif swift(>=5.18)
        return "5.1.8"
        #elseif swift(>=5.17)
        return "5.1.7"
        #elseif swift(>=5.16)
        return "5.1.6"
        #elseif swift(>=5.15)
        return "5.1.5"
        #elseif swift(>=5.14)
        return "5.1.4"
        #elseif swift(>=5.13)
        return "5.1.3"
        #elseif swift(>=5.12)
        return "5.1.2"
        #elseif swift(>=5.11)
        return "5.1.1"
        #elseif swift(>=5.10)
        return "5.1.0"
        #elseif swift(>=5.09)
        return "5.0.9"
        #elseif swift(>=5.08)
        return "5.0.8"
        #elseif swift(>=5.07)
        return "5.0.7"
        #elseif swift(>=5.06)
        return "5.0.6"
        #elseif swift(>=5.05)
        return "5.0.5"
        #elseif swift(>=5.04)
        return "5.0.4"
        #elseif swift(>=5.03)
        return "5.0.3"
        #elseif swift(>=5.02)
        return "5.0.2"
        #elseif swift(>=5.01)
        return "5.0.1"
        #elseif swift(>=5.00)
        return "5.0.0"
        #elseif swift(>=4.49)
        return "4.4.9"
        #elseif swift(>=4.48)
        return "4.4.8"
        #elseif swift(>=4.47)
        return "4.4.7"
        #elseif swift(>=4.46)
        return "4.4.6"
        #elseif swift(>=4.45)
        return "4.4.5"
        #elseif swift(>=4.44)
        return "4.4.4"
        #elseif swift(>=4.43)
        return "4.4.3"
        #elseif swift(>=4.42)
        return "4.4.2"
        #elseif swift(>=4.41)
        return "4.4.1"
        #elseif swift(>=4.40)
        return "4.4.0"
        #elseif swift(>=4.39)
        return "4.3.9"
        #elseif swift(>=4.38)
        return "4.3.8"
        #elseif swift(>=4.37)
        return "4.3.7"
        #elseif swift(>=4.36)
        return "4.3.6"
        #elseif swift(>=4.35)
        return "4.3.5"
        #elseif swift(>=4.34)
        return "4.3.4"
        #elseif swift(>=4.33)
        return "4.3.3"
        #elseif swift(>=4.32)
        return "4.3.2"
        #elseif swift(>=4.31)
        return "4.3.1"
        #elseif swift(>=4.30)
        return "4.3.0"
        #elseif swift(>=4.29)
        return "4.2.9"
        #elseif swift(>=4.28)
        return "4.2.8"
        #elseif swift(>=4.27)
        return "4.2.7"
        #elseif swift(>=4.26)
        return "4.2.6"
        #elseif swift(>=4.25)
        return "4.2.5"
        #elseif swift(>=4.24)
        return "4.2.4"
        #elseif swift(>=4.23)
        return "4.2.3"
        #elseif swift(>=4.22)
        return "4.2.2"
        #elseif swift(>=4.21)
        return "4.2.1"
        #elseif swift(>=4.20)
        return "4.2.0"
        #elseif swift(>=4.19)
        return "4.1.9"
        #elseif swift(>=4.18)
        return "4.1.8"
        #elseif swift(>=4.17)
        return "4.1.7"
        #elseif swift(>=4.16)
        return "4.1.6"
        #elseif swift(>=4.15)
        return "4.1.5"
        #elseif swift(>=4.14)
        return "4.1.4"
        #elseif swift(>=4.13)
        return "4.1.3"
        #elseif swift(>=4.12)
        return "4.1.2"
        #elseif swift(>=4.11)
        return "4.1.1"
        #elseif swift(>=4.10)
        return "4.1.0"
        #elseif swift(>=4.09)
        return "4.0.9"
        #elseif swift(>=4.08)
        return "4.0.8"
        #elseif swift(>=4.07)
        return "4.0.7"
        #elseif swift(>=4.06)
        return "4.0.6"
        #elseif swift(>=4.05)
        return "4.0.5"
        #elseif swift(>=4.04)
        return "4.0.4"
        #elseif swift(>=4.03)
        return "4.0.3"
        #elseif swift(>=4.02)
        return "4.0.2"
        #elseif swift(>=4.01)
        return "4.0.1"
        #elseif swift(>=4.00)
        return "4.0.0"
        #elseif swift(>=3.49)
        return "3.4.9"
        #elseif swift(>=3.48)
        return "3.4.8"
        #elseif swift(>=3.47)
        return "3.4.7"
        #elseif swift(>=3.46)
        return "3.4.6"
        #elseif swift(>=3.45)
        return "3.4.5"
        #elseif swift(>=3.44)
        return "3.4.4"
        #elseif swift(>=3.43)
        return "3.4.3"
        #elseif swift(>=3.42)
        return "3.4.2"
        #elseif swift(>=3.41)
        return "3.4.1"
        #elseif swift(>=3.40)
        return "3.4.0"
        #elseif swift(>=3.39)
        return "3.3.9"
        #elseif swift(>=3.38)
        return "3.3.8"
        #elseif swift(>=3.37)
        return "3.3.7"
        #elseif swift(>=3.36)
        return "3.3.6"
        #elseif swift(>=3.35)
        return "3.3.5"
        #elseif swift(>=3.34)
        return "3.3.4"
        #elseif swift(>=3.33)
        return "3.3.3"
        #elseif swift(>=3.32)
        return "3.3.2"
        #elseif swift(>=3.31)
        return "3.3.1"
        #elseif swift(>=3.30)
        return "3.3.0"
        #elseif swift(>=3.29)
        return "3.2.9"
        #elseif swift(>=3.28)
        return "3.2.8"
        #elseif swift(>=3.27)
        return "3.2.7"
        #elseif swift(>=3.26)
        return "3.2.6"
        #elseif swift(>=3.25)
        return "3.2.5"
        #elseif swift(>=3.24)
        return "3.2.4"
        #elseif swift(>=3.23)
        return "3.2.3"
        #elseif swift(>=3.22)
        return "3.2.2"
        #elseif swift(>=3.21)
        return "3.2.1"
        #elseif swift(>=3.20)
        return "3.2.0"
        #elseif swift(>=3.19)
        return "3.1.9"
        #elseif swift(>=3.18)
        return "3.1.8"
        #elseif swift(>=3.17)
        return "3.1.7"
        #elseif swift(>=3.16)
        return "3.1.6"
        #elseif swift(>=3.15)
        return "3.1.5"
        #elseif swift(>=3.14)
        return "3.1.4"
        #elseif swift(>=3.13)
        return "3.1.3"
        #elseif swift(>=3.12)
        return "3.1.2"
        #elseif swift(>=3.11)
        return "3.1.1"
        #elseif swift(>=3.10)
        return "3.1.0"
        #elseif swift(>=3.09)
        return "3.0.9"
        #elseif swift(>=3.08)
        return "3.0.8"
        #elseif swift(>=3.07)
        return "3.0.7"
        #elseif swift(>=3.06)
        return "3.0.6"
        #elseif swift(>=3.05)
        return "3.0.5"
        #elseif swift(>=3.04)
        return "3.0.4"
        #elseif swift(>=3.03)
        return "3.0.3"
        #elseif swift(>=3.02)
        return "3.0.2"
        #elseif swift(>=3.01)
        return "3.0.1"
        #elseif swift(>=3.00)
        return "3.0.0"
        #elseif swift(>=2.49)
        return "2.4.9"
        #elseif swift(>=2.48)
        return "2.4.8"
        #elseif swift(>=2.47)
        return "2.4.7"
        #elseif swift(>=2.46)
        return "2.4.6"
        #elseif swift(>=2.45)
        return "2.4.5"
        #elseif swift(>=2.44)
        return "2.4.4"
        #elseif swift(>=2.43)
        return "2.4.3"
        #elseif swift(>=2.42)
        return "2.4.2"
        #elseif swift(>=2.41)
        return "2.4.1"
        #elseif swift(>=2.40)
        return "2.4.0"
        #elseif swift(>=2.39)
        return "2.3.9"
        #elseif swift(>=2.38)
        return "2.3.8"
        #elseif swift(>=2.37)
        return "2.3.7"
        #elseif swift(>=2.36)
        return "2.3.6"
        #elseif swift(>=2.35)
        return "2.3.5"
        #elseif swift(>=2.34)
        return "2.3.4"
        #elseif swift(>=2.33)
        return "2.3.3"
        #elseif swift(>=2.32)
        return "2.3.2"
        #elseif swift(>=2.31)
        return "2.3.1"
        #elseif swift(>=2.30)
        return "2.3.0"
        #elseif swift(>=2.29)
        return "2.2.9"
        #elseif swift(>=2.28)
        return "2.2.8"
        #elseif swift(>=2.27)
        return "2.2.7"
        #elseif swift(>=2.26)
        return "2.2.6"
        #elseif swift(>=2.25)
        return "2.2.5"
        #elseif swift(>=2.24)
        return "2.2.4"
        #elseif swift(>=2.23)
        return "2.2.3"
        #elseif swift(>=2.22)
        return "2.2.2"
        #elseif swift(>=2.21)
        return "2.2.1"
        #elseif swift(>=2.20)
        return "2.2.0"
        #elseif swift(>=2.19)
        return "2.1.9"
        #elseif swift(>=2.18)
        return "2.1.8"
        #elseif swift(>=2.17)
        return "2.1.7"
        #elseif swift(>=2.16)
        return "2.1.6"
        #elseif swift(>=2.15)
        return "2.1.5"
        #elseif swift(>=2.14)
        return "2.1.4"
        #elseif swift(>=2.13)
        return "2.1.3"
        #elseif swift(>=2.12)
        return "2.1.2"
        #elseif swift(>=2.11)
        return "2.1.1"
        #elseif swift(>=2.10)
        return "2.1.0"
        #elseif swift(>=2.09)
        return "2.0.9"
        #elseif swift(>=2.08)
        return "2.0.8"
        #elseif swift(>=2.07)
        return "2.0.7"
        #elseif swift(>=2.06)
        return "2.0.6"
        #elseif swift(>=2.05)
        return "2.0.5"
        #elseif swift(>=2.04)
        return "2.0.4"
        #elseif swift(>=2.03)
        return "2.0.3"
        #elseif swift(>=2.02)
        return "2.0.2"
        #elseif swift(>=2.01)
        return "2.0.1"
        #elseif swift(>=2.00)
        return "2.0.0"
        #elseif swift(>=1.49)
        return "1.4.9"
        #elseif swift(>=1.48)
        return "1.4.8"
        #elseif swift(>=1.47)
        return "1.4.7"
        #elseif swift(>=1.46)
        return "1.4.6"
        #elseif swift(>=1.45)
        return "1.4.5"
        #elseif swift(>=1.44)
        return "1.4.4"
        #elseif swift(>=1.43)
        return "1.4.3"
        #elseif swift(>=1.42)
        return "1.4.2"
        #elseif swift(>=1.41)
        return "1.4.1"
        #elseif swift(>=1.40)
        return "1.4.0"
        #elseif swift(>=1.39)
        return "1.3.9"
        #elseif swift(>=1.38)
        return "1.3.8"
        #elseif swift(>=1.37)
        return "1.3.7"
        #elseif swift(>=1.36)
        return "1.3.6"
        #elseif swift(>=1.35)
        return "1.3.5"
        #elseif swift(>=1.34)
        return "1.3.4"
        #elseif swift(>=1.33)
        return "1.3.3"
        #elseif swift(>=1.32)
        return "1.3.2"
        #elseif swift(>=1.31)
        return "1.3.1"
        #elseif swift(>=1.30)
        return "1.3.0"
        #elseif swift(>=1.29)
        return "1.2.9"
        #elseif swift(>=1.28)
        return "1.2.8"
        #elseif swift(>=1.27)
        return "1.2.7"
        #elseif swift(>=1.26)
        return "1.2.6"
        #elseif swift(>=1.25)
        return "1.2.5"
        #elseif swift(>=1.24)
        return "1.2.4"
        #elseif swift(>=1.23)
        return "1.2.3"
        #elseif swift(>=1.22)
        return "1.2.2"
        #elseif swift(>=1.21)
        return "1.2.1"
        #elseif swift(>=1.20)
        return "1.2.0"
        #elseif swift(>=1.19)
        return "1.1.9"
        #elseif swift(>=1.18)
        return "1.1.8"
        #elseif swift(>=1.17)
        return "1.1.7"
        #elseif swift(>=1.16)
        return "1.1.6"
        #elseif swift(>=1.15)
        return "1.1.5"
        #elseif swift(>=1.14)
        return "1.1.4"
        #elseif swift(>=1.13)
        return "1.1.3"
        #elseif swift(>=1.12)
        return "1.1.2"
        #elseif swift(>=1.11)
        return "1.1.1"
        #elseif swift(>=1.10)
        return "1.1.0"
        #elseif swift(>=1.09)
        return "1.0.9"
        #elseif swift(>=1.08)
        return "1.0.8"
        #elseif swift(>=1.07)
        return "1.0.7"
        #elseif swift(>=1.06)
        return "1.0.6"
        #elseif swift(>=1.05)
        return "1.0.5"
        #elseif swift(>=1.04)
        return "1.0.4"
        #elseif swift(>=1.03)
        return "1.0.3"
        #elseif swift(>=1.02)
        return "1.0.2"
        #elseif swift(>=1.01)
        return "1.0.1"
        #elseif swift(>=1.00)
        return "1.0.0"
        #endif

        return nil
    }

    private func getXcodeVersion() -> String? {
        guard let xcode = Bundle(path: "/Applications/Xcode.app") else { return nil }
        guard let xcodeVersion = xcode.infoDictionary?["CFBundleShortVersionString"] as? String else { return nil }

        return xcodeVersion
    }

    // MARK: - DEBUG Methods

    private func printAllSwiftVersion() -> Double? {
        for majorVerion in (1...5).reversed() {
            for minorVersion in (0..<5).reversed() {
                for buildVersion in (0..<10).reversed() {
                    print("#elseif swift(>=\(majorVerion).\(minorVersion)\(buildVersion))")
                    print("return \"\(majorVerion).\(minorVersion).\(buildVersion)\"")
                }
            }
        }

        return nil
    }
}

その他

  • 外部ライブラリを使用していてそのバージョンを取得したい場合は、下記のライブラリのロジックを見れば参考になるでしょうか。(詳しく見れてないです)
IKEH
macOS Engineer Objective-C / Swift / Python
https://ikeh1024.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした