1 #Region "Microsoft.VisualBasic::a1c14c20f2acf914567e924bd797f5b1, Microsoft.VisualBasic.Core\ApplicationServices\VBDev\Signature\LicenseMgr.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms Of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 Of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed In the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy Of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module LicenseMgr
35     
36     '         Properties: Ignores, Template
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Function: AddRegion, Insert, Inserts, RemoveRegion
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Runtime.CompilerServices
47 Imports System.Text
48 Imports System.Text.RegularExpressions
49 Imports Microsoft.VisualBasic.Language
50 Imports Microsoft.VisualBasic.Language.UnixBash
51 Imports Microsoft.VisualBasic.Linq
52 Imports Microsoft.VisualBasic.Text.Xml.Models
53
54 Namespace ApplicationServices.Development
55
56     ''' <summary>
57     ''' Source code license manager
58     ''' </summary>
59     Public Module LicenseMgr
60
61         ''' <summary>
62         ''' The license template example.
63         ''' </summary>
64         ''' <returns></returns>
65         Public ReadOnly Property Template As LicenseInfo
66             <MethodImpl(MethodImplOptions.AggressiveInlining)>
67             Get
68                 Return New LicenseInfo With {
69                     .Authors = {
70                         New NamedValue With {
71                             .name = "asuka",
72                             .text = "amethyst.asuka@gcmodeller.org"
73                         }
74                     },
75                     .Brief = "Permission is hereby granted, free Of charge, To any person obtaining a copy
76 Of this software and associated documentation files (the ""Software""), To deal
77 In the Software without restriction, including without limitation the rights
78 To use, copy, modify, merge, publish, distribute, sublicense, and/or sell
79 copies Of the Software, and To permit persons To whom the Software is
80 furnished To do so, subject To the following conditions:
81
82 The above copyright notice and this permission notice shall be included in
83 all copies or substantial portions Of the Software.
84
85 THE SOFTWARE IS PROVIDED ""As Is"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
86 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
87 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
88 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
89 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
90 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
91 THE SOFTWARE.",
92                     .Copyright = "Copyright (c) 2016 amethyst.asuka@gcmodeller.org",
93                     .Title = "The MIT License (MIT)"
94             }
95             End Get
96         End Property
97
98         Sub New()
99             Call Template.GetXml.SaveTo(App.HOME & "/License.Template.Xml")
100         End Sub
101
102         ''' <summary>
103         ''' Enumerates all Of the VisualStudio IDE auTo generated source file.
104         ''' 
105         ''' ```json
106         ''' {".+\.Designer\.vb", "AssemblyInfo\.vb"}
107         ''' ```
108         ''' </summary>
109         ''' <returns></returns>
110         Public Property Ignores As String() = {".+\.Designer\.vb""AssemblyInfo\.vb"}
111
112         ''' <summary>
113         ''' 
114         ''' </summary>
115         ''' <param name="src">*.vb path</param>
116         ''' <param name="info">License meta data</param>
117         ''' <returns></returns>
118         <Extension>
119         Public Function Insert(src As String, info As LicenseInfo, rootDir$) As Boolean
120             Dim file As String = PathExtensions.RelativePath(rootDir, src, appendParent:=False)
121             Dim [in] As String = src.ReadAllText
122             Dim path As String = src
123
124             src = Trim([in])
125             src = RemoveRegion(src)
126             src = AddRegion(src, info, file)
127
128             Try
129                 Return src.SaveTo(path, Encoding.UTF8)
130             Catch ex As Exception
131                 Dim backup As String = src & ".bak"
132
133                 ex = New Exception(path, ex)
134
135                 Call [in].SaveTo(backup, Encoding.UTF8)
136                 Call App.LogException(ex)
137
138                 Return False
139             End Try
140         End Function
141
142         Public Function RemoveRegion(src As StringAs String
143             Dim region As String = Regex.Match(src, LicenseMgr.Region, RegexOptions.Singleline).Value
144
145             If Not String.IsNullOrEmpty(region) Then
146                 src = src.Replace(region, "")
147                 src = src.Trim(vbCr, vbLf)
148             End If
149
150             Return src
151         End Function
152
153         Public Function AddRegion(src As String, info As LicenseInfo, file As StringAs String
154             Dim sb As New StringBuilder
155
156             Call sb.AppendLine($"#Region ""Microsoft.VisualBasic::{SecurityString.GetMd5Hash(src)}, {file}""")
157             Call sb.AppendLine()
158             Call sb.AppendLine("    ' Author:")
159             Call sb.AppendLine("    ' ")
160
161             For Each author As NamedValue In info.Authors.SafeQuery
162                 Call sb.AppendLine($"    '       {author.name} ({author.text})")
163             Next
164             Call sb.AppendLine("    ' ")
165             Call sb.AppendLine("    ' " & info.Copyright)
166             Call sb.AppendLine("    ' ")
167             Call sb.AppendLine("    ' ")
168             Call sb.AppendLine("    ' " & info.Title)
169             Call sb.AppendLine("    ' ")
170
171             For Each line As String In info.Brief.LineTokens
172                 Call sb.AppendLine("    ' " & line)
173             Next
174
175             sb.AppendLine()
176             sb.AppendLine()
177             sb.AppendLine()
178             sb.AppendLine("    ' /********************************************************************************/")
179             sb.AppendLine()
180             sb.AppendLine("    ' Summaries:")
181             sb.AppendLine()
182
183             For Each line As String In VBCodeSignature.SummaryModules(vb:=src).LineTokens
184                 Call sb.AppendLine("    ' " & line)
185             Next
186
187             sb.AppendLine("    ' /********************************************************************************/")
188             Call sb.AppendLine()
189             Call sb.AppendLine($"#End Region")
190             Call sb.AppendLine()
191
192             Call sb.AppendLine(src)
193
194             Return sb.ToString
195         End Function
196
197         Const Region As String = "^#Region "".+?\.vb"".+?#End Region"
198
199         ''' <summary>
200         ''' 
201         ''' </summary>
202         ''' <param name="info">The root directory</param>
203         ''' <returns></returns>
204         Public Function Inserts(info As LicenseInfo, rootDir$) As String()
205             Dim fails As New List(Of String)
206
207             For Each vb As String In ls - l - r - "*.vb" <= rootDir
208                 Dim skip As Boolean = False
209
210                 ' ignores all Of the IDE auTo generated source file.
211                 For Each ig As String In LicenseMgr.Ignores
212                     If Regex.Match(vb, ig, RegexICSng).Success Then
213                         skip = True
214                         Exit For
215                     End If
216                 Next
217
218                 If skip Then
219                     Continue For
220                 End If
221
222                 If Not vb.Insert(info, rootDir) Then
223                     fails += vb
224                 End If
225             Next
226
227             Return fails
228         End Function
229     End Module
230 End Namespace