functions/license/New-PSFLicense.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function New-PSFLicense
{
<#
    .SYNOPSIS
        Creates a new license object and registers it
     
    .DESCRIPTION
        This function creates a new license object used by the PSFramework licensing component. The license is automatically registered in the current process' license store.
     
    .PARAMETER Product
        The product that is being licensed
     
    .PARAMETER Manufacturer
        The entity that produced the licensed product
     
    .PARAMETER ProductVersion
        The version of the licensed product
     
    .PARAMETER ProductType
        What kind of product is te license for?
        Options: Module, Script, Library, Application, Other
     
    .PARAMETER Name
        Most licenses used have a name. Feel free to register that name as well.
     
    .PARAMETER Version
        What version is the license?
     
    .PARAMETER Date
        When was the product licensed with the registered license
     
    .PARAMETER Type
        Default: Free
        This shows the usual limitations that apply to this license. By Default, licenses are considered free and may be modified, but require attribution when used in your own product.
     
    .PARAMETER Text
        The full text of your license.
     
    .PARAMETER Description
        A description of the content. Useful when describing how some license is used within your own product.
     
    .PARAMETER Parent
        The license of the product within which the product of this license is used.
     
    .PARAMETER Confirm
        If this switch is enabled, you will be prompted for confirmation before executing any operations that change state.
         
    .PARAMETER WhatIf
        If this switch is enabled, no actions are performed but informational messages will be displayed that explain what would happen if the command were to run.
     
    .EXAMPLE
        PS C:\> New-PSFLicense -Product 'Awesome Test Product' -Manufacturer 'Awesome Inc.' -ProductVersion '1.0.1.0' -ProductType Application -Name FreeBSD -Version "3.0.0.0" -Date (Get-Date -Year 2016 -Month 11 -Day 28 -Hour 0 -Minute 0 -Second 0) -Text @"
        Copyright (c) 2016, Awesome Inc.
        All rights reserved.
 
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
 
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
 
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
        ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
        (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
        LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
        ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
        SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
        The views and conclusions contained in the software and documentation are those
        of the authors and should not be interpreted as representing official policies,
        either expressed or implied, of the FreeBSD Project.
        "@
     
        This registers the Awesome Test Prodct as licensed under the common FreeBSD license.
#>

    
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
    [OutputType([PSFramework.License.License])]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $Product,
        
        [String]
        $Manufacturer = "ACME ltd.",
        
        [Version]
        $ProductVersion = "1.0.0.0",
        
        [Parameter(Mandatory = $true)]
        [PSFramework.License.ProductType]
        $ProductType,
        
        [String]
        $Name = "Unknown",
        
        [Version]
        $Version = "1.0.0.0",
        
        [DateTime]
        $Date = (Get-Date -Year 1989 -Month 10 -Day 3 -Hour 0 -Minute 0 -Second 0),
        
        [PSFramework.License.LicenseType]
        $Type = "Free",
        
        [Parameter(Mandatory = $true)]
        [String]
        $Text,
        
        [string]
        $Description,
        
        [PSFramework.License.License]
        $Parent
    )
    
    # Create and fill object
    $license = New-Object PSFramework.License.License
    $license.Product = $Product
    $license.Manufacturer = $Manufacturer
    $license.ProductVersion = $ProductVersion
    $license.ProductType = $ProductType
    $license.LicenseName = $Name
    $license.LicenseVersion = $Version
    $license.LicenseDate = $Date
    $license.LicenseType = $Type
    $license.LicenseText = $Text
    $license.Description = $Description
    $license.Parent = $Parent
    if ($PSCmdlet.ShouldProcess("$($license.Product) $($license.ProductVersion) ($($license.LicenseName))", "Create License"))
    {
        if (-not ([PSFramework.License.LicenseHost]::Get() | Where-Object { ($_.Product -eq $license.Product) -and ($_.ProductVersion -eq $license.ProductVersion) }))
        {
            [PSFramework.License.LicenseHost]::Add($license)
        }
        
        return $license
    }
}