$ErrorActionPreference = 'Stop'
$SolutionPath = 'C:\Users\usree\Downloads\PAD_CodeReview_Toolkit\solution'
$OutputFolder = 'C:\Users\usree\Downloads\PAD_CodeReview_Toolkit\robin_output'
Add-Type -AssemblyName System.IO.Compression.FileSystem
New-Item -ItemType Directory -Force -Path $OutputFolder | Out-Null
$WorkRoot = Join-Path $env:TEMP ('RobinExtract_' + (Get-Date -Format 'yyyyMMddHHmmss'))
New-Item -ItemType Directory -Force -Path $WorkRoot | Out-Null
$LogFile = Join-Path $OutputFolder 'extraction_log.txt'
$Summary = New-Object System.Collections.Generic.List[string]
$FuncRegex = New-Object System.Text.RegularExpressions.Regex(
'(?ms)(?<![\w.])FUNCTION[ \t]+(\S+)[ \t]+GLOBAL[ \t]*$.*?^[ \t]*END FUNCTION\b')
function Write-Log([string]$Message) {
$line = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss') + ' ' + $Message
Add-Content -Path $LogFile -Value $line -Encoding UTF8
}
function ConvertFrom-JsonEscaped([string]$Text) {
$t = [regex]::Replace($Text, '\\u([0-9a-fA-F]{4})', { param($m) [string][char][Convert]::ToInt32($m.Groups[1].Value, 16) })
$t = $t.Replace('\\', [string][char]0)
$t = $t.Replace('\r\n', "`n").Replace('\n', "`n").Replace('\r', "`n").Replace('\t', "`t").Replace('\"', '"').Replace('\/', '/')
return $t.Replace([string][char]0, '\')
}
function Get-DefinitionText([string]$Raw) {
# <Definition> holds the flow script as a JSON string: "....\r\n...."
$r = $Raw.Trim()
if ($r.StartsWith('"') -and $r.EndsWith('"')) {
$r = ConvertFrom-JsonEscaped ($r.Substring(1, $r.Length - 2))
}
return ($r -replace "`r`n", "`n" -replace "`r", "`n")
}
function Split-RobinScript([string]$Script) {
# Returns ordered subflows: Main (top-level code, not wrapped in FUNCTION) first, then each FUNCTION block
$result = [ordered]@{}
$matches2 = $FuncRegex.Matches($Script)
$mainEnd = $Script.Length
if ($matches2.Count -gt 0) { $mainEnd = $matches2[0].Index }
$mainLines = $Script.Substring(0, $mainEnd) -split "`n" | Where-Object {
$_ -notmatch '^\s*(@@|IMPORT\s|@SENSITIVE)'
}
$mainBody = ($mainLines -join "`n").Trim()
if ($mainBody) {
$result['Main'] = "FUNCTION Main GLOBAL`n" + $mainBody + "`nEND FUNCTION"
}
foreach ($m in $matches2) {
$name = $m.Groups[1].Value
if (-not $result.Contains($name)) { $result[$name] = $m.Value.Trim() }
}
return $result
}
function Save-Flow([string]$FlowName, $Subflows, [string]$Source) {
$safe = [regex]::Replace($FlowName, '[\\/:*?"<>|]', '_')
$outFile = Join-Path $OutputFolder ($safe + '.txt')
$content = (@($Subflows.Values) -join "`r`n`r`n") -replace "(?<!`r)`n", "`r`n"
[System.IO.File]::WriteAllText($outFile, $content, (New-Object System.Text.UTF8Encoding($false)))
$line = 'OK | ' + $FlowName + ' | ' + $Subflows.Count + ' subflows (' + (@($Subflows.Keys) -join ', ') + ') | ' + $outFile
Write-Log ($line + ' | source: ' + $Source)
$Summary.Add($line)
}
try {
Write-Log ('Start. Source: ' + $SolutionPath)
# 1. Collect solution .zip files (a single file or every .zip in the folder)
if (Test-Path $SolutionPath -PathType Leaf) {
$Zips = @(Get-Item $SolutionPath)
} elseif (Test-Path $SolutionPath -PathType Container) {
$Zips = @(Get-ChildItem -Path $SolutionPath -File -Filter '*.zip')
} else {
throw ('Solution path not found: ' + $SolutionPath)
}
if ($Zips.Count -eq 0) { throw ('No solution .zip found in ' + $SolutionPath) }
foreach ($zip in $Zips) {
# 2. Unzip
$root = Join-Path $WorkRoot $zip.BaseName
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip.FullName, $root)
Write-Log ('Unzipped ' + $zip.Name)
# 3. Every desktop flow (Category 6) listed in customizations.xml - any number of flows
$custPath = Join-Path $root 'customizations.xml'
if (-not (Test-Path $custPath)) {
$Summary.Add('SKIP | ' + $zip.Name + ' | customizations.xml not found (not a solution export?)')
continue
}
[xml]$cx = Get-Content -Path $custPath -Raw -Encoding UTF8
$desktopFlows = @($cx.SelectNodes('//Workflow') | Where-Object { [string]$_.Category -eq '6' })
$Summary.Add('SOLUTION | ' + $zip.Name + ' | desktop flows found: ' + $desktopFlows.Count)
Write-Log ('Desktop flows found: ' + $desktopFlows.Count)
$usedNames = @{}
foreach ($wf in $desktopFlows) {
$flowName = [string]$wf.Name
$flowId = ([string]$wf.WorkflowId).Trim('{', '}').ToLower()
if ($usedNames.ContainsKey($flowName)) { $flowName = $flowName + '_' + $flowId.Substring(0, 8) }
$usedNames[$flowName] = 1
# 4a. v2 schema: script is in this flow's own <Definition> element
$script = ''
$defNode = $wf.SelectSingleNode('Definition')
if ($defNode -and $defNode.InnerText.Trim()) {
$script = Get-DefinitionText $defNode.InnerText
$src = 'customizations.xml <Definition>'
}
# 4b. Fallback (older exports): files that belong to this flow (GUID in path or binary linked to it)
if (-not $script) {
$parts = New-Object System.Collections.Generic.List[string]
foreach ($f in Get-ChildItem -Path $root -Recurse -File) {
$belongs = $f.FullName.ToLower().Contains($flowId)
$binXml = ''
if ($f.Directory.Parent) { $binXml = Join-Path $f.Directory.Parent.FullName 'desktopflowbinary.xml' }
if (-not $belongs -and $binXml -and (Test-Path $binXml)) {
$belongs = ((Get-Content $binXml -Raw) -match [regex]::Escape($flowId))
}
if ($belongs -and $f.Extension -in @('.json', '.xml', '.txt', '.robin')) {
$t = [System.IO.File]::ReadAllText($f.FullName)
if ($t -match 'FUNCTION') {
$u = $t
for ($i = 0; $i -lt 3 -and $u -match '\\n|\\r'; $i++) { $u = ConvertFrom-JsonEscaped $u }
$parts.Add(($u -replace "`r`n", "`n"))
}
}
}
$script = $parts -join "`n"
$src = 'flow files'
}
if (-not $script.Trim()) {
$Summary.Add('EMPTY | ' + $flowName + ' | no code found in export')
Write-Log ('No code found for ' + $flowName)
continue
}
$subflows = Split-RobinScript $script
Save-Flow $flowName $subflows $src
}
}
Write-Log 'Completed.'
} catch {
Write-Log ('ERROR: ' + $_.Exception.Message)
$Summary.Add('ERROR | ' + $_.Exception.Message)
} finally {
Remove-Item -Path $WorkRoot -Recurse -Force -ErrorAction SilentlyContinue
}
$Summary.Add('Log: ' + $LogFile)
Write-Output ($Summary -join "`r`n")