Skip to content

Commit

Permalink
Automatically installing the "Firefox for Playwright" browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianis58 committed Mar 16, 2023
1 parent 9ae33f1 commit 70fc5ad
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
46 changes: 36 additions & 10 deletions WindowsIsoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@

Directory.CreateDirectory(config.DownloadFolder);

var exitCode = InstallPlaywrightDependencies();
if (exitCode != 0)
{
Console.WriteLine("[Fatal] Failed to install Playwright dependencies.");
return -1;
}

Console.WriteLine("[Success] Playwright dependencies installed.");

Console.WriteLine("[Info] Trying to obtain Windows 11 iso download link...");

using var playwright = await Playwright.CreateAsync();
Expand Down Expand Up @@ -84,7 +93,8 @@

if(null == downloadButton)
{
Console.WriteLine("[Error] Download button was not found.");
Console.WriteLine("[Fatal] Download button was not found.");
return -1;
}
else
{
Expand All @@ -98,24 +108,33 @@
{
httpClient.Timeout = TimeSpan.FromHours(2);

using (var filestream = new FileStream(Path.Combine(config.DownloadFolder, config.DownloadFilename), FileMode.Create, FileAccess.Write, FileShare.None))
try
{
using (var filestream = new FileStream(Path.Combine(config.DownloadFolder, config.DownloadFilename), FileMode.Create, FileAccess.Write, FileShare.None))
{
var progress = new Progress<float>();
progress.ProgressChanged += ProgressChanged;
await httpClient.DownloadAsync(isoFileUrl, filestream, progress);
}
}
catch(UnauthorizedAccessException e)
{
var progress = new Progress<float>();
progress.ProgressChanged += ProgressChanged;
await httpClient.DownloadAsync(isoFileUrl, filestream, progress);
Console.WriteLine($"[Fatal] Can't write at path: {Path.Combine(config.DownloadFolder, config.DownloadFilename)}. Try again from a terminal running as administrator.");
return -1;
}
}
}
else
{
Console.WriteLine($"[Error] Found a download link but it seems incorrect: {isoFileUrl}");
Console.WriteLine($"[Fatal] Found a download link but it seems incorrect: {isoFileUrl}");
return -1;
}
}

Console.WriteLine();
Console.WriteLine("[Success] ISO Downloaded successfully! Exiting with code 0.");
Console.WriteLine();
Console.WriteLine("[Success] ISO Downloaded successfully! Exiting with code 0.");

return 0;
return 0;
}

void ProgressChanged(object? sender, float e)
{
Expand All @@ -138,3 +157,10 @@ void ErrorLoadingConfig()
{
Console.WriteLine("[Fatal] Unable to load the configuration. Exiting with code -1.");
}

int InstallPlaywrightDependencies()
{
Console.WriteLine("[Info] Installing dependencies for Playwright...");

return Microsoft.Playwright.Program.Main(new[] { "install", "firefox" });
}
2 changes: 1 addition & 1 deletion WindowsIsoDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
19 changes: 9 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ Windows Iso Downloader is a tool that allows to programatically download the lat
It can be usefull in projects that allows you to build a custom installer for Windows 11, to reduce manual actions.

## Building
1. Build it with the command
You'll need the dotnet 6 SDK installed to build it.
Then run:
```
dotnet build
```
2. If it's the first time you launch it and you don't use Microsoft Playwright on your computer, you'll have to run these commands in PowerShell in the build output directory:

## Run it
1. Build it with above instructions or download it from the releases section.
2. Open a terminal as administrator
3. cd to the right folder then run:
```
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\playwright.ps1 install
./WindowsIsoDownloader.exe
```
3. Run the WindowsIsoDownloader.exe produced at the step 1.
4. The result will be a windows11.iso file in C:\windows11.iso

## Why not publishing a release?
I prefer not to publish a pre-built executable. The goal is to have as little code as possible,
so that it's very easy to audit it by reading the sources.
4. Sit back and relax. 😎 The result will be a windows11.iso file in C:\windows11.iso

## License
MIT License
Expand Down

0 comments on commit 70fc5ad

Please sign in to comment.