Files
shorebird/packages/shorebird_cli/lib/src/executables/powershell.dart
T
2025-02-14 13:04:26 -06:00

50 lines
1.5 KiB
Dart

import 'dart:io';
import 'package:mason_logger/mason_logger.dart';
import 'package:scoped_deps/scoped_deps.dart';
import 'package:shorebird_cli/src/shorebird_process.dart';
/// A reference to a [Powershell] instance.
final powershellRef = create(Powershell.new);
/// The [Powershell] instance available in the current zone.
Powershell get powershell => read(powershellRef);
/// A wrapper around all powershell related functionality.
class Powershell {
/// Name of the powershell executable.
static const executable = 'powershell.exe';
/// Execute a powershell command with the provided [arguments].
Future<ShorebirdProcessResult> pwsh(
List<String> arguments, {
String? workingDirectory,
bool runInShell = false,
}) async {
final result = await process.run(executable, arguments, runInShell: true);
if (result.exitCode != ExitCode.success.code) {
throw ProcessException(
executable,
arguments,
'${result.stderr}',
result.exitCode,
);
}
return result;
}
/// Returns the version string of the given executable file.
Future<String> getExeVersionString(File exeFile) async {
final exePath = exeFile.path;
final pwshCommand = '(Get-Item -Path $exePath).VersionInfo.ProductVersion';
final result = await pwsh(['-Command', pwshCommand], runInShell: true);
var versionString = (result.stdout as String).trim();
if (!versionString.contains('+')) {
versionString += '+0';
}
return versionString;
}
}