| 1 | #Region "Microsoft.VisualBasic::8bfe84fb501b81f8d91b8612e4adce17, Microsoft.VisualBasic.Core\CommandLine\InteropService\SharedORM\CodeGenerator.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 CodeGenerator |
| 35 | ' |
| 36 | ' Constructor: (+2 Overloads) Sub New |
| 37 | ' Function: EnumeratesAPI, GetManualPage |
| 38 | ' |
| 39 | ' Class APITuple |
| 40 | ' |
| 41 | ' Properties: API, CLI |
| 42 | ' |
| 43 | ' |
| 44 | ' /********************************************************************************/ |
| 45 | |
| 46 | #End Region |
| 47 | |
| 48 | Imports System.Reflection |
| 49 | Imports System.Runtime.CompilerServices |
| 50 | Imports Microsoft.VisualBasic.CommandLine.ManView |
| 51 | Imports Microsoft.VisualBasic.CommandLine.Reflection.EntryPoints |
| 52 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
| 53 | Imports Microsoft.VisualBasic.Language |
| 54 | |
| 55 | Namespace CommandLine.InteropService.SharedORM |
| 56 | |
| 57 | Public MustInherit Class CodeGenerator |
| 58 | |
| 59 | Protected ReadOnly App As Interpreter |
| 60 | ''' <summary> |
| 61 | ''' 目标应用程序模块的文件名,不包含有文件拓展名 |
| 62 | ''' </summary> |
| 63 | Protected ReadOnly exe$ |
| 64 | |
| 65 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 66 | Sub New(CLI As Type) |
| 67 | Call Me.New(New Interpreter(type:=CLI)) |
| 68 | End Sub |
| 69 | |
| 70 | Sub New(App As Interpreter) |
| 71 | Me.App = App |
| 72 | Me.exe = App.Type _ |
| 73 | .Assembly _ |
| 74 | .CodeBase _ |
| 75 | .BaseName |
| 76 | End Sub |
| 77 | |
| 78 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
| 79 | Public Function GetManualPage() As String |
| 80 | Return App.HelpSummary(markdown:=False) |
| 81 | End Function |
| 82 | |
| 83 | Public MustOverride Function GetSourceCode() As String |
| 84 | |
| 85 | Public Iterator Function EnumeratesAPI() As IEnumerable(Of APITuple) |
| 86 | Dim help$ |
| 87 | Dim CLI As NamedValue(Of CommandLine) |
| 88 | |
| 89 | For Each api As APIEntryPoint In App.APIList |
| 90 | ' 2018-11-02 usage是空的,说明可能没有额外的参数,只需要调用命令即可 |
| 91 | ' 在这里Usage可能是空值 |
| 92 | Dim apiUsage$ = api.Usage Or EmptyString |
| 93 | Dim usageCommand As CommandLine |
| 94 | |
| 95 | If apiUsage.StringEmpty Then |
| 96 | usageCommand = New CommandLine With { |
| 97 | .Name = api.Name, |
| 98 | .BoolFlags = {}, |
| 99 | .cliCommandArgvs = api.Name, |
| 100 | .SingleValue = api.Name, |
| 101 | .Tokens = {api.Name}, |
| 102 | .__arguments = New List(Of NamedValue(Of String)) |
| 103 | } |
| 104 | Call $"{api.EntryPointFullName(relativePath:=True)} is nothing!".Warning |
| 105 | Else |
| 106 | usageCommand = apiUsage.CommandLineModel |
| 107 | End If |
| 108 | |
| 109 | Try |
| 110 | help = |
| 111 | $"``` |
| 112 | {apiUsage.Replace("<", "<")} |
| 113 | ```" & vbCrLf & api.Info |
| 114 | |
| 115 | CLI = New NamedValue(Of CommandLine) With { |
| 116 | .Name = api.EntryPoint.Name, |
| 117 | .Description = help, |
| 118 | .Value = usageCommand |
| 119 | } |
| 120 | |
| 121 | Yield New APITuple With { |
| 122 | .CLI = CLI, |
| 123 | .API = api.EntryPoint |
| 124 | } |
| 125 | Catch ex As Exception |
| 126 | ex = New Exception(api.EntryPointFullName(False), ex) |
| 127 | Throw ex |
| 128 | End Try |
| 129 | Next |
| 130 | End Function |
| 131 | End Class |
| 132 | |
| 133 | Public Class APITuple |
| 134 | |
| 135 | Public Property CLI As NamedValue(Of CommandLine) |
| 136 | Public Property API As MethodInfo |
| 137 | |
| 138 | End Class |
| 139 | End Namespace |