Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to specify an order for the Classes? #18

Closed
gaelcolas opened this issue Aug 5, 2018 · 5 comments
Closed

How to specify an order for the Classes? #18

gaelcolas opened this issue Aug 5, 2018 · 5 comments
Labels
12 - Discussion Input requested from the PoshCode team 20 - Enhancement

Comments

@gaelcolas
Copy link
Contributor

As you know Classes sometimes need to be imported or merged in a specific order.

I haven't found an elegant way of importing them in a specific order without prepending a number in the file name (which I'm not a fan of either).

e.g.:

1.ClassA.ps1
2.ClassC.ps1
3.ClassB.ps1

I tend to prefer specifying the order in a PSD1 and/or sorting the files within the Classes folder for merging them in the PSM1, but that's a bit over-engineered:

$order = {(Import-PowerShellDataFile -EA 0 .\Classes\classes.psd1).order.indexOf($_.BaseName)}
Get-ChildItem $FilePath -Filter *.ps1 -Recurse | Sort-Object $Order | Merge-ToModule $ModuleInfo

Happy to go with file names (less code), if that's your intent.

@Jaykul
Copy link
Member

Jaykul commented Aug 5, 2018

We specifically talked about this during design.

The only correct answer is for PowerShell to fix this so the order doesn't matter.

Anything else is a workaround.

In my opinion, the simplest workarounds are the best: number your files to force the order. It's not only simple, it's easy and obvious (people who look at the code can see you deliberately ordered the files). In the worst case, we need to add an explicit Sort-Object Name to preserve the order in a cross-platform way (I'm not totally sure Get-ChildItem is inherently ordered), but this has been working for me.

The only other ordering that we follow is that the SourceDirectories are processed in order. If you really want to, you can use and abuse that to get anything you need (please don't). In fact, at design time I gave in to adding the "enum" folder (which I personally think is a bad idea, and won't use) because so many of the folks who were present were convinced that enums are different, and this would be enough for them to handle their dependencies.

I would be happy to consider automatically determining the order at compile time if anyone wants to have a go at that, but right now I don't really think it's worth the effort (except that I'd love for that to be built in to PowerShell).

@gaelcolas
Copy link
Contributor Author

Good for me.

@Jaykul
Copy link
Member

Jaykul commented Aug 14, 2018

What do you think of adding a sort-of pre-compiler directive option, like a line at the top of the file:

#using class OtherClass, EnumFour

Then we could just read the very first line and sort by that? I mean, it's still a hack, but it's a simple hack, and it doesn't require much parsing, nor affect line numbering.

It would not affect the current solution if people want to number them instead, but doing it this way would allow your class files to be in alphabetical order in your IDE without a major impact.

Either way, should we also add something where we attempt to load the module, and try to detect errors caused by bad order?

@Jaykul
Copy link
Member

Jaykul commented Oct 20, 2018

Is this actually a problem?

I just tried putting this in a Test.psm1 file:

class Computer {
    [string]$Name
    [Person]$Owner
    [string]ToString() { return $this.Name }
}

class Person {
    [string]$Name
    [Computer]$Computer
    [string]ToString() { return $this.Name }
}

function New-User {
    [CmdletBinding()]
    param([Parameter(Mandatory)]$UserName, $ComputerName)
    $P = [Person]@{
        Name = $UserName
    }
    $C = [Computer]@{
        Name = $ComputerName
        Owner = $P
    }
    $P.Computer = $C

    $P
}

That seems to work even in PowerShell 5 ...

@gaelcolas
Copy link
Contributor Author

You're right, it is not a problem in a compiled PSM1.
It matters only when dot sourcing, which is what I use to do for reasons similar to #35.
I was importing the classes in the sessions similar to scripts to process (yeah, really hacky) for troubleshooting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
12 - Discussion Input requested from the PoshCode team 20 - Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants