Skip to content

Commit

Permalink
📝 Add WindowsSubSystemLinux command run support
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Jul 22, 2024
1 parent 07f91d2 commit c20344b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/windows/ms_open.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:url_launcher/url_launcher_string.dart';

void openMSStoreProduct(String productID) {
Expand All @@ -7,3 +9,9 @@ void openMSStoreProduct(String productID) {
void openMSSetting(String name) {
launchUrlString("ms-settings:$name");
}

void openInExplorer(String path) async {
await Process.run("explorer.exe", [path]);
}

void openWSLDirExplorer() async => openInExplorer("C:\\Program Files\\WSL");
1 change: 1 addition & 0 deletions lib/windows/sh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Future<Process> enableFeature(String featurename) async {
"/norestart"
]);
}

61 changes: 61 additions & 0 deletions lib/windows/wsl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'dart:io';

class WindowsSubSystemLinux {
static void shutdown(String target, {String? distro}) async {
await Process.run("wsl", [
...distro != null ? ["-d", distro] : [],
"--shutdown"
]);
}

static Future<Process> sudo({
String? distro,
String? user,
required String password,
Iterable<String> commands = const [],
}) async {
return await exec(
commands: ["echo", password, "|", "sudo", "-S", ...commands],
distro: distro,
user: user,
);
}

static Future<Process> exec({
String? distro,
String? user,
Iterable<String> commands = const [],
}) async {
return await Process.start(
"wsl",
[
...distro != null ? ["-d", distro] : [],
...user != null ? ["-u", user] : [],
"-e",
"sh",
"-c",
commands.join(" ")
],
);
}

static Future<List<String>> getAvailableDistro() async {
return (await Process.run("wsl", ["-l", "-q"]))
.stdout
.toString()
.split("\n")
.map((line) => line.trim())
.where((line) => line.isNotEmpty)
.toList();
}

static List<String> getAvailableDistroSync() {
return Process.runSync("wsl", ["-l", "-q"])
.stdout
.toString()
.split("\n")
.map((line) => line.trim())
.where((line) => line.isNotEmpty)
.toList();
}
}

0 comments on commit c20344b

Please sign in to comment.