1 #Region "Microsoft.VisualBasic::0f97ac8f305393a209638ed971d077f4, Microsoft.VisualBasic.Core\Language\Language\UnixBash\FileSystem\PathMapper.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 PathMapper
35     
36     '         Properties: platform
37     
38     '         FunctionGetMapPath, HOME
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.IO
46
47 Namespace Language.UnixBash.FileSystem
48
49     ''' <summary>
50     ''' Maps the linux path to the Windows path.(这个模块是将Linux路径映射为Windows路径的)
51     ''' </summary>
52     Public Module PathMapper
53
54         ''' <summary>
55         ''' Gets a <see cref="System.PlatformID"/> enumeration value that identifies the operating system
56         ''' platform.
57         ''' </summary>
58         ''' <returns></returns>
59         Public ReadOnly Property platform As PlatformID = Environment.OSVersion.Platform
60
61         ''' <summary>
62         ''' Map linux path on Windows:
63         ''' 
64         ''' + [~ -> C:\User\&lt;user_name>]
65         ''' + [# -> <see cref="App.HOME"/>]
66         ''' + [/ -> C:\]
67         ''' + [/usr/bin -> C:\Program Files\]
68         ''' + [/usr -> C:\User\]
69         ''' + [- -> <see cref="App.PreviousDirectory"/>]
70         ''' 
71         ''' </summary>
72         ''' <param name="path"></param>
73         ''' <returns></returns>
74         Public Function GetMapPath(path As StringAs String
75             If platform = PlatformID.MacOSX OrElse platform = PlatformID.Unix Then
76                 Return path
77             End If
78
79             If path.First = "~" Then ' HOME
80                 path = Mid(path, 2)
81
82                 If path.First = "/" Then
83                     path = App.UserHOME & "/" & path
84                 Else
85                     ' ~username
86                     Dim DIR As String = App.UserHOME.ParentPath
87                     path = DIR & "/" & path
88                 End If
89             ElseIf path.First = "#"Then
90                 path = Mid(path, 2)
91                 Return $"{App.HOME}/{path}"
92
93             ElseIf path.First = "/" Then  ' /   ROOT
94                 path = "C:\" & path
95             ElseIf InStr(path, "/usr/bin", CompareMethod.Text) = 1 Then
96                 path = Mid(path, 9)
97                 path = "C:\Program Files/" & path
98             ElseIf InStr(path, "/usr", CompareMethod.Text) = 1 Then
99                 path = Mid(path, 5)
100                 path = App.UserHOME.ParentPath & "/" & path
101             ElseIf InStr(path, "-/") = 1 Then
102                 ' 前一个文件夹
103                 path = Mid(path, 2)
104                 path = App.PreviousDirectory & "/" & path
105             ElseIf path = "-" Then
106                 path = App.PreviousDirectory
107             End If
108
109             Return path
110         End Function
111
112         ''' <summary>
113         ''' Get user home folder
114         ''' </summary>
115         ''' <returns></returns>
116         Public Function HOME() As String
117             If platform = PlatformID.MacOSX OrElse platform = PlatformID.Unix Then
118
119                 ' Fixed error:
120
121                 'Unhandled Exception
122                 'System.TypeInitializationException : The Type initializer for 'Microsoft.VisualBasic.App' threw an exception. ---> Microsoft.VisualBasic.VBDebugger+VisualBasicAppException: @HOME ---> System.Exception: Environment variable error, there is no 'HOMEDRIVE'
123                 '  --- End of inner exception stack trace ---
124                 '  at Microsoft.VisualBasic.VBDebugger.Assertion(Boolean test, System.String msg, System.String calls) < 0x410e6520 + 0x00037> in <filename unknown>:0 
125                 '  at Microsoft.VisualBasic.Language.UnixBash.PathMapper.HOME() < 0x410e62c0 + 0x0009f> in <filename unknown>: 0 
126                 '  at Microsoft.VisualBasic.App..cctor() < 0x410ddfc0 + 0x0017f> in <filename unknown>:0 
127                 '  --- End of inner exception stack trace ---
128                 '  at MathApp.Program.Main() < 0x410ddd60 + 0x0000b> in <filename unknown>: 0 
129                 '[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException : The Type initializer for 'Microsoft.VisualBasic.App' threw an exception. ---> Microsoft.VisualBasic.VBDebugger+VisualBasicAppException: @HOME ---> System.Exception: Environment variable error, there is no 'HOMEDRIVE'
130                 '  --- End of inner exception stack trace ---
131                 '  at Microsoft.VisualBasic.VBDebugger.Assertion(Boolean test, System.String msg, System.String calls) < 0x410e6520 + 0x00037> in <filename unknown>:0 
132                 '  at Microsoft.VisualBasic.Language.UnixBash.PathMapper.HOME() < 0x410e62c0 + 0x0009f> in <filename unknown>: 0 
133                 '  at Microsoft.VisualBasic.App..cctor() < 0x410ddfc0 + 0x0017f> in <filename unknown>:0 
134                 '  --- End of inner exception stack trace ---
135                 '  at MathApp.Program.Main() < 0x410ddd60 + 0x0000b> in <filename unknown>: 0 
136
137                 Return Environment.GetEnvironmentVariable("HOME")
138             End If
139
140             Dim homeDrive As String = Environment.GetEnvironmentVariable("HOMEDRIVE")
141             Dim homePath = Environment.GetEnvironmentVariable("HOMEPATH")
142
143             Call VBDebugger.Assertion(Not String.IsNullOrEmpty(homeDrive), "Environment variable error, there is no 'HOMEDRIVE'")
144             Call VBDebugger.Assertion(Not String.IsNullOrEmpty(homePath), "Environment variable error, there is no 'HOMEPATH'")
145
146             Dim fullHomePath As String = homeDrive & Path.DirectorySeparatorChar & homePath
147             Return fullHomePath
148         End Function
149     End Module
150 End Namespace