Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context #6050

Closed
wants to merge 8 commits into from
Closed

Context #6050

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 37 additions & 35 deletions Sources/TuistAcceptanceTesting/TuistAcceptanceTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ open class TuistAcceptanceTestCase: XCTestCase {
]!
)

do {
// Environment
environment = try MockEnvironment()
Environment.shared = environment
} catch {
XCTFail("Failed to setup environment")
}
try! TuistContext.initializeSharedInstace()
}

override open func tearDown() async throws {
Expand All @@ -70,7 +64,7 @@ open class TuistAcceptanceTestCase: XCTestCase {
)
}

public func run(_ command: (some AsyncParsableCommand).Type, _ arguments: [String] = []) async throws {
public func run(_ command: (some AsyncParsableCommand).Type, _ arguments: [String] = [], context _: Context) async throws {
let arguments = arguments + [
"--path", fixturePath.pathString,
]
Expand All @@ -79,91 +73,103 @@ open class TuistAcceptanceTestCase: XCTestCase {
try await parsedCommand.run()
}

public func run(_ command: RunCommand.Type, _ arguments: String...) async throws {
try await run(command, arguments)
public func run(_ command: RunCommand.Type, _ arguments: String..., context: Context) async throws {
try await run(command, arguments, context: context)
}

public func run(_ command: RunCommand.Type, _ arguments: [String] = []) async throws {
public func run(_ command: RunCommand.Type, _ arguments: [String] = [], context: Context) async throws {
let arguments = [
"--path", fixturePath.pathString,
] + arguments

let parsedCommand = try command.parse(arguments)
try await parsedCommand.run()
try await parsedCommand.run(context: context)
}

public func run(_ command: EditCommand.Type, _ arguments: String...) async throws {
try await run(command, arguments)
public func run(_ command: EditCommand.Type, _ arguments: String..., context: Context) async throws {
try await run(command, arguments, context: context)
}

public func run(_ command: EditCommand.Type, _ arguments: [String] = []) async throws {
public func run(_ command: EditCommand.Type, _ arguments: [String] = [], context: Context) async throws {
let arguments = arguments + [
"--path", fixturePath.pathString,
"--permanent",
]

let parsedCommand = try command.parse(arguments)
try await parsedCommand.run()
try await parsedCommand.run(context: context)

xcodeprojPath = try FileHandler.shared.contentsOfDirectory(fixturePath)
.first(where: { $0.basename == "Manifests.xcodeproj" })
workspacePath = try FileHandler.shared.contentsOfDirectory(fixturePath)
.first(where: { $0.basename == "Manifests.xcworkspace" })
}

public func run(_ command: MigrationTargetsByDependenciesCommand.Type, _ arguments: String...) throws {
try run(command, arguments)
public func run(_ command: MigrationTargetsByDependenciesCommand.Type, _ arguments: String..., context: Context) throws {
try run(command, arguments, context: context)
}

public func run(_ command: MigrationTargetsByDependenciesCommand.Type, _ arguments: [String] = []) throws {
public func run(
_ command: MigrationTargetsByDependenciesCommand.Type,
_ arguments: [String] = [],
context: Context
) async throws {
let parsedCommand = try command.parse(arguments)
try parsedCommand.run()
try await parsedCommand.run(context: context)
}

public func run(_ command: TestCommand.Type, _ arguments: [String] = []) async throws {
public func run(_ command: TestCommand.Type, _ arguments: [String] = [], context: Context) async throws {
let arguments = arguments + [
"--derived-data-path", derivedDataPath.pathString,
"--path", fixturePath.pathString,
]

let parsedCommand = try command.parse(arguments)
try await parsedCommand.run()
try await parsedCommand.run(context: context)
}

public func run(_ command: BuildCommand.Type, _ arguments: [String] = []) async throws {
public func run(_ command: BuildCommand.Type, _ arguments: [String] = [], context: Context) async throws {
let arguments = arguments + [
"--derived-data-path", derivedDataPath.pathString,
"--path", fixturePath.pathString,
]

let parsedCommand = try command.parse(arguments)
try await parsedCommand.run()
try await parsedCommand.run(context: context)
}

public func run(_ command: BuildCommand.Type, _ arguments: String...) async throws {
try await run(command, arguments)
public func run(_ command: BuildCommand.Type, _ arguments: String..., context: Context) async throws {
try await run(command, arguments, context: context)
}

public func run(_ command: GenerateCommand.Type, _ arguments: [String] = []) async throws {
public func run(_ command: GenerateCommand.Type, _ arguments: [String] = [], context: Context) async throws {
let arguments = arguments + [
"--no-open",
"--path", fixturePath.pathString,
]

let parsedCommand = try command.parse(arguments)
try await parsedCommand.run()
try await parsedCommand.run(context: context)

xcodeprojPath = try FileHandler.shared.contentsOfDirectory(fixturePath)
.first(where: { $0.extension == "xcodeproj" })
workspacePath = try FileHandler.shared.contentsOfDirectory(fixturePath)
.first(where: { $0.extension == "xcworkspace" })
}

public func run(_ command: (some AsyncParsableCommand).Type, _ arguments: String...) async throws {
try await run(command, Array(arguments))
public func run(
_ command: (some ContextualizedAsyncParsableCommand).Type,
_ arguments: String...,
context: Context
) async throws {
try await run(command, Array(arguments), context: context)
}

public func run(_ command: (some ParsableCommand).Type, _ arguments: [String] = []) throws {
public func run(
_ command: (some ContextualizedAsyncParsableCommand).Type,
_ arguments: [String] = [],
context _: Context
) throws {
if String(describing: command) == "InitCommand" {
fixturePath = fixtureTemporaryDirectory.path.appending(
component: arguments[arguments.firstIndex(where: { $0 == "--name" })! + 1]
Expand All @@ -176,10 +182,6 @@ open class TuistAcceptanceTestCase: XCTestCase {
try parsedCommand.run()
}

public func run(_ command: (some ParsableCommand).Type, _ arguments: String...) throws {
try run(command, Array(arguments))
}

public func addEmptyLine(to file: String) throws {
let filePath = try fixturePath.appending(RelativePath(validating: file))
var contents = try FileHandler.shared.readTextFile(filePath)
Expand Down
17 changes: 9 additions & 8 deletions Sources/TuistAsyncQueue/AsyncQueuePersistor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ public protocol AsyncQueuePersisting {
final class AsyncQueuePersistor: AsyncQueuePersisting {
// MARK: - Attributes

let directory: AbsolutePath
let cacheDirectory: AbsolutePath
let jsonEncoder = JSONEncoder()
var queueDirectory: AbsolutePath { cacheDirectory.appending(components: ["Queue"]) }

// MARK: - Init

init(directory: AbsolutePath = Environment.shared.queueDirectory) {
self.directory = directory
init(cacheDirectory: AbsolutePath = TuistContext.shared.environment.cacheDirectory) {
self.cacheDirectory = cacheDirectory
}

func write(event: some AsyncQueueEvent) throws {
let path = directory.appending(component: filename(event: event))
let path = queueDirectory.appending(component: filename(event: event))
try createDirectoryIfNeeded()
let data = try jsonEncoder.encode(event)
try data.write(to: path.url)
Expand All @@ -47,13 +48,13 @@ final class AsyncQueuePersistor: AsyncQueuePersisting {
}

func delete(filename: String) throws {
let path = directory.appending(component: filename)
let path = queueDirectory.appending(component: filename)
guard FileHandler.shared.exists(path) else { return }
try FileHandler.shared.delete(path)
}

func readAll() throws -> [AsyncQueueEventTuple] {
let paths = FileHandler.shared.glob(directory, glob: "*.json")
let paths = FileHandler.shared.glob(queueDirectory, glob: "*.json")
var events: [AsyncQueueEventTuple] = []
for eventPath in paths {
let fileName = eventPath.basenameWithoutExt
Expand Down Expand Up @@ -91,7 +92,7 @@ final class AsyncQueuePersistor: AsyncQueuePersisting {
}

private func createDirectoryIfNeeded() throws {
guard !FileManager.default.fileExists(atPath: directory.pathString) else { return }
try FileManager.default.createDirectory(atPath: directory.pathString, withIntermediateDirectories: true)
guard !FileManager.default.fileExists(atPath: queueDirectory.pathString) else { return }
try FileManager.default.createDirectory(atPath: queueDirectory.pathString, withIntermediateDirectories: true)
}
}
4 changes: 2 additions & 2 deletions Sources/TuistAutomation/Utilities/Formatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Formatter: Formatting {

init() {
formatter = XCBeautifier(
colored: Environment.shared.shouldOutputBeColoured,
colored: TuistContext.shared.environment.shouldOutputBeColoured,
renderer: Self.renderer(),
preserveUnbeautifiedLines: false,
additionalLines: { nil }
Expand All @@ -24,7 +24,7 @@ final class Formatter: Formatting {
}

private static func renderer() -> Renderer {
if Environment.shared.isGitHubActions {
if TuistContext.shared.environment.isGitHubActions {
return .gitHubActions
} else {
return .terminal
Expand Down
12 changes: 6 additions & 6 deletions Sources/TuistAutomation/XcodeBuild/XcodeBuildController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public final class XcodeBuildController: XcodeBuildControlling {
)

private let formatter: Formatting
private let environment: Environmenting
private let context: Context

public convenience init() {
self.init(formatter: Formatter(), environment: Environment.shared)
self.init(formatter: Formatter(), context: TuistContext.shared)
}

init(
formatter: Formatting,
environment: Environmenting
context: Context
) {
self.formatter = formatter
self.environment = environment
self.context = context
}

public func build(
Expand Down Expand Up @@ -295,14 +295,14 @@ public final class XcodeBuildController: XcodeBuildControlling {
switch event {
case let .standardError(errorData):
guard let line = String(data: errorData, encoding: .utf8) else { return nil }
if self?.environment.isVerbose == true {
if self?.context.environment.isVerbose == true {
return SystemEvent.standardError(XcodeBuildOutput(raw: line))
} else {
return SystemEvent.standardError(XcodeBuildOutput(raw: self?.formatter.format(line) ?? ""))
}
case let .standardOutput(outputData):
guard let line = String(data: outputData, encoding: .utf8) else { return nil }
if self?.environment.isVerbose == true {
if self?.context.environment.isVerbose == true {
return SystemEvent.standardOutput(XcodeBuildOutput(raw: line))
} else {
return SystemEvent.standardOutput(XcodeBuildOutput(raw: self?.formatter.format(line) ?? ""))
Expand Down
6 changes: 5 additions & 1 deletion Sources/TuistKit/Commands/BuildCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct BuildOptions: ParsableArguments {
}

/// Command that builds a target from the project in the current directory.
public struct BuildCommand: AsyncParsableCommand {
public struct BuildCommand: ContextualizedAsyncParsableCommand {
public init() {}

public static var configuration: CommandConfiguration {
Expand All @@ -92,6 +92,10 @@ public struct BuildCommand: AsyncParsableCommand {
var buildOptions: BuildOptions

public func run() async throws {
try await run(context: try TuistContext())
}

public func run(context _: any TuistSupport.Context) async throws {
let absolutePath: AbsolutePath
if let path = buildOptions.path {
absolutePath = try AbsolutePath(validating: path, relativeTo: FileHandler.shared.currentPath)
Expand Down
9 changes: 7 additions & 2 deletions Sources/TuistKit/Commands/CleanCommand.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ArgumentParser
import Foundation
import TuistCore
import TuistSupport

public struct CleanCommand<T: CleanCategory>: ParsableCommand {
public struct CleanCommand<T: CleanCategory>: ContextualizedAsyncParsableCommand {
public init() {}

public static var configuration: CommandConfiguration {
Expand All @@ -22,7 +23,11 @@ public struct CleanCommand<T: CleanCategory>: ParsableCommand {
)
var path: String?

public func run() throws {
public func run() async throws {
try await run(context: TuistContext())
}

public func run(context _: Context) async throws {
try CleanService().run(
categories: cleanCategories,
path: path
Expand Down
6 changes: 5 additions & 1 deletion Sources/TuistKit/Commands/DumpCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import TSCBasic
import TuistLoader
import TuistSupport

struct DumpCommand: AsyncParsableCommand {
struct DumpCommand: ContextualizedAsyncParsableCommand {
static var configuration: CommandConfiguration {
CommandConfiguration(
commandName: "dump",
Expand All @@ -25,6 +25,10 @@ struct DumpCommand: AsyncParsableCommand {
var manifest: DumpableManifest = .project

func run() async throws {
try await run(context: try TuistContext())
}

func run(context _: any Context) async throws {
try await DumpService().run(path: path, manifest: manifest)
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/TuistKit/Commands/EditCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import TSCBasic
import TuistGenerator
import TuistSupport

public struct EditCommand: AsyncParsableCommand {
public struct EditCommand: ContextualizedAsyncParsableCommand {
public init() {}

public static var configuration: CommandConfiguration {
Expand Down Expand Up @@ -34,6 +34,10 @@ public struct EditCommand: AsyncParsableCommand {
var onlyCurrentDirectory: Bool = false

public func run() async throws {
try await run(context: try TuistContext())
}

public func run(context _: any Context) async throws {
try await EditService().run(
path: path,
permanent: permanent,
Expand Down
6 changes: 5 additions & 1 deletion Sources/TuistKit/Commands/GenerateCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
import TuistCore
import TuistSupport

public struct GenerateCommand: AsyncParsableCommand, HasTrackableParameters {
public struct GenerateCommand: ContextualizedAsyncParsableCommand, HasTrackableParameters {
public init() {}
public static var analyticsDelegate: TrackableParametersDelegate?

Expand All @@ -30,6 +30,10 @@ public struct GenerateCommand: AsyncParsableCommand, HasTrackableParameters {
var noOpen: Bool = false

public func run() async throws {
try await run(context: try TuistContext())
}

public func run(context _: Context) async throws {
try await GenerateService().run(
path: path,
noOpen: noOpen
Expand Down
6 changes: 5 additions & 1 deletion Sources/TuistKit/Commands/GraphCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TuistLoader
import TuistSupport

/// Command that generates and exports a dot graph from the workspace or project in the current directory.
public struct GraphCommand: AsyncParsableCommand, HasTrackableParameters {
public struct GraphCommand: ContextualizedAsyncParsableCommand, HasTrackableParameters {
public init() {}

public static var analyticsDelegate: TrackableParametersDelegate?
Expand Down Expand Up @@ -74,6 +74,10 @@ public struct GraphCommand: AsyncParsableCommand, HasTrackableParameters {
var outputPath: String?

public func run() async throws {
try await run(context: try TuistContext())
}

public func run(context _: any Context) async throws {
GraphCommand.analyticsDelegate?.addParameters(
[
"format": AnyCodable(format.rawValue),
Expand Down