Skip to content

refactor(pivot): one function per file, and the three long functions in steps - #171

Merged
eizedev merged 6 commits into
mainfrom
refactor/pivot-split
Nov 22, 2026
Merged

eizedev merged 6 commits into
mainfrom
refactor/pivot-split

Conversation

@eizedev

@eizedev eizedev commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Refs #163

PowerShellUniversal.Component.Pivot.psm1 held 66 functions, the settings and two C# sources in here-strings. This pull request splits it into one function per file and shortens the three Pivot functions over 100 code lines. Commands, parameters, help and output stay the same.

What changed

  • Public/: the 11 exported commands, one per file. Export-ModuleMember takes the file names; FunctionsToExport in the manifest is unchanged.
  • Private/: the helpers, one per file. Private/Settings/ holds the $Script: limits, patterns and state by topic. They load after the functions, because the report engine's settings call one.
  • Private/ReportWriter.cs and Private/HttpTransport.cs: the C# the report engine compiles on first use. Get-UDPivotTypeSource gives each a type name from its content hash. The two here-strings used to compute that separately.
  • The .psm1 only loads. A source checkout loads shared/powershell directly. It is recognised by the missing Private/SHARED.txt, which the build writes. The old check (no Private/ folder at all) no longer works once the module has its own Private/.
  • build/Build-Module.ps1 copies Public/ and Private/ (.ps1 and .cs) into the built module.
  • New-UDPivotReport, Invoke-UDPivotTemplateStore and Invoke-UDPivotReport run their steps through helpers with one job each. Inputs are passed as parameters, and the six script blocks of the template store became functions. Each step keeps its order, side effects and messages. Calls that read $Headers, $User, $Roles or $Query of the caller still do so: through -Cmdlet $PSCmdlet, or from the same function as before.
  • Helpers that need many of a public command's parameters get its $PSBoundParameters as -Bound. Parameters with defaults (-Id, -Locale, -TimeZone, -Theme, -FileName, -MaxBytes, -TimeoutSec) are passed on their own, because they are not in $PSBoundParameters. A test checks that every $Bound['key'] a helper reads is a parameter of a public command.
  • Tests: four tests read source text from the module file. They now read Private/Settings/ or Public/; only their paths changed. New: PivotModule.Tests.ps1 (Public/ matches FunctionsToExport, the -Bound keys, the admin panel's template count, an empty -Id) and a build test that the built module carries every Public/ and Private/ file and, imported in a process of its own, exports the manifest's commands. Build-Module.ps1 refuses a Private/*.ps1 named like one of shared/powershell, which the build copies into the same folder.
  • AGENTS.md describes the layout of a split module. The Pivot CHANGELOG has an entry under Unreleased.

Commit by commit: the move with the test paths (every function's text unchanged, split by the parser's extents), then one commit per shortened function, the baseline, and a last commit with names and build checks. Pester passes on each commit.

Why the .psm1 dot-sources instead of a merged file in the build

PSU imports the module into each app runspace. Its docs set no file layout for a module; they show a .psd1 with a .psm1 root module. The PSU plugin's module template (eizedev/claude-plugins#89) and AGENTS.md use Public/ and Private/ dot-sourced at import. Dot-sourcing keeps the tested source and the shipped module the same, and error positions point to the real files.

The cost, measured on macOS with PowerShell 7.6 (built module, Endpoint stub; not measured on PSU or Windows):

Import Before (one file) After (98 files)
First import in a fresh process about 115 ms about 168 ms
Each further runspace in the same process about 10 ms about 21 ms

I do not consider about 11 ms per runspace import material. If it turns out to matter on PSU, the build can merge the files into the shipped .psm1 without changing the source layout.

Growth numbers

Offender Before After
PowerShellUniversal.Component.Pivot.psm1 (file) 2,731 10
New-UDPivotReport 267 95 (55 of them parameters)
Invoke-UDPivotTemplateStore 158 51
Invoke-UDPivotReport 150 55

The baseline is lowered in this pull request. No new offender. The largest Pivot helper now has 93 code lines (ConvertTo-UDPivotFieldDefault, unchanged).

Checks

  • Get-Command -Module with parameter sets, parameter attributes and OutputType, Get-Help -Full of every command, and the exported functions: identical before and after. Checked for the source module and the built module.
  • docs/site/Build-Site.ps1: docs/site/generated/ is identical before and after. Its examples run the real New-UDPivotReport.
  • Pester: 362 passed, 0 failed, 7 skipped (main: 357 and 7; five tests are new). All modules built. With PIVOTKIT_TEST_CLI=1, the two tests against the installed pivotkit command line pass too.
  • npm test: 165 passed. PSScriptAnalyzer as in CI: 0 findings. Growth: all four guards ok.
  • Build-Module.ps1 for all five modules. The built Pivot module carries the same files as before plus Public/, Private/*.ps1, Private/Settings/ and Private/*.cs.
  • Places checked: AGENTS.md, the Pivot README and CHANGELOG, docs/ (no page names the module file), docs/development, Build-Site, CI, and the tests that read source text.

Not in this pull request

🤖 Generated with Claude Code

eizedev and others added 6 commits November 22, 2026 23:53
PowerShellUniversal.Component.Pivot.psm1 held 66 functions, the module's
settings and two C# sources in here-strings. It now only loads:

- Public/: the 11 exported commands, one per file (exports follow the files)
- Private/: the 55 helpers, one per file
- Private/Settings/: the $Script: limits, patterns and state, by topic,
  loaded after the functions
- Private/ReportWriter.cs and HttpTransport.cs: the C# the report engine
  compiles on first use; Get-UDPivotTypeSource gives each a per-content
  type name, which both here-strings computed by hand before

Every function's text moved unchanged (split by the parser's extents).
Build-Module copies Public/ and Private/ (.ps1 and .cs) into the built
module and refuses a file named like one of shared/powershell. A source
checkout loads shared/powershell directly, recognised by the missing
SHARED.txt that the build writes into Private/.

Four tests read the module's source text: they read the tip ids and
import limits from Private/Settings and the help and error-mark lines from
Public/ now; only their paths changed. A new build test checks that the
built module carries every Public/ and Private/ file.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
New-UDPivotReport had 267 code lines. It now checks its parameters and
puts the props together from helpers, each with one job and its inputs as
parameters: the parameter checks, the server's rows or the reader's
problem, the storage key, the props every report has, export, view, field
and display props, the templates and their store (shipped views, store
views, the merged list, its cap, the store's endpoints), the events and
the admin panel. The order of the steps, their messages and the props
stay as they were. 95 code lines remain, 55 of them parameters.

PivotModule.Tests.ps1 checks that Public/ matches FunctionsToExport, that
every $Bound['key'] a helper reads is a parameter of a public command,
the admin panel's template count, and an empty -Id.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Invoke-UDPivotTemplateStore had 158 code lines and six script blocks that
read its variables through scope. Each block is a helper now, with its
inputs as parameters: the store context (scope, user key, folder, limit,
request), the stored views and one custom-store entry, which views a
report lists, the views of a name, a custom store's request, deleting
one file, clearing stored copies, a free file name and writing a view.
The steps keep their order and messages; ConvertTo-UDPivotViewFile and
Get-UDPivotSchemaUrl are still called from the function itself, so they
read its -Headers as before.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Invoke-UDPivotReport had 150 code lines. It now runs its steps through
helpers with their inputs as parameters: the parameters of each engine,
the REST engine's inputs, the output path and format, the view or layout
and title, the REST render or link, and for the command line the data
file, the arguments, the answer and the run in a private work folder.
ShouldProcess, the order of the checks and the messages stay where they
were.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The module file and the three long functions are within the limits now:
Pivot.psm1 from 2,731 to 10 code lines, New-UDPivotReport from 267 to 95,
Invoke-UDPivotTemplateStore from 158 to 51, Invoke-UDPivotReport from 150
to 55.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Get-UDPivotReportTemplate checks, registers the store's endpoints and
  returns props: Resolve-UDPivotReportTemplate says that better.
- Build-Module refuses a name clash with shared/powershell only where the
  shared copy lands (Private/*.ps1, not Private/Settings or .cs files).
- The build test imports the built folder in a process of its own and
  compares its exports with the manifest.
- The -Bound test also reads $Bound.Keys -contains 'X' and says what it
  does not check.

Refs #163

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@eizedev
eizedev merged commit 0f29450 into main Nov 22, 2026
11 checks passed
@eizedev
eizedev deleted the refactor/pivot-split branch November 22, 2026 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant