-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontext.dart
More file actions
228 lines (198 loc) · 6.86 KB
/
context.dart
File metadata and controls
228 lines (198 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import 'dart:io';
import 'package:analyzer/src/dart/analysis/byte_store.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:celest_cli/src/analytics/interface.dart';
import 'package:celest_cli/src/analytics/noop.dart';
import 'package:celest_cli/src/cli/cli_runtime.dart';
import 'package:celest_cli/src/commands/auth/cli_auth.dart';
import 'package:celest_cli/src/database/cache/cache_database.dart';
import 'package:celest_cli/src/database/project/project_database.dart';
import 'package:celest_cli/src/logging/cli_logger.dart';
import 'package:celest_cli/src/performance/local_perf.dart';
import 'package:celest_cli/src/process/logging_process_manager.dart';
import 'package:celest_cli/src/project/celest_project.dart';
import 'package:celest_cli/src/project/project_paths.dart';
import 'package:celest_cli/src/serialization/json_generator.dart';
import 'package:celest_cli/src/storage/storage.dart';
import 'package:celest_cli/src/types/type_helper.dart';
import 'package:celest_cli/src/utils/connection_monitor.dart';
import 'package:celest_cli/src/version.dart';
import 'package:celest_cloud/celest_cloud.dart';
import 'package:file/file.dart';
import 'package:file/local.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as http;
import 'package:http/retry.dart' as http;
import 'package:io/ansi.dart' show ansiOutputEnabled;
import 'package:logging/logging.dart';
import 'package:mason_logger/mason_logger.dart' as mason_logger;
import 'package:meta/meta.dart';
import 'package:native_storage/native_storage.dart';
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart';
import 'package:process/process.dart';
extension PlatformContext on path.Context {
path.Context get url => path.url;
path.Context get windows => path.windows;
path.Context get posix => path.posix;
path.Context get project =>
path.Context(current: projectPaths.projectRoot, style: style);
}
Future<ProjectPaths> init({
required String projectRoot,
ParentProject? parentProject,
String? configHome,
String? clientDir,
String? outputsDir,
@visibleForTesting CacheDatabase? cacheDb,
@visibleForTesting ByteStore? byteStore,
@visibleForTesting ProjectDatabase? projectDb,
}) async {
celestProject = await CelestProject.init(
projectRoot: projectRoot,
parentProject: parentProject,
configHome: configHome,
clientDir: clientDir,
outputsDir: outputsDir,
// ignore: invalid_use_of_visible_for_testing_member
cacheDb: cacheDb,
// ignore: invalid_use_of_visible_for_testing_member
byteStore: byteStore,
// ignore: invalid_use_of_visible_for_testing_member
projectDb: projectDb,
);
return celestProject.projectPaths;
}
late CelestProject celestProject;
ProjectPaths? _projectPaths;
ProjectPaths get projectPaths => _projectPaths ?? celestProject.projectPaths;
@visibleForTesting
set projectPaths(ProjectPaths? value) {
_projectPaths = value;
}
// Need a new instance of InheritanceManager3 for each invocation since it
// has a builtin cache.
InheritanceManager3 get inheritanceManager => InheritanceManager3();
final TypeHelper typeHelper = TypeHelper();
final JsonGenerator jsonGenerator = JsonGenerator();
final Logger _cloudLogger = Logger('Celest.Cloud');
final CelestCloud cloud = CelestCloud.http(
baseUri,
authenticator: authenticator,
httpClient: httpClient,
logger: Logger.detached('')
..onRecord.listen((record) {
_cloudLogger.finest(record.message, record.error, record.stackTrace);
}),
);
String? _celestLocalPath;
@visibleForTesting
set celestLocalPath(String? value) {
_celestLocalPath = value;
}
/// The path to the local checkout of the `celest-dev/celest` repo, if set.
String? get celestLocalPath {
if (_celestLocalPath case final localPathOverride?) {
return localPathOverride;
}
var celestLocalPath = platform.environment['CELEST_LOCAL_PATH'];
if (celestLocalPath != null) {
celestLocalPath = p.canonicalize(p.normalize(celestLocalPath));
if (fileSystem.directory(celestLocalPath).existsSync()) {
Logger.root.finest('Using local Celest at $celestLocalPath');
} else {
Logger.root.warning(
'CELEST_LOCAL_PATH is set to $celestLocalPath, but the directory '
'does not exist. Ignoring.',
);
celestLocalPath = null;
}
}
return celestLocalPath;
}
/// The identifier for the current CLI environment.
///
/// This should be used in Sentry/PostHog to identify events.
final String kCliEnvironment = switch (CliRuntime.current) {
CliRuntime.aot || CliRuntime.pubGlobal => 'release',
CliRuntime.local => 'debug',
};
/// Whether the current terminal supports ANSI output.
///
/// See: https://bixense.com/clicolors/
bool get ansiColorsEnabled {
if (platform.environment.containsKey('NO_COLOR')) {
return false;
}
if (platform.environment.containsKey('CLICOLOR_FORCE')) {
return true;
}
return ansiOutputEnabled;
}
/// The base URL for the Celest control plane.
Uri baseUri = Uri.parse(
platform.environment['CELEST_API_URI'] ?? 'https://cloud-hub.fly.dev',
);
/// Global CLI (mason) logger.
///
/// Set by [Cli.configure].
mason_logger.Logger cliLogger = CliLogger();
/// Global CLI log level.
///
/// Set by [Cli.configure].
Level kCliLogLevel = Level.INFO;
/// Global verbosity setting.
///
/// Set by [Cli.configure].
late bool verbose;
/// Whether Celest is being tested.
///
/// Defaults to `false`.
bool kCelestTest = false;
/// Global HTTP client.
///
/// Set by [Cli.configure].
http.Client httpClient = http.RetryClient(
http.IOClient(
HttpClient()
..userAgent = 'Celest-CLI/$packageVersion'
..idleTimeout = const Duration(seconds: 60)
..connectionTimeout = const Duration(seconds: 4),
),
whenError: (e, _) => e is SocketException || e is http.ClientException,
);
/// Global analytics instance.
///
/// Set by [Cli.configure].
Analytics analytics = const NoopAnalytics();
/// Global performance tracker.
///
/// Set by [Cli.configure].
CelestPerformance performance = const CelestPerformance();
/// Global process manager.
///
/// Set by [Cli.configure].
ProcessManager processManager = const LoggingProcessManager();
/// Global file system interface.
///
/// Set by [Cli.configure].
FileSystem fileSystem = const LocalFileSystem();
/// Global local file system.
FileSystem get localFileSystem => const LocalFileSystem();
/// Global path context.
path.Context get p => localFileSystem.path;
/// Global platform.
///
/// Set by [Cli.configure].
Platform platform = const LocalPlatform();
/// Global storage interface.
///
/// Set by [Cli.configure].
Storage storage = Storage();
/// Global secure storage interface.
///
/// Set by [Cli.configure].
NativeSecureStorage secureStorage = storage.secure;
/// The isolated [secureStorage] interface.
IsolatedNativeStorage get isolatedSecureStorage => secureStorage.isolated;
ConnectionMonitor connectionMonitor = ConnectionMonitor();