Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit e20cb21

Browse files
authored
Set up environment and logging infrastructure (#142)
2 parents 1ab040b + 0fee19d commit e20cb21

File tree

9 files changed

+82
-9
lines changed

9 files changed

+82
-9
lines changed

.github/workflows/virtualos.yml

+13-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616
jobs:
1717
build:
1818
name: "Build"
19-
runs-on: "macos-135
19+
runs-on: "macos-13"
2020
steps:
2121
- uses: actions/checkout@v4
2222
- uses: jdx/mise-action@v2
@@ -31,7 +31,7 @@ jobs:
3131
3232
bundle:
3333
name: "Bundle"
34-
runs-on: "macos-135
34+
runs-on: "macos-13"
3535
steps:
3636
- uses: actions/checkout@v4
3737
- uses: jdx/mise-action@v2
@@ -49,7 +49,7 @@ jobs:
4949

5050
test:
5151
name: "Test"
52-
runs-on: "macos-135
52+
runs-on: "macos-13"
5353
steps:
5454
- uses: actions/checkout@v4
5555
- uses: jdx/mise-action@v2
@@ -73,6 +73,16 @@ jobs:
7373
- name: Run
7474
run: mise run lint
7575

76+
implicit-imports:
77+
name: Implicit imports
78+
runs-on: macos-15
79+
steps:
80+
- uses: actions/checkout@v4
81+
- uses: jdx/mise-action@v2
82+
with:
83+
experimental: true
84+
- run: tuist inspect implicit-imports
85+
7686
cache:
7787
name: Cache
7888
runs-on: macos-15

Package.swift

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
let packageSettings = PackageSettings(baseSettings: .settings(base: [
1010
"SWIFT_STRICT_CONCURRENCY": "complete",
11+
"GENERATE_MASTER_OBJECT_FILE": "YES",
1112
]))
1213
#endif
1314

Sources/VirtualOSEnvironment/Environment.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct Environment: Environmenting {
3939

4040
/// It returns the current environment.
4141
/// - Returns: It should not be used directly, but dependency-injected down from the root of the program.
42-
static func current() async throws -> Environment {
42+
public static func current() async throws -> Environment {
4343
let variables = ProcessInfo.processInfo.environment
4444
let fileSystem = FileSystem()
4545
let homeDirectory = try variables["HOME"].map { try AbsolutePath(validating: $0) }

Sources/VirtualOSEnvironmentInterface/ServiceContext+Environment.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ enum EnvironmentKey: Sendable, ServiceContextKey {
55
}
66

77
extension ServiceContext {
8-
public internal(set) var environment: Environmenting? {
8+
public var environment: Environmenting? {
99
get {
1010
self[EnvironmentKey.self]
1111
}

Sources/VirtualOSLogging/Logger.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ enum LoggerKey: ServiceContextKey {
1010
}
1111

1212
extension ServiceContext {
13-
public internal(set) var logger: Logger? {
13+
public var logger: Logger? {
1414
get {
1515
self[LoggerKey.self]
1616
}
@@ -24,7 +24,7 @@ extension Logger {
2424
/// Creates an instance of the default logger.
2525
/// - Parameter logFilePath: The file where file logs should be stored.
2626
/// - Returns: An instance of `Logger`.
27-
static func makeDefaultLogger(logFilePath: AbsolutePath) -> Logger {
27+
public static func makeDefaultLogger(logFilePath: AbsolutePath) -> Logger {
2828
Logger(label: "dev.tuist.virtualos") { label in
2929
MultiplexLogHandler([
3030
try! FileLogHandler(label: label, localFile: URL(fileURLWithPath: logFilePath.pathString)),

Sources/VirtualOSPull/PullCommand.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import ArgumentParser
2+
import Logging
3+
import ServiceContextModule
4+
import VirtualOSLogging
25

36
/**
47
This command is responsible from pulling images from remote registries.
@@ -18,5 +21,7 @@ public struct PullCommand: AsyncParsableCommand {
1821

1922
public init() {}
2023

21-
public func run() async throws {}
24+
public func run() async throws {
25+
ServiceContext.current?.logger?.info("Pulling images")
26+
}
2227
}

Sources/virtualos/VirtualOS.swift

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import ArgumentParser
2+
import FileSystem
3+
import Foundation
4+
import Logging
5+
import Path
6+
import ServiceContextModule
7+
import VirtualOSEnvironment
8+
import VirtualOSEnvironmentInterface
9+
import VirtualOSLogging
10+
11+
@main
12+
private enum VirtualOS {
13+
static func main() async throws {
14+
let fileSystem = FileSystem()
15+
let environment = try await Environment.current()
16+
let logFilePath = environment.cacheDirectory.appending(components: ["logs", "\(UUID().uuidString).log"])
17+
if try await !fileSystem.exists(logFilePath.parentDirectory) {
18+
try await fileSystem.makeDirectory(at: logFilePath.parentDirectory)
19+
}
20+
let logger = Logger.makeDefaultLogger(logFilePath: logFilePath)
21+
var serviceContext = ServiceContext.topLevel
22+
serviceContext.logger = logger
23+
serviceContext.environment = environment
24+
25+
await ServiceContext.$current.withValue(serviceContext) {
26+
defer {
27+
showCompletion(logFilePath: logFilePath)
28+
}
29+
30+
do {
31+
var command = try VirtualOSCommand.parseAsRoot()
32+
if var asyncCommand = command as? AsyncParsableCommand {
33+
try await asyncCommand.run()
34+
} else {
35+
try command.run()
36+
}
37+
} catch {
38+
showCompletion(logFilePath: logFilePath)
39+
VirtualOSCommand.exit(withError: error)
40+
}
41+
}
42+
}
43+
44+
private static func showCompletion(logFilePath: AbsolutePath) {
45+
if !CommandLine.arguments.contains("--help"), !CommandLine.arguments.contains("-h") {
46+
print("Logs available at: \(logFilePath.pathString)")
47+
}
48+
}
49+
}

Sources/virtualos/VirtualOSCommand.swift

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Foundation
33
import VirtualOSPull
44
import VirtualOSRun
55

6-
@main
76
struct VirtualOSCommand: AsyncParsableCommand {
87
static let configuration = CommandConfiguration(
98
commandName: "virtualos",

Tuist/ProjectDescriptionHelpers/Module.swift

+9
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,20 @@ public enum Module: CaseIterable {
5050
case .virtualos: return [
5151
.target(name: Module.pull.targetName),
5252
.target(name: Module.run.targetName),
53+
.target(name: Module.logging.targetName),
54+
.target(name: Module.environment.targetName),
55+
.target(name: Module.environment.interfaceTargetName!),
56+
.external(name: "ServiceContextModule"),
5357
.external(name: "ArgumentParser"),
58+
.external(name: "Logging"),
59+
.external(name: "Path"),
5460
]
5561
case .pull: return [
62+
.target(name: Module.logging.targetName),
5663
.external(name: "ArgumentParser"),
5764
.external(name: "Path"),
65+
.external(name: "ServiceContextModule"),
66+
.external(name: "Logging"),
5867
]
5968
case .run: return [
6069
.external(name: "ArgumentParser"),

0 commit comments

Comments
 (0)