Skip to content

A PowerShell module to prepend boilerplate license headers recursively or to a single file.

License

Notifications You must be signed in to change notification settings

marckassay/PrependLicense

Repository files navigation

PrependLicense

PrependLicense is a PowerShell module that automates prepending license headers to code files.

GitHub license PS Gallery

Features

  • Predefined GPL, MIT license functions via Add-GPLHeader and Add-MITHeader
  • Prepending custom header is available via Add-Header function which can also be used for unknown file types
  • Simulate what will happen via WhatIf switch on any of the "Add" functions as listed below
  • Preserves end-of-line (EOL) markings and sensitive to DTD tags (<!DOCTYPE>) in HTML files
  • Allows you to set your own comment brackets for file types

Caveat

In order to add license headers to files, this module needs to know the opening and closing comment brackets for each file type by extension that you wish to modify. Most likely you will need to modify these tables using the Set-FileTypeTable and Set-BracketTable functions. See documentation in the Usage section for these functions.

Instructions

To install, run the following command in PowerShell.

$ Install-Module PrependLicense

Usage

All of the 3 Add functions below have the option of running in simulation mode, via WhatIf switch. I recommend to initially switch WhatIf to verify what will and will not be modified.

Add-GPLHeader

$ Add-GPLHeader -Path .\src\ -ProgramName 'AiT' -ProgramDescription 'Another Interval Timer' -Author 'Marc Kassay'

Using the WhatIf switch.

$ Add-GPLHeader -Path .\src\ -ProgramName 'AiT' -ProgramDescription 'Another Interval Timer' -Author 'Marc Kassay' -WhatIf

Add-MITHeader

Notice this function doesn't take the ProgramDescription parameter as the Add-GPLHeader requires.

$ Add-MITHeader -Path .\src\ -ProgramName 'AiT' -Author 'Marc Kassay'

Add-Header

When using the generic Add-Header function, I recommend to use "here-string" as shown below. For information on "here-string" visit this about link and goto the section titled "HERE-STRINGS". Below, $MarcsLicense is a header variable that is used with Add-Header. The value passed to Header parameter will be applied to all file types that are predefined, unless either of the following conditions are true:

  • the Include parameter was used. The Include parameter can be used to target files types that are not predefined. If this is used, then the value for Header parameter must be in a form of a comment as it will be used "as-is". To see a session using Add-Header for known and unknown file types see the example file here example/example-1.txt

  • the Set-FileTypeTable function was used. With this function you can essentially remove and add file types along their corresponding comment brackets with Set-BracketTable function. For more information, see these functions below.

$ $MarcsLicense = @"
   AS OF: JAN2018:
   DO NOT: 'SELL', 'TRADE' or 'EXCHANGE' CODE BELOW!!!
"@
$ Add-Header -Path .\src\ -Header $MarcsLicense

Using Add-Header to apply a custom header in a form of a comment to unknown file types that are set in the Include parameter.

$ $MarcsLicense = @"
   %% AS OF: JAN2018:
   %% DO NOT: 'SELL', 'TRADE' or 'EXCHANGE' CODE BELOW!!!
"@
$ Add-Header -Path .\src\ -Header $MarcsLicense -Include '*.m52, *.m53'

Get-FileTypeTable

Outputs the $FileTypeTable. The following is the default value for this variable:

$ Get-FileTypeTable
Name                           Value
----                           -----
5                              *.rb
4                              *.scss, *.css
3                              *.html
2                              *.psm1, *.psd1, *.ps1
1                              *.ts, *.js, *.cp, *.java, *.class

Set-FileTypeTable

You may overwrite the default $FileTypeTable by piping a hashtable to this function.

$ $ProjectFileTypes = @{
 1 = "*.aml, *.bml"
 2 = "*.as, *.bs"
}
$ $ProjectFileTypes | Set-FileTypeTable
$ Get-FileTypeTable
Name                           Value
----                           -----
2                              *.as, *.bs
1                              *.aml, *.bml

In an addition to piping, you can add key value pairs as passing them in as arguments.

$ Set-FileTypeTable 3 "*.txt"
$ Get-FileTypeTable
Name                           Value
----                           -----
3                              *.txt
2                              *.as, *.bs
1                              *.aml, *.bml

Get-BracketTable

Outputs the $BracketTable. The following is the default value for this variable:

$ Get-BracketTable
Name                           Value
----                           -----
5                              =begin,    =end
4                              /*,        */
3                              <!--,      -->
2                              <#,        #>
1                              /**,       */

Set-BracketTable

This has the same behavior as Set-FileTypeTable except the constraint on the value parameter. By analysing the values from a Get-BracketTable call, you may have noticed that opening and closing bracket must be separated by a comma which is required. Also value must be in a string and you cannot modify any existing entries. If needed, you can pipe into this function with another hashtable to overwrite the default $BracketTable.

About

A PowerShell module to prepend boilerplate license headers recursively or to a single file.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published