Skip to content

Commit b196577

Browse files
derekxu16Commit Queue
authored andcommitted
[dartdev] Move some resident compiler utils from package:dartdev to dart:vmservice_io
TEST=CI Change-Id: Ibabbb47cc5951f35dfe09259c7320fa8f930f157 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394521 Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Derek Xu <[email protected]>
1 parent 0ac63b6 commit b196577

File tree

4 files changed

+82
-39
lines changed

4 files changed

+82
-39
lines changed

pkg/dartdev/lib/src/resident_frontend_utils.dart

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66
import 'dart:io' show File, FileSystemException;
7+
import 'dart:vmservice_io' show getResidentCompilerInfoFileConsideringArgsImpl;
78

89
import 'package:args/args.dart';
910
import 'package:frontend_server/resident_frontend_server_utils.dart'
@@ -12,8 +13,6 @@ import 'package:path/path.dart' as p;
1213

1314
import 'commands/compilation_server.dart' show CompilationServerCommand;
1415
import 'resident_frontend_constants.dart';
15-
import 'unified_analytics.dart';
16-
import 'utils.dart' show maybeUriToFilename;
1716

1817
/// The Resident Frontend Compiler's shutdown command.
1918
final residentServerShutdownCommand = jsonEncode(
@@ -22,34 +21,10 @@ final residentServerShutdownCommand = jsonEncode(
2221
},
2322
);
2423

25-
/// The default resident frontend compiler information file.
26-
///
27-
/// Resident frontend compiler info files contain the contents:
28-
/// `address:$address port:$port`.
29-
File? get defaultResidentServerInfoFile {
30-
var dartConfigDir = getDartStorageDirectory();
31-
if (dartConfigDir == null) return null;
32-
33-
return File(p.join(dartConfigDir.path, 'dartdev_compilation_server_info'));
34-
}
35-
36-
File? getResidentCompilerInfoFileConsideringArgs(final ArgResults args) {
37-
if (args.wasParsed(CompilationServerCommand.residentCompilerInfoFileFlag)) {
38-
return File(maybeUriToFilename(
39-
args[CompilationServerCommand.residentCompilerInfoFileFlag],
40-
));
41-
} else if (args.wasParsed(
42-
CompilationServerCommand.legacyResidentServerInfoFileFlag,
43-
)) {
44-
return File(maybeUriToFilename(
45-
args[CompilationServerCommand.legacyResidentServerInfoFileFlag],
46-
));
47-
} else if (defaultResidentServerInfoFile != null) {
48-
return defaultResidentServerInfoFile!;
49-
} else {
50-
return null;
51-
}
52-
}
24+
File? getResidentCompilerInfoFileConsideringArgs(final ArgResults args) =>
25+
getResidentCompilerInfoFileConsideringArgsImpl(
26+
args[CompilationServerCommand.residentCompilerInfoFileFlag] ??
27+
args[CompilationServerCommand.legacyResidentServerInfoFileFlag]);
5328

5429
final String packageConfigName = p.join('.dart_tool', 'package_config.json');
5530

pkg/dartdev/lib/src/utils.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ ArgParser globalDartdevOptionsParser({bool verbose = false}) {
109109
return argParser;
110110
}
111111

112-
/// Try parsing [maybeUri] as a file uri or [maybeUri] itself if that fails.
113-
String maybeUriToFilename(String maybeUri) {
114-
try {
115-
return Uri.parse(maybeUri).toFilePath();
116-
} catch (_) {
117-
return maybeUri;
118-
}
119-
}
120-
121112
/// Given a data structure which is a Map of String to dynamic values, return
122113
/// the same structure (`Map<String, dynamic>`) with the correct runtime types.
123114
Map<String, dynamic> castStringKeyedMap(dynamic untyped) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
part of vmservice_io;
6+
7+
/// If [maybeUri] is a file uri, this function returns the result of converting
8+
/// [maybeUri] to a file path. Otherwise, it returns [maybeUri] back unmodified.
9+
String maybeUriToFilename(String maybeUri) {
10+
try {
11+
return Uri.parse(maybeUri).toFilePath();
12+
} catch (_) {
13+
return maybeUri;
14+
}
15+
}
16+
17+
/// Joins [a] and [b] with a backslash if [Platform.isWindows] is true,
18+
/// otherwise joins [a] and [b] with a slash.
19+
String joinPathComponents(final String a, final String b) =>
20+
!Platform.isWindows ? '$a/$b' : '$a\\$b';
21+
22+
/// The user's home directory for the current platform, or [null] if it can't be
23+
/// found.
24+
Directory? get homeDir {
25+
final envKey = Platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME';
26+
final envValue = Platform.environment[envKey];
27+
28+
if (envValue == null) {
29+
return null;
30+
}
31+
32+
final dir = Directory(envValue);
33+
return dir.existsSync() ? dir : null;
34+
}
35+
36+
/// The directory used to store the default resident compiler info file, which
37+
/// is the `.dart` subdirectory of the user's home directory. This function will
38+
/// return [null] the directory is inaccessible.
39+
Directory? getDartStorageDirectory() {
40+
var dir = homeDir;
41+
if (dir == null) {
42+
return null;
43+
} else {
44+
return Directory(joinPathComponents(dir.path, '.dart'));
45+
}
46+
}
47+
48+
/// The default resident frontend compiler information file.
49+
///
50+
/// Resident frontend compiler info files contain the contents:
51+
/// `address:$address port:$port`.
52+
File? get defaultResidentServerInfoFile {
53+
var dartConfigDir = getDartStorageDirectory();
54+
if (dartConfigDir == null) return null;
55+
56+
return File(
57+
joinPathComponents(dartConfigDir.path, 'dartdev_compilation_server_info'),
58+
);
59+
}
60+
61+
// Used in `pkg/dartdev/lib/src/resident_frontend_utils.dart` and in
62+
// `sdk/lib/_internal/vm/bin/vmservice_io.dart`.
63+
File? getResidentCompilerInfoFileConsideringArgsImpl(
64+
/// If either `--resident-compiler-info-file` or `--resident-server-info-file`
65+
/// was supplied on the command line, the CLI argument should be forwarded as
66+
/// the argument to this parameter. If neither option was supplied, the
67+
/// argument to this parameter should be [null].
68+
String? residentCompilerInfoFilePathArgumentFromCli,
69+
) {
70+
if (residentCompilerInfoFilePathArgumentFromCli != null) {
71+
return File(
72+
maybeUriToFilename(residentCompilerInfoFilePathArgumentFromCli),
73+
);
74+
}
75+
return defaultResidentServerInfoFile!;
76+
}

sdk/lib/_internal/vm/bin/vmservice_io.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'dart:convert';
99
import 'dart:io';
1010
import 'dart:_vmservice';
1111

12+
part 'resident_compiler_utils.dart';
1213
part 'vmservice_server.dart';
1314

1415
// The TCP ip/port that dds listens on.

0 commit comments

Comments
 (0)