1
1
Imports System.IO
2
- Imports System.Runtime.InteropServices.ComTypes
3
- Imports DeveloperCore.REPL
2
+ Imports System.Threading
4
3
Imports Microsoft.CodeAnalysis
5
4
Imports Microsoft.CodeAnalysis.Emit
6
5
Imports Microsoft.CodeAnalysis.VisualBasic
7
6
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
7
+ Imports NuGet.Common
8
+ Imports NuGet.Configuration
9
+ Imports NuGet.PackageManagement
10
+ Imports NuGet.Packaging
11
+ Imports NuGet.Packaging.Core
12
+ Imports NuGet.ProjectManagement
13
+ Imports NuGet.Protocol.Core.Types
14
+ Imports NuGet.Resolver
8
15
'TODO: Multiple statements
9
16
Public Class REPL
10
17
Private _imports As New List( Of ImportsStatementSyntax)
11
18
Private _state As New Dictionary( Of String , Object )
12
19
Private _references As New List( Of MetadataReference)
13
20
Private _trees As New List( Of SyntaxTree)
21
+ Private ReadOnly _nugetCache As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DeveloperCore.REPL" , "NuGetCache" )
14
22
15
23
Public Function Evaluate(str As String ) As EvaluationResults
16
24
Dim newStatement As StatementSyntax = SyntaxCreator.ParseStatement(str)
@@ -104,33 +112,143 @@ Public Class REPL
104
112
Return result
105
113
End Function
106
114
107
- Public Sub AddReference(ref As String )
115
+ Public Async Function AddReference(ref As String ) As Task
108
116
If IsValidPath(ref) AndAlso File.Exists(ref) Then
109
117
Select Case Path.GetExtension(ref)
110
118
Case ".dll" , ".exe"
111
119
_references.Add(MetadataReference.CreateFromFile(ref))
112
- Case ".vbproj" , ".csproj"
113
- 'TODO: Project references
114
120
Case ".vb"
115
121
_trees.Add(SyntaxFactory.ParseSyntaxTree(File.ReadAllText(ref)))
116
122
End Select
117
- Else
118
- 'TODO: NuGet
123
+ ElseIf ref.StartsWith( "nuget:" ) Then
124
+ 'TODO: Add package selector
125
+ Directory.CreateDirectory(_nugetCache)
126
+ Dim parts As String () = ref.Split( ":"c )
127
+ Dim name As String = parts( 1 ).Trim
128
+ Dim version As String = If (parts.Length > 2 , parts( 2 ).Trim, "" )
129
+ Dim providers As New List( Of Lazy( Of INuGetResourceProvider))
130
+ providers.AddRange(Repository.Provider.GetCoreV3())
131
+ Dim sourceRepository As New SourceRepository( New PackageSource( "https://api.nuget.org/v3/index.json" ), providers)
132
+ Dim log As New Logger
133
+ Dim nugetSettings As Settings = Settings.LoadDefaultSettings(_nugetCache)
134
+ Dim sourceProvider As New PackageSourceProvider(nugetSettings)
135
+ Dim repositoryProvider As New SourceRepositoryProvider(sourceProvider, providers)
136
+ Dim project As New FolderNuGetProject(_nugetCache)
137
+ Dim manager As New NuGetPackageManager(repositoryProvider, nugetSettings, _nugetCache) With {.PackagesFolderNuGetProject = project}
138
+ Dim searchSource As PackageSearchResource = sourceRepository.GetResource( Of PackageSearchResource)()
139
+ Dim package As IPackageSearchMetadata = ( Await searchSource.SearchAsync(name, New SearchFilter( True ) With {.IncludeDelisted = False , .SupportedFrameworks = { "netstandard2.0" }}, 0 , 10 , log, CancellationToken.None)).FirstOrDefault
140
+ If package Is Nothing Then Throw New Exception( $"Package {name} not found" )
141
+ Dim prerelease As Boolean = True
142
+ Dim unListed As Boolean = False
143
+ Dim resolutionContext As New ResolutionContext(DependencyBehavior.Lowest, prerelease, unListed, VersionConstraints.None)
144
+ Dim context As New ProjectContext
145
+ Dim identity As New PackageIdentity(package.Identity.Id, package.Identity.Version)
146
+ Await manager.InstallPackageAsync(project, identity, resolutionContext, context, sourceRepository, {}, CancellationToken.None)
119
147
End If
120
- End Sub
148
+ End Function
121
149
122
- Private Shared Function IsValidPath(path As String ) As String
150
+ Private Shared Function IsValidPath(path As String ) As Boolean
123
151
Dim fi As FileInfo = Nothing
124
152
Try
125
153
fi = New FileInfo(path)
126
154
Catch __unusedArgumentException1__ As ArgumentException
127
155
Catch __unusedPathTooLongException2__ As PathTooLongException
128
156
Catch __unusedNotSupportedException3__ As NotSupportedException
129
157
End Try
130
- If ReferenceEquals(fi, Nothing ) Then
131
- Return False
132
- Else
133
- Return True
134
- End If
158
+ Return If (fi Is Nothing , False , True )
135
159
End Function
160
+ End Class
161
+
162
+ Public Class Logger
163
+ Implements ILogger
164
+ Private logs As New List( Of String )()
165
+
166
+ Public Sub LogDebug(data As String ) Implements ILogger.LogDebug
167
+ logs.Add(data)
168
+ End Sub
169
+
170
+ Public Sub LogVerbose(data As String ) Implements ILogger.LogVerbose
171
+ logs.Add(data)
172
+ End Sub
173
+
174
+ Public Sub LogInformation(data As String ) Implements ILogger.LogInformation
175
+ logs.Add(data)
176
+ End Sub
177
+
178
+ Public Sub LogMinimal(data As String ) Implements ILogger.LogMinimal
179
+ logs.Add(data)
180
+ End Sub
181
+
182
+ Public Sub LogWarning(data As String ) Implements ILogger.LogWarning
183
+ logs.Add(data)
184
+ End Sub
185
+
186
+ Public Sub LogError(data As String ) Implements ILogger.LogError
187
+ logs.Add(data)
188
+ End Sub
189
+
190
+ Public Sub LogInformationSummary(data As String ) Implements ILogger.LogInformationSummary
191
+ logs.Add(data)
192
+ End Sub
193
+
194
+ Public Sub Log(level As LogLevel, data As String ) Implements ILogger.Log
195
+ logs.Add(data)
196
+ End Sub
197
+
198
+ Public Sub Log(message As ILogMessage) Implements ILogger.Log
199
+ logs.Add(message.Message)
200
+ End Sub
201
+
202
+ Public Function LogAsync(level As LogLevel, data As String ) As Task Implements ILogger.LogAsync
203
+ logs.Add(data)
204
+ Return Task.CompletedTask
205
+ End Function
206
+
207
+ Public Function LogAsync(message As ILogMessage) As Task Implements ILogger.LogAsync
208
+ logs.Add(message.Message)
209
+ Return Task.CompletedTask
210
+ End Function
211
+ End Class
212
+
213
+ Public Class ProjectContext
214
+ Implements INuGetProjectContext
215
+ Private logs As New List( Of String )()
216
+
217
+ Public Function GetLogs() As List( Of String )
218
+ Return logs
219
+ End Function
220
+
221
+ Public Sub Log(level As MessageLevel, message As String , ParamArray args As Object ()) Implements INuGetProjectContext.Log
222
+ Dim formattedMessage = String .Format(message, args)
223
+ logs.Add(formattedMessage)
224
+ End Sub
225
+
226
+ Public Function ResolveFileConflict(message As String ) As FileConflictAction Implements INuGetProjectContext.ResolveFileConflict
227
+ logs.Add(message)
228
+ Return FileConflictAction.Ignore
229
+ End Function
230
+
231
+ Public Property PackageExtractionContext As PackageExtractionContext Implements INuGetProjectContext.PackageExtractionContext
232
+
233
+ Public ReadOnly Property ExecutionContext As NuGet.ProjectManagement.ExecutionContext Implements INuGetProjectContext.ExecutionContext
234
+
235
+ Public Property OriginalPackagesConfig As XDocument Implements INuGetProjectContext.OriginalPackagesConfig
236
+
237
+ Public Property SourceControlManagerProvider As ISourceControlManagerProvider Implements INuGetProjectContext.SourceControlManagerProvider
238
+
239
+ Public Sub ReportError(message As String ) Implements INuGetProjectContext.ReportError
240
+ logs.Add(message)
241
+ End Sub
242
+
243
+ Public Sub Log(message As ILogMessage) Implements INuGetProjectContext.Log
244
+ Log(message.Level, message.Message)
245
+ End Sub
246
+
247
+ Public Sub ReportError(message As ILogMessage) Implements INuGetProjectContext.ReportError
248
+ ReportError(message.Message)
249
+ End Sub
250
+
251
+ Public Property ActionType As NuGetActionType Implements INuGetProjectContext.ActionType
252
+
253
+ Public Property OperationId As Guid Implements INuGetProjectContext.OperationId
136
254
End Class
0 commit comments