1+ Import-Module PSWorkflow
2+ function Show-Menu {
3+ Clear-Host
4+ Write-Host " _ _______ __ "
5+ Write-Host " ___ ____ ________ _| | / / ___// / "
6+ Write-Host " / _ \/ __ `/ ___/ / / / | /| / /\__ \/ / "
7+ Write-Host " / __/ /_/ (__ ) /_/ /| |/ |/ /___/ / /___"
8+ Write-Host " \___/\__,_/____/\__, / |__/|__//____/_____/"
9+ Write-Host " /____/ "
10+
11+ Write-Host " 1: Install Distro"
12+ Write-Host " 2: Show currently installed distros"
13+ Write-Host " 3: Unregister Distro"
14+ Write-Host " H: Help"
15+ Write-Host " Q: Quit"
16+ }
17+
18+ Show-Menu
19+ $selection = Read-Host " Choose what you want to do"
20+ switch ($selection ) {
21+
22+ " 1" {
23+ $DistroName = Read-Host " Name of your distro"
24+ if ($DistroName ) {
25+ Write-Host " You've choosen [$DistroName ] as the name for your distro"
26+ } else {
27+ Write-Warning - Message " No name input."
28+ }
29+ $DockerContainer = Read-Host " Docker Hub repo name (for example: archlinux:latest)"
30+ if ($DockerContainer ) {
31+ Write-Host " You've choosen [$DockerContainer ] as your Docker container"
32+ } else {
33+ Write-Warning - Message " No Docker container name input."
34+ }
35+ }
36+
37+ " 2" {
38+ wsl.exe - l - v
39+ }
40+
41+ " 3" {
42+ wsl.exe - l
43+ Read-Host " Name of your distro that you want to unregister"
44+ if ($UnregisterDistro ) {
45+ Write-Host " You've choosen [$UnregisterDistro ] as the name for your distro"
46+ } else {
47+ Write-Warning - Message " No name input."
48+ }
49+ }
50+
51+ " H" {
52+ Start-Process " https://github.com/redcode-labs/easyWSL/blob/master/README.md"
53+ exit
54+ }
55+
56+ " q" {
57+ exit
58+ }
59+ " Q" {
60+ exit
61+ }
62+ default {
63+ Write-Host " Not a correct command"
64+ Show-Menu
65+ $selection = Read-Host " Choose what you want to do"
66+ switch ($selection ) {
67+
68+ " 1" {
69+ $DistroName = Read-Host " Name of your distro"
70+ if ($DistroName ) {
71+ Write-Host " You've choosen [$DistroName ] as the name for your distro"
72+ } else {
73+ Write-Warning - Message " No name input."
74+ }
75+ $DockerContainer = Read-Host " Docker Hub repo name (for example: archlinux:latest)"
76+ if ($DockerContainer ) {
77+ Write-Host " You've choosen [$DockerContainer ] as your Docker container"
78+ } else {
79+ Write-Warning - Message " No Docker container name input."
80+ }
81+ }
82+
83+ " 2" {
84+ wsl.exe - l - v
85+ }
86+
87+ " 3" {
88+ wsl.exe - l
89+ Read-Host " Name of your distro that you want to unregister"
90+ if ($UnregisterDistro ) {
91+ Write-Host " You've choosen [$UnregisterDistro ] as the name for your distro"
92+ } else {
93+ Write-Warning - Message " No name input."
94+ }
95+ }
96+
97+ " H" {
98+ Start-Process " https://github.com/redcode-labs/easyWSL/blob/master/README.md"
99+ exit
100+ }
101+
102+ " q" {
103+ exit
104+ }
105+ " Q" {
106+ exit
107+ }
108+ default {
109+ exit
110+ }
111+ }
112+ }
113+ }
114+ function ChooseInstallationDirectory {
115+ do
116+ {
117+ $script :DistroInstallationDirectory = Read-Host - Prompt " Type the path to directory where you want to store all your custom made distros "
118+ if (($script :DistroInstallationDirectory.Substring ($script :DistroInstallationDirectory.Length - 1 ) -eq " \" )) {
119+ $script :DistroInstallationDirectory = $script :DistroInstallationDirectory.Substring (0 , $script :DistroInstallationDirectory.Length - 1 )
120+ }
121+
122+ $script :DistroDir = $script :DistroInstallationDirectory + " \" + $script :DistroName
123+ if (-Not (Test-Path - Path $script :DistroInstallationDirectory )) {write-Host " This is not a correct path!" }
124+ } while (-Not (Test-Path - Path $script :DistroInstallationDirectory ))
125+
126+ }
127+
128+
129+ ChooseInstallationDirectory
130+
131+
132+ $confirmation = Read-Host " Are you sure you want to install" $DistroName " to path" $DistroDir " [y/n]"
133+ while ($confirmation -eq " n" )
134+ {
135+ if ($confirmation -eq ' y' ) {break }
136+ ChooseInstallationDirectory
137+ $confirmation = Read-Host " Are you sure you want to install" $DistroName " to path" $DistroDir " ? [y/n]"
138+ }
139+
140+ $confirmation = Read-Host " Do you want to set" $DistroName " as a default wsl distro? [y/n]"
141+ if ($confirmation -eq ' y' ) {
142+ $DefaultDistro = " true"
143+ }
144+ else {
145+ $DefaultDistro = " false"
146+ }
147+
148+
149+ $DockerWingetPackageName = " Docker.DockerDesktop"
150+ $DockerContainerLocal = " wsl" + $DistroName
151+ $DockerExportTarName = " install.tar"
152+
153+
154+ workflow Install-easyWSL {
155+ param (
156+ $DockerWingetPackageName ,
157+ $DistroInstallationDirectory ,
158+ $DistroName ,
159+ $DockerExportTarName ,
160+ $DistroDir ,
161+ $DockerContainerLocal ,
162+ $DockerContainer
163+ )
164+
165+ Set-ExecutionPolicy Unrestricted
166+
167+ # check if Docker is installed on a machine, if not install it
168+ if (-Not (Get-Command docker - errorAction SilentlyContinue))
169+ {
170+ if (Get-Command winget - errorAction SilentlyContinue)
171+ {
172+ winget install - e -- id Docker.DockerDesktop
173+ } else {
174+ Invoke-WebRequest - Uri " https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe" - OutFile $env: USERPROFILE " \Downloads"
175+ Start-Process - Wait " c:\Users\$env: USERNAME \Downloads\Docker Desktop Installer.exe"
176+ }
177+ }
178+
179+ # ask user to confirm rebooting machine
180+ $confirmation = Read-Host " Your computer will have to be rebooted. Are you ready to do that now? [y/n]"
181+ do
182+ {
183+ if (-Not ($confirmation -eq ' y' )) {
184+ $confirmation = Read-Host " Maybe now? [y/n]"
185+ }
186+ } while ($confirmation -eq " n" )
187+
188+ Restart-Computer - Wait
189+
190+ # create a directory for the distribution inside a user specified directory
191+ New-Item - Path $DistroInstallationDirectory - Name $DistroName - ItemType " directory"
192+
193+ # run the docker container with a chosen distro and extract the rootfs to the .tar file located in a distribution directory
194+ docker run - it -- name $DockerContainerLocal $DockerContainer
195+ docker export -- output= $DistroDir + " \" + $DockerExportTarName $DockerContainerLocal
196+
197+ # register the distribution in wsl
198+ wsl.exe -- import $DistroName $DistroDir $DistroDir + " \" + $DockerExportTarName
199+
200+ # set the distro default in wsl if the user specified that he want do it
201+ if ($DefaultDistro -eq " true" ) {
202+ wsl.exe -- set-default $DistroName
203+ }
204+
205+ # post setup, linux commands in here are specified to be for Arch-based systems
206+ wsl.exe -- distro $DistroName " pacman -Syu -y && pacman -S sudo -y && pacman -S vim -y && echo export EDITOR=/usr/bin/vim >> ~/.bashrc && pwconv && grpconv && chmod 0744 /etc/shadow && chmod 0744 /etc/gshadow && exit"
207+ }
208+
209+ Install-easyWSL $DockerWingetPackageName $DistroInstallationDirectory $DistroName $DockerExportTarName $DistroDir $DockerContainerLocal $DockerContainer
0 commit comments