From 693dd414df1e3996aee349aad69f210a8208a0cd Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 23 Aug 2024 01:43:26 +0200 Subject: [PATCH] Add scripts for configuring `git` --- scripts/Common/Software/git/Manage.ps1 | 104 +++++++++++++++++++++++- scripts/Windows/OS/Install.ps1 | 12 +-- scripts/Windows/Software/git/Manage.ps1 | 44 +++------- 3 files changed, 120 insertions(+), 40 deletions(-) diff --git a/scripts/Common/Software/git/Manage.ps1 b/scripts/Common/Software/git/Manage.ps1 index 3bc76b77..cba9ae6e 100644 --- a/scripts/Common/Software/git/Manage.ps1 +++ b/scripts/Common/Software/git/Manage.ps1 @@ -4,8 +4,108 @@ param ( ) . "$PSScriptRoot/../../Scripts/Software.ps1"; +. "$PSScriptRoot/../../Scripts/System.ps1"; . "$PSScriptRoot/../../Types/InstallerAction.ps1"; -Start-SoftwareInstaller @PSBoundParameters ` - -Configurator { +$null = New-Module { + param( + [hashtable] $Parameters + ) + + $configure = { + param( + [string] $User + ) + + $root = "valhalla"; + + if ($User) { + $root = "$root$($IsWindows ? ".windows" : '').users.$User"; + $sudoArgs = @("-u", $User); + $configArgs = @("--global"); + } else { + $sudoArgs = @(); + $configArgs = @("--system"); + } + + <# + .SYNOPSIS + Gets the specified git configuration. + + .PARAMETER Name + THe name of the configuration to get. + #> + function Get-GitConfig { + param( + [string] $Name + ) + + Get-Config "$root.git.$Name"; + } + + <# + .SYNOPSIS + Sets a configuration option in git. + #> + function Set-GitConfig { + sudo @sudoArgs git config @configArgs $args; + } + + if ((-not $IsWindows) -or $User) { + $branch = Get-GitConfig "defaultBranch"; + + if ($branch) { + Set-GitConfig "init.defaultBranch" $branch; + } + } + + # Git Flow + . { + $dir = New-TemporaryDirectory; + $key = "flow"; + $mainBranch = Get-GitConfig "$key.mainBranch"; + $devBranch = Get-GitConfig "$key.devBranch"; + + & { + git -C "$dir" init; + git -C "$dir" config user.name "PortValhalla"; + git -C "$dir" config user.email "no-reply@valhal.la"; + git -C "$dir" commit --allow-empty -m "Initial commit"; + git -C "$dir" branch master; + git -C "$dir" branch dev; + git -C "$dir" flow init --defaults; + } | Out-Null; + + if ($mainBranch) { + git -C "$dir" branch $mainBranch | Out-Null;; + sudo @sudoArgs git -C "$dir" flow config set @configArgs master $mainBranch; + } + + if ($devBranch) { + git -C "$dir" branch $devBranch | Out-Null; + sudo @sudoArgs git -C "$dir" flow config set @configArgs develop $devBranch; + } + + Remove-Item -Recurse -Force $dir; + }; + + # Aliases + [PSCustomObject] $aliases = Get-GitConfig "aliases"; + + foreach ($alias in ($aliases | Get-Member -MemberType Properties)) { + Set-GitConfig "alias.$($alias.Name)" $aliases.$($alias.Name); + } }; + + Start-SoftwareInstaller @Parameters ` + -Configurator { + & $configure @PSBoundParameters; + } ` + -UserConfigurator { + param( + [hashtable] $Arguments + ) + + & $configure -User $Arguments.Name; + }; +} $PSBoundParameters; diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 1658f41e..45e53b17 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -134,16 +134,16 @@ $null = New-Module { }; } - if (-not (Test-Command "git")) { - Install-ChocoPackage git; - refreshenv; - continue; - } - if (-not (Test-Winget)) { . "$PSScriptRoot/../Software/winget/Manage.ps1"; continue; } + + if (-not (Test-Command "git")) { + Install-WingetPackage Git.Git; + refreshenv; + continue; + } if (-not (& { $null = wsl --status; $?; })) { wsl --install --no-launch; diff --git a/scripts/Windows/Software/git/Manage.ps1 b/scripts/Windows/Software/git/Manage.ps1 index 8eb32247..5b761630 100644 --- a/scripts/Windows/Software/git/Manage.ps1 +++ b/scripts/Windows/Software/git/Manage.ps1 @@ -7,38 +7,13 @@ param ( . "$PSScriptRoot/../../../Common/Scripts/Software.ps1"; . "$PSScriptRoot/../../../Common/Scripts/Config.ps1"; -$null = New-Module { +& { param( [hashtable] $Parameters ) . "$PSScriptRoot/../../../Common/Types/InstallerAction.ps1"; - - <# - .SYNOPSIS - Gets a configuration value related to git. - - .PARAMETER Name - The name of the configuration to get. - - .PARAMETER User - The name of the user to get the configuration for. - #> - function Get-GitOption { - param( - [Parameter(Mandatory)] - [string] $Name, - [string] $User - ) - - $config = "git.$Name"; - - if ($User) { - Get-UserConfig -UserName $User -Name $config; - } else { - Get-Config $config; - } - } + $base = "$PSScriptRoot/../../../Common/Software/git/Manage.ps1"; Start-SoftwareInstaller @Parameters ` -Installer { @@ -47,7 +22,7 @@ $null = New-Module { ) $params = "/WindowsTerminalProfile"; - $defaultBranch = Get-GitOption "defaultBranch"; + $defaultBranch = Get-Config "valhalla.git.defaultBranch"; if ($defaultBranch) { $params += " /DefaultBranchName:`"$defaultBranch`""; @@ -56,8 +31,13 @@ $null = New-Module { Install-ChocoPackage git -ArgumentList "--params",$params; } ` -Configurator { - & "$PSScriptRoot/../../../Common/Software/git/Manage.ps1" @Parameters; - }; + & $base ([InstallerAction]::Configure); + } ` + -UserConfigurator { + param( + $Arguments + ) - Export-ModuleMember -Function @(); -} -ArgumentList $PSBoundParameters + & $base ([InstallerAction]::ConfigureUser) @PSBoundParameterrs; + }; +} $PSBoundParameters;