Compare commits

...

6 Commits

Author SHA1 Message Date
Bartosz Polaczyk e6b56024b9 Merge pull request #90 from polac24/bump-cocoapods-007
Bump CocoaPods plugin version
2022-02-21 10:25:45 +01:00
Bartosz Polaczyk 8c34a31110 Bump CocoaPods plugin version 2022-02-19 08:54:22 +01:00
Bartosz Polaczyk f7c32d6e80 Merge pull request #87 from polac24/20220217-custom-mappings-envs
Customize rewritting dependency paths
2022-02-18 11:30:51 +01:00
Bartosz Polaczyk b3a16ae5d0 Merge pull request #88 from polac24/20220217-decorate-logs
Decorate logs with a target name
2022-02-18 11:30:35 +01:00
Bartosz Polaczyk d013fe4c81 Decorate logs with a target name 2022-02-17 20:35:13 +01:00
Bartosz Polaczyk 4aefee078e Customize rewritting paths 2022-02-17 20:13:25 +01:00
10 changed files with 73 additions and 25 deletions
+1
View File
@@ -313,6 +313,7 @@ _Note that for the `producer` mode, the prebuild build phase and `xccc`, `xcld`,
| `out_of_band_mappings` | A dictionary of files path remapping that should be applied to make it absolute path agnostic on a list of dependencies. Useful if a project refers files out of repo root, either compilation files or precompiled dependencies. Keys represent generic replacement and values are substrings that should be replaced. Example: for mapping `["COOL_LIBRARY": "/CoolLibrary"]` `/CoolLibrary/main.swift`will be represented as `$(COOL_LIBRARY)/main.swift`). Warning: remapping order is not-deterministic so avoid remappings with multiple matchings. | `[:]` | ⬜️ |
| `disable_certificate_verification` | A Boolean value that opts-in SSL certificate validation is disabled | `false` | ⬜️ |
| `disable_vfs_overlay` | A feature flag to disable virtual file system overlay support (temporary) | `false` | ⬜️ |
| `custom_rewrite_envs` | A list of extra ENVs that should be used as placeholders in the dependency list. ENV rewrite process is optimistic - does nothing if an ENV is not defined in the pre/postbuild process. | `[]` | ⬜️ |
## Backend cache server
@@ -119,7 +119,7 @@ extension PostbuildContext {
dSYMPath = try env.readEnv(key: "DWARF_DSYM_FOLDER_PATH")
.appendingPathComponent(env.readEnv(key: "DWARF_DSYM_FILE_NAME"))
builtProductsDir = try env.readEnv(key: "BUILT_PRODUCTS_DIR")
if let contentsFolderPath = env.readEnv(key: "CONTENTS_FOLDER_PATH") {
if let contentsFolderPath: String = env.readEnv(key: "CONTENTS_FOLDER_PATH") {
bundleDir = productsDir.appendingPathComponent(contentsFolderPath)
} else {
bundleDir = nil
@@ -35,6 +35,7 @@ public class XCPostbuild {
do {
config = try XCRemoteCacheConfigReader(env: env, fileManager: fileManager).readConfiguration()
context = try PostbuildContext(config, env: env)
updateProcessTag(context.targetName)
let counterFactory: FileStatsCoordinator.CountersFactory = { file, count in
ExclusiveFileCounter(ExclusiveFile(file, mode: .override), countersCount: count)
}
@@ -66,8 +67,8 @@ public class XCPostbuild {
// Initialize dependencies
let primaryGitBranch = GitBranch(repoLocation: config.primaryRepo, branch: config.primaryBranch)
let gitClient = GitClientImpl(repoRoot: config.repoRoot, primary: primaryGitBranch, shell: shellGetStdout)
let envsRemapper = try StringDependenciesRemapperFactory().build(
orderKeys: DependenciesMapping.rewrittenEnvs,
let envsRemapper = try PathDependenciesRemapperFactory().build(
orderKeys: DependenciesMapping.rewrittenEnvs + config.customRewriteEnvs,
envs: env,
customMappings: config.outOfBandMappings
)
@@ -31,6 +31,7 @@ public class XCPrebuild {
do {
config = try XCRemoteCacheConfigReader(env: env, fileManager: fileManager).readConfiguration()
context = try PrebuildContext(config, env: env)
updateProcessTag(context.targetName)
} catch {
// Fatal error:
exit(1, "FATAL: Prebuild initialization failed with error: \(error)")
@@ -115,8 +116,8 @@ public class XCPrebuild {
)
let client: NetworkClient = config.disableHttpCache ? networkClient : cacheNetworkClient
let remoteNetworkClient = RemoteNetworkClientImpl(client, urlBuilder)
let envsRemapper = try StringDependenciesRemapperFactory().build(
orderKeys: DependenciesMapping.rewrittenEnvs,
let envsRemapper = try PathDependenciesRemapperFactory().build(
orderKeys: DependenciesMapping.rewrittenEnvs + config.customRewriteEnvs,
envs: env,
customMappings: config.outOfBandMappings
)
@@ -133,6 +133,9 @@ public struct XCRemoteCacheConfig: Encodable {
var disableCertificateVerification: Bool = false
/// A feature flag to disable virtual file system overlay support (temporary)
var disableVFSOverlay: Bool = false
/// A list of extra ENVs that should be used as placeholders in the dependency list.
/// ENV rewrite process is optimistic - does nothing if an ENV is not defined in the pre/postbuild process.
var customRewriteEnvs: [String] = []
}
extension XCRemoteCacheConfig {
@@ -186,6 +189,7 @@ extension XCRemoteCacheConfig {
merge.outOfBandMappings = scheme.outOfBandMappings ?? outOfBandMappings
merge.disableCertificateVerification = scheme.disableCertificateVerification ?? disableCertificateVerification
merge.disableVFSOverlay = scheme.disableVFSOverlay ?? disableVFSOverlay
merge.customRewriteEnvs = scheme.customRewriteEnvs ?? customRewriteEnvs
return merge
}
@@ -248,6 +252,7 @@ struct ConfigFileScheme: Decodable {
let outOfBandMappings: [String: String]?
let disableCertificateVerification: Bool?
let disableVFSOverlay: Bool?
let customRewriteEnvs: [String]?
// Yams library doesn't support encoding strategy, see https://github.com/jpsim/Yams/issues/84
enum CodingKeys: String, CodingKey {
@@ -293,6 +298,7 @@ struct ConfigFileScheme: Decodable {
case outOfBandMappings = "out_of_band_mappings"
case disableCertificateVerification = "disable_certificate_verification"
case disableVFSOverlay = "disable_vfs_overlay"
case customRewriteEnvs = "custom_rewrite_envs"
}
}
@@ -19,24 +19,28 @@
import Foundation
enum StringDependenciesRemapperFactoryError: Error {
enum PathDependenciesRemapperFactoryError: Error {
/// Remapping keys are duplicated and can lead to undetermined results
case mappingKeyDuplication
}
class StringDependenciesRemapperFactory {
class PathDependenciesRemapperFactory {
func build(
orderKeys: [String],
envs: [String: String],
customMappings: [String: String]
) throws -> StringDependenciesRemapper {
let mappingMap = try envs.merging(customMappings) { envValue, outOfBandMapping in
throw StringDependenciesRemapperFactoryError.mappingKeyDuplication
throw PathDependenciesRemapperFactoryError.mappingKeyDuplication
}
let mappingOrderKeys = orderKeys + customMappings.keys
let mappings: [StringDependenciesRemapper.Mapping] = try mappingOrderKeys.map { key in
let localValue: String = try mappingMap.readEnv(key: key)
return StringDependenciesRemapper.Mapping(generic: "$(\(key))", local: localValue)
let mappings: [StringDependenciesRemapper.Mapping] = mappingOrderKeys.compactMap { key in
guard let localURL: URL = mappingMap.readEnv(key: key) else {
debugLog("\(key) ENV to map a dependency is not defined")
return nil
}
infoLog("Found url to remapp: \(localURL). Remapping: \(localURL.standardized.path)")
return StringDependenciesRemapper.Mapping(generic: "$(\(key))", local: localURL.standardized.path)
}
return StringDependenciesRemapper(mappings: mappings)
}
+13 -7
View File
@@ -22,34 +22,36 @@ import Foundation
import os.log
private var processTag: String = ""
public func exit(_ exitCode: Int32, _ message: String) -> Never {
os_log("%{public}@", log: OSLog.default, type: .error, message)
os_log("%{public}@%{public}@", log: OSLog.default, type: .error, processTag, message)
printError(errorMessage: message)
exit(exitCode)
}
func defaultLog(_ message: String) {
os_log("%{public}@", log: OSLog.default, type: .default, message)
os_log("%{public}@%{public}@", log: OSLog.default, type: .default, processTag, message)
}
func errorLog(_ message: String) {
os_log("%{public}@", log: OSLog.default, type: .error, message)
os_log("%{public}@%{public}@", log: OSLog.default, type: .error, processTag, message)
}
func infoLog(_ message: String) {
os_log("%{public}@", log: OSLog.default, type: .info, message)
os_log("%{public}@%{public}@", log: OSLog.default, type: .info, processTag, message)
}
func debugLog(_ message: String) {
os_log("%{public}@", log: OSLog.default, type: .debug, message)
os_log("%{public}@%{public}@", log: OSLog.default, type: .debug, processTag, message)
}
func printError(errorMessage: String) {
fputs("error: \(errorMessage)\n", stderr)
fputs("error: \(processTag)\(errorMessage)\n", stderr)
}
func printWarning(_ message: String) {
print("warning: \(message)")
print("warning: \(processTag)\(message)")
}
/// Prints a message to the user. It shows in Xcode (if applies) or console output
@@ -57,3 +59,7 @@ func printWarning(_ message: String) {
func printToUser(_ message: String) {
print("[RC] \(message)")
}
func updateProcessTag(_ tag: String) {
processTag = "(\(tag)) "
}
+8 -1
View File
@@ -24,8 +24,15 @@ enum EnvironmentError: Error {
}
extension Dictionary where Key == String, Value == String {
func readEnv(key: String) throws -> URL {
func readEnv(key: String) -> URL? {
guard let value = self[key].map(URL.init(fileURLWithPath:)) else {
return nil
}
return value
}
func readEnv(key: String) throws -> URL {
guard let value: URL = readEnv(key: key) else {
throw EnvironmentError.missingEnv(key)
}
return value
@@ -20,11 +20,11 @@
@testable import XCRemoteCache
import XCTest
class StringDependenciesRemapperFactoryTests: XCTestCase {
private var factory: StringDependenciesRemapperFactory!
class PathDependenciesRemapperFactoryTests: XCTestCase {
private var factory: PathDependenciesRemapperFactory!
override func setUp() {
factory = StringDependenciesRemapperFactory()
factory = PathDependenciesRemapperFactory()
}
func testMappingsFromEnvMaps() throws {
@@ -38,8 +38,30 @@ class StringDependenciesRemapperFactoryTests: XCTestCase {
XCTAssertEqual(localPaths, ["/tmp/root/some.swift"])
}
func testInvalidMappingsFromEnvFails() throws {
XCTAssertThrowsError(
func testMappingsGenericWhenMappingHasParentDir() throws {
let remapper = try factory.build(
orderKeys: ["SRC_ROOT"],
envs: ["SRC_ROOT": "/tmp/root/extra/.."],
customMappings: [:]
)
let localPaths = try remapper.replace(genericPaths: ["$(SRC_ROOT)/some.swift"])
XCTAssertEqual(localPaths, ["/tmp/root/some.swift"])
}
func testMappingsLocalWhenMappingHasParentDir() throws {
let remapper = try factory.build(
orderKeys: ["SRC_ROOT"],
envs: ["SRC_ROOT": "/tmp/root/excessive/.."],
customMappings: [:]
)
let localPaths = try remapper.replace(localPaths: ["/tmp/root/some.swift"])
XCTAssertEqual(localPaths, ["$(SRC_ROOT)/some.swift"])
}
func testMissingEnvIsSkipped() throws {
XCTAssertNoThrow(
try factory.build(
orderKeys: ["SRC_ROOT"],
envs: ["NO_SRC_ROOT": ""],
@@ -13,5 +13,5 @@
# limitations under the License.
module CocoapodsXcremotecache
VERSION = "0.0.6"
VERSION = "0.0.7"
end