common.dart 1.93 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'file_system.dart';
6
import 'platform.dart';
7

8 9 10 11
/// Whether the tool started from the daemon, as opposed to the command line.
// TODO(jonahwilliams): remove once IDE updates have rolled.
bool isRunningFromDaemon = false;

12 13
/// Return the absolute path of the user's home directory
String get homeDirPath {
14 15 16 17 18
  String path = platform.isWindows
      ? platform.environment['USERPROFILE']
      : platform.environment['HOME'];
  if (path != null) {
    path = fs.path.absolute(path);
19
  }
20
  return path;
21 22
}

23 24 25 26
/// Throw a specialized exception for expected situations
/// where the tool should exit with a clear message to the user
/// and no stack trace unless the --verbose option is specified.
/// For example: network errors
27
void throwToolExit(String message, { int exitCode }) {
28
  throw ToolExit(message, exitCode: exitCode);
29 30
}

31 32 33 34 35
/// Specialized exception for expected situations
/// where the tool should exit with a clear message to the user
/// and no stack trace unless the --verbose option is specified.
/// For example: network errors
class ToolExit implements Exception {
36
  ToolExit(this.message, { this.exitCode });
37 38 39 40 41

  final String message;
  final int exitCode;

  @override
42
  String toString() => 'Exception: $message';
43
}
44 45 46 47 48 49 50 51 52 53 54

/// Indicates to the linter that the given future is intentionally not `await`-ed.
///
/// Has the same functionality as `unawaited` from `package:pedantic`.
///
/// In an async context, it is normally expected than all Futures are awaited,
/// and that is the basis of the lint unawaited_futures which is turned on for
/// the flutter_tools package. However, there are times where one or more
/// futures are intentionally not awaited. This function may be used to ignore a
/// particular future. It silences the unawaited_futures lint.
void unawaited(Future<void> future) { }