os.dart 5.65 KB
Newer Older
1 2 3 4
// Copyright 2015 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 'dart:async';
6

7 8 9
import 'package:archive/archive.dart';
import 'package:path/path.dart' as path;

10
import 'context.dart';
11
import 'file_system.dart';
12
import 'io.dart';
13
import 'platform.dart';
14
import 'process.dart';
15
import 'process_manager.dart';
16 17

/// Returns [OperatingSystemUtils] active in the current app context (i.e. zone).
18
OperatingSystemUtils get os => context[OperatingSystemUtils];
19 20

abstract class OperatingSystemUtils {
21
  factory OperatingSystemUtils() {
22
    if (platform.isWindows) {
23 24
      return new _WindowsUtils();
    } else {
Adam Barth's avatar
Adam Barth committed
25
      return new _PosixUtils();
26 27 28
    }
  }

29 30 31
  OperatingSystemUtils._private();


32 33
  // TODO(tvolkert): Remove these and migrate callers to Platform
  String get operatingSystem => platform.operatingSystem;
34 35 36 37
  bool get isMacOS => operatingSystem == 'macos';
  bool get isWindows => operatingSystem == 'windows';
  bool get isLinux => operatingSystem == 'linux';

38
  /// Make the given file executable. This may be a no-op on some platforms.
39
  ProcessResult makeExecutable(File file);
40 41 42 43

  /// Return the path (with symlinks resolved) to the given executable, or `null`
  /// if `which` was not able to locate the binary.
  File which(String execName);
44

45 46 47
  /// Return the File representing a new pipe.
  File makePipe(String path);

48
  void unzip(File file, Directory targetDirectory);
49 50

  /// Returns the name of the [binaryName] executable.
51
  ///
52 53 54 55
  /// No-op on most OS.
  /// On Windows it returns [binaryName].[winExtension], if [winExtension] is
  /// specified, or [binaryName].exe otherwise.
  String getExecutableName(String binaryName, { String winExtension });
56 57
}

58 59 60
class _PosixUtils extends OperatingSystemUtils {
  _PosixUtils() : super._private();

61
  @override
62
  ProcessResult makeExecutable(File file) {
63
    return processManager.runSync(<String>['chmod', 'a+x', file.path]);
64
  }
65

66 67
  /// Return the path to the given executable, or `null` if `which` was not able
  /// to locate the binary.
68
  @override
69
  File which(String execName) {
70
    ProcessResult result = processManager.runSync(<String>['which', execName]);
71 72 73
    if (result.exitCode != 0)
      return null;
    String path = result.stdout.trim().split('\n').first.trim();
74
    return fs.file(path);
75
  }
76 77 78 79 80 81

  // unzip -o -q zipfile -d dest
  @override
  void unzip(File file, Directory targetDirectory) {
    runSync(<String>['unzip', '-o', '-q', file.path, '-d', targetDirectory.path]);
  }
82 83 84 85

  @override
  File makePipe(String path) {
    runSync(<String>['mkfifo', path]);
86
    return fs.file(path);
87
  }
88 89 90

  @override
  String getExecutableName(String binaryName, { String winExtension }) => binaryName;
91 92
}

93 94 95
class _WindowsUtils extends OperatingSystemUtils {
  _WindowsUtils() : super._private();

96
  // This is a no-op.
97
  @override
98 99
  ProcessResult makeExecutable(File file) {
    return new ProcessResult(0, 0, null, null);
100
  }
101

102
  @override
103
  File which(String execName) {
104
    ProcessResult result = processManager.runSync(<String>['where', execName]);
105 106
    if (result.exitCode != 0)
      return null;
107
    return fs.file(result.stdout.trim().split('\n').first.trim());
108 109 110 111 112 113 114 115 116 117 118
  }

  @override
  void unzip(File file, Directory targetDirectory) {
    Archive archive = new ZipDecoder().decodeBytes(file.readAsBytesSync());

    for (ArchiveFile archiveFile in archive.files) {
      // The archive package doesn't correctly set isFile.
      if (!archiveFile.isFile || archiveFile.name.endsWith('/'))
        continue;

119
      File destFile = fs.file(path.join(targetDirectory.path, archiveFile.name));
120 121 122 123
      if (!destFile.parent.existsSync())
        destFile.parent.createSync(recursive: true);
      destFile.writeAsBytesSync(archiveFile.content);
    }
124
  }
125 126 127 128 129

  @override
  File makePipe(String path) {
    throw new UnsupportedError('makePipe is not implemented on Windows.');
  }
130 131 132 133 134 135 136 137

  @override
  String getExecutableName(String binaryName, { String winExtension }) {
    winExtension ??= 'exe';
    if (path.extension(binaryName).isEmpty && winExtension.isNotEmpty)
      return '$binaryName.$winExtension';
    return binaryName;
  }
138
}
139 140

Future<int> findAvailablePort() async {
141
  ServerSocket socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0);
142 143 144 145
  int port = socket.port;
  await socket.close();
  return port;
}
Devon Carew's avatar
Devon Carew committed
146

147
const int _kMaxSearchIterations = 20;
Devon Carew's avatar
Devon Carew committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

/// This method will attempt to return a port close to or the same as
/// [defaultPort]. Failing that, it will return any available port.
Future<int> findPreferredPort(int defaultPort, { int searchStep: 2 }) async {
  int iterationCount = 0;

  while (iterationCount < _kMaxSearchIterations) {
    int port = defaultPort + iterationCount * searchStep;
    if (await _isPortAvailable(port))
      return port;
    iterationCount++;
  }

  return findAvailablePort();
}

Future<bool> _isPortAvailable(int port) async {
  try {
166
    // TODO(ianh): This is super racy.
167
    ServerSocket socket = await ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, port);
Devon Carew's avatar
Devon Carew committed
168 169 170 171 172 173
    await socket.close();
    return true;
  } catch (error) {
    return false;
  }
}
174 175 176 177 178 179 180

/// Find and return the project root directory relative to the specified
/// directory or the current working directory if none specified.
/// Return `null` if the project root could not be found
/// or if the project root is the flutter repository root.
String findProjectRoot([String directory]) {
  const String kProjectRootSentinel = 'pubspec.yaml';
181
  directory ??= fs.currentDirectory.path;
182
  while (true) {
183
    if (fs.isFileSync(path.join(directory, kProjectRootSentinel)))
184
      return directory;
185
    String parent = path.dirname(directory);
186 187 188 189
    if (directory == parent) return null;
    directory = parent;
  }
}