From b196577f19a7496a41f2d3ec5cc61065b899e08b Mon Sep 17 00:00:00 2001 From: Derek Xu Date: Thu, 28 Nov 2024 16:18:52 +0000 Subject: [PATCH] [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 Commit-Queue: Derek Xu --- .../lib/src/resident_frontend_utils.dart | 35 ++------- pkg/dartdev/lib/src/utils.dart | 9 --- .../vm/bin/resident_compiler_utils.dart | 76 +++++++++++++++++++ sdk/lib/_internal/vm/bin/vmservice_io.dart | 1 + 4 files changed, 82 insertions(+), 39 deletions(-) create mode 100644 sdk/lib/_internal/vm/bin/resident_compiler_utils.dart diff --git a/pkg/dartdev/lib/src/resident_frontend_utils.dart b/pkg/dartdev/lib/src/resident_frontend_utils.dart index befd94c5c907..2e545c73a2b6 100644 --- a/pkg/dartdev/lib/src/resident_frontend_utils.dart +++ b/pkg/dartdev/lib/src/resident_frontend_utils.dart @@ -4,6 +4,7 @@ import 'dart:convert'; import 'dart:io' show File, FileSystemException; +import 'dart:vmservice_io' show getResidentCompilerInfoFileConsideringArgsImpl; import 'package:args/args.dart'; import 'package:frontend_server/resident_frontend_server_utils.dart' @@ -12,8 +13,6 @@ import 'package:path/path.dart' as p; import 'commands/compilation_server.dart' show CompilationServerCommand; import 'resident_frontend_constants.dart'; -import 'unified_analytics.dart'; -import 'utils.dart' show maybeUriToFilename; /// The Resident Frontend Compiler's shutdown command. final residentServerShutdownCommand = jsonEncode( @@ -22,34 +21,10 @@ final residentServerShutdownCommand = jsonEncode( }, ); -/// The default resident frontend compiler information file. -/// -/// Resident frontend compiler info files contain the contents: -/// `address:$address port:$port`. -File? get defaultResidentServerInfoFile { - var dartConfigDir = getDartStorageDirectory(); - if (dartConfigDir == null) return null; - - return File(p.join(dartConfigDir.path, 'dartdev_compilation_server_info')); -} - -File? getResidentCompilerInfoFileConsideringArgs(final ArgResults args) { - if (args.wasParsed(CompilationServerCommand.residentCompilerInfoFileFlag)) { - return File(maybeUriToFilename( - args[CompilationServerCommand.residentCompilerInfoFileFlag], - )); - } else if (args.wasParsed( - CompilationServerCommand.legacyResidentServerInfoFileFlag, - )) { - return File(maybeUriToFilename( - args[CompilationServerCommand.legacyResidentServerInfoFileFlag], - )); - } else if (defaultResidentServerInfoFile != null) { - return defaultResidentServerInfoFile!; - } else { - return null; - } -} +File? getResidentCompilerInfoFileConsideringArgs(final ArgResults args) => + getResidentCompilerInfoFileConsideringArgsImpl( + args[CompilationServerCommand.residentCompilerInfoFileFlag] ?? + args[CompilationServerCommand.legacyResidentServerInfoFileFlag]); final String packageConfigName = p.join('.dart_tool', 'package_config.json'); diff --git a/pkg/dartdev/lib/src/utils.dart b/pkg/dartdev/lib/src/utils.dart index d462605c289b..b355106619a0 100644 --- a/pkg/dartdev/lib/src/utils.dart +++ b/pkg/dartdev/lib/src/utils.dart @@ -109,15 +109,6 @@ ArgParser globalDartdevOptionsParser({bool verbose = false}) { return argParser; } -/// Try parsing [maybeUri] as a file uri or [maybeUri] itself if that fails. -String maybeUriToFilename(String maybeUri) { - try { - return Uri.parse(maybeUri).toFilePath(); - } catch (_) { - return maybeUri; - } -} - /// Given a data structure which is a Map of String to dynamic values, return /// the same structure (`Map`) with the correct runtime types. Map castStringKeyedMap(dynamic untyped) { diff --git a/sdk/lib/_internal/vm/bin/resident_compiler_utils.dart b/sdk/lib/_internal/vm/bin/resident_compiler_utils.dart new file mode 100644 index 000000000000..29f85161e25b --- /dev/null +++ b/sdk/lib/_internal/vm/bin/resident_compiler_utils.dart @@ -0,0 +1,76 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +part of vmservice_io; + +/// If [maybeUri] is a file uri, this function returns the result of converting +/// [maybeUri] to a file path. Otherwise, it returns [maybeUri] back unmodified. +String maybeUriToFilename(String maybeUri) { + try { + return Uri.parse(maybeUri).toFilePath(); + } catch (_) { + return maybeUri; + } +} + +/// Joins [a] and [b] with a backslash if [Platform.isWindows] is true, +/// otherwise joins [a] and [b] with a slash. +String joinPathComponents(final String a, final String b) => + !Platform.isWindows ? '$a/$b' : '$a\\$b'; + +/// The user's home directory for the current platform, or [null] if it can't be +/// found. +Directory? get homeDir { + final envKey = Platform.operatingSystem == 'windows' ? 'APPDATA' : 'HOME'; + final envValue = Platform.environment[envKey]; + + if (envValue == null) { + return null; + } + + final dir = Directory(envValue); + return dir.existsSync() ? dir : null; +} + +/// The directory used to store the default resident compiler info file, which +/// is the `.dart` subdirectory of the user's home directory. This function will +/// return [null] the directory is inaccessible. +Directory? getDartStorageDirectory() { + var dir = homeDir; + if (dir == null) { + return null; + } else { + return Directory(joinPathComponents(dir.path, '.dart')); + } +} + +/// The default resident frontend compiler information file. +/// +/// Resident frontend compiler info files contain the contents: +/// `address:$address port:$port`. +File? get defaultResidentServerInfoFile { + var dartConfigDir = getDartStorageDirectory(); + if (dartConfigDir == null) return null; + + return File( + joinPathComponents(dartConfigDir.path, 'dartdev_compilation_server_info'), + ); +} + +// Used in `pkg/dartdev/lib/src/resident_frontend_utils.dart` and in +// `sdk/lib/_internal/vm/bin/vmservice_io.dart`. +File? getResidentCompilerInfoFileConsideringArgsImpl( + /// If either `--resident-compiler-info-file` or `--resident-server-info-file` + /// was supplied on the command line, the CLI argument should be forwarded as + /// the argument to this parameter. If neither option was supplied, the + /// argument to this parameter should be [null]. + String? residentCompilerInfoFilePathArgumentFromCli, +) { + if (residentCompilerInfoFilePathArgumentFromCli != null) { + return File( + maybeUriToFilename(residentCompilerInfoFilePathArgumentFromCli), + ); + } + return defaultResidentServerInfoFile!; +} diff --git a/sdk/lib/_internal/vm/bin/vmservice_io.dart b/sdk/lib/_internal/vm/bin/vmservice_io.dart index e92c6761ee98..fbc017532f21 100644 --- a/sdk/lib/_internal/vm/bin/vmservice_io.dart +++ b/sdk/lib/_internal/vm/bin/vmservice_io.dart @@ -9,6 +9,7 @@ import 'dart:convert'; import 'dart:io'; import 'dart:_vmservice'; +part 'resident_compiler_utils.dart'; part 'vmservice_server.dart'; // The TCP ip/port that dds listens on.