Skip to content

Commit

Permalink
🐛 fix windows terminal profile settings update
Browse files Browse the repository at this point in the history
  • Loading branch information
AmineDjeghri committed Apr 28, 2024
1 parent 64bc3bd commit 761bd6a
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 90 deletions.
2 changes: 1 addition & 1 deletion docs/windows_workflow/browser_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The Following extensions are all available in brave, chrome & edge (Chromium-bas
- `Reddit Enhancement Suite`: A suite of tools to enhance your Reddit browsing experience.
- `Return YouTube Dislike`: Return the dislike count on YouTube.
- `SingleFile`: Save a complete page into a single HTML file.
- `Shopping Assistant de Revolut`
- `Shopping Assistant de Revolut`
- `Tree Style Tab`: Show tabs like a tree.
- `Widilo Cashback`

Expand Down
3 changes: 2 additions & 1 deletion docs/windows_workflow/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"face": "FiraCode Nerd Font",
"size": 12.0
},
"opacity": 90
"opacity": 90,
"startingDirectory": "."
},
"list":
[
Expand Down
272 changes: 184 additions & 88 deletions docs/windows_workflow/setup_windows.ps1
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
# Check if the script is running with administrative privileges
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
Write-Host "Please run this script as an administrator."
exit
}
## Check if the script is running with administrative privileges
#if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
#{
# Write-Host "Please run this script as an administrator."
# exit
#}

function Install-WSL
{
# Enable WSL feature
wsl.exe --install -d 'ubuntu'
wsl.exe --install -d 'ubuntu' --no-launch
Write-Host "WSL installation started. If ubuntu doesn't start, please run a new terminal and type 'ubuntu' in powershell." -ForegroundColor Yellow
$wslStatus = wsl.exe --list --quiet
Write-Host "All available WSL instances: $wslStatus" -ForegroundColor Yellow
if (wsl -l |Where { $_.Replace("`0", "") -match '^Ubuntu' })
{
Write-Host "WSL is installed." -ForegroundColor Green

# updating windows terminal settings
$settingsFilePath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
$settingsContent = Get-Content -Path $settingsFilePath -Raw | ConvertFrom-Json
$ubuntuProfile = $settingsContent.profiles.list | Where-Object { $_.name -eq "Ubuntu" }

if ($ubuntuProfile)
{
Write-Host "Found Ubuntu profile in settings. Updating default behavior..."

$ubuntuProfile | Add-Member -NotePropertyName "startingDirectory" -NotePropertyValue "~" -Force
$ubuntuProfile | Add-Member -NotePropertyName "commandline" -NotePropertyValue "ubuntu run" -Force

# Save the updated settings back to the file
$settingsContent | ConvertTo-Json -Depth 100 | Set-Content -Path $settingsFilePath

Write-Host "Ubuntu profile updated with default behavior set to %homeuser%."
}
else
{
Write-Host "Ubuntu profile not found in settings."
}

Write-Host "Settings update completed."

# run wsl
wsl -d 'Ubuntu'
}
else
{
Write-Host "WSL installation failed." -ForegroundColor Red
}
}

function Export-WSL
Expand Down Expand Up @@ -171,77 +209,107 @@ function Install-Windows-Apps
Install-Apps -selectedApps $selectedApps -apps $apps
}

# function to install fonts
function Install-FiraCode-Font
function Update-Terminal-Settings()
# used inside the function Update-Terminal-UI
{
$windowsTerminalSettingsDirectory = "$env:UserProfile\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState"
$settingsJsonFileName = "settings.json"
# Check if the Font is installed
$fontName = "JetBrainsMonoNerdFont-Regular.ttf" # Adjust the font file name as needed
$fontPath = Join-Path -Path ([System.IO.Path]::GetFullPath("C:\Windows\Fonts")) -ChildPath $fontName
$isFontInstalled = Test-Path -Path $fontPath

$confirmationMessage = "Do you want to update the default apperance of Windows Terminal (font, size) ? The following file will be modified:`n`n$windowsTerminalSettingsDirectory\$settingsJsonFileName`n`nDo you want to continue?"
$confirmation = $host.ui.PromptForChoice("Confirmation", $confirmationMessage, @("&Yes", "&No"), 1)

if ($confirmation -eq 0)
if (-not $isFontInstalled)
{
Write-Host "Font $( $fontName ) is not installed. Please install the font" -ForegroundColor Red
exit
}
else
{
Write-Host "Updating Settings..."

$fontZipUrl = "https://github.com/AmineDjeghri/awesome-os-setup/raw/main/docs/JetBrainsMonoNerdFont-Regular.zip"
$fontsPath = "$env:TEMP\JetBrainsMono"

# Create the destination directory if it doesn't exist
if (-not(Test-Path -Path $fontsPath -PathType Container))
{
New-Item -ItemType Directory -Path $fontsPath | Out-Null
}

# Download the font zip file & extract the file
$fontZipPath = Join-Path -Path $fontsPath -ChildPath "JetBrainsMonoNerdFont-Regular.zip"
Invoke-WebRequest -Uri $fontZipUrl -OutFile $fontZipPath

Write-Host "Extracting font files..."
Expand-Archive -Path $fontZipPath -DestinationPath $fontsPath -Force

# Install all TTF files
$getFonts = Get-ChildItem -Path $fontsPath -Include '*.ttf', '*.ttc', '*.otf' -recurse
# Specify the path to your settings file
$settingsFilePath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"

$systemFontsPath = "C:\Windows\Fonts"
$getFonts = Get-ChildItem $fontsPath -Include '*.ttf', '*.ttc', '*.otf' -recurse
# Read the content of the settings file
$settingsContent = Get-Content -Path $settingsFilePath -Raw | ConvertFrom-Json

foreach ($fontFile in $getFonts)
# Update the default profile settings if they exist
if ($settingsContent.profiles -and $settingsContent.profiles.defaults)
{
$targetPath = Join-Path $systemFontsPath $fontFile.Name

if (Test-Path -Path $targetPath)
{
$FontFile.Name + " already installed"
$fontObject = New-Object PSObject -Property @{
face = "JetBrainsMono Nerd Font"
size = 12.0
}
else
$schemesObject = $settingsContent.schemes
# Check if the "Night Owl" scheme exists in the list of schemes
$schemeExists = $false
foreach ($scheme in $schemesObject)
{

"Installing font " + $fontFile.Name
#Extract Font information for Reqistry
$ShellFolder = (New-Object -COMObject Shell.Application).Namespace($fontsPath)
$ShellFile = $shellFolder.ParseName($fontFile.name)
$ShellFileType = $shellFolder.GetDetailsOf($shellFile, 2)
#Set the $FontType Variable
If ($ShellFileType -Like '*TrueType font file*')
if ($scheme.name -eq "Night Owl")
{
$FontType = '(TrueType)'
$schemeExists = $true
Write-Host "Scheme 'Night Owl' is present." -ForegroundColor Green
break
}
#Update Registry and copy font to font directory
$RegName = $shellFolder.GetDetailsOf($shellFile, 21) + ' ' + $FontType
New-ItemProperty -Name $RegName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $fontFile.name -Force | out-null
Copy-item $fontFile.FullName -Destination $systemFontsPath
"Done"
}

# If the scheme does not exist, create it
if (-not $schemeExists)
{
Write-Host "Scheme 'Night Owl' not found. Adding it now." -ForegroundColor Yellow
$newScheme = @{
name = "Night Owl"
cursorColor = "#80a4c2"
selectionBackground = "#1d3b53"
background = "#011627"
foreground = "#D6DEEB"
black = "#011627"
blue = "#82AAFF"
cyan = "#21C7A8"
green = "#22DA6E"
purple = "#C792EA"
red = "#EF5350"
white = "#FFFFFF"
yellow = "#c5e478"
brightBlack = "#575656"
brightBlue = "#82AAFF"
brightCyan = "#7FDBCA"
brightGreen = "#22DA6E"
brightPurple = "#C792EA"
brightRed = "#EF5350"
brightWhite = "#FFFFFF"
brightYellow = "#FFEB95"
}

$settingsContent.schemes += $newScheme
}

# Update the default profile settings

$settingsContent.profiles.defaults | Add-Member -NotePropertyName "colorScheme" -NotePropertyValue "Night Owl" -Force
$settingsContent.profiles.defaults | Add-Member -NotePropertyName "font" -NotePropertyValue $fontObject -Force
$settingsContent.profiles.defaults | Add-Member -NotePropertyName "opacity" -NotePropertyValue 90 -Force
$settingsContent.profiles.defaults | Add-Member -NotePropertyName "elevate" -NotePropertyValue $true -Force
$settingsContent.profiles.defaults | Add-Member -NotePropertyName "selectionBackground" -NotePropertyValue "#FFFF00" -Force
}

# Convert back to JSON and save the updated settings
$settingsContent | ConvertTo-Json -depth 100 | Set-Content -Path $settingsFilePath

Write-Host "Windows Terminal settings updated successfully." -ForegroundColor Green
}
}

Write-Host "Font installation complete." -ForegroundColor Yellow
# function to install fonts
function Update-Terminal-UI()
{
$windowsTerminalSettingsDirectory = "$env:UserProfile\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState"
$settingsJsonFileName = "settings.json"

# Clean up
Remove-Item -Path $fontZipPath -Force
Remove-Item -Path $fontsPath -Recurse -Force
$confirmationMessage = "Do you want to update the default apperance of Windows Terminal (font, size) ? The following file will be modified:`n`n$windowsTerminalSettingsDirectory\$settingsJsonFileName`n`nDo you want to continue?"
$confirmation = $host.ui.PromptForChoice("Confirmation", $confirmationMessage, @("&Yes", "&No"), 0)

if ($confirmation -eq 0)
{
# Check if the Font is installed
$fontName = "JetBrainsMonoNerdFont-Regular.ttf" # Adjust the font file name as needed
$fontPath = Join-Path -Path ([System.IO.Path]::GetFullPath("C:\Windows\Fonts")) -ChildPath $fontName
Expand All @@ -250,45 +318,73 @@ function Install-FiraCode-Font
if ($isFontInstalled)
{
Write-Host "Font $( $fontName ) is installed." -ForegroundColor Green
}
else
{

Write-Host "Font $( $fontName ) is not installed. Installing..." -ForegroundColor Yellow
$fontZipUrl = "https://github.com/AmineDjeghri/awesome-os-setup/raw/main/docs/JetBrainsMonoNerdFont-Regular.zip"
$fontsPath = "$env:TEMP\JetBrainsMono"

# Copy the json file to the Windows Terminal settings directory
$settingsJsonUrl = "https://raw.githubusercontent.com/AmineDjeghri/awesome-os-setup/main/docs/windows_workflow/settings.json"
# Create the destination directory if it doesn't exist
if (-not (Test-Path -Path $fontsPath -PathType Container))
{
New-Item -ItemType Directory -Path $fontsPath | Out-Null
}

# Specify the path to your settings file
$settingsFilePath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
# Download the font zip file & extract the file
$fontZipPath = Join-Path -Path $fontsPath -ChildPath "JetBrainsMonoNerdFont-Regular.zip"
Invoke-WebRequest -Uri $fontZipUrl -OutFile $fontZipPath

# Read the content of the settings file
$settingsContent = Get-Content -Path $settingsFilePath -Raw | ConvertFrom-Json
Write-Host "Extracting font files..."
Expand-Archive -Path $fontZipPath -DestinationPath $fontsPath -Force

# Update the default profile settings if they exist
if ($settingsContent.profiles -and $settingsContent.profiles.defaults)
# Install all TTF files
$getFonts = Get-ChildItem -Path $fontsPath -Include '*.ttf', '*.ttc', '*.otf' -recurse

$systemFontsPath = "C:\Windows\Fonts"
$getFonts = Get-ChildItem $fontsPath -Include '*.ttf', '*.ttc', '*.otf' -recurse

foreach ($fontFile in $getFonts)
{
$fontObject = New-Object PSObject -Property @{
face = "JetBrainsMono Nerd Font"
size = 12.0
$targetPath = Join-Path $systemFontsPath $fontFile.Name

if (Test-Path -Path $targetPath)
{
$FontFile.Name + " already installed"
}
else
{

$settingsContent.profiles.defaults.colorScheme = "Night Owl"
$settingsContent.profiles.defaults.elevate = $true
$settingsContent.profiles.defaults.font = $fontObject
$settingsContent.profiles.defaults.opacity = 90
"Installing font " + $fontFile.Name
#Extract Font information for Reqistry
$ShellFolder = (New-Object -COMObject Shell.Application).Namespace($fontsPath)
$ShellFile = $shellFolder.ParseName($fontFile.name)
$ShellFileType = $shellFolder.GetDetailsOf($shellFile, 2)
#Set the $FontType Variable
If ($ShellFileType -Like '*TrueType font file*')
{
$FontType = '(TrueType)'
}
#Update Registry and copy font to font directory
$RegName = $shellFolder.GetDetailsOf($shellFile, 21) + ' ' + $FontType
New-ItemProperty -Name $RegName -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Fonts" -PropertyType string -Value $fontFile.name -Force | out-null
Copy-item $fontFile.FullName -Destination $systemFontsPath
"Done"
}
}
Write-Host "Font installation complete." -ForegroundColor Yellow

# Convert back to JSON and save the updated settings
$settingsContent | ConvertTo-Json -depth 100 | Set-Content -Path $settingsFilePath

Write-Host "Windows Terminal settings updated successfully."

}
else
{
Write-Host "Font $( $fontName ) is not installed. Please make sure that you installed the font correctly" -ForegroundColor Red
# Clean up
Remove-Item -Path $fontZipPath -Force
Remove-Item -Path $fontsPath -Recurse -Force
}

Update-Terminal-Settings
}
else
{
Write-Host "Operation canceled. Windows Terminal's appearance wasn't updated."
Write-Host "Operation canceled. Windows Terminal's appearance wasn't updated." -ForegroundColor Yellow
}


Expand Down Expand Up @@ -323,8 +419,8 @@ function Show-Menu
Clear-Host
Write-Host "===== Windows/WSL Management Menu =====" -ForegroundColor Yellow
Write-Host "1. Install Windows Apps"
Write-Host "2. Install FiraCode font in Windows Terminal"
Write-Host "3. Install WSL"
Write-Host "2. Update Windows Terminal UI"
Write-Host "3. Install WSL (Ubuntu)"
Write-Host "4. Setup WSL with Linux"
Write-Host "5. Export WSL & Backup"
Write-Host "6. Optimize WSL size (coming soon...)"
Expand All @@ -343,7 +439,7 @@ function Execute-Choice
Install-Windows-Apps
}
'2' {
Install-FiraCode-Font
Update-Terminal-UI
}
'3' {
Install-WSL
Expand Down

0 comments on commit 761bd6a

Please sign in to comment.