RamblingCookieMonster / PSDepend

PowerShell Dependency Handler
MIT License
285 stars 76 forks source link

Handling interdependent classes #141

Open iRon7 opened 7 months ago

iRon7 commented 7 months ago

Background:

From my project, I have created a few PowerShell classes currently contained by a single file. As the project is growing (the number and size of the classes), I would like to separate my classes over multiple files. The problem with doing so is that the classes are interdependent. see: Is it possible to declare two interdependent classes each in a separate file?

mcve setup:

. $PSScriptRoot\Source\Classes\a.ps1
. $PSScriptRoot\Source\Classes\b.ps1
Export-ModuleMember -Function a, b

The issue(s) with this are in fact shown by VSCode (the PSScriptAnalyzer)

2024-04-19_12h09_10

In the comment of the related StackOverflow question @TheMadTechnician, suggests that maybe this is something PSDepend could help.

If that is correct, can you show (or refer to) an example based on this mcve?

johlju commented 7 months ago

I think you looking for ModuleBuilder or create a new module project using Sampler's Plaster templates (which will build a module using ModuleBuilder).

iRon7 commented 7 months ago

@johlju,

What I understand from the ModuleBuilder is that is pushes the author to setup a module according to certain guidelines (that only slightly differ from my own) and prepares the module for publishing. But issues #18, #35 and the line:

3. To force classes to be in a certain order, you can prefix their file names with numbers, like 01-User.ps1

Let's me believe that it won't resolve the issue of handling interdependent classes during the design (development and testing) phase...

johlju commented 7 months ago

Nothing can help resolve that, unless an editor can parse and understand the AST (which I know none that can). For a class to be known during development (assuming the goal is intellisense) it needs to be loaded into the session, and a class that is loaded into a session cannot be changed unless starting a new session.

During testing you should build the module, then load the entire module in to a session, then run tests on the built module. 🤔