|
5 | 5 | using System.Reactive.Linq;
|
6 | 6 | using System.Reflection;
|
7 | 7 | using Microsoft.DotNet.Interactive;
|
8 |
| -using Microsoft.DotNet.Interactive.App.Connection; |
9 | 8 | using Microsoft.DotNet.Interactive.Commands;
|
10 | 9 | using Microsoft.DotNet.Interactive.Connection;
|
11 | 10 | using Microsoft.DotNet.Interactive.Events;
|
@@ -41,7 +40,7 @@ public InteractiveService(string installingDirectory)
|
41 | 40 |
|
42 | 41 | public async Task<bool> StartAsync(string workingDirectory, CancellationToken ct = default)
|
43 | 42 | {
|
44 |
| - this.kernel = await this.CreateKernelAsync(workingDirectory, ct); |
| 43 | + this.kernel = await this.CreateKernelAsync(workingDirectory, true, ct); |
45 | 44 | return true;
|
46 | 45 | }
|
47 | 46 |
|
@@ -84,7 +83,51 @@ public async Task<bool> StartAsync(string workingDirectory, CancellationToken ct
|
84 | 83 | return await this.SubmitCommandAsync(command, ct);
|
85 | 84 | }
|
86 | 85 |
|
87 |
| - private async Task<Kernel> CreateKernelAsync(string workingDirectory, CancellationToken ct = default) |
| 86 | + public bool RestoreDotnetInteractive() |
| 87 | + { |
| 88 | + this.WriteLine("Restore dotnet interactive tool"); |
| 89 | + // write RestoreInteractive.config from embedded resource to this.workingDirectory |
| 90 | + var assembly = Assembly.GetAssembly(typeof(InteractiveService))!; |
| 91 | + var resourceName = "AutoGen.DotnetInteractive.RestoreInteractive.config"; |
| 92 | + using (var stream = assembly.GetManifestResourceStream(resourceName)!) |
| 93 | + using (var fileStream = File.Create(Path.Combine(this.installingDirectory, "RestoreInteractive.config"))) |
| 94 | + { |
| 95 | + stream.CopyTo(fileStream); |
| 96 | + } |
| 97 | + |
| 98 | + // write dotnet-tool.json from embedded resource to this.workingDirectory |
| 99 | + |
| 100 | + resourceName = "AutoGen.DotnetInteractive.dotnet-tools.json"; |
| 101 | + using (var stream2 = assembly.GetManifestResourceStream(resourceName)!) |
| 102 | + using (var fileStream2 = File.Create(Path.Combine(this.installingDirectory, "dotnet-tools.json"))) |
| 103 | + { |
| 104 | + stream2.CopyTo(fileStream2); |
| 105 | + } |
| 106 | + |
| 107 | + var psi = new ProcessStartInfo |
| 108 | + { |
| 109 | + FileName = "dotnet", |
| 110 | + Arguments = $"tool restore --configfile RestoreInteractive.config", |
| 111 | + WorkingDirectory = this.installingDirectory, |
| 112 | + RedirectStandardInput = true, |
| 113 | + RedirectStandardOutput = true, |
| 114 | + RedirectStandardError = true, |
| 115 | + UseShellExecute = false, |
| 116 | + CreateNoWindow = true, |
| 117 | + }; |
| 118 | + |
| 119 | + using var process = new Process { StartInfo = psi }; |
| 120 | + process.OutputDataReceived += this.PrintProcessOutput; |
| 121 | + process.ErrorDataReceived += this.PrintProcessOutput; |
| 122 | + process.Start(); |
| 123 | + process.BeginErrorReadLine(); |
| 124 | + process.BeginOutputReadLine(); |
| 125 | + process.WaitForExit(); |
| 126 | + |
| 127 | + return process.ExitCode == 0; |
| 128 | + } |
| 129 | + |
| 130 | + private async Task<Kernel> CreateKernelAsync(string workingDirectory, bool restoreWhenFail = true, CancellationToken ct = default) |
88 | 131 | {
|
89 | 132 | try
|
90 | 133 | {
|
@@ -139,13 +182,13 @@ await rootProxyKernel.SendAsync(
|
139 | 182 |
|
140 | 183 | return compositeKernel;
|
141 | 184 | }
|
142 |
| - catch (CommandLineInvocationException ex) when (ex.Message.Contains("Cannot find a tool in the manifest file that has a command named 'dotnet-interactive'")) |
| 185 | + catch (CommandLineInvocationException) when (restoreWhenFail) |
143 | 186 | {
|
144 | 187 | var success = this.RestoreDotnetInteractive();
|
145 | 188 |
|
146 | 189 | if (success)
|
147 | 190 | {
|
148 |
| - return await this.CreateKernelAsync(workingDirectory, ct); |
| 191 | + return await this.CreateKernelAsync(workingDirectory, false, ct); |
149 | 192 | }
|
150 | 193 |
|
151 | 194 | throw;
|
@@ -176,50 +219,6 @@ private void WriteLine(string data)
|
176 | 219 | this.Output?.Invoke(this, data);
|
177 | 220 | }
|
178 | 221 |
|
179 |
| - private bool RestoreDotnetInteractive() |
180 |
| - { |
181 |
| - this.WriteLine("Restore dotnet interactive tool"); |
182 |
| - // write RestoreInteractive.config from embedded resource to this.workingDirectory |
183 |
| - var assembly = Assembly.GetAssembly(typeof(InteractiveService))!; |
184 |
| - var resourceName = "AutoGen.DotnetInteractive.RestoreInteractive.config"; |
185 |
| - using (var stream = assembly.GetManifestResourceStream(resourceName)!) |
186 |
| - using (var fileStream = File.Create(Path.Combine(this.installingDirectory, "RestoreInteractive.config"))) |
187 |
| - { |
188 |
| - stream.CopyTo(fileStream); |
189 |
| - } |
190 |
| - |
191 |
| - // write dotnet-tool.json from embedded resource to this.workingDirectory |
192 |
| - |
193 |
| - resourceName = "AutoGen.DotnetInteractive.dotnet-tools.json"; |
194 |
| - using (var stream2 = assembly.GetManifestResourceStream(resourceName)!) |
195 |
| - using (var fileStream2 = File.Create(Path.Combine(this.installingDirectory, "dotnet-tools.json"))) |
196 |
| - { |
197 |
| - stream2.CopyTo(fileStream2); |
198 |
| - } |
199 |
| - |
200 |
| - var psi = new ProcessStartInfo |
201 |
| - { |
202 |
| - FileName = "dotnet", |
203 |
| - Arguments = $"tool restore --configfile RestoreInteractive.config", |
204 |
| - WorkingDirectory = this.installingDirectory, |
205 |
| - RedirectStandardInput = true, |
206 |
| - RedirectStandardOutput = true, |
207 |
| - RedirectStandardError = true, |
208 |
| - UseShellExecute = false, |
209 |
| - CreateNoWindow = true, |
210 |
| - }; |
211 |
| - |
212 |
| - using var process = new Process { StartInfo = psi }; |
213 |
| - process.OutputDataReceived += this.PrintProcessOutput; |
214 |
| - process.ErrorDataReceived += this.PrintProcessOutput; |
215 |
| - process.Start(); |
216 |
| - process.BeginErrorReadLine(); |
217 |
| - process.BeginOutputReadLine(); |
218 |
| - process.WaitForExit(); |
219 |
| - |
220 |
| - return process.ExitCode == 0; |
221 |
| - } |
222 |
| - |
223 | 222 | private void PrintProcessOutput(object sender, DataReceivedEventArgs e)
|
224 | 223 | {
|
225 | 224 | if (!string.IsNullOrEmpty(e.Data))
|
|
0 commit comments