| 1 | #Region "Microsoft.VisualBasic::e8eebaab4a25fcdee88300088f845699, Microsoft.VisualBasic.Core\CommandLine\Reflection\Grouping.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 | ' Class Grouping |
| 35 | ' |
| 36 | ' Properties: GroupData |
| 37 | ' |
| 38 | ' Constructor: (+1 Overloads) Sub New |
| 39 | ' Function: GetEnumerator, IEnumerable_GetEnumerator, ToString |
| 40 | ' Class Groups |
| 41 | ' |
| 42 | ' Properties: Data |
| 43 | ' |
| 44 | ' Constructor: (+1 Overloads) Sub New |
| 45 | ' |
| 46 | ' |
| 47 | ' |
| 48 | ' |
| 49 | ' /********************************************************************************/ |
| 50 | |
| 51 | #End Region |
| 52 | |
| 53 | Imports Microsoft.VisualBasic.CommandLine.Reflection |
| 54 | Imports Microsoft.VisualBasic.CommandLine.Reflection.EntryPoints |
| 55 | Imports Microsoft.VisualBasic.Linq |
| 56 | Imports Microsoft.VisualBasic.Scripting.TokenIcer.Prefix |
| 57 | Imports Microsoft.VisualBasic.Serialization.JSON |
| 58 | |
| 59 | Namespace CommandLine |
| 60 | |
| 61 | Public Class Grouping : Implements IEnumerable(Of Groups) |
| 62 | |
| 63 | Public Class Groups : Inherits GroupingDefineAttribute |
| 64 | |
| 65 | ''' <summary> |
| 66 | ''' 这个分组之中的API列表 |
| 67 | ''' </summary> |
| 68 | ''' <returns></returns> |
| 69 | Public Property Data As APIEntryPoint() |
| 70 | |
| 71 | Public Sub New(attr As GroupingDefineAttribute) |
| 72 | MyBase.New(attr.Name) |
| 73 | |
| 74 | Description = attr.Description |
| 75 | End Sub |
| 76 | End Class |
| 77 | |
| 78 | Public Property GroupData As Dictionary(Of String, Groups) |
| 79 | |
| 80 | ''' <summary> |
| 81 | ''' |
| 82 | ''' </summary> |
| 83 | ''' <param name="CLI"> |
| 84 | ''' 主要是需要从这个类型定义之中得到<see cref="GroupingDefineAttribute"/>数据 |
| 85 | ''' </param> |
| 86 | Sub New(CLI As Interpreter) |
| 87 | Dim type As Type = CLI.Type |
| 88 | Dim gs = From x As Object |
| 89 | In type.GetCustomAttributes(GetType(GroupingDefineAttribute), True) |
| 90 | Select DirectCast(x, GroupingDefineAttribute) |
| 91 | Dim api = (From x As APIEntryPoint |
| 92 | In CLI.APIList |
| 93 | Let g As GroupAttribute() = x.EntryPoint _ |
| 94 | .GetCustomAttributes(GetType(GroupAttribute), True) _ |
| 95 | .Select(Function(o) DirectCast(o, GroupAttribute)) _ |
| 96 | .ToArray |
| 97 | Select If(g.Length = 0, {New GroupAttribute(undefined)}, g) _ |
| 98 | .Select(Function(gx) New With {gx, x})) _ |
| 99 | .IteratesALL _ |
| 100 | .GroupBy(Function(x) x.gx.Name) _ |
| 101 | .ToDictionary(Function(x) x.Key, |
| 102 | Function(x) |
| 103 | Return x.Select(Function(o) o.x).ToArray |
| 104 | End Function) |
| 105 | |
| 106 | GroupData = New Dictionary(Of String, Groups) |
| 107 | |
| 108 | For Each g As GroupingDefineAttribute In gs |
| 109 | If api.ContainsKey(g.Name) Then |
| 110 | Dim apiList As APIEntryPoint() = api(g.Name) |
| 111 | |
| 112 | Call GroupData.Add( |
| 113 | g.Name, New Groups(g) With { |
| 114 | .Data = apiList |
| 115 | }) |
| 116 | Call api.Remove(g.Name) |
| 117 | Else |
| 118 | #If DEBUG Then |
| 119 | Call $"No data found for grouping {g.GetJson}".Warning |
| 120 | #End If |
| 121 | End If |
| 122 | Next |
| 123 | |
| 124 | If api.Count > 0 Then |
| 125 | For Each g In api |
| 126 | Dim gK As New GroupingDefineAttribute(g.Key) |
| 127 | |
| 128 | Call GroupData.Add( |
| 129 | g.Key, New Groups(gK) With { |
| 130 | .Data = g.Value |
| 131 | }) |
| 132 | Next |
| 133 | |
| 134 | Call GroupData.SortByKey |
| 135 | End If |
| 136 | End Sub |
| 137 | |
| 138 | Public Overrides Function ToString() As String |
| 139 | Return GroupData.Keys.ToArray.GetJson |
| 140 | End Function |
| 141 | |
| 142 | Public Iterator Function GetEnumerator() As IEnumerator(Of Groups) Implements IEnumerable(Of Groups).GetEnumerator |
| 143 | For Each x In GroupData.Values |
| 144 | Yield x |
| 145 | Next |
| 146 | End Function |
| 147 | |
| 148 | Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator |
| 149 | Yield GetEnumerator() |
| 150 | End Function |
| 151 | End Class |
| 152 | End Namespace |