|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:path/path.dart' as p; |
| 8 | + |
| 9 | +// Replaces the path separators according to current platform. |
| 10 | +String _replaceSeparators(String path) { |
| 11 | + if (Platform.isWindows) { |
| 12 | + return path.replaceAll(p.posix.separator, p.windows.separator); |
| 13 | + } else { |
| 14 | + return path.replaceAll(p.windows.separator, p.posix.separator); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/// Replaces the path separators according to current platform, and normalizes . |
| 19 | +/// and .. in the path. If a relative path is passed in, it is resolved relative |
| 20 | +/// to the config path, and the absolute path is returned. |
| 21 | +String normalizePath(String path, String? configFilename) { |
| 22 | + final resolveInConfigDir = |
| 23 | + (configFilename == null) || p.isAbsolute(path) || path.startsWith('**'); |
| 24 | + return _replaceSeparators(p.normalize(resolveInConfigDir |
| 25 | + ? path |
| 26 | + : p.absolute(p.join(p.dirname(configFilename), path)))); |
| 27 | +} |
| 28 | + |
| 29 | +/// Replaces any variable names in the path with the corresponding value. |
| 30 | +String substituteVars(String path) { |
| 31 | + for (final variable in _variables) { |
| 32 | + final key = '\$${variable.key}'; |
| 33 | + if (path.contains(key)) { |
| 34 | + path = path.replaceAll(key, variable.value); |
| 35 | + } |
| 36 | + } |
| 37 | + return path; |
| 38 | +} |
| 39 | + |
| 40 | +class _LazyVariable { |
| 41 | + _LazyVariable(this.key, this._cmd, this._args); |
| 42 | + final String key; |
| 43 | + final String _cmd; |
| 44 | + final List<String> _args; |
| 45 | + String? _value; |
| 46 | + String get value => _value ??= firstLineOfStdout(_cmd, _args); |
| 47 | +} |
| 48 | + |
| 49 | +final _variables = <_LazyVariable>[ |
| 50 | + _LazyVariable('XCODE', 'xcode-select', ['-p']), |
| 51 | + _LazyVariable('IOS_SDK', 'xcrun', ['--show-sdk-path', '--sdk', 'iphoneos']), |
| 52 | + _LazyVariable('MACOS_SDK', 'xcrun', ['--show-sdk-path', '--sdk', 'macosx']), |
| 53 | +]; |
| 54 | + |
| 55 | +String firstLineOfStdout(String cmd, List<String> args) { |
| 56 | + final result = Process.runSync(cmd, args); |
| 57 | + assert(result.exitCode == 0); |
| 58 | + return (result.stdout as String) |
| 59 | + .split('\n') |
| 60 | + .where((line) => line.isNotEmpty) |
| 61 | + .first; |
| 62 | +} |
0 commit comments