1 #Region "Microsoft.VisualBasic::a33eeb2ceb20590d35f71ff136dff097, Microsoft.VisualBasic.Core\ApplicationServices\VBDev\XmlDoc\ProjectSpace.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 ProjectSpace
35     
36     '         Constructor: (+1 OverloadsSub New
37     
38     '         Function: EnsureProject, GetEnumerator, GetProject, IEnumerable_GetEnumerator, ToString
39     
40     '         Sub: ImportFromXmlDocFile, ImportFromXmlDocFolder, LoadFile
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 ' Copyright (c) Bendyline LLC. All rights reserved. Licensed under the Apache License, Version 2.0.
48 '    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 
49
50
51 Imports System.IO
52 Imports System.Xml
53 Imports Microsoft.VisualBasic.ApplicationServices.Development.XmlDoc.Serialization
54 Imports Microsoft.VisualBasic.FileIO.FileSystem
55 Imports Microsoft.VisualBasic.Language
56 Imports Microsoft.VisualBasic.Language.UnixBash
57
58 Namespace ApplicationServices.Development.XmlDoc.Assembly
59
60     ''' <summary>
61     ''' A collection of one or more projects put together, and their attendant namespaces.
62     ''' </summary>
63     Public Class ProjectSpace : Implements IEnumerable(Of Project)
64
65         Protected projects As New List(Of Project)
66         Protected handle$
67
68         ReadOnly excludeVBSpecific As Boolean
69
70         Sub New(excludeVBSpecific As Boolean)
71             Me.excludeVBSpecific = excludeVBSpecific
72         End Sub
73
74         Public Overrides Function ToString() As String
75             Return handle
76         End Function
77
78         Public Function GetProject(name As StringAs Project
79             For Each p As Project In Me.projects
80                 If p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) Then
81                     Return p
82                 End If
83             Next
84
85             Return Nothing
86         End Function
87
88         Private Function EnsureProject(name As StringAs Project
89             Dim p As Project = Me.GetProject(name)
90
91             If p Is Nothing Then
92                 p = New Project(name)
93                 Me.projects.Add(p)
94             End If
95
96             Return p
97         End Function
98
99         ''' <summary>
100         ''' Batch imports of <see cref="ImportFromXmlDocFile(String)"/>
101         ''' </summary>
102         ''' <param name="path"></param>
103         Public Sub ImportFromXmlDocFolder(path As String)
104             If Not Directory.Exists(path) Then
105                 Throw New InvalidOperationException(path)
106             Else
107                 handle = path
108             End If
109
110             Dim files = (ls - l - "*.xml" <= path).Select(AddressOf GetFileInfo)
111
112             For Each fi As FileInfo In files
113                 Try
114                     Call Me.LoadFile(fi)
115                     Call $"Loaded {fi}".__DEBUG_ECHO
116                 Catch ex As Exception
117                     ' 可能有其他的不是CLR Assembly XML的文件在这里,忽略掉这个错误
118                     ex = New Exception(fi.FullName, ex)
119                     Call App.LogException(ex)
120                 End Try
121             Next
122         End Sub
123
124         ''' <summary>
125         ''' Imports xdoc document data from a specific xml file.
126         ''' </summary>
127         ''' <param name="path"></param>
128         Public Sub ImportFromXmlDocFile(path As String)
129             If Not path.FileExists Then
130                 Throw New InvalidOperationException(path)
131             Else
132                 handle = path
133                 LoadFile(New FileInfo(path))
134             End If
135         End Sub
136
137         Private Sub LoadFile(fi As FileInfo)
138             Using fs As New FileStream(fi.FullName, FileMode.Open)
139                 Dim streamWriter As New StreamReader(fs)
140                 Dim xml$ = streamWriter.ReadToEnd.TrimAssemblyDoc()
141                 Dim s As New StringReader(xml)
142
143                 Using xr As XmlReader = XmlReader.Create(s)
144                     Dim xd As New XmlDocument()
145
146                     Call xd.Load(xr)
147
148                     Dim nameNode As XmlNode = xd _
149                         .DocumentElement _
150                         .SelectSingleNode("assembly/name")
151
152                     If nameNode IsNot Nothing Then
153                         xml = nameNode.InnerText
154                         Call EnsureProject(xml) _
155                             .ProcessXmlDoc(xd, excludeVBSpecific)
156                     End If
157                 End Using
158             End Using
159         End Sub
160
161         Public Iterator Function GetEnumerator() As IEnumerator(Of Project) Implements IEnumerable(Of Project).GetEnumerator
162             For Each proj As Project In projects
163                 Yield proj
164             Next
165         End Function
166
167         Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
168             Yield GetEnumerator()
169         End Function
170     End Class
171 End Namespace