mirror of
https://github.com/Nikolay-Shirokov/cc-1c-skills.git
synced 2026-06-12 17:04:57 +03:00
Auto-build: opencode (powershell) from 6d119eb
This commit is contained in:
@@ -0,0 +1,869 @@
|
||||
# cf-edit v1.4 — Edit 1C configuration root (Configuration.xml)
|
||||
# Source: https://github.com/Nikolay-Shirokov/cc-1c-skills
|
||||
param(
|
||||
[Parameter(Mandatory)][Alias('Path')][string]$ConfigPath,
|
||||
[string]$DefinitionFile,
|
||||
[ValidateSet("modify-property","add-childObject","remove-childObject","add-defaultRole","remove-defaultRole","set-defaultRoles","set-panels","set-home-page")]
|
||||
[string]$Operation,
|
||||
[string]$Value,
|
||||
[switch]$NoValidate
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
|
||||
# --- Mode validation ---
|
||||
if ($DefinitionFile -and $Operation) { Write-Error "Cannot use both -DefinitionFile and -Operation"; exit 1 }
|
||||
if (-not $DefinitionFile -and -not $Operation) { Write-Error "Either -DefinitionFile or -Operation is required"; exit 1 }
|
||||
|
||||
# --- Resolve path ---
|
||||
if (-not [System.IO.Path]::IsPathRooted($ConfigPath)) {
|
||||
$ConfigPath = Join-Path (Get-Location).Path $ConfigPath
|
||||
}
|
||||
if (Test-Path $ConfigPath -PathType Container) {
|
||||
$candidate = Join-Path $ConfigPath "Configuration.xml"
|
||||
if (Test-Path $candidate) { $ConfigPath = $candidate }
|
||||
else { Write-Error "No Configuration.xml in directory"; exit 1 }
|
||||
}
|
||||
if (-not (Test-Path $ConfigPath)) { Write-Error "File not found: $ConfigPath"; exit 1 }
|
||||
$resolvedPath = (Resolve-Path $ConfigPath).Path
|
||||
$script:configDir = [System.IO.Path]::GetDirectoryName($resolvedPath)
|
||||
|
||||
# --- Load XML with PreserveWhitespace ---
|
||||
$script:xmlDoc = New-Object System.Xml.XmlDocument
|
||||
$script:xmlDoc.PreserveWhitespace = $true
|
||||
$script:xmlDoc.Load($resolvedPath)
|
||||
|
||||
$script:addCount = 0
|
||||
$script:removeCount = 0
|
||||
$script:modifyCount = 0
|
||||
|
||||
function Info([string]$msg) { Write-Host "[INFO] $msg" }
|
||||
function Warn([string]$msg) { Write-Host "[WARN] $msg" }
|
||||
|
||||
# --- Detect structure ---
|
||||
$root = $script:xmlDoc.DocumentElement
|
||||
$script:mdNs = "http://v8.1c.ru/8.3/MDClasses"
|
||||
$script:xrNs = "http://v8.1c.ru/8.3/xcf/readable"
|
||||
$script:xsiNs = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
$script:v8Ns = "http://v8.1c.ru/8.1/data/core"
|
||||
|
||||
$script:cfgEl = $null
|
||||
foreach ($child in $root.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq "Configuration") {
|
||||
$script:cfgEl = $child; break
|
||||
}
|
||||
}
|
||||
if (-not $script:cfgEl) { Write-Error "No <Configuration> element found"; exit 1 }
|
||||
|
||||
$script:propsEl = $null
|
||||
$script:childObjsEl = $null
|
||||
foreach ($child in $script:cfgEl.ChildNodes) {
|
||||
if ($child.NodeType -ne 'Element') { continue }
|
||||
if ($child.LocalName -eq "Properties") { $script:propsEl = $child }
|
||||
if ($child.LocalName -eq "ChildObjects") { $script:childObjsEl = $child }
|
||||
}
|
||||
|
||||
$script:objName = ""
|
||||
foreach ($child in $script:propsEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq "Name") {
|
||||
$script:objName = $child.InnerText.Trim(); break
|
||||
}
|
||||
}
|
||||
Info "Configuration: $($script:objName)"
|
||||
|
||||
# --- Canonical type order for ChildObjects (44 types) ---
|
||||
$script:typeOrder = @(
|
||||
"Language","Subsystem","StyleItem","Style",
|
||||
"CommonPicture","SessionParameter","Role","CommonTemplate",
|
||||
"FilterCriterion","CommonModule","CommonAttribute","ExchangePlan",
|
||||
"XDTOPackage","WebService","HTTPService","WSReference",
|
||||
"EventSubscription","ScheduledJob","SettingsStorage","FunctionalOption",
|
||||
"FunctionalOptionsParameter","DefinedType","CommonCommand","CommandGroup",
|
||||
"Constant","CommonForm","Catalog","Document",
|
||||
"DocumentNumerator","Sequence","DocumentJournal","Enum",
|
||||
"Report","DataProcessor","InformationRegister","AccumulationRegister",
|
||||
"ChartOfCharacteristicTypes","ChartOfAccounts","AccountingRegister",
|
||||
"ChartOfCalculationTypes","CalculationRegister",
|
||||
"BusinessProcess","Task","IntegrationService"
|
||||
)
|
||||
|
||||
# --- Type → on-disk directory name (plural) ---
|
||||
$script:typeToDir = @{
|
||||
"Language"="Languages"; "Subsystem"="Subsystems"; "StyleItem"="StyleItems"; "Style"="Styles"
|
||||
"CommonPicture"="CommonPictures"; "SessionParameter"="SessionParameters"; "Role"="Roles"; "CommonTemplate"="CommonTemplates"
|
||||
"FilterCriterion"="FilterCriteria"; "CommonModule"="CommonModules"; "CommonAttribute"="CommonAttributes"; "ExchangePlan"="ExchangePlans"
|
||||
"XDTOPackage"="XDTOPackages"; "WebService"="WebServices"; "HTTPService"="HTTPServices"; "WSReference"="WSReferences"
|
||||
"EventSubscription"="EventSubscriptions"; "ScheduledJob"="ScheduledJobs"; "SettingsStorage"="SettingsStorages"; "FunctionalOption"="FunctionalOptions"
|
||||
"FunctionalOptionsParameter"="FunctionalOptionsParameters"; "DefinedType"="DefinedTypes"; "CommonCommand"="CommonCommands"; "CommandGroup"="CommandGroups"
|
||||
"Constant"="Constants"; "CommonForm"="CommonForms"; "Catalog"="Catalogs"; "Document"="Documents"
|
||||
"DocumentNumerator"="DocumentNumerators"; "Sequence"="Sequences"; "DocumentJournal"="DocumentJournals"; "Enum"="Enums"
|
||||
"Report"="Reports"; "DataProcessor"="DataProcessors"; "InformationRegister"="InformationRegisters"; "AccumulationRegister"="AccumulationRegisters"
|
||||
"ChartOfCharacteristicTypes"="ChartsOfCharacteristicTypes"; "ChartOfAccounts"="ChartsOfAccounts"; "AccountingRegister"="AccountingRegisters"
|
||||
"ChartOfCalculationTypes"="ChartsOfCalculationTypes"; "CalculationRegister"="CalculationRegisters"
|
||||
"BusinessProcess"="BusinessProcesses"; "Task"="Tasks"; "IntegrationService"="IntegrationServices"
|
||||
}
|
||||
|
||||
# --- XML manipulation helpers (from subsystem-edit pattern) ---
|
||||
function Get-ChildIndent($container) {
|
||||
foreach ($child in $container.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Whitespace' -or $child.NodeType -eq 'SignificantWhitespace') {
|
||||
if ($child.Value -match '^\r?\n(\t+)$') { return $Matches[1] }
|
||||
if ($child.Value -match '^\r?\n(\t+)') { return $Matches[1] }
|
||||
}
|
||||
}
|
||||
$depth = 0; $current = $container
|
||||
while ($current -and $current -ne $script:xmlDoc.DocumentElement) { $depth++; $current = $current.ParentNode }
|
||||
return "`t" * ($depth + 1)
|
||||
}
|
||||
|
||||
function Insert-BeforeElement($container, $newNode, $refNode, $childIndent) {
|
||||
$ws = $script:xmlDoc.CreateWhitespace("`r`n$childIndent")
|
||||
if ($refNode) {
|
||||
$container.InsertBefore($ws, $refNode) | Out-Null
|
||||
$container.InsertBefore($newNode, $ws) | Out-Null
|
||||
} else {
|
||||
$trailing = $container.LastChild
|
||||
if ($trailing -and ($trailing.NodeType -eq 'Whitespace' -or $trailing.NodeType -eq 'SignificantWhitespace')) {
|
||||
$container.InsertBefore($ws, $trailing) | Out-Null
|
||||
$container.InsertBefore($newNode, $trailing) | Out-Null
|
||||
} else {
|
||||
$container.AppendChild($ws) | Out-Null
|
||||
$container.AppendChild($newNode) | Out-Null
|
||||
$parentIndent = if ($childIndent.Length -gt 1) { $childIndent.Substring(0, $childIndent.Length - 1) } else { "" }
|
||||
$closeWs = $script:xmlDoc.CreateWhitespace("`r`n$parentIndent")
|
||||
$container.AppendChild($closeWs) | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Remove-NodeWithWhitespace($node) {
|
||||
$parent = $node.ParentNode
|
||||
$prev = $node.PreviousSibling
|
||||
$next = $node.NextSibling
|
||||
if ($prev -and ($prev.NodeType -eq 'Whitespace' -or $prev.NodeType -eq 'SignificantWhitespace')) {
|
||||
$parent.RemoveChild($prev) | Out-Null
|
||||
} elseif ($next -and ($next.NodeType -eq 'Whitespace' -or $next.NodeType -eq 'SignificantWhitespace')) {
|
||||
$parent.RemoveChild($next) | Out-Null
|
||||
}
|
||||
$parent.RemoveChild($node) | Out-Null
|
||||
}
|
||||
|
||||
function Expand-SelfClosingElement($container, $parentIndent) {
|
||||
if (-not $container.HasChildNodes -or $container.IsEmpty) {
|
||||
$closeWs = $script:xmlDoc.CreateWhitespace("`r`n$parentIndent")
|
||||
$container.AppendChild($closeWs) | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
function Import-Fragment([string]$xmlString) {
|
||||
$wrapper = "<_W xmlns=`"$($script:mdNs)`" xmlns:xsi=`"$($script:xsiNs)`" xmlns:v8=`"$($script:v8Ns)`" xmlns:xr=`"$($script:xrNs)`" xmlns:xs=`"http://www.w3.org/2001/XMLSchema`">$xmlString</_W>"
|
||||
$frag = New-Object System.Xml.XmlDocument
|
||||
$frag.PreserveWhitespace = $true
|
||||
$frag.LoadXml($wrapper)
|
||||
$nodes = @()
|
||||
foreach ($child in $frag.DocumentElement.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element') {
|
||||
$nodes += $script:xmlDoc.ImportNode($child, $true)
|
||||
}
|
||||
}
|
||||
return ,$nodes
|
||||
}
|
||||
|
||||
# --- Parse batch value (split by ;;) ---
|
||||
function Parse-BatchValue([string]$val) {
|
||||
$items = @()
|
||||
foreach ($part in $val.Split(";;")) {
|
||||
$trimmed = $part.Trim()
|
||||
if ($trimmed) { $items += $trimmed }
|
||||
}
|
||||
return ,$items
|
||||
}
|
||||
|
||||
# --- LocalString properties ---
|
||||
$mlProps = @("Synonym","BriefInformation","DetailedInformation","Copyright","VendorInformationAddress","ConfigurationInformationAddress")
|
||||
# Scalar properties
|
||||
$scalarProps = @("Name","Version","Vendor","Comment","NamePrefix","UpdateCatalogAddress")
|
||||
# Ref properties
|
||||
$refProps = @("DefaultLanguage")
|
||||
|
||||
# --- Operation: modify-property ---
|
||||
function Do-ModifyProperty([string]$batchVal) {
|
||||
$items = Parse-BatchValue $batchVal
|
||||
foreach ($item in $items) {
|
||||
$eqIdx = $item.IndexOf("=")
|
||||
if ($eqIdx -lt 1) {
|
||||
Write-Error "Invalid property format '$item', expected 'Key=Value'"
|
||||
exit 1
|
||||
}
|
||||
$propName = $item.Substring(0, $eqIdx).Trim()
|
||||
$propValue = $item.Substring($eqIdx + 1).Trim()
|
||||
|
||||
# Find property element
|
||||
$propEl = $null
|
||||
foreach ($child in $script:propsEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq $propName) {
|
||||
$propEl = $child; break
|
||||
}
|
||||
}
|
||||
if (-not $propEl) {
|
||||
Write-Error "Property '$propName' not found in Properties"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($mlProps -contains $propName) {
|
||||
# LocalString
|
||||
if (-not $propValue) {
|
||||
$propEl.InnerXml = ""
|
||||
} else {
|
||||
$indent = Get-ChildIndent $script:propsEl
|
||||
$escaped = [System.Security.SecurityElement]::Escape($propValue)
|
||||
$mlXml = "`r`n$indent`t<v8:item>`r`n$indent`t`t<v8:lang>ru</v8:lang>`r`n$indent`t`t<v8:content>$escaped</v8:content>`r`n$indent`t</v8:item>`r`n$indent"
|
||||
$propEl.InnerXml = $mlXml
|
||||
}
|
||||
} elseif ($scalarProps -contains $propName -or $refProps -contains $propName) {
|
||||
# Simple text
|
||||
if (-not $propValue) { $propEl.InnerXml = "" }
|
||||
else { $propEl.InnerText = $propValue }
|
||||
} else {
|
||||
# Enum or other — just set text
|
||||
$propEl.InnerText = $propValue
|
||||
}
|
||||
|
||||
$script:modifyCount++
|
||||
Info "Set $propName = `"$propValue`""
|
||||
}
|
||||
}
|
||||
|
||||
# --- Operation: add-childObject ---
|
||||
function Do-AddChildObject([string]$batchVal) {
|
||||
if (-not $script:childObjsEl) { Write-Error "No <ChildObjects> element found"; exit 1 }
|
||||
|
||||
$items = Parse-BatchValue $batchVal
|
||||
$cfgIndent = Get-ChildIndent $script:cfgEl
|
||||
|
||||
# Expand self-closing if needed
|
||||
if (-not $script:childObjsEl.HasChildNodes -or $script:childObjsEl.IsEmpty) {
|
||||
Expand-SelfClosingElement $script:childObjsEl $cfgIndent
|
||||
}
|
||||
$childIndent = Get-ChildIndent $script:childObjsEl
|
||||
|
||||
foreach ($item in $items) {
|
||||
$dotIdx = $item.IndexOf(".")
|
||||
if ($dotIdx -lt 1) {
|
||||
Write-Error "Invalid format '$item', expected 'Type.Name'"
|
||||
exit 1
|
||||
}
|
||||
$typeName = $item.Substring(0, $dotIdx)
|
||||
$objNameVal = $item.Substring($dotIdx + 1)
|
||||
|
||||
# Check type is valid
|
||||
$typeIdx = $script:typeOrder.IndexOf($typeName)
|
||||
if ($typeIdx -lt 0) {
|
||||
Write-Error "Unknown type '$typeName'"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that the referenced object actually exists on disk.
|
||||
# cf-edit add-childObject is a low-level operation for rare scenarios
|
||||
# (e.g. restoring a rolled-back Configuration.xml when object files are intact).
|
||||
# For creating NEW objects, meta-compile/role-compile/subsystem-compile already
|
||||
# auto-register in Configuration.xml — calling cf-edit add-childObject there is
|
||||
# unnecessary and error-prone.
|
||||
$typeDir = $script:typeToDir[$typeName]
|
||||
$objFile = Join-Path (Join-Path $script:configDir $typeDir) "$objNameVal.xml"
|
||||
if (-not (Test-Path $objFile)) {
|
||||
$hintSkill = switch ($typeName) {
|
||||
"Subsystem" { "subsystem-compile" }
|
||||
"Role" { "role-compile" }
|
||||
default { "meta-compile" }
|
||||
}
|
||||
Write-Error @"
|
||||
Object file not found: $typeDir/$objNameVal.xml
|
||||
cf-edit add-childObject only references objects that already exist on disk.
|
||||
To create a new $typeName, use $hintSkill (auto-registers in Configuration.xml):
|
||||
/$hintSkill with {"type":"$typeName","name":"$objNameVal"}
|
||||
"@
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Dedup check
|
||||
$existing = $false
|
||||
foreach ($child in $script:childObjsEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq $typeName -and $child.InnerText -eq $objNameVal) {
|
||||
$existing = $true; break
|
||||
}
|
||||
}
|
||||
if ($existing) {
|
||||
Warn "Already exists: $typeName.$objNameVal"
|
||||
continue
|
||||
}
|
||||
|
||||
# Find insertion point: after last element of same type, or after last element of preceding type
|
||||
$insertBefore = $null
|
||||
$lastSameType = $null
|
||||
$lastPrecedingType = $null
|
||||
$currentTypeIdx = -1
|
||||
|
||||
foreach ($child in $script:childObjsEl.ChildNodes) {
|
||||
if ($child.NodeType -ne 'Element') { continue }
|
||||
$childTypeIdx = $script:typeOrder.IndexOf($child.LocalName)
|
||||
if ($childTypeIdx -lt 0) { continue }
|
||||
|
||||
if ($child.LocalName -eq $typeName) {
|
||||
# Same type — check alphabetical order
|
||||
if ($child.InnerText -gt $objNameVal -and -not $insertBefore) {
|
||||
# Insert before this element (alphabetical)
|
||||
$insertBefore = $child
|
||||
}
|
||||
$lastSameType = $child
|
||||
} elseif ($childTypeIdx -lt $typeIdx) {
|
||||
$lastPrecedingType = $child
|
||||
} elseif ($childTypeIdx -gt $typeIdx -and -not $insertBefore) {
|
||||
# First element of a later type — insert before it
|
||||
$insertBefore = $child
|
||||
}
|
||||
}
|
||||
|
||||
# Create element
|
||||
$newEl = $script:xmlDoc.CreateElement($typeName, $script:mdNs)
|
||||
$newEl.InnerText = $objNameVal
|
||||
|
||||
if ($insertBefore) {
|
||||
Insert-BeforeElement $script:childObjsEl $newEl $insertBefore $childIndent
|
||||
} else {
|
||||
# Append at end (or after last same/preceding type)
|
||||
Insert-BeforeElement $script:childObjsEl $newEl $null $childIndent
|
||||
}
|
||||
|
||||
$script:addCount++
|
||||
Info "Added: $typeName.$objNameVal"
|
||||
}
|
||||
}
|
||||
|
||||
# --- Operation: remove-childObject ---
|
||||
function Do-RemoveChildObject([string]$batchVal) {
|
||||
if (-not $script:childObjsEl) { Write-Error "No <ChildObjects> element found"; exit 1 }
|
||||
|
||||
$items = Parse-BatchValue $batchVal
|
||||
foreach ($item in $items) {
|
||||
$dotIdx = $item.IndexOf(".")
|
||||
if ($dotIdx -lt 1) {
|
||||
Write-Error "Invalid format '$item', expected 'Type.Name'"
|
||||
exit 1
|
||||
}
|
||||
$typeName = $item.Substring(0, $dotIdx)
|
||||
$objNameVal = $item.Substring($dotIdx + 1)
|
||||
|
||||
$found = $false
|
||||
foreach ($child in @($script:childObjsEl.ChildNodes)) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq $typeName -and $child.InnerText -eq $objNameVal) {
|
||||
Remove-NodeWithWhitespace $child
|
||||
$script:removeCount++
|
||||
Info "Removed: $typeName.$objNameVal"
|
||||
$found = $true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (-not $found) { Warn "Not found: $typeName.$objNameVal" }
|
||||
}
|
||||
}
|
||||
|
||||
# --- Operation: add-defaultRole ---
|
||||
function Do-AddDefaultRole([string]$batchVal) {
|
||||
$items = Parse-BatchValue $batchVal
|
||||
|
||||
# Find DefaultRoles element
|
||||
$rolesEl = $null
|
||||
foreach ($child in $script:propsEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq "DefaultRoles") {
|
||||
$rolesEl = $child; break
|
||||
}
|
||||
}
|
||||
if (-not $rolesEl) { Write-Error "No <DefaultRoles> element found in Properties"; exit 1 }
|
||||
|
||||
$propsIndent = Get-ChildIndent $script:propsEl
|
||||
if (-not $rolesEl.HasChildNodes -or $rolesEl.IsEmpty) {
|
||||
Expand-SelfClosingElement $rolesEl $propsIndent
|
||||
}
|
||||
$roleIndent = Get-ChildIndent $rolesEl
|
||||
|
||||
foreach ($item in $items) {
|
||||
$roleName = $item
|
||||
if (-not $roleName.StartsWith("Role.")) { $roleName = "Role.$roleName" }
|
||||
|
||||
# Dedup
|
||||
$existing = $false
|
||||
foreach ($child in $rolesEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.InnerText.Trim() -eq $roleName) {
|
||||
$existing = $true; break
|
||||
}
|
||||
}
|
||||
if ($existing) {
|
||||
Warn "DefaultRole already exists: $roleName"
|
||||
continue
|
||||
}
|
||||
|
||||
$fragXml = "<xr:Item xsi:type=`"xr:MDObjectRef`">$roleName</xr:Item>"
|
||||
$nodes = Import-Fragment $fragXml
|
||||
if ($nodes.Count -gt 0) {
|
||||
Insert-BeforeElement $rolesEl $nodes[0] $null $roleIndent
|
||||
$script:addCount++
|
||||
Info "Added DefaultRole: $roleName"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# --- Operation: remove-defaultRole ---
|
||||
function Do-RemoveDefaultRole([string]$batchVal) {
|
||||
$items = Parse-BatchValue $batchVal
|
||||
|
||||
$rolesEl = $null
|
||||
foreach ($child in $script:propsEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq "DefaultRoles") {
|
||||
$rolesEl = $child; break
|
||||
}
|
||||
}
|
||||
if (-not $rolesEl) { Write-Error "No <DefaultRoles> element found"; exit 1 }
|
||||
|
||||
foreach ($item in $items) {
|
||||
$roleName = $item
|
||||
if (-not $roleName.StartsWith("Role.")) { $roleName = "Role.$roleName" }
|
||||
|
||||
$found = $false
|
||||
foreach ($child in @($rolesEl.ChildNodes)) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.InnerText.Trim() -eq $roleName) {
|
||||
Remove-NodeWithWhitespace $child
|
||||
$script:removeCount++
|
||||
Info "Removed DefaultRole: $roleName"
|
||||
$found = $true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (-not $found) { Warn "DefaultRole not found: $roleName" }
|
||||
}
|
||||
}
|
||||
|
||||
# --- Operation: set-panels ---
|
||||
# Canonical English aliases — preferred form, used in docs and error messages.
|
||||
$script:panelUuids = @{
|
||||
"sections" = "b553047f-c9aa-4157-978d-448ecad24248"
|
||||
"open" = "cbab57f2-a0f3-4f0a-89ea-4cb19570ab75"
|
||||
"favorites" = "13322b22-3960-4d68-93a6-fe2dd7f28ca3"
|
||||
"history" = "c933ac92-92cd-459d-81cc-e0c8a83ced99"
|
||||
"functions" = "b2735bd3-d822-4430-ba59-c9e869693b24"
|
||||
}
|
||||
# Russian synonyms — silently accepted (cf-info displays Russian names; users
|
||||
# may copy them straight into cf-edit value).
|
||||
$script:panelSynonyms = @{
|
||||
"разделов" = "sections"; "разделы" = "sections"
|
||||
"открытых" = "open"; "открытые" = "open"
|
||||
"избранного" = "favorites";"избранное" = "favorites"
|
||||
"истории" = "history"; "история" = "history"
|
||||
"функций" = "functions";"функции" = "functions"
|
||||
}
|
||||
|
||||
function Build-PanelEntryXml($entry, [string]$indent) {
|
||||
# String alias -> <panel><uuid>...</uuid></panel>
|
||||
if ($entry -is [string]) {
|
||||
$key = $entry.ToLowerInvariant()
|
||||
if ($script:panelSynonyms.ContainsKey($key)) { $key = $script:panelSynonyms[$key] }
|
||||
if (-not $script:panelUuids.ContainsKey($key)) {
|
||||
Write-Error "Unknown panel alias '$entry'. Allowed: $(($script:panelUuids.Keys | Sort-Object) -join ', ')"
|
||||
exit 1
|
||||
}
|
||||
$u = $script:panelUuids[$key]
|
||||
$instId = [guid]::NewGuid().ToString()
|
||||
return "$indent<panel id=`"$instId`">`r`n$indent`t<uuid>$u</uuid>`r`n$indent</panel>"
|
||||
}
|
||||
# Object {group: [...]} -> <group id=""><group><panel/></group>...</group> (stack)
|
||||
if ($entry.PSObject.Properties['group']) {
|
||||
$children = $entry.group
|
||||
if (-not $children -or $children.Count -eq 0) {
|
||||
Write-Error "group must contain at least one entry"
|
||||
exit 1
|
||||
}
|
||||
$gid = [guid]::NewGuid().ToString()
|
||||
$inner = ""
|
||||
foreach ($child in $children) {
|
||||
$childXml = Build-PanelEntryXml $child "$indent`t`t"
|
||||
$inner += "$indent`t<group>`r`n$childXml`r`n$indent`t</group>`r`n"
|
||||
}
|
||||
return "$indent<group id=`"$gid`">`r`n$inner$indent</group>"
|
||||
}
|
||||
Write-Error "Panel entry must be a string alias or object {group:[...]}, got: $($entry | ConvertTo-Json -Compress)"
|
||||
exit 1
|
||||
}
|
||||
|
||||
function Do-SetPanels($valArg) {
|
||||
# Accept string (JSON), PSCustomObject, or hashtable
|
||||
$layout = $valArg
|
||||
if ($layout -is [string]) {
|
||||
try { $layout = $layout | ConvertFrom-Json } catch {
|
||||
Write-Error "set-panels value must be valid JSON object, got: $valArg"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
if (-not $layout) {
|
||||
Write-Error "set-panels value is empty"
|
||||
exit 1
|
||||
}
|
||||
|
||||
$sides = @("top","left","right","bottom")
|
||||
$bodyParts = @()
|
||||
foreach ($side in $sides) {
|
||||
$entries = $null
|
||||
if ($layout.PSObject.Properties[$side]) { $entries = $layout.$side }
|
||||
if ($null -eq $entries) { continue }
|
||||
# Normalize to array
|
||||
if ($entries -isnot [System.Array] -and $entries -isnot [System.Collections.IList]) {
|
||||
$entries = @($entries)
|
||||
}
|
||||
foreach ($entry in $entries) {
|
||||
$entryXml = Build-PanelEntryXml $entry "`t`t"
|
||||
$bodyParts += "`t<$side>`r`n$entryXml`r`n`t</$side>"
|
||||
}
|
||||
}
|
||||
|
||||
# Reject unknown side keys (catches typos like "Top" vs "top")
|
||||
foreach ($prop in $layout.PSObject.Properties) {
|
||||
if ($sides -notcontains $prop.Name) {
|
||||
Write-Error "Unknown side '$($prop.Name)'. Allowed: $($sides -join ', ')"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
$body = $bodyParts -join "`r`n"
|
||||
$declarations = @"
|
||||
<panelDef id="b553047f-c9aa-4157-978d-448ecad24248"/>
|
||||
<panelDef id="13322b22-3960-4d68-93a6-fe2dd7f28ca3"/>
|
||||
<panelDef id="c933ac92-92cd-459d-81cc-e0c8a83ced99"/>
|
||||
<panelDef id="cbab57f2-a0f3-4f0a-89ea-4cb19570ab75"/>
|
||||
<panelDef id="b2735bd3-d822-4430-ba59-c9e869693b24"/>
|
||||
"@
|
||||
$bodyBlock = if ($body) { "$body`r`n" } else { "" }
|
||||
$caiXml = @"
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ClientApplicationInterface xmlns="http://v8.1c.ru/8.2/managed-application/core" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="InterfaceLayouter">
|
||||
$bodyBlock$declarations
|
||||
</ClientApplicationInterface>
|
||||
"@
|
||||
|
||||
$extDir = Join-Path $script:configDir "Ext"
|
||||
if (-not (Test-Path $extDir)) { New-Item -ItemType Directory -Path $extDir -Force | Out-Null }
|
||||
$caiPath = Join-Path $extDir "ClientApplicationInterface.xml"
|
||||
$utf8Bom = New-Object System.Text.UTF8Encoding($true)
|
||||
[System.IO.File]::WriteAllText($caiPath, $caiXml, $utf8Bom)
|
||||
$script:modifyCount++
|
||||
Info "Wrote panel layout: $caiPath"
|
||||
}
|
||||
|
||||
# --- Operation: set-home-page ---
|
||||
# Russian → English type aliases for form-ref normalization
|
||||
$script:ruTypeMap = @{
|
||||
"справочник" = "Catalog"
|
||||
"документ" = "Document"
|
||||
"перечисление" = "Enum"
|
||||
"отчёт" = "Report"
|
||||
"отчет" = "Report"
|
||||
"обработка" = "DataProcessor"
|
||||
"общаяформа" = "CommonForm"
|
||||
"журналдокументов" = "DocumentJournal"
|
||||
"планвидовхарактеристик" = "ChartOfCharacteristicTypes"
|
||||
"плансчетов" = "ChartOfAccounts"
|
||||
"планвидоврасчета" = "ChartOfCalculationTypes"
|
||||
"планвидоврасчёта" = "ChartOfCalculationTypes"
|
||||
"регистрсведений" = "InformationRegister"
|
||||
"регистрнакопления" = "AccumulationRegister"
|
||||
"регистрбухгалтерии" = "AccountingRegister"
|
||||
"регистррасчета" = "CalculationRegister"
|
||||
"регистррасчёта" = "CalculationRegister"
|
||||
"бизнеспроцесс" = "BusinessProcess"
|
||||
"задача" = "Task"
|
||||
"планобмена" = "ExchangePlan"
|
||||
"хранилищенастроек" = "SettingsStorage"
|
||||
}
|
||||
# plural folder → singular type
|
||||
$script:dirToType = @{}
|
||||
foreach ($k in $script:typeToDir.Keys) { $script:dirToType[$script:typeToDir[$k].ToLowerInvariant()] = $k }
|
||||
|
||||
function Normalize-FormRef([string]$s) {
|
||||
$s = $s.Trim()
|
||||
if (-not $s) { return $s }
|
||||
# UUID — leave as-is
|
||||
if ($s -match '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$') { return $s }
|
||||
# Path form?
|
||||
if ($s.Contains("/") -or $s.Contains("\")) {
|
||||
$parts = $s.Replace("\","/").Split("/") | Where-Object { $_ -ne "" -and $_.ToLowerInvariant() -ne "ext" }
|
||||
# Strip trailing Form.xml
|
||||
if ($parts.Count -gt 0 -and $parts[-1].ToLowerInvariant() -eq "form.xml") {
|
||||
$parts = @($parts[0..($parts.Count - 2)])
|
||||
}
|
||||
if ($parts.Count -ge 2) {
|
||||
$typeDir = $parts[0]
|
||||
$typeSingular = $script:dirToType[$typeDir.ToLowerInvariant()]
|
||||
if ($typeSingular) {
|
||||
if ($typeSingular -eq "CommonForm" -and $parts.Count -ge 2) {
|
||||
return "CommonForm.$($parts[1])"
|
||||
}
|
||||
if ($parts.Count -ge 4 -and $parts[2].ToLowerInvariant() -eq "forms") {
|
||||
return "$typeSingular.$($parts[1]).Form.$($parts[3])"
|
||||
}
|
||||
}
|
||||
}
|
||||
return $s
|
||||
}
|
||||
# Dot form — translate Russian head and 'Форма' segment, auto-insert 'Form'
|
||||
$segs = $s.Split(".")
|
||||
if ($segs.Count -ge 1) {
|
||||
$head = $segs[0].ToLowerInvariant()
|
||||
if ($script:ruTypeMap.ContainsKey($head)) { $segs[0] = $script:ruTypeMap[$head] }
|
||||
for ($i = 1; $i -lt $segs.Count; $i++) {
|
||||
if ($segs[$i] -eq "Форма") { $segs[$i] = "Form" }
|
||||
}
|
||||
# Auto-insert Form: for object types with 3 segments (Type.Object.FormName)
|
||||
if ($segs.Count -eq 3 -and $script:typeOrder -contains $segs[0] -and $segs[0] -ne "CommonForm") {
|
||||
$segs = @($segs[0], $segs[1], "Form", $segs[2])
|
||||
}
|
||||
}
|
||||
return ($segs -join ".")
|
||||
}
|
||||
|
||||
# Accept short DSL or canonical XML keys (silently)
|
||||
function Get-FieldValue($obj, [string[]]$keys) {
|
||||
foreach ($k in $keys) {
|
||||
if ($obj.PSObject.Properties[$k]) { return $obj.PSObject.Properties[$k].Value }
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
function Build-HomePageItemXml($entry, [string]$indent) {
|
||||
# Resolve fields
|
||||
if ($entry -is [string]) {
|
||||
$formRef = Normalize-FormRef $entry
|
||||
$height = 10
|
||||
$common = $true
|
||||
$roles = $null
|
||||
} else {
|
||||
$formRaw = Get-FieldValue $entry @("form","Form")
|
||||
if (-not $formRaw) { Write-Error "Home page item: 'form' is required, got: $($entry | ConvertTo-Json -Compress)"; exit 1 }
|
||||
$formRef = Normalize-FormRef ([string]$formRaw)
|
||||
$h = Get-FieldValue $entry @("height","Height")
|
||||
$height = if ($null -ne $h) { [int]$h } else { 10 }
|
||||
$vis = Get-FieldValue $entry @("visibility","Visibility")
|
||||
$common = if ($null -ne $vis) { [bool]$vis } else { $true }
|
||||
$roles = Get-FieldValue $entry @("roles")
|
||||
}
|
||||
|
||||
$visParts = @()
|
||||
$visParts += "$indent`t`t<xr:Common>$($common.ToString().ToLower())</xr:Common>"
|
||||
if ($roles) {
|
||||
# roles is PSCustomObject {Role.X: bool, ...}
|
||||
foreach ($prop in $roles.PSObject.Properties) {
|
||||
$rname = $prop.Name
|
||||
if (-not $rname.StartsWith("Role.") -and -not ($rname -match '^[0-9a-fA-F]{8}-')) { $rname = "Role.$rname" }
|
||||
$rval = ([bool]$prop.Value).ToString().ToLower()
|
||||
$escName = [System.Security.SecurityElement]::Escape($rname)
|
||||
$visParts += "$indent`t`t<xr:Value name=`"$escName`">$rval</xr:Value>"
|
||||
}
|
||||
}
|
||||
$visBlock = $visParts -join "`r`n"
|
||||
$escForm = [System.Security.SecurityElement]::Escape($formRef)
|
||||
return @"
|
||||
$indent<Item>
|
||||
$indent`t<Form>$escForm</Form>
|
||||
$indent`t<Height>$height</Height>
|
||||
$indent`t<Visibility>
|
||||
$visBlock
|
||||
$indent`t</Visibility>
|
||||
$indent</Item>
|
||||
"@
|
||||
}
|
||||
|
||||
function Do-SetHomePage($valArg) {
|
||||
$layout = $valArg
|
||||
if ($layout -is [string]) {
|
||||
try { $layout = $layout | ConvertFrom-Json } catch {
|
||||
Write-Error "set-home-page value must be valid JSON object"; exit 1
|
||||
}
|
||||
}
|
||||
if (-not $layout) { Write-Error "set-home-page value is empty"; exit 1 }
|
||||
|
||||
$allowedTemplates = @("OneColumn","TwoColumnsEqualWidth","TwoColumnsVariableWidth")
|
||||
$tmpl = Get-FieldValue $layout @("template","WorkingAreaTemplate")
|
||||
if (-not $tmpl) { $tmpl = "TwoColumnsEqualWidth" }
|
||||
if ($allowedTemplates -notcontains $tmpl) {
|
||||
Write-Error "Unknown template '$tmpl'. Allowed: $($allowedTemplates -join ', ')"; exit 1
|
||||
}
|
||||
|
||||
$leftItems = Get-FieldValue $layout @("left","LeftColumn")
|
||||
$rightItems = Get-FieldValue $layout @("right","RightColumn")
|
||||
|
||||
# Reject unknown keys
|
||||
$known = @("template","WorkingAreaTemplate","left","LeftColumn","right","RightColumn")
|
||||
foreach ($prop in $layout.PSObject.Properties) {
|
||||
if ($known -notcontains $prop.Name) {
|
||||
Write-Error "Unknown key '$($prop.Name)'. Allowed: template, left, right"; exit 1
|
||||
}
|
||||
}
|
||||
|
||||
if ($tmpl -eq "OneColumn" -and $rightItems) {
|
||||
Write-Error "Template 'OneColumn' cannot have items in 'right' column"; exit 1
|
||||
}
|
||||
|
||||
function Build-Column([string]$tag, $items) {
|
||||
if (-not $items) { return "`t<$tag/>" }
|
||||
if ($items -isnot [System.Array] -and $items -isnot [System.Collections.IList]) {
|
||||
$items = @($items)
|
||||
}
|
||||
if ($items.Count -eq 0) { return "`t<$tag/>" }
|
||||
$itemBlocks = @()
|
||||
foreach ($it in $items) {
|
||||
$itemBlocks += Build-HomePageItemXml $it "`t`t"
|
||||
}
|
||||
$body = $itemBlocks -join "`r`n"
|
||||
return "`t<$tag>`r`n$body`r`n`t</$tag>"
|
||||
}
|
||||
|
||||
$leftXml = Build-Column "LeftColumn" $leftItems
|
||||
$rightXml = Build-Column "RightColumn" $rightItems
|
||||
|
||||
$hpXml = @"
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<HomePageWorkArea xmlns="http://v8.1c.ru/8.3/xcf/extrnprops" xmlns:xr="http://v8.1c.ru/8.3/xcf/readable" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.17">
|
||||
<WorkingAreaTemplate>$tmpl</WorkingAreaTemplate>
|
||||
$leftXml
|
||||
$rightXml
|
||||
</HomePageWorkArea>
|
||||
"@
|
||||
|
||||
$extDir = Join-Path $script:configDir "Ext"
|
||||
if (-not (Test-Path $extDir)) { New-Item -ItemType Directory -Path $extDir -Force | Out-Null }
|
||||
$hpPath = Join-Path $extDir "HomePageWorkArea.xml"
|
||||
$utf8Bom = New-Object System.Text.UTF8Encoding($true)
|
||||
[System.IO.File]::WriteAllText($hpPath, $hpXml, $utf8Bom)
|
||||
$script:modifyCount++
|
||||
Info "Wrote home page layout: $hpPath"
|
||||
}
|
||||
|
||||
# --- Operation: set-defaultRoles ---
|
||||
function Do-SetDefaultRoles([string]$batchVal) {
|
||||
$items = Parse-BatchValue $batchVal
|
||||
|
||||
$rolesEl = $null
|
||||
foreach ($child in $script:propsEl.ChildNodes) {
|
||||
if ($child.NodeType -eq 'Element' -and $child.LocalName -eq "DefaultRoles") {
|
||||
$rolesEl = $child; break
|
||||
}
|
||||
}
|
||||
if (-not $rolesEl) { Write-Error "No <DefaultRoles> element found"; exit 1 }
|
||||
|
||||
# Clear all existing children
|
||||
while ($rolesEl.HasChildNodes) {
|
||||
$rolesEl.RemoveChild($rolesEl.FirstChild) | Out-Null
|
||||
}
|
||||
|
||||
if ($items.Count -eq 0) {
|
||||
$script:modifyCount++
|
||||
Info "Cleared DefaultRoles"
|
||||
return
|
||||
}
|
||||
|
||||
$propsIndent = Get-ChildIndent $script:propsEl
|
||||
$roleIndent = "$propsIndent`t"
|
||||
|
||||
# Add closing whitespace
|
||||
$closeWs = $script:xmlDoc.CreateWhitespace("`r`n$propsIndent")
|
||||
$rolesEl.AppendChild($closeWs) | Out-Null
|
||||
|
||||
foreach ($item in $items) {
|
||||
$roleName = $item
|
||||
if (-not $roleName.StartsWith("Role.")) { $roleName = "Role.$roleName" }
|
||||
|
||||
$fragXml = "<xr:Item xsi:type=`"xr:MDObjectRef`">$roleName</xr:Item>"
|
||||
$nodes = Import-Fragment $fragXml
|
||||
if ($nodes.Count -gt 0) {
|
||||
Insert-BeforeElement $rolesEl $nodes[0] $null $roleIndent
|
||||
}
|
||||
}
|
||||
|
||||
$script:modifyCount++
|
||||
Info "Set DefaultRoles: $($items.Count) roles"
|
||||
}
|
||||
|
||||
# --- Execute operations ---
|
||||
$operations = @()
|
||||
if ($DefinitionFile) {
|
||||
if (-not [System.IO.Path]::IsPathRooted($DefinitionFile)) {
|
||||
$DefinitionFile = Join-Path (Get-Location).Path $DefinitionFile
|
||||
}
|
||||
$jsonText = Get-Content -Raw -Encoding UTF8 $DefinitionFile
|
||||
$ops = $jsonText | ConvertFrom-Json
|
||||
if ($ops -is [System.Array]) {
|
||||
foreach ($op in $ops) { $operations += $op }
|
||||
} else {
|
||||
$operations += $ops
|
||||
}
|
||||
} else {
|
||||
$operations += @{ operation = $Operation; value = $Value }
|
||||
}
|
||||
|
||||
foreach ($op in $operations) {
|
||||
$opName = if ($op.operation) { "$($op.operation)" } else { "$Operation" }
|
||||
# Pass value through as-is (object or string); set-panels needs object form
|
||||
$opValue = if ($null -ne $op.value) { $op.value } else { $Value }
|
||||
$opValueStr = if ($opValue -is [string]) { $opValue } else { "$opValue" }
|
||||
|
||||
switch ($opName) {
|
||||
"modify-property" { Do-ModifyProperty $opValueStr }
|
||||
"add-childObject" { Do-AddChildObject $opValueStr }
|
||||
"remove-childObject" { Do-RemoveChildObject $opValueStr }
|
||||
"add-defaultRole" { Do-AddDefaultRole $opValueStr }
|
||||
"remove-defaultRole" { Do-RemoveDefaultRole $opValueStr }
|
||||
"set-defaultRoles" { Do-SetDefaultRoles $opValueStr }
|
||||
"set-panels" { Do-SetPanels $opValue }
|
||||
"set-home-page" { Do-SetHomePage $opValue }
|
||||
default { Write-Error "Unknown operation: $opName"; exit 1 }
|
||||
}
|
||||
}
|
||||
|
||||
# --- Save ---
|
||||
$settings = New-Object System.Xml.XmlWriterSettings
|
||||
$settings.Encoding = New-Object System.Text.UTF8Encoding($true)
|
||||
$settings.Indent = $false
|
||||
$settings.NewLineHandling = [System.Xml.NewLineHandling]::None
|
||||
|
||||
$memStream = New-Object System.IO.MemoryStream
|
||||
$writer = [System.Xml.XmlWriter]::Create($memStream, $settings)
|
||||
$script:xmlDoc.Save($writer)
|
||||
$writer.Flush(); $writer.Close()
|
||||
|
||||
$bytes = $memStream.ToArray()
|
||||
$memStream.Close()
|
||||
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
|
||||
if ($text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) }
|
||||
$text = $text.Replace('encoding="utf-8"', 'encoding="UTF-8"')
|
||||
|
||||
$utf8Bom = New-Object System.Text.UTF8Encoding($true)
|
||||
[System.IO.File]::WriteAllText($resolvedPath, $text, $utf8Bom)
|
||||
Info "Saved: $resolvedPath"
|
||||
|
||||
# --- Auto-validate ---
|
||||
if (-not $NoValidate) {
|
||||
$validateScript = Join-Path (Join-Path $PSScriptRoot "..\..\cf-validate") "scripts\cf-validate.ps1"
|
||||
$validateScript = [System.IO.Path]::GetFullPath($validateScript)
|
||||
if (Test-Path $validateScript) {
|
||||
Write-Host ""
|
||||
Write-Host "--- Running cf-validate ---"
|
||||
& powershell.exe -NoProfile -File $validateScript -ConfigPath $resolvedPath
|
||||
}
|
||||
}
|
||||
|
||||
# --- Summary ---
|
||||
Write-Host ""
|
||||
Write-Host "=== cf-edit summary ==="
|
||||
Write-Host " Configuration: $($script:objName)"
|
||||
Write-Host " Added: $($script:addCount)"
|
||||
Write-Host " Removed: $($script:removeCount)"
|
||||
Write-Host " Modified: $($script:modifyCount)"
|
||||
exit 0
|
||||
@@ -0,0 +1,822 @@
|
||||
#!/usr/bin/env python3
|
||||
# cf-edit v1.4 — Edit 1C configuration root (Configuration.xml)
|
||||
# Source: https://github.com/Nikolay-Shirokov/cc-1c-skills
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid as _uuid
|
||||
from html import escape as html_escape
|
||||
from lxml import etree
|
||||
|
||||
MD_NS = "http://v8.1c.ru/8.3/MDClasses"
|
||||
XR_NS = "http://v8.1c.ru/8.3/xcf/readable"
|
||||
XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
V8_NS = "http://v8.1c.ru/8.1/data/core"
|
||||
XS_NS = "http://www.w3.org/2001/XMLSchema"
|
||||
|
||||
# Canonical type order for ChildObjects (44 types)
|
||||
TYPE_ORDER = [
|
||||
"Language", "Subsystem", "StyleItem", "Style",
|
||||
"CommonPicture", "SessionParameter", "Role", "CommonTemplate",
|
||||
"FilterCriterion", "CommonModule", "CommonAttribute", "ExchangePlan",
|
||||
"XDTOPackage", "WebService", "HTTPService", "WSReference",
|
||||
"EventSubscription", "ScheduledJob", "SettingsStorage", "FunctionalOption",
|
||||
"FunctionalOptionsParameter", "DefinedType", "CommonCommand", "CommandGroup",
|
||||
"Constant", "CommonForm", "Catalog", "Document",
|
||||
"DocumentNumerator", "Sequence", "DocumentJournal", "Enum",
|
||||
"Report", "DataProcessor", "InformationRegister", "AccumulationRegister",
|
||||
"ChartOfCharacteristicTypes", "ChartOfAccounts", "AccountingRegister",
|
||||
"ChartOfCalculationTypes", "CalculationRegister",
|
||||
"BusinessProcess", "Task", "IntegrationService",
|
||||
]
|
||||
|
||||
# Type → on-disk directory name (plural)
|
||||
TYPE_TO_DIR = {
|
||||
"Language": "Languages", "Subsystem": "Subsystems", "StyleItem": "StyleItems", "Style": "Styles",
|
||||
"CommonPicture": "CommonPictures", "SessionParameter": "SessionParameters", "Role": "Roles", "CommonTemplate": "CommonTemplates",
|
||||
"FilterCriterion": "FilterCriteria", "CommonModule": "CommonModules", "CommonAttribute": "CommonAttributes", "ExchangePlan": "ExchangePlans",
|
||||
"XDTOPackage": "XDTOPackages", "WebService": "WebServices", "HTTPService": "HTTPServices", "WSReference": "WSReferences",
|
||||
"EventSubscription": "EventSubscriptions", "ScheduledJob": "ScheduledJobs", "SettingsStorage": "SettingsStorages", "FunctionalOption": "FunctionalOptions",
|
||||
"FunctionalOptionsParameter": "FunctionalOptionsParameters", "DefinedType": "DefinedTypes", "CommonCommand": "CommonCommands", "CommandGroup": "CommandGroups",
|
||||
"Constant": "Constants", "CommonForm": "CommonForms", "Catalog": "Catalogs", "Document": "Documents",
|
||||
"DocumentNumerator": "DocumentNumerators", "Sequence": "Sequences", "DocumentJournal": "DocumentJournals", "Enum": "Enums",
|
||||
"Report": "Reports", "DataProcessor": "DataProcessors", "InformationRegister": "InformationRegisters", "AccumulationRegister": "AccumulationRegisters",
|
||||
"ChartOfCharacteristicTypes": "ChartsOfCharacteristicTypes", "ChartOfAccounts": "ChartsOfAccounts", "AccountingRegister": "AccountingRegisters",
|
||||
"ChartOfCalculationTypes": "ChartsOfCalculationTypes", "CalculationRegister": "CalculationRegisters",
|
||||
"BusinessProcess": "BusinessProcesses", "Task": "Tasks", "IntegrationService": "IntegrationServices",
|
||||
}
|
||||
|
||||
ML_PROPS = ["Synonym", "BriefInformation", "DetailedInformation", "Copyright", "VendorInformationAddress", "ConfigurationInformationAddress"]
|
||||
SCALAR_PROPS = ["Name", "Version", "Vendor", "Comment", "NamePrefix", "UpdateCatalogAddress"]
|
||||
REF_PROPS = ["DefaultLanguage"]
|
||||
|
||||
|
||||
def localname(el):
|
||||
return etree.QName(el.tag).localname
|
||||
|
||||
|
||||
def info(msg):
|
||||
print(f"[INFO] {msg}")
|
||||
|
||||
|
||||
def warn(msg):
|
||||
print(f"[WARN] {msg}")
|
||||
|
||||
|
||||
def get_child_indent(container):
|
||||
if container.text and "\n" in container.text:
|
||||
after_nl = container.text.rsplit("\n", 1)[-1]
|
||||
if after_nl and not after_nl.strip():
|
||||
return after_nl
|
||||
for child in container:
|
||||
if child.tail and "\n" in child.tail:
|
||||
after_nl = child.tail.rsplit("\n", 1)[-1]
|
||||
if after_nl and not after_nl.strip():
|
||||
return after_nl
|
||||
depth = 0
|
||||
current = container
|
||||
while current is not None:
|
||||
depth += 1
|
||||
current = current.getparent()
|
||||
return "\t" * depth
|
||||
|
||||
|
||||
def insert_before_closing(container, new_el, child_indent):
|
||||
children = list(container)
|
||||
if len(children) == 0:
|
||||
parent_indent = child_indent[:-1] if len(child_indent) > 0 else ""
|
||||
container.text = "\r\n" + child_indent
|
||||
new_el.tail = "\r\n" + parent_indent
|
||||
container.append(new_el)
|
||||
else:
|
||||
last = children[-1]
|
||||
new_el.tail = last.tail
|
||||
last.tail = "\r\n" + child_indent
|
||||
container.append(new_el)
|
||||
|
||||
|
||||
def insert_before_ref(container, new_el, ref_el, child_indent):
|
||||
"""Insert new_el before ref_el inside container."""
|
||||
idx = list(container).index(ref_el)
|
||||
prev = ref_el.getprevious()
|
||||
if prev is not None:
|
||||
new_el.tail = prev.tail
|
||||
prev.tail = "\r\n" + child_indent
|
||||
else:
|
||||
new_el.tail = container.text
|
||||
container.text = "\r\n" + child_indent
|
||||
container.insert(idx, new_el)
|
||||
|
||||
|
||||
def remove_with_indent(el):
|
||||
parent = el.getparent()
|
||||
prev = el.getprevious()
|
||||
if prev is not None:
|
||||
if el.tail:
|
||||
prev.tail = el.tail
|
||||
else:
|
||||
if el.tail:
|
||||
parent.text = el.tail
|
||||
parent.remove(el)
|
||||
|
||||
|
||||
def expand_self_closing(container, parent_indent):
|
||||
if len(container) == 0 and not (container.text and container.text.strip()):
|
||||
container.text = "\r\n" + parent_indent
|
||||
|
||||
|
||||
def import_fragment(xml_string):
|
||||
wrapper = (
|
||||
f'<_W xmlns="{MD_NS}" xmlns:xsi="{XSI_NS}" xmlns:v8="{V8_NS}" '
|
||||
f'xmlns:xr="{XR_NS}" xmlns:xs="{XS_NS}">{xml_string}</_W>'
|
||||
)
|
||||
frag = etree.fromstring(wrapper.encode("utf-8"))
|
||||
return list(frag)
|
||||
|
||||
|
||||
def parse_batch_value(val):
|
||||
items = []
|
||||
for part in val.split(";;"):
|
||||
trimmed = part.strip()
|
||||
if trimmed:
|
||||
items.append(trimmed)
|
||||
return items
|
||||
|
||||
|
||||
def save_xml_bom(tree, path):
|
||||
xml_bytes = etree.tostring(tree, xml_declaration=True, encoding="UTF-8")
|
||||
xml_bytes = xml_bytes.replace(b"<?xml version='1.0' encoding='UTF-8'?>", b'<?xml version="1.0" encoding="utf-8"?>')
|
||||
if not xml_bytes.endswith(b"\n"):
|
||||
xml_bytes += b"\n"
|
||||
with open(path, "wb") as f:
|
||||
f.write(b"\xef\xbb\xbf")
|
||||
f.write(xml_bytes)
|
||||
|
||||
|
||||
def main():
|
||||
sys.stdout.reconfigure(encoding="utf-8")
|
||||
sys.stderr.reconfigure(encoding="utf-8")
|
||||
parser = argparse.ArgumentParser(description="Edit 1C configuration root (Configuration.xml)", allow_abbrev=False)
|
||||
parser.add_argument("-ConfigPath", "-Path", required=True)
|
||||
parser.add_argument("-DefinitionFile", default=None)
|
||||
parser.add_argument("-Operation", default=None, choices=["modify-property", "add-childObject", "remove-childObject", "add-defaultRole", "remove-defaultRole", "set-defaultRoles", "set-panels", "set-home-page"])
|
||||
parser.add_argument("-Value", default=None)
|
||||
parser.add_argument("-NoValidate", action="store_true")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.DefinitionFile and args.Operation:
|
||||
print("Cannot use both -DefinitionFile and -Operation", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not args.DefinitionFile and not args.Operation:
|
||||
print("Either -DefinitionFile or -Operation is required", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
config_path = args.ConfigPath
|
||||
if not os.path.isabs(config_path):
|
||||
config_path = os.path.join(os.getcwd(), config_path)
|
||||
if os.path.isdir(config_path):
|
||||
candidate = os.path.join(config_path, "Configuration.xml")
|
||||
if os.path.isfile(candidate):
|
||||
config_path = candidate
|
||||
else:
|
||||
print("No Configuration.xml in directory", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(config_path):
|
||||
print(f"File not found: {config_path}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
resolved_path = os.path.abspath(config_path)
|
||||
config_dir = os.path.dirname(resolved_path)
|
||||
|
||||
xml_parser = etree.XMLParser(remove_blank_text=False)
|
||||
tree = etree.parse(resolved_path, xml_parser)
|
||||
xml_root = tree.getroot()
|
||||
|
||||
add_count = 0
|
||||
remove_count = 0
|
||||
modify_count = 0
|
||||
|
||||
cfg_el = None
|
||||
for child in xml_root:
|
||||
if isinstance(child.tag, str) and localname(child) == "Configuration":
|
||||
cfg_el = child
|
||||
break
|
||||
if cfg_el is None:
|
||||
print("No <Configuration> element found", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
props_el = None
|
||||
child_objs_el = None
|
||||
for child in cfg_el:
|
||||
if not isinstance(child.tag, str):
|
||||
continue
|
||||
if localname(child) == "Properties":
|
||||
props_el = child
|
||||
if localname(child) == "ChildObjects":
|
||||
child_objs_el = child
|
||||
|
||||
obj_name = ""
|
||||
if props_el is not None:
|
||||
for child in props_el:
|
||||
if isinstance(child.tag, str) and localname(child) == "Name":
|
||||
obj_name = (child.text or "").strip()
|
||||
break
|
||||
info(f"Configuration: {obj_name}")
|
||||
|
||||
# --- Operations ---
|
||||
def do_modify_property(batch_val):
|
||||
nonlocal modify_count
|
||||
items = parse_batch_value(batch_val)
|
||||
for item in items:
|
||||
eq_idx = item.find("=")
|
||||
if eq_idx < 1:
|
||||
print(f"Invalid property format '{item}', expected 'Key=Value'", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
prop_name = item[:eq_idx].strip()
|
||||
prop_value = item[eq_idx + 1:].strip()
|
||||
|
||||
prop_el = None
|
||||
for child in props_el:
|
||||
if isinstance(child.tag, str) and localname(child) == prop_name:
|
||||
prop_el = child
|
||||
break
|
||||
if prop_el is None:
|
||||
print(f"Property '{prop_name}' not found in Properties", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if prop_name in ML_PROPS:
|
||||
for ch in list(prop_el):
|
||||
prop_el.remove(ch)
|
||||
if not prop_value:
|
||||
prop_el.text = None
|
||||
else:
|
||||
indent = get_child_indent(props_el)
|
||||
item_el = etree.SubElement(prop_el, f"{{{V8_NS}}}item")
|
||||
lang_el = etree.SubElement(item_el, f"{{{V8_NS}}}lang")
|
||||
lang_el.text = "ru"
|
||||
content_el = etree.SubElement(item_el, f"{{{V8_NS}}}content")
|
||||
content_el.text = prop_value
|
||||
prop_el.text = "\r\n" + indent + "\t"
|
||||
item_el.text = "\r\n" + indent + "\t\t"
|
||||
lang_el.tail = "\r\n" + indent + "\t\t"
|
||||
content_el.tail = "\r\n" + indent + "\t"
|
||||
item_el.tail = "\r\n" + indent
|
||||
elif prop_name in SCALAR_PROPS or prop_name in REF_PROPS:
|
||||
for ch in list(prop_el):
|
||||
prop_el.remove(ch)
|
||||
if not prop_value:
|
||||
prop_el.text = None
|
||||
else:
|
||||
prop_el.text = prop_value
|
||||
else:
|
||||
for ch in list(prop_el):
|
||||
prop_el.remove(ch)
|
||||
prop_el.text = prop_value
|
||||
|
||||
modify_count += 1
|
||||
info(f'Set {prop_name} = "{prop_value}"')
|
||||
|
||||
def do_add_child_object(batch_val):
|
||||
nonlocal add_count
|
||||
if child_objs_el is None:
|
||||
print("No <ChildObjects> element found", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
items = parse_batch_value(batch_val)
|
||||
cfg_indent = get_child_indent(cfg_el)
|
||||
if len(child_objs_el) == 0 and not (child_objs_el.text and child_objs_el.text.strip()):
|
||||
expand_self_closing(child_objs_el, cfg_indent)
|
||||
child_indent = get_child_indent(child_objs_el)
|
||||
|
||||
for item in items:
|
||||
dot_idx = item.find(".")
|
||||
if dot_idx < 1:
|
||||
print(f"Invalid format '{item}', expected 'Type.Name'", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
type_name = item[:dot_idx]
|
||||
obj_name_val = item[dot_idx + 1:]
|
||||
|
||||
if type_name not in TYPE_ORDER:
|
||||
print(f"Unknown type '{type_name}'", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
type_idx = TYPE_ORDER.index(type_name)
|
||||
|
||||
# Check that the referenced object actually exists on disk.
|
||||
# cf-edit add-childObject is a low-level operation for rare scenarios
|
||||
# (e.g. restoring a rolled-back Configuration.xml when object files are intact).
|
||||
# For creating NEW objects, meta-compile/role-compile/subsystem-compile already
|
||||
# auto-register in Configuration.xml — calling cf-edit add-childObject there is
|
||||
# unnecessary and error-prone.
|
||||
type_dir = TYPE_TO_DIR.get(type_name)
|
||||
obj_file = os.path.join(config_dir, type_dir, f"{obj_name_val}.xml")
|
||||
if not os.path.exists(obj_file):
|
||||
hint_skill = {"Subsystem": "subsystem-compile", "Role": "role-compile"}.get(type_name, "meta-compile")
|
||||
print(
|
||||
f"Object file not found: {type_dir}/{obj_name_val}.xml\n"
|
||||
f"cf-edit add-childObject only references objects that already exist on disk.\n"
|
||||
f"To create a new {type_name}, use {hint_skill} (auto-registers in Configuration.xml):\n"
|
||||
f' /{hint_skill} with {{"type":"{type_name}","name":"{obj_name_val}"}}',
|
||||
file=sys.stderr
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Dedup
|
||||
exists = False
|
||||
for child in child_objs_el:
|
||||
if isinstance(child.tag, str) and localname(child) == type_name and (child.text or "") == obj_name_val:
|
||||
exists = True
|
||||
break
|
||||
if exists:
|
||||
warn(f"Already exists: {type_name}.{obj_name_val}")
|
||||
continue
|
||||
|
||||
# Find insertion point
|
||||
insert_before = None
|
||||
for child in child_objs_el:
|
||||
if not isinstance(child.tag, str):
|
||||
continue
|
||||
child_type_name = localname(child)
|
||||
if child_type_name not in TYPE_ORDER:
|
||||
continue
|
||||
child_type_idx = TYPE_ORDER.index(child_type_name)
|
||||
|
||||
if child_type_name == type_name:
|
||||
if (child.text or "") > obj_name_val and insert_before is None:
|
||||
insert_before = child
|
||||
elif child_type_idx > type_idx and insert_before is None:
|
||||
insert_before = child
|
||||
|
||||
new_el = etree.Element(f"{{{MD_NS}}}{type_name}")
|
||||
new_el.text = obj_name_val
|
||||
|
||||
if insert_before is not None:
|
||||
insert_before_ref(child_objs_el, new_el, insert_before, child_indent)
|
||||
else:
|
||||
insert_before_closing(child_objs_el, new_el, child_indent)
|
||||
|
||||
add_count += 1
|
||||
info(f"Added: {type_name}.{obj_name_val}")
|
||||
|
||||
def do_remove_child_object(batch_val):
|
||||
nonlocal remove_count
|
||||
if child_objs_el is None:
|
||||
print("No <ChildObjects> element found", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
items = parse_batch_value(batch_val)
|
||||
for item in items:
|
||||
dot_idx = item.find(".")
|
||||
if dot_idx < 1:
|
||||
print(f"Invalid format '{item}', expected 'Type.Name'", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
type_name = item[:dot_idx]
|
||||
obj_name_val = item[dot_idx + 1:]
|
||||
|
||||
found = False
|
||||
for child in list(child_objs_el):
|
||||
if isinstance(child.tag, str) and localname(child) == type_name and (child.text or "") == obj_name_val:
|
||||
remove_with_indent(child)
|
||||
remove_count += 1
|
||||
info(f"Removed: {type_name}.{obj_name_val}")
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
warn(f"Not found: {type_name}.{obj_name_val}")
|
||||
|
||||
def do_add_default_role(batch_val):
|
||||
nonlocal add_count
|
||||
items = parse_batch_value(batch_val)
|
||||
|
||||
roles_el = None
|
||||
for child in props_el:
|
||||
if isinstance(child.tag, str) and localname(child) == "DefaultRoles":
|
||||
roles_el = child
|
||||
break
|
||||
if roles_el is None:
|
||||
print("No <DefaultRoles> element found in Properties", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
props_indent = get_child_indent(props_el)
|
||||
if len(roles_el) == 0 and not (roles_el.text and roles_el.text.strip()):
|
||||
expand_self_closing(roles_el, props_indent)
|
||||
role_indent = get_child_indent(roles_el)
|
||||
|
||||
for item in items:
|
||||
role_name = item
|
||||
if not role_name.startswith("Role."):
|
||||
role_name = f"Role.{role_name}"
|
||||
|
||||
exists = False
|
||||
for child in roles_el:
|
||||
if isinstance(child.tag, str) and (child.text or "").strip() == role_name:
|
||||
exists = True
|
||||
break
|
||||
if exists:
|
||||
warn(f"DefaultRole already exists: {role_name}")
|
||||
continue
|
||||
|
||||
frag_xml = f'<xr:Item xsi:type="xr:MDObjectRef">{role_name}</xr:Item>'
|
||||
nodes = import_fragment(frag_xml)
|
||||
if nodes:
|
||||
insert_before_closing(roles_el, nodes[0], role_indent)
|
||||
add_count += 1
|
||||
info(f"Added DefaultRole: {role_name}")
|
||||
|
||||
def do_remove_default_role(batch_val):
|
||||
nonlocal remove_count
|
||||
items = parse_batch_value(batch_val)
|
||||
|
||||
roles_el = None
|
||||
for child in props_el:
|
||||
if isinstance(child.tag, str) and localname(child) == "DefaultRoles":
|
||||
roles_el = child
|
||||
break
|
||||
if roles_el is None:
|
||||
print("No <DefaultRoles> element found", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
for item in items:
|
||||
role_name = item
|
||||
if not role_name.startswith("Role."):
|
||||
role_name = f"Role.{role_name}"
|
||||
|
||||
found = False
|
||||
for child in list(roles_el):
|
||||
if isinstance(child.tag, str) and (child.text or "").strip() == role_name:
|
||||
remove_with_indent(child)
|
||||
remove_count += 1
|
||||
info(f"Removed DefaultRole: {role_name}")
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
warn(f"DefaultRole not found: {role_name}")
|
||||
|
||||
def do_set_default_roles(batch_val):
|
||||
nonlocal modify_count
|
||||
items = parse_batch_value(batch_val)
|
||||
|
||||
roles_el = None
|
||||
for child in props_el:
|
||||
if isinstance(child.tag, str) and localname(child) == "DefaultRoles":
|
||||
roles_el = child
|
||||
break
|
||||
if roles_el is None:
|
||||
print("No <DefaultRoles> element found", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# Clear all existing children
|
||||
for ch in list(roles_el):
|
||||
roles_el.remove(ch)
|
||||
roles_el.text = None
|
||||
|
||||
if not items:
|
||||
modify_count += 1
|
||||
info("Cleared DefaultRoles")
|
||||
return
|
||||
|
||||
props_indent = get_child_indent(props_el)
|
||||
role_indent = props_indent + "\t"
|
||||
|
||||
roles_el.text = "\r\n" + props_indent
|
||||
|
||||
for item in items:
|
||||
role_name = item
|
||||
if not role_name.startswith("Role."):
|
||||
role_name = f"Role.{role_name}"
|
||||
|
||||
frag_xml = f'<xr:Item xsi:type="xr:MDObjectRef">{role_name}</xr:Item>'
|
||||
nodes = import_fragment(frag_xml)
|
||||
if nodes:
|
||||
insert_before_closing(roles_el, nodes[0], role_indent)
|
||||
|
||||
modify_count += 1
|
||||
info(f"Set DefaultRoles: {len(items)} roles")
|
||||
|
||||
# --- set-panels (writes Ext/ClientApplicationInterface.xml from scratch) ---
|
||||
# Canonical English aliases — preferred form, used in docs and error messages.
|
||||
PANEL_UUIDS = {
|
||||
"sections": "b553047f-c9aa-4157-978d-448ecad24248",
|
||||
"open": "cbab57f2-a0f3-4f0a-89ea-4cb19570ab75",
|
||||
"favorites": "13322b22-3960-4d68-93a6-fe2dd7f28ca3",
|
||||
"history": "c933ac92-92cd-459d-81cc-e0c8a83ced99",
|
||||
"functions": "b2735bd3-d822-4430-ba59-c9e869693b24",
|
||||
}
|
||||
# Russian synonyms — silently accepted (cf-info displays Russian names;
|
||||
# users may copy them straight into cf-edit value).
|
||||
PANEL_SYNONYMS = {
|
||||
"разделов": "sections", "разделы": "sections",
|
||||
"открытых": "open", "открытые": "open",
|
||||
"избранного": "favorites","избранное": "favorites",
|
||||
"истории": "history", "история": "history",
|
||||
"функций": "functions", "функции": "functions",
|
||||
}
|
||||
|
||||
def build_panel_entry_xml(entry, indent):
|
||||
if isinstance(entry, str):
|
||||
key = entry.lower()
|
||||
key = PANEL_SYNONYMS.get(key, key)
|
||||
if key not in PANEL_UUIDS:
|
||||
allowed = ", ".join(sorted(PANEL_UUIDS.keys()))
|
||||
print(f"Unknown panel alias '{entry}'. Allowed: {allowed}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
inst = str(_uuid.uuid4())
|
||||
return f'{indent}<panel id="{inst}">\r\n{indent}\t<uuid>{PANEL_UUIDS[key]}</uuid>\r\n{indent}</panel>'
|
||||
if isinstance(entry, dict) and "group" in entry:
|
||||
children = entry["group"]
|
||||
if not children:
|
||||
print("group must contain at least one entry", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
gid = str(_uuid.uuid4())
|
||||
inner = ""
|
||||
for child in children:
|
||||
child_xml = build_panel_entry_xml(child, indent + "\t\t")
|
||||
inner += f"{indent}\t<group>\r\n{child_xml}\r\n{indent}\t</group>\r\n"
|
||||
return f'{indent}<group id="{gid}">\r\n{inner}{indent}</group>'
|
||||
print(f"Panel entry must be string alias or {{group:[...]}}, got: {entry!r}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
def do_set_panels(value):
|
||||
nonlocal modify_count
|
||||
layout = value
|
||||
if isinstance(layout, str):
|
||||
try:
|
||||
layout = json.loads(layout)
|
||||
except json.JSONDecodeError:
|
||||
print(f"set-panels value must be valid JSON object", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not isinstance(layout, dict) or not layout:
|
||||
print("set-panels value must be non-empty object", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
sides = ("top", "left", "right", "bottom")
|
||||
# Reject unknown side keys
|
||||
for k in layout.keys():
|
||||
if k not in sides:
|
||||
print(f"Unknown side '{k}'. Allowed: {', '.join(sides)}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
body_parts = []
|
||||
for side in sides:
|
||||
entries = layout.get(side)
|
||||
if entries is None:
|
||||
continue
|
||||
if not isinstance(entries, list):
|
||||
entries = [entries]
|
||||
for entry in entries:
|
||||
entry_xml = build_panel_entry_xml(entry, "\t\t")
|
||||
body_parts.append(f"\t<{side}>\r\n{entry_xml}\r\n\t</{side}>")
|
||||
body = "\r\n".join(body_parts)
|
||||
body_block = body + "\r\n" if body else ""
|
||||
declarations = (
|
||||
'\t<panelDef id="b553047f-c9aa-4157-978d-448ecad24248"/>\r\n'
|
||||
'\t<panelDef id="13322b22-3960-4d68-93a6-fe2dd7f28ca3"/>\r\n'
|
||||
'\t<panelDef id="c933ac92-92cd-459d-81cc-e0c8a83ced99"/>\r\n'
|
||||
'\t<panelDef id="cbab57f2-a0f3-4f0a-89ea-4cb19570ab75"/>\r\n'
|
||||
'\t<panelDef id="b2735bd3-d822-4430-ba59-c9e869693b24"/>'
|
||||
)
|
||||
cai_xml = (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\r\n'
|
||||
'<ClientApplicationInterface xmlns="http://v8.1c.ru/8.2/managed-application/core" '
|
||||
'xmlns:xs="http://www.w3.org/2001/XMLSchema" '
|
||||
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
|
||||
'xsi:type="InterfaceLayouter">\r\n'
|
||||
f'{body_block}{declarations}\r\n'
|
||||
'</ClientApplicationInterface>'
|
||||
)
|
||||
ext_dir = os.path.join(config_dir, "Ext")
|
||||
os.makedirs(ext_dir, exist_ok=True)
|
||||
cai_path = os.path.join(ext_dir, "ClientApplicationInterface.xml")
|
||||
with open(cai_path, "w", encoding="utf-8-sig", newline="") as fh:
|
||||
fh.write(cai_xml)
|
||||
modify_count += 1
|
||||
info(f"Wrote panel layout: {cai_path}")
|
||||
|
||||
# --- set-home-page (writes Ext/HomePageWorkArea.xml from scratch) ---
|
||||
RU_TYPE_MAP = {
|
||||
"справочник": "Catalog", "документ": "Document", "перечисление": "Enum",
|
||||
"отчёт": "Report", "отчет": "Report", "обработка": "DataProcessor",
|
||||
"общаяформа": "CommonForm", "журналдокументов": "DocumentJournal",
|
||||
"планвидовхарактеристик": "ChartOfCharacteristicTypes",
|
||||
"плансчетов": "ChartOfAccounts",
|
||||
"планвидоврасчета": "ChartOfCalculationTypes",
|
||||
"планвидоврасчёта": "ChartOfCalculationTypes",
|
||||
"регистрсведений": "InformationRegister",
|
||||
"регистрнакопления": "AccumulationRegister",
|
||||
"регистрбухгалтерии": "AccountingRegister",
|
||||
"регистррасчета": "CalculationRegister",
|
||||
"регистррасчёта": "CalculationRegister",
|
||||
"бизнеспроцесс": "BusinessProcess",
|
||||
"задача": "Task", "планобмена": "ExchangePlan",
|
||||
"хранилищенастроек": "SettingsStorage",
|
||||
}
|
||||
DIR_TO_TYPE = {v.lower(): k for k, v in TYPE_TO_DIR.items()}
|
||||
UUID_RE = __import__("re").compile(r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
|
||||
|
||||
def normalize_form_ref(s):
|
||||
s = (s or "").strip()
|
||||
if not s:
|
||||
return s
|
||||
if UUID_RE.match(s):
|
||||
return s
|
||||
if "/" in s or "\\" in s:
|
||||
parts = [p for p in s.replace("\\", "/").split("/") if p and p.lower() != "ext"]
|
||||
if parts and parts[-1].lower() == "form.xml":
|
||||
parts = parts[:-1]
|
||||
if len(parts) >= 2:
|
||||
type_dir = parts[0]
|
||||
type_singular = DIR_TO_TYPE.get(type_dir.lower())
|
||||
if type_singular:
|
||||
if type_singular == "CommonForm" and len(parts) >= 2:
|
||||
return f"CommonForm.{parts[1]}"
|
||||
if len(parts) >= 4 and parts[2].lower() == "forms":
|
||||
return f"{type_singular}.{parts[1]}.Form.{parts[3]}"
|
||||
return s
|
||||
segs = s.split(".")
|
||||
if segs:
|
||||
head = segs[0].lower()
|
||||
if head in RU_TYPE_MAP:
|
||||
segs[0] = RU_TYPE_MAP[head]
|
||||
for i in range(1, len(segs)):
|
||||
if segs[i] == "Форма":
|
||||
segs[i] = "Form"
|
||||
if len(segs) == 3 and segs[0] in TYPE_ORDER and segs[0] != "CommonForm":
|
||||
segs = [segs[0], segs[1], "Form", segs[2]]
|
||||
return ".".join(segs)
|
||||
|
||||
def get_field(obj, keys):
|
||||
for k in keys:
|
||||
if isinstance(obj, dict) and k in obj:
|
||||
return obj[k]
|
||||
return None
|
||||
|
||||
def build_home_page_item_xml(entry, indent):
|
||||
if isinstance(entry, str):
|
||||
form_ref = normalize_form_ref(entry)
|
||||
height = 10
|
||||
common = True
|
||||
roles = None
|
||||
elif isinstance(entry, dict):
|
||||
form_raw = get_field(entry, ["form", "Form"])
|
||||
if not form_raw:
|
||||
print(f"Home page item: 'form' is required, got: {entry!r}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
form_ref = normalize_form_ref(str(form_raw))
|
||||
h = get_field(entry, ["height", "Height"])
|
||||
height = int(h) if h is not None else 10
|
||||
vis = get_field(entry, ["visibility", "Visibility"])
|
||||
common = bool(vis) if vis is not None else True
|
||||
roles = get_field(entry, ["roles"])
|
||||
else:
|
||||
print(f"Home page item must be string or object, got: {entry!r}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
vis_parts = [f"{indent}\t\t<xr:Common>{str(common).lower()}</xr:Common>"]
|
||||
if roles and isinstance(roles, dict):
|
||||
for rname, rval in roles.items():
|
||||
if not rname.startswith("Role.") and not UUID_RE.match(rname):
|
||||
rname = f"Role.{rname}"
|
||||
rval_s = str(bool(rval)).lower()
|
||||
vis_parts.append(f'{indent}\t\t<xr:Value name="{html_escape(rname, quote=True)}">{rval_s}</xr:Value>')
|
||||
vis_block = "\r\n".join(vis_parts)
|
||||
esc_form = html_escape(form_ref, quote=True)
|
||||
return (
|
||||
f"{indent}<Item>\r\n"
|
||||
f"{indent}\t<Form>{esc_form}</Form>\r\n"
|
||||
f"{indent}\t<Height>{height}</Height>\r\n"
|
||||
f"{indent}\t<Visibility>\r\n"
|
||||
f"{vis_block}\r\n"
|
||||
f"{indent}\t</Visibility>\r\n"
|
||||
f"{indent}</Item>"
|
||||
)
|
||||
|
||||
def do_set_home_page(value):
|
||||
nonlocal modify_count
|
||||
layout = value
|
||||
if isinstance(layout, str):
|
||||
try:
|
||||
layout = json.loads(layout)
|
||||
except json.JSONDecodeError:
|
||||
print("set-home-page value must be valid JSON object", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not isinstance(layout, dict) or not layout:
|
||||
print("set-home-page value must be non-empty object", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
allowed_templates = ("OneColumn", "TwoColumnsEqualWidth", "TwoColumnsVariableWidth")
|
||||
tmpl = get_field(layout, ["template", "WorkingAreaTemplate"]) or "TwoColumnsEqualWidth"
|
||||
if tmpl not in allowed_templates:
|
||||
print(f"Unknown template '{tmpl}'. Allowed: {', '.join(allowed_templates)}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
left_items = get_field(layout, ["left", "LeftColumn"])
|
||||
right_items = get_field(layout, ["right", "RightColumn"])
|
||||
|
||||
known = {"template", "WorkingAreaTemplate", "left", "LeftColumn", "right", "RightColumn"}
|
||||
for k in layout.keys():
|
||||
if k not in known:
|
||||
print(f"Unknown key '{k}'. Allowed: template, left, right", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if tmpl == "OneColumn" and right_items:
|
||||
print("Template 'OneColumn' cannot have items in 'right' column", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
def build_column(tag, items):
|
||||
if not items:
|
||||
return f"\t<{tag}/>"
|
||||
if not isinstance(items, list):
|
||||
items = [items]
|
||||
if not items:
|
||||
return f"\t<{tag}/>"
|
||||
blocks = [build_home_page_item_xml(it, "\t\t") for it in items]
|
||||
body = "\r\n".join(blocks)
|
||||
return f"\t<{tag}>\r\n{body}\r\n\t</{tag}>"
|
||||
|
||||
left_xml = build_column("LeftColumn", left_items)
|
||||
right_xml = build_column("RightColumn", right_items)
|
||||
|
||||
hp_xml = (
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\r\n'
|
||||
'<HomePageWorkArea xmlns="http://v8.1c.ru/8.3/xcf/extrnprops" '
|
||||
'xmlns:xr="http://v8.1c.ru/8.3/xcf/readable" '
|
||||
'xmlns:xs="http://www.w3.org/2001/XMLSchema" '
|
||||
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.17">\r\n'
|
||||
f'\t<WorkingAreaTemplate>{tmpl}</WorkingAreaTemplate>\r\n'
|
||||
f'{left_xml}\r\n'
|
||||
f'{right_xml}\r\n'
|
||||
'</HomePageWorkArea>'
|
||||
)
|
||||
|
||||
ext_dir = os.path.join(config_dir, "Ext")
|
||||
os.makedirs(ext_dir, exist_ok=True)
|
||||
hp_path = os.path.join(ext_dir, "HomePageWorkArea.xml")
|
||||
with open(hp_path, "w", encoding="utf-8-sig", newline="") as fh:
|
||||
fh.write(hp_xml)
|
||||
modify_count += 1
|
||||
info(f"Wrote home page layout: {hp_path}")
|
||||
|
||||
# --- Execute operations ---
|
||||
operations = []
|
||||
if args.DefinitionFile:
|
||||
def_file = args.DefinitionFile
|
||||
if not os.path.isabs(def_file):
|
||||
def_file = os.path.join(os.getcwd(), def_file)
|
||||
with open(def_file, "r", encoding="utf-8-sig") as fh:
|
||||
ops = json.loads(fh.read())
|
||||
if isinstance(ops, list):
|
||||
operations = ops
|
||||
else:
|
||||
operations = [ops]
|
||||
else:
|
||||
operations = [{"operation": args.Operation, "value": args.Value or ""}]
|
||||
|
||||
for op in operations:
|
||||
op_name = op.get("operation", args.Operation or "")
|
||||
op_value = op.get("value", args.Value or "")
|
||||
|
||||
if op_name == "modify-property":
|
||||
do_modify_property(op_value if isinstance(op_value, str) else str(op_value))
|
||||
elif op_name == "add-childObject":
|
||||
do_add_child_object(op_value if isinstance(op_value, str) else str(op_value))
|
||||
elif op_name == "remove-childObject":
|
||||
do_remove_child_object(op_value if isinstance(op_value, str) else str(op_value))
|
||||
elif op_name == "add-defaultRole":
|
||||
do_add_default_role(op_value if isinstance(op_value, str) else str(op_value))
|
||||
elif op_name == "remove-defaultRole":
|
||||
do_remove_default_role(op_value if isinstance(op_value, str) else str(op_value))
|
||||
elif op_name == "set-defaultRoles":
|
||||
do_set_default_roles(op_value if isinstance(op_value, str) else str(op_value))
|
||||
elif op_name == "set-panels":
|
||||
do_set_panels(op_value)
|
||||
elif op_name == "set-home-page":
|
||||
do_set_home_page(op_value)
|
||||
else:
|
||||
print(f"Unknown operation: {op_name}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# --- Save ---
|
||||
save_xml_bom(tree, resolved_path)
|
||||
info(f"Saved: {resolved_path}")
|
||||
|
||||
# --- Auto-validate ---
|
||||
if not args.NoValidate:
|
||||
validate_script = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..", "cf-validate", "scripts", "cf-validate.py"))
|
||||
if os.path.isfile(validate_script):
|
||||
print()
|
||||
print("--- Running cf-validate ---")
|
||||
subprocess.run([sys.executable, validate_script, "-ConfigPath", "-Path", resolved_path])
|
||||
|
||||
# --- Summary ---
|
||||
print()
|
||||
print("=== cf-edit summary ===")
|
||||
print(f" Configuration: {obj_name}")
|
||||
print(f" Added: {add_count}")
|
||||
print(f" Removed: {remove_count}")
|
||||
print(f" Modified: {modify_count}")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user