Conditionnal directives - LaurentDardenne/Template GitHub Wiki

Unlike the conditional directives of programming languages the function _Edit-Template _reverses the behavior, the directives specify the code to be deleted.

To set a directive : #<DEFINE %Directive_Name%>
To cancel a directive : #<UNDEF %Directive_Name%>

You can choose any word for a directive name, except : 'Remove','Include' et 'Uncomment'

Each of these directives must be placed at the start line and may be preceded by one or more spaces or tab characters.
The directive name should not contain spaces or tabs.

Directives may nested beings :

  #<DEFINE %Directive_Name_A%>

  #<DEFINE %Directive_Name_B%>
  #<UNDEF %Directive_Name_B%>

  #<UNDEF %Directive_Name_A%>

This in not allowed :

 #<DEFINE %Directive_Name_A%>

 #<DEFINE %Directive_Name_B%>
 #<UNDEF %Directive_Name_A%>  #End of directive erroneous

 #<UNDEF %Directive_Name_B%>

Nor it :

 #<DEFINE %Directive_Name_A%>

 #<UNDEF %Directive_Name_B%>
 #<DEFINE %Directive_Name_B%>

 #<UNDEF %Directive_Name_A%>

The presence of orphan directives DEFINE or UNDEF generate an error.

@'
 Function Test-Directive {
  Write-Host "Test"
  #<DEFINE %DEBUG%>
   Write-Debug "$DebugPreference"
  #<UNDEF %DEBUG%>
 }
'@ > C:\Temp\Test1.PS1

Get-Content C:\Temp\Test1.PS1 -ReadCount 0|
 Edit-Template -Clean

The '-Clean' parameter will filter all lines containing a statement of a directive (DEFINE or UNDEF), so configured the transformed code will match this:

 Function Test-Directive {
   Write-Host "Test"
   Write-Debug "$DebugPreference"
 }
⚠️ **GitHub.com Fallback** ⚠️