Skip to content

Commit

Permalink
[dartdev] Move some resident compiler utils from package:dartdev to d…
Browse files Browse the repository at this point in the history
…art: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]>
  • Loading branch information
derekxu16 authored and Commit Queue committed Nov 28, 2024
1 parent 0ac63b6 commit b196577
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
35 changes: 5 additions & 30 deletions pkg/dartdev/lib/src/resident_frontend_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand All @@ -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');

Expand Down
9 changes: 0 additions & 9 deletions pkg/dartdev/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic>`) with the correct runtime types.
Map<String, dynamic> castStringKeyedMap(dynamic untyped) {
Expand Down
76 changes: 76 additions & 0 deletions sdk/lib/_internal/vm/bin/resident_compiler_utils.dart
Original file line number Diff line number Diff line change
@@ -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!;
}
1 change: 1 addition & 0 deletions sdk/lib/_internal/vm/bin/vmservice_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b196577

Please sign in to comment.