Coding Tools Installation Guides

  1. Git and GitHub Setup
  2. Windows Terminal Setup
  3. Remote Server Administration Tools (RSAT)
  4. Python Setup

Git and GitHub Setup {#git-and-github}

First, we check if Git is installed. If not, we install it using winget install -e --id Git.Git. Then, we retrieve the username of the current user and validate it. If a Git username is already set and it's the same as the current username, we check the email. If the email is different, we ask the user if they want to update it. If the username is different, we ask the user if they want to overwrite it. Finally, we clone a repository if it doesn't exist.

# Check if Git is installed
if (!(Get-Command git -ErrorAction SilentlyContinue)) {
    # Install Git
    winget install -e --id Git.Git
}

# Get the username
$username = [Environment]::UserName

# Validate the username
if ([string]::IsNullOrEmpty($username)) {
    Write-Error "Unable to retrieve the username."
    exit 1
}

# Check if a Git username is already set
$gitUsername = git config --global user.name
$gitEmail = git config --global user.email
$newEmail = "$username@users.noreply.github.com"

if (![string]::IsNullOrEmpty($gitUsername) -and $gitUsername -eq $username) {
    # If the username is the same, check the email
    if ($gitEmail -ne $newEmail) {
        # Ask the user if they want to update the email
        $updateEmail = Read-Host "The Git email is set to ($gitEmail), but the new email would be ($newEmail). Do you want to update it? (y/n)"
        if ($updateEmail -eq 'y') {
            # Update the Git email
            git config --global user.email "$newEmail"
        }
    }
} else {
    # Ask the user if they want to overwrite the existing username
    $overwrite = Read-Host "A Git username is already set ($gitUsername). Do you want to overwrite it? (y/n)"
    if ($overwrite -eq 'y') {
        # Set up the Git configuration
        git config --global user.name "$username"
        git config --global user.email "$newEmail"
    }
}

# Define the repository name
$repo = "$username.github.io"

# Define the repository path
$repoPath = "$env:USERPROFILE\Source\Repos"

# Clone the repository if it doesn't exist
if (!(Test-Path -Path "$repoPath\$repo")) {
    # Ensure the parent directory exists
    New-Item -ItemType Directory -Path $repoPath -Force -ErrorAction SilentlyContinue

    cd $repoPath
    git clone --depth 1 https://github.com/$username/$repo.git
} else {
    Write-Output "The directory $repoPath\$repo already exists."
}

Windows Terminal Setup {#windows-terminal}

We install the Windows Terminal using winget install -e --id=Microsoft.WindowsTerminal, and then open the settings file.

# Install Windows Terminal
winget install -e --id=Microsoft.WindowsTerminal

# Open the settings file
code $env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json

Installing PowerShell {#installing-powershell}

PowerShell 7, also known as PowerShell Core, can be installed via the Windows Package Manager (winget). This version of PowerShell runs side-by-side with Windows PowerShell 5.1 and provides various enhancements and cross-platform support.

# Install PowerShell 7
winget install -e --id=Microsoft.PowerShell

# Verify the installation
pwsh -v

This command installs the latest version of PowerShell 7 and verifies the installation by checking the version. PowerShell 7 is a cross-platform task automation solution that includes a command-line shell, an object-oriented scripting language, and a set of tools for managing and automating the administration of your systems.

Remote Server Administration Tools (RSAT) {#remote-server-administration-tools-rsat}

We check if RSAT is already installed. If not, we install it using Add-WindowsCapability -Online -Name Rsat.GroupPolicy.Management.Tools~~~~0.0.1.0.

# Check if RSAT is already installed
if (!(Get-WindowsCapability -Online | Where-Object { $_.Name -like "Rsat.GroupPolicy.Management.Tools*" -and $_.State -eq "Installed" })) {
    # Install RSAT
    Add-WindowsCapability -Online -Name Rsat.GroupPolicy.Management.Tools~~~~0.0.1.0
} else {
    Write-Output "RSAT is already installed."
}

We can uninstall it using Remove-WindowsCapability -Online -Name Rsat.GroupPolicy.Management.Tools~~~~0.0.1.0.

Python Setup {#python}

We install Python using winget install python3 --source winget --scope machine.

# Install Python
# https://github.com/microsoft/winget-pkgs/issues/85695#issuecomment-1370891556
winget install python3 --source winget --scope machine

We can test the speed of different mirrors by downloading a file from each mirror and measuring the time it takes. Here's a PowerShell script that does this:

# Define the mirrors
$mirrors = @(
    "https://pypi.org",
    "https://mirrors.aliyun.com/pypi",
    "https://mirrors.jlu.edu.cn/pypi",
    "https://pypi.mirrors.ustc.edu.cn",
    "https://pypi.tuna.tsinghua.edu.cn"
)

# Define the test file URL path
$testFile = "/packages/53/7f/55721ad0501a9076dbc354cc8c63ffc2d6f1ef360f49ad0fbcce19d68538/pip-20.3.4.tar.gz#sha256=6773934e5f5fc3eaa8c5a44949b5b924fc122daa0a8aa9f80c835b4ca2a543fc"

# Start a job for each mirror
$jobs = $mirrors | ForEach-Object {
    Start-Job -ScriptBlock {
        param($mirror, $testFile)

        try {
            # Measure the time it takes to download the test file
            $time = Measure-Command {
                Invoke-WebRequest -Uri ($mirror + $testFile) -OutFile $null
            }

            return New-Object PSObject -Property @{
                "Mirror" = $mirror
                "Time" = $time.TotalSeconds
            }
        } catch {
            return New-Object PSObject -Property @{
                "Mirror" = $mirror
                "Result" = "Failed: $($_.Exception.Message)"
            }
        }
    } -ArgumentList $_, $testFile
}

# Wait for all jobs to complete
$jobs | Wait-Job

# Receive the results of all jobs
$results = $jobs | Receive-Job

# Stop and remove all jobs
$jobs | Stop-Job
$jobs | Remove-Job

# Print the time for each site
$results | ForEach-Object {
    if ($_.PSObject.Properties.Name -contains "Time") {
        Write-Output ("Mirror: " + $_.Mirror + ", Time: " + $_.Time + " seconds")
    } else {
        Write-Output ("Mirror: " + $_.Mirror + ", Result: " + $_.Result)
    }
}

# Find the fastest mirror
$fastestMirror = ($results | Where-Object { $_.PSObject.Properties.Name -contains "Time" } | Sort-Object { $_.Time -as [decimal] } | Select-Object -First 1).Mirror

# Print the fastest mirror
Write-Output ("Fastest Mirror: " + $fastestMirror)

# Create a pip configuration file
New-Item -Path $HOME\pip\pip.ini -ItemType File -Force | Out-Null

# Set the fastest mirror as the index-url
Add-Content -Path $HOME\pip\pip.ini -Value "[global]`nindex-url = $fastestMirror/simple" | Out-Null