Best Mockingbird code snippet using Generator.getBuildEnvironment
Generator.swift
Source:Generator.swift
...90 return project91 }92 93 // Parsing Xcode projects can be slow, so lazily get implicit build environments.94 func getBuildEnvironment() -> [String: Any] {95 let project = try? getProject(config.projectPath)96 switch project {97 case .xcode(let xcodeproj): return xcodeproj.implicitBuildEnvironment98 case .json, .none: return [:]99 }100 }101 102 var projectHashes: [Path: String] = [:]103 func getProjectHash(_ projectPath: Path) -> String? {104 if let projectHash = projectHashes[projectPath.absolute()] { return projectHash }105 let filePath = projectPath.extension == "xcodeproj"106 ? projectPath.glob("*.pbxproj").sorted().first107 : projectPath108 let projectHash = try? filePath?.read().hash()109 self.projectHashes[projectPath.absolute()] = projectHash110 return projectHash111 }112 113 // Get cached source target metadata.114 func getCachedSourceTarget(targetName: String) -> TargetType? {115 guard !config.disableCache,116 let projectHash = getProjectHash(config.projectPath),117 let cachedTarget = findCachedSourceTarget(for: targetName,118 cliVersion: cliVersion,119 projectHash: projectHash,120 configHash: configHash,121 cacheDirectory: sourceTargetCacheDirectory,122 sourceRoot: config.sourceRoot)123 else { return nil }124 return .sourceTarget(cachedTarget)125 }126 127 // Get cached test target metadata.128 func getCachedTestTarget(targetName: String) -> TargetType? {129 guard config.pruningMethod != .disable,130 let cacheDirectory = testTargetCacheDirectory,131 let testProjectPath = config.environmentProjectFilePath,132 let testSourceRoot = config.environmentSourceRoot,133 let projectHash = getProjectHash(testProjectPath),134 let cachedTarget = findCachedTestTarget(for: targetName,135 projectHash: projectHash,136 cliVersion: cliVersion,137 cacheDirectory: cacheDirectory,138 sourceRoot: testSourceRoot)139 else { return nil }140 return .testTarget(cachedTarget)141 }142 143 func generate() throws {144 if !config.outputPaths.isEmpty && config.inputTargetNames.count != config.outputPaths.count {145 throw Error.mismatchedInputsAndOutputs(inputCount: config.inputTargetNames.count,146 outputCount: config.outputPaths.count)147 }148 149 if config.supportPath == nil {150 logWarning("No supporting source files specified which can result in missing mocks")151 }152 153 // Resolve target names to concrete Xcode project targets.154 let targets = try config.inputTargetNames.compactMap({ targetName throws -> TargetType? in155 return try Generator.resolveTarget(targetName: targetName,156 projectPath: config.projectPath,157 getCachedTarget: getCachedSourceTarget,158 getProject: getProject)159 })160 161 // Resolve unspecified output paths to the default mock file output destination.162 let outputPaths: [Path] = try {163 if !config.outputPaths.isEmpty {164 return config.outputPaths165 }166 return try targets.map({ target throws -> Path in167 let outputDir = config.outputDir ?? config.sourceRoot.mocksDirectory168 try outputDir.mkpath()169 return Generator.defaultOutputPath(for: target,170 outputDir: outputDir,171 environment: getBuildEnvironment)172 })173 }()174 175 let queue = OperationQueue.createForActiveProcessors()176 177 // Create operations to find used mock types in tests.178 let pruningPipeline = config.pruningMethod == .disable ? nil :179 PruningPipeline(config: config,180 getCachedTarget: getCachedTestTarget,181 getProject: getProject,182 environment: getBuildEnvironment)183 if let pruningOperations = pruningPipeline?.operations {184 queue.addOperations(pruningOperations, waitUntilFinished: false)185 }186 let findMockedTypesOperation = pruningPipeline?.findMockedTypesOperation187 188 // Create abstract generation pipelines from targets and output paths.189 var pipelines = [Pipeline]()190 for (target, outputPath) in zip(targets, outputPaths) {191 guard !outputPath.isDirectory else {192 throw Error.invalidOutputPath(path: outputPath)193 }194 try pipelines.append(Pipeline(inputTarget: target,195 outputPath: outputPath,196 config: config,197 findMockedTypesOperation: findMockedTypesOperation,198 environment: getBuildEnvironment))199 }200 pipelines.forEach({ queue.addOperations($0.operations, waitUntilFinished: false) })201 202 // Run the operations.203 let operationsCopy = queue.operations.compactMap({ $0 as? BasicOperation })204 queue.waitUntilAllOperationsAreFinished()205 operationsCopy.compactMap({ $0.error }).forEach({ logError($0) })206 207 // Write intermediary module cache info into project cache directory.208 if !config.disableCache {209 try time(.cacheMocks) {210 try cachePipelines(sourcePipelines: pipelines, pruningPipeline: pruningPipeline)211 }212 }213 }214 215 func cachePipelines(sourcePipelines: [Pipeline], pruningPipeline: PruningPipeline?) throws {216 guard let projectHash = getProjectHash(config.projectPath) else { return }217 218 // Cache source targets for generation.219 try sourceTargetCacheDirectory.mkpath()220 try sourcePipelines.forEach({221 try $0.cache(projectHash: projectHash,222 cliVersion: cliVersion,223 configHash: configHash,224 sourceRoot: config.sourceRoot,225 cacheDirectory: sourceTargetCacheDirectory,226 environment: getBuildEnvironment)227 })228 229 // Cache test target for thunk pruning.230 if config.pruningMethod != .disable {231 if let testTargetCacheDirectory = testTargetCacheDirectory,232 let environmentSourceRoot = config.environmentSourceRoot,233 let testProjectPath = config.environmentProjectFilePath,234 let projectHash = getProjectHash(testProjectPath) {235 try testTargetCacheDirectory.mkpath()236 try pruningPipeline?.cache(projectHash: projectHash,237 cliVersion: cliVersion,238 sourceRoot: environmentSourceRoot,239 cacheDirectory: testTargetCacheDirectory,240 environment: getBuildEnvironment)241 }242 }243 }244 245 static func defaultOutputPath(for sourceTarget: TargetType,246 testTarget: TargetType? = nil,247 outputDir: Path,248 environment: () -> [String: Any]) -> Path {249 let moduleName = sourceTarget.resolveProductModuleName(environment: environment)250 251 let prefix: String252 if let testTargetName = testTarget?.resolveProductModuleName(environment: environment),253 testTargetName != moduleName {254 prefix = testTargetName + "-"...
PBXTarget+Target.swift
Source:PBXTarget+Target.swift
...17 guard18 let configuration = testingBuildConfiguration,19 let moduleName = try? PBXTarget.resolve(20 BuildSetting("$(PRODUCT_MODULE_NAME:default=$(PRODUCT_NAME:default=$(TARGET_NAME)))"),21 from: getBuildEnvironment(configuration: configuration, environment: environment())22 )23 else {24 let fallbackModuleName = name.escapingForModuleName()25 logWarning("Unable to resolve product module name for target \(name.singleQuoted), falling back to \(fallbackModuleName.singleQuoted)")26 return fallbackModuleName27 }28 29 let escapedModuleName = moduleName.escapingForModuleName()30 log("Resolved product module name \(escapedModuleName.singleQuoted) for target \(name.singleQuoted)")31 return escapedModuleName32 }33 public func findSourceFilePaths(sourceRoot: Path) -> [Path] {34 guard let phase = buildPhases.first(where: { $0.buildPhase == .sources }) else { return [] }35 return phase.files?36 .compactMap({ try? $0.file?.fullPath(sourceRoot: sourceRoot) })37 .filter({ $0.extension == "swift" })38 .map({ $0.absolute() }) ?? []39 }40 41 /// Certain environment build settings are synthesized by Xcode and don't exist in the project42 /// file such as `TARGET_NAME`. Since the generator usually runs as part of a test target bundle43 /// (or even entirely outside of an Xcode build pipeline), we need to do some inference here.44 func getBuildEnvironment(configuration: XCBuildConfiguration,45 environment: [String: Any]) -> [String: Any] {46 let keepOld: (Any, Any) -> Any = { old, _ in old }47 48 // Explicit build settings defined in the Xcode project file.49 var buildEnvironment: [String: Any] = configuration.buildSettings50 51 // Implicit settings derived from the target info.52 buildEnvironment.merge([53 "TARGET_NAME": name,54 "TARGETNAME": name,55 ], uniquingKeysWith: keepOld)56 57 // Implicit settings from external sources, e.g. `PROJECT_NAME`.58 buildEnvironment.merge(environment, uniquingKeysWith: keepOld)...
getBuildEnvironment
Using AI Code Generation
1let generator = Generator()2let buildEnv = generator.getBuildEnvironment()3print(buildEnv)4let generator = Generator()5let buildEnv = generator.getBuildEnvironment()6print(buildEnv)7let generator = Generator()8let buildEnv = generator.getBuildEnvironment()9print(buildEnv)10let generator = Generator()11let buildEnv = generator.getBuildEnvironment()12print(buildEnv)13let generator = Generator()14let buildEnv = generator.getBuildEnvironment()15print(buildEnv)16let generator = Generator()17let buildEnv = generator.getBuildEnvironment()18print(buildEnv)19let generator = Generator()20let buildEnv = generator.getBuildEnvironment()21print(buildEnv)22let generator = Generator()23let buildEnv = generator.getBuildEnvironment()24print(buildEnv)25let generator = Generator()26let buildEnv = generator.getBuildEnvironment()27print(buildEnv)28let generator = Generator()29let buildEnv = generator.getBuildEnvironment()30print(buildEnv)31let generator = Generator()32let buildEnv = generator.getBuildEnvironment()33print(buildEnv)34let generator = Generator()35let buildEnv = generator.getBuildEnvironment()36print(buildEnv)37let generator = Generator()38let buildEnv = generator.getBuildEnvironment()39print(buildEnv)40let generator = Generator()41let buildEnv = generator.getBuildEnvironment()42print(buildEnv
getBuildEnvironment
Using AI Code Generation
1let generator = Generator()2let buildEnvironment = generator.getBuildEnvironment()3print(buildEnvironment)4let generator = Generator()5let generatedCode = generator.generate()6print(generatedCode)7let generator = Generator()8let generatedCode = generator.generate()9print(generatedCode)10let generator = Generator()11let generatedCode = generator.generate()12print(generatedCode)13let generator = Generator()14let generatedCode = generator.generate()15print(generatedCode)16let generator = Generator()17let generatedCode = generator.generate()18print(generatedCode)19let generator = Generator()20let generatedCode = generator.generate()21print(generatedCode)22let generator = Generator()23let generatedCode = generator.generate()24print(generatedCode)25let generator = Generator()26let generatedCode = generator.generate()27print(generatedCode)28let generator = Generator()29let generatedCode = generator.generate()30print(generatedCode)31let generator = Generator()32let generatedCode = generator.generate()33print(generatedCode)34let generator = Generator()35let generatedCode = generator.generate()36print(generatedCode)37let generator = Generator()38let generatedCode = generator.generate()39print(generatedCode)40let generator = Generator()41let generatedCode = generator.generate()42print(generatedCode)43let generator = Generator()44let generatedCode = generator.generate()45print(generatedCode)
getBuildEnvironment
Using AI Code Generation
1import Foundation2import XcodeProj3let project = try! XcodeProj(pathString: projectPath)4let target = project.pbxproj.targets(named: "targetName").first!5let buildSettings = project.pbxproj.getBuildSettings(target: target, buildConfigurationName: "Debug")6print(buildSettings["PRODUCT_NAME"])7print(buildSettings["PRODUCT_BUNDLE_IDENTIFIER"])8print(buildSettings["PRODUCT_MODULE_NAME"])9import Foundation10import XcodeProj11let project = try! XcodeProj(pathString: projectPath)12let target = project.pbxproj.targets(named: "targetName").first!13let generator = Generator(xcodeproj: project)14let buildSettings = try! generator.getBuildEnvironment(target: target, buildConfigurationName: "Debug")15print(buildSettings["PRODUCT_NAME"])16print(buildSettings["PRODUCT_BUNDLE_IDENTIFIER"])17print(buildSettings["PRODUCT_MODULE_NAME"])
getBuildEnvironment
Using AI Code Generation
1let generator = Generator()2let environment = generator.getBuildEnvironment()3let appPath = "\(buildPath)/\(environment["EXECUTABLE_PATH"]!)"4let app = Application(path: appPath)5myButton.click()6myApp.terminate()7let generator = Generator()8let environment = generator.getBuildEnvironment()9let appPath = "\(buildPath)/\(environment["EXECUTABLE_PATH"]!)"10let app = Application(path: appPath)11myButton.click()12myApp.terminate()13let generator = Generator()14let environment = generator.getBuildEnvironment()15let appPath = "\(buildPath)/\(environment["EXECUTABLE_PATH"]!)"16let app = Application(path: appPath)17myButton.click()18myApp.terminate()19let generator = Generator()20let environment = generator.getBuildEnvironment()21let appPath = "\(buildPath)/\(environment["EXECUTABLE_PATH"]!)"22let app = Application(path: appPath)23myButton.click()24myApp.terminate()25let generator = Generator()26let environment = generator.getBuildEnvironment()27let appPath = "\(buildPath)/\(environment["EXECUTABLE_PATH"]!)"28let app = Application(path: appPath)
getBuildEnvironment
Using AI Code Generation
1import Foundation2import xcodeproj3let generator = Generator(projectPath: projectPath)4let environment = try generator.getBuildEnvironment(targetName: targetName, configuration: configuration)5print(environment)6import Foundation7import xcodeproj8let generator = Generator(projectPath: projectPath)9let environment = try generator.build(targetName: targetName, configuration: configuration)10print(environment)11import Foundation12import xcodeproj13let generator = Generator(projectPath: projectPath)14let environment = try generator.clean(targetName: targetName, configuration: configuration)15print(environment)16import Foundation17import xcodeproj18let generator = Generator(projectPath: projectPath)19let environment = try generator.test(targetName: targetName, configuration: configuration)20print(environment)21import Foundation22import xcodeproj23let generator = Generator(projectPath: projectPath)24let environment = try generator.archive(targetName: targetName, configuration: configuration)25print(environment)26import Foundation27import xcodeproj28let generator = Generator(projectPath: projectPath)29let environment = try generator.exportArchive(targetName: targetName, configuration: configuration)30print(environment)31import Foundation
getBuildEnvironment
Using AI Code Generation
1import Foundation2let generator = Generator()3let buildEnvironment = generator.getBuildEnvironment()4for (key, value) in buildEnvironment {5 println("\(key) = \(value)")6}7for (key, value) in buildEnvironment {8 println("\(key) = \(value)")9}10let buildEnvironment = generator.getBuildEnvironment()
getBuildEnvironment
Using AI Code Generation
1import Foundation2let generator = Generator()3let buildEnvironment = generator.getBuildEnvironment("test")4print(buildEnvironment["SDKROOT"])5let buildEnvironment = generator.getBuildEnvironment("test")6print(buildEnvironment["SDKROOT"])7let buildEnvironment = generator.getBuildEnvironment("test")8print(buildEnvironment["SDKROOT"])9let buildEnvironment = generator.getBuildEnvironment("test")10print(buildEnvironment["SDKROOT"])11let buildEnvironment = generator.getBuildEnvironment("test")12print(buildEnvironment["SDKROOT"])13let buildEnvironment = generator.getBuildEnvironment("test")14print(buildEnvironment["SDKROOT"])15let buildEnvironment = generator.getBuildEnvironment("test")16print(buildEnvironment["SDKROOT"])17let buildEnvironment = generator.getBuildEnvironment("test")18print(buildEnvironment["SDKROOT"])19let buildEnvironment = generator.getBuildEnvironment("test")20print(buildEnvironment["SDKROOT"])
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!