Skip to content

Commit 2e3fb63

Browse files
Document PowerShell v3 compatibility and command registration
Added critical rules to ensure dbatools supports PowerShell v3 by prohibiting the use of ::new() and other v5+ features, recommending New-Object for object instantiation. Also documented the requirement to register new commands in both dbatools.psd1 and dbatools.psm1, and updated checklists and golden rules accordingly.
1 parent 996b7e6 commit 2e3fb63

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

CLAUDE.md

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ param(
3939
- Use `[switch]` for boolean flags, not `[bool]` parameters
4040
- Keep non-boolean attributes with values: `[Parameter(ValueFromPipelineByPropertyName = "Name")]`
4141

42+
### POWERSHELL v3 COMPATIBILITY
43+
44+
**CRITICAL RULE**: dbatools must support PowerShell v3. NEVER use `::new()` or other PowerShell v5+ syntax constructs.
45+
46+
**Do NOT use:**
47+
- `[ClassName]::new()` - Use `New-Object` instead
48+
- Advanced type accelerators only available in v5+
49+
- Other v5+ language features
50+
51+
```powershell
52+
# CORRECT - PowerShell v3 compatible
53+
$object = New-Object -TypeName System.Collections.Hashtable
54+
$collection = New-Object System.Collections.ArrayList
55+
56+
# WRONG - PowerShell v5+ only
57+
$object = [System.Collections.Hashtable]::new()
58+
$collection = [System.Collections.ArrayList]::new()
59+
```
60+
61+
When in doubt about version compatibility, use the `New-Object` cmdlet approach.
62+
4263
### SPLAT USAGE REQUIREMENT
4364

4465
**USE SPLATS ONLY FOR 3+ PARAMETERS**
@@ -279,6 +300,37 @@ AfterAll {
279300

280301
## DBATOOLS-SPECIFIC CONVENTIONS
281302

303+
### Command Registration
304+
305+
**CRITICAL RULE**: When adding a new command, you MUST register it in TWO places:
306+
307+
1. **Add to dbatools.psd1** - In the `FunctionsToExport` array
308+
2. **Add to dbatools.psm1** - In the explicit command export section
309+
310+
Both registrations are required for the command to be properly exported and discoverable.
311+
312+
```powershell
313+
# Example: Adding a new command "Get-DbaNewFeature"
314+
315+
# 1. In dbatools.psd1, add to FunctionsToExport:
316+
FunctionsToExport = @(
317+
'Get-DbaDatabase'
318+
'Get-DbaNewFeature' # ADD HERE
319+
'Set-DbaDatabase'
320+
# ... other commands
321+
)
322+
323+
# 2. In dbatools.psm1, add to explicit exports section:
324+
Export-ModuleMember -Function @(
325+
'Get-DbaDatabase'
326+
'Get-DbaNewFeature' # ADD HERE
327+
'Set-DbaDatabase'
328+
# ... other commands
329+
)
330+
```
331+
332+
Failure to register in both locations will result in the command not being available when users import the module.
333+
282334
### Pull Request Naming
283335

284336
**PR titles should follow this format:**
@@ -417,6 +469,11 @@ These types of tests bloat the test suite. Only add them if explicitly requested
417469
- [ ] No `= $true` used in parameter attributes (use modern syntax)
418470
- [ ] Splats used only for 3+ parameters
419471

472+
**Version Compatibility:**
473+
- [ ] No `::new()` syntax used (PowerShell v3+ compatible)
474+
- [ ] No v5+ language features used
475+
- [ ] `New-Object` used for object instantiation
476+
420477
**Style Requirements:**
421478
- [ ] Double quotes used for all strings
422479
- [ ] **MANDATORY**: Hashtable assignments perfectly aligned
@@ -432,6 +489,10 @@ These types of tests bloat the test suite. Only add them if explicitly requested
432489
- [ ] Temporary resource cleanup implemented properly
433490
- [ ] Splat usage follows 3+ parameter rule strictly
434491

492+
**Command Registration (if adding new commands):**
493+
- [ ] Command added to `FunctionsToExport` in dbatools.psd1
494+
- [ ] Command added to `Export-ModuleMember` in dbatools.psm1
495+
435496
**Test Management:**
436497
- [ ] Parameter validation test updated if parameters were added/removed
437498
- [ ] No additional unit tests added unless explicitly requested
@@ -444,8 +505,10 @@ The golden rules for dbatools code:
444505

445506
1. **NEVER use backticks** - Use splats for 3+ parameters, direct syntax for 1-2
446507
2. **NEVER use `= $true` in parameter attributes** - Use modern syntax: `[Parameter(Mandatory)]` not `[Parameter(Mandatory = $true)]`
447-
3. **ALWAYS align hashtables** - Equals signs must line up vertically
448-
4. **ALWAYS preserve comments** - Every comment stays exactly as written
449-
5. **ALWAYS use double quotes** - SQL Server module standard
450-
6. **ALWAYS use unique variable names** - Prevent scope collisions
451-
7. **ALWAYS use descriptive splatnames** - `$splatConnection`, not `$splat`
508+
3. **NEVER use `::new()` syntax** - Use `New-Object` for PowerShell v3 compatibility
509+
4. **ALWAYS align hashtables** - Equals signs must line up vertically
510+
5. **ALWAYS preserve comments** - Every comment stays exactly as written
511+
6. **ALWAYS use double quotes** - SQL Server module standard
512+
7. **ALWAYS use unique variable names** - Prevent scope collisions
513+
8. **ALWAYS use descriptive splatnames** - `$splatConnection`, not `$splat`
514+
9. **ALWAYS register new commands** - Add to both dbatools.psd1 and dbatools.psm1

0 commit comments

Comments
 (0)