Functions/GenXdev.AI/Approve-NewTextFileContent.ps1
<##############################################################################
Part of PowerShell module : GenXdev.AI Original cmdlet filename : Approve-NewTextFileContent.ps1 Original author : René Vaessen / GenXdev Version : 1.302.2025 ################################################################################ Copyright (c) René Vaessen / GenXdev Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ################################################################################> ############################################################################### <# .SYNOPSIS Interactive file content comparison and approval using WinMerge. .DESCRIPTION Facilitates content comparison and merging through WinMerge by creating a temporary file with proposed changes. The user can interactively review and modify changes before approving. Returns approval status and final content. .PARAMETER ContentPath The path to the target file for comparison and potential update. If the file doesn't exist, it will be created. .PARAMETER NewContent The proposed new content to compare against the existing file content. .PARAMETER Monitor The monitor to use, 0 = default, -1 is discard, -2 = Configured secondary monitor, defaults to $Global:DefaultSecondaryMonitor or 2 if not found .PARAMETER Width The initial width of the webbrowser window .PARAMETER Height The initial height of the webbrowser window .PARAMETER X The initial X position of the webbrowser window .PARAMETER Y The initial Y position of the webbrowser window .PARAMETER AcceptLang Set the browser accept-lang http header .PARAMETER Force Force enable debugging port, stopping existing browsers if needed .PARAMETER Edge Opens in Microsoft Edge .PARAMETER Chrome Opens in Google Chrome .PARAMETER Chromium Opens in Microsoft Edge or Google Chrome, depending on what the default browser is .PARAMETER Firefox Opens in Firefox .PARAMETER All Opens in all registered modern browsers .PARAMETER Left Place browser window on the left side of the screen .PARAMETER Right Place browser window on the right side of the screen .PARAMETER Top Place browser window on the top side of the screen .PARAMETER Bottom Place browser window on the bottom side of the screen .PARAMETER Centered Place browser window in the center of the screen .PARAMETER FullScreen Opens in fullscreen mode .PARAMETER Private Opens in incognito/private browsing mode .PARAMETER ApplicationMode Hide the browser controls .PARAMETER NoBrowserExtensions Prevent loading of browser extensions .PARAMETER DisablePopupBlocker Disable the popup blocker .PARAMETER NewWindow Don't re-use existing browser window, instead, create a new one .PARAMETER FocusWindow Focus the browser window after opening .PARAMETER SetForeground Set the browser window to foreground after opening .PARAMETER Maximize Maximize the window after positioning .PARAMETER SetRestored Restore the window to normal state after positioning .PARAMETER PassThru Returns a PowerShell object of the browserprocess .PARAMETER NoBorders Removes the borders of the window .PARAMETER RestoreFocus Restore PowerShell window focus .PARAMETER SideBySide Position browser window either fullscreen on different monitor than PowerShell, or side by side with PowerShell on the same monitor. .PARAMETER KeysToSend Keystrokes to send to the Window, see documentation for cmdlet GenXdev.Windows\Send-Key .PARAMETER SendKeyEscape Escape control characters and modifiers when sending keys .PARAMETER SendKeyHoldKeyboardFocus Hold keyboard focus on target window when sending keys .PARAMETER SendKeyUseShiftEnter Use Shift+Enter instead of Enter when sending keys .PARAMETER SendKeyDelayMilliSeconds Delay between different input strings in milliseconds when sending keys .PARAMETER SessionOnly Use alternative settings stored in session for AI preferences .PARAMETER ClearSession Clear alternative settings stored in session for AI preferences .PARAMETER SkipSession Store settings only in persistent preferences without affecting session .EXAMPLE $result = Approve-NewTextFileContent -ContentPath "C:\temp\myfile.txt" ` -NewContent "New file content" .EXAMPLE Approve-NewTextFileContent -ContentPath "C:\temp\myfile.txt" -NewContent "New content" -e -fs .NOTES Returns a hashtable with these properties: - approved: True if changes were saved, False if discarded - approvedAsIs: True if content was accepted without modifications - savedContent: Final content if modified by user - userDeletedFile: True if user deleted existing file #> function Approve-NewTextFileContent { [CmdletBinding()] param ( ######################################################################## [Parameter( Mandatory = $true, Position = 0, HelpMessage = 'Path to the target file for comparison' )] [ValidateNotNullOrEmpty()] [string]$ContentPath, ###################################################################### [Parameter( Mandatory = $false, Position = 1, HelpMessage = ('The monitor to use, 0 = default, -1 is discard, ' + '-2 = Configured secondary monitor, defaults to ' + "`$Global:DefaultSecondaryMonitor or 2 if not found") )] [Alias('m', 'mon')] [int] $Monitor = -2, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The initial width of the webbrowser window' )] [int] $Width = -1, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The initial height of the webbrowser window' )] [int] $Height = -1, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The initial X position of the webbrowser window' )] [int] $X = -999999, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'The initial Y position of the webbrowser window' )] [int] $Y = -999999, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Set the browser accept-lang http header' )] [Alias('lang', 'locale')] [string] $AcceptLang = $null, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Force enable debugging port, stopping existing ' + 'browsers if needed') )] [switch] $Force, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Opens in Microsoft Edge' )] [Alias('e')] [switch] $Edge, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Opens in Google Chrome' )] [Alias('ch')] [switch] $Chrome, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Opens in Microsoft Edge or Google Chrome, ' + 'depending on what the default browser is') )] [Alias('c')] [switch] $Chromium, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Opens in Firefox' )] [Alias('ff')] [switch] $Firefox, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Opens in all registered modern browsers' )] [switch] $All, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Place browser window on the left side of the screen' )] [switch] $Left, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Place browser window on the right side of the screen' )] [switch] $Right, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Place browser window on the top side of the screen' )] [switch] $Top, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Place browser window on the bottom side of the screen' )] [switch] $Bottom, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Place browser window in the center of the screen' )] [switch] $Centered, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Opens in fullscreen mode' )] [Alias('fs', 'f')] [switch] $FullScreen, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Opens in incognito/private browsing mode' )] [Alias('incognito', 'inprivate')] [switch] $Private, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Hide the browser controls' )] [Alias('a', 'app', 'appmode')] [switch] $ApplicationMode, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Prevent loading of browser ' + 'extensions') )] [Alias('de', 'ne', 'NoExtensions')] [switch] $NoBrowserExtensions, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Disable the popup blocker' )] [Alias('allowpopups')] [switch] $DisablePopupBlocker, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ("Don't re-use existing browser window, instead, " + 'create a new one') )] [Alias('nw', 'new')] [switch] $NewWindow, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Focus the browser window ' + 'after opening') )] [Alias('fw', 'focus')] [switch] $FocusWindow, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Set the browser window to foreground ' + 'after opening') )] [Alias('fg')] [switch] $SetForeground, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Maximize the window after positioning' )] [switch] $Maximize, ######################################################################## [Parameter( Mandatory = $false, HelpMessage = ('Restore the window to normal state ' + 'after positioning') )] [switch] $SetRestored, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Returns a PowerShell object of ' + 'the browserprocess') )] [Alias('pt')] [switch]$PassThru, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Removes the borders of the window' )] [Alias('nb')] [switch] $NoBorders, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = 'Restore PowerShell window focus' )] [Alias('rf', 'bg')] [switch] $RestoreFocus, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Position browser window either fullscreen on ' + 'different monitor than PowerShell, or side by side with ' + 'PowerShell on the same monitor.') )] [Alias('sbs')] [switch] $SideBySide, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Keystrokes to send to the Window, ' + 'see documentation for cmdlet GenXdev.Windows\Send-Key') )] [string[]] $KeysToSend, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Escape control characters and modifiers ' + 'when sending keys') )] [Alias('Escape')] [switch] $SendKeyEscape, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Hold keyboard focus on target window ' + 'when sending keys') )] [Alias('HoldKeyboardFocus')] [switch] $SendKeyHoldKeyboardFocus, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Use Shift+Enter instead of Enter ' + 'when sending keys') )] [Alias('UseShiftEnter')] [switch] $SendKeyUseShiftEnter, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Delay between different input strings in ' + 'milliseconds when sending keys') )] [Alias('DelayMilliSeconds')] [int] $SendKeyDelayMilliSeconds, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Use alternative settings stored in session for AI ' + 'preferences') )] [switch] $SessionOnly, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Clear alternative settings stored in session for AI ' + 'preferences') )] [switch] $ClearSession, ####################################################################### [Parameter( Mandatory = $false, HelpMessage = ('Store settings only in persistent preferences without ' + 'affecting session') )] [Alias('FromPreferences')] [switch] $SkipSession ####################################################################### ) begin { # ensure content path exists, create if missing $contentPath = GenXdev.FileSystem\Expand-Path $ContentPath ` -CreateFile # log the target file path for debugging Microsoft.PowerShell.Utility\Write-Verbose ( "Target file path: ${contentPath}") } process { # check initial file existence for tracking deletion $existed = [System.IO.File]::Exists($contentPath) # log initial file existence state Microsoft.PowerShell.Utility\Write-Verbose ( "File existed before comparison: ${existed}") # create temporary file with matching extension for comparison $tempFile = GenXdev.FileSystem\Expand-Path ( [System.IO.Path]::GetTempFileName() + [System.IO.Path]::GetExtension($contentPath)) ` -CreateDirectory # log temporary file creation Microsoft.PowerShell.Utility\Write-Verbose ( "Created temp comparison file: ${tempFile}") # write proposed content to temp file $NewContent | Microsoft.PowerShell.Utility\Out-File $tempFile -Force # launch winmerge for interactive comparison $null = GenXdev.AI\Invoke-WinMerge ` -SourcecodeFilePath $tempFile ` -TargetcodeFilePath $contentPath ` -Wait # prepare result tracking object $result = @{ approved = [System.IO.File]::Exists($contentPath) } if ($result.approved) { # read file content to check for modifications $content = [System.IO.File]::ReadAllText($contentPath) # compare original and modified content $changed = $content.Trim() -ne $NewContent.Trim() # track if content was accepted without changes $result.approvedAsIs = -not $changed if ($changed) { # store the user-modified content $result.savedContent = $content } } elseif ($existed) { # track if user deleted the existing file $result.userDeletedFile = $true } # log the comparison result Microsoft.PowerShell.Utility\Write-Verbose ( "Comparison result: $( $result | Microsoft.PowerShell.Utility\ConvertTo-Json)") return $result } end { } } |