BuildEvironment (构建环境)

swift-version Mac OSMac OSLinuxWindows GitHub License

将您的密钥和秘密信息从源代码中分离出来。专为 SPM 和 Xcode 项目设计。

Latest Release

描述

BuildEvironment 项目允许您将密钥和秘密信息存储在版本控制仓库之外。 秘密信息可以存储在 .env 文件或环境变量中。 简单、轻量且实用的解决方案,与平台无关。

使用方法

使用 Swift Package

.package(url: "https://github.com/DimaRU/BuildEnvironment.git", from: "1.0.0"),
plugins: [ .plugin(name: "BuildEnvPlugin", package: "BuildEnvironment") ]
platforms: [.macOS(.v11), .iOS(.v14), .watchOS(.v7), .tvOS(.v14)],

使用 Xcode 项目

.env 和/或 buildenv.config 文件添加到项目的根目录。

必须添加其中一个文件。

如果只有 .env 文件存在,将会生成带有默认设置的代码

注意

.env 文件不应提交到 git,因为它包含您的秘密信息。

如果您的项目包含 buildenv.config

buildenv.config 不包含秘密信息,可以添加到版本控制仓库中。

示例

只有一个 .env 文件

# Sample api key
apiKey = "123456"

生成的代码

// Code generated from .env file 
// Don't edit! All changes will be lost.

public enum BuildEnvironment {
    public static let apiKey: String = "123456"
}

同时有 .envbuildenv.config 文件

# Sample BuildEnvPlugin config file

# Generated enum name. Default: BuildEnvironment
name: BuildEnvironment
# Generated enum access level.
# Must be one of: public, package, internal. Default: public
access: internal
# Obfuscate data by encode: yes/no. Default: no
encode: yes

# Environment variable list.
# Format: swift_variable_name=$environment_variable_name
userName= $USER
homeDir= $HOME

生成的代码

// Code generated from .env file 
// Don't edit! All changes will be lost.

enum BuildEnvironment {
    static let userName: String = {
        let encrypted: [UInt8] = [191, 121, 218, 23, 127, 221, 219, 20, 179, 99, 13, 164]
        let count = encrypted.count / 2
        return String(unsafeUninitializedCapacity: count) { ptr in
            (0..<count).forEach { ptr[$0] = encrypted[$0] ^ encrypted[$0 + count] }
            return count
        }
    }()
    static let homeDir: String = {
        let encrypted: [UInt8] = [245, 33, 241, 173, 4, 190, 7, 163, 71, 155, 203, 189, 211, 218, 116, 130, 200, 118, 205, 40, 199, 42, 242, 191, 207, 170]
        let count = encrypted.count / 2
        return String(unsafeUninitializedCapacity: count) { ptr in
            (0..<count).forEach { ptr[$0] = encrypted[$0] ^ encrypted[$0 + count] }
            return count
        }
    }()
    static let apiKey: String = {
        let encrypted: [UInt8] = [41, 83, 240, 172, 236, 228, 24, 97, 195, 152, 217, 210]
        let count = encrypted.count / 2
        return String(unsafeUninitializedCapacity: count) { ptr in
            (0..<count).forEach { ptr[$0] = encrypted[$0] ^ encrypted[$0 + count] }
            return count
        }
    }()
}

示例项目

贡献

欢迎提交 Pull Request。 对于重大更改,请先开设一个 Issue 讨论您想要更改的内容。

许可

该项目基于 MIT 许可 - 详情请参阅 LICENSE 文件