common.dart 1.95 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import '../globals.dart' as globals;
6

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

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

22 23 24 25
/// 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
26
void throwToolExit(String message, { int exitCode }) {
27
  throw ToolExit(message, exitCode: exitCode);
28 29
}

30 31 32 33 34
/// 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 {
35
  ToolExit(this.message, { this.exitCode });
36 37 38 39 40

  final String message;
  final int exitCode;

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

/// 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) { }