Skip to content

Commit 21f3f8a

Browse files
committed
Add TemplateResult type to pass along creation results to caller
This change fixes PowerShell/vscode-powershell#390 which states that the New Project experience in VS Code doesn't work well with project paths which use characters like '~' to represent the home directory. This change causes the fully resolved DestinationPath of the template creation to be passed back to the editor so that it can be opened.
1 parent 2037bd1 commit 21f3f8a

File tree

6 files changed

+86
-12
lines changed

6 files changed

+86
-12
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/ProjectTemplate.cs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public static readonly
2222
public class NewProjectFromTemplateResponse
2323
{
2424
public bool CreationSuccessful { get; set; }
25+
26+
public string DestinationPath { get; set; }
2527
}
2628

2729
public class GetProjectTemplatesRequest

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ private Task HandleNewProjectFromTemplateRequest(
322322
await requestContext.SendResult(
323323
new NewProjectFromTemplateResponse
324324
{
325-
CreationSuccessful = task.Result
325+
CreationSuccessful = task.Result.IsSuccess,
326+
DestinationPath = task.Result.DestinationPath
326327
});
327328
});
328329

src/PowerShellEditorServices/Nano.PowerShellEditorServices.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="Extensions\IEditorOperations.cs" />
7878
<Compile Include="Templates\TemplateService.cs" />
7979
<Compile Include="Templates\TemplateDetails.cs" />
80+
<Compile Include="Templates\TemplateResult.cs" />
8081
<Compile Include="Language\AstOperations.cs" />
8182
<Compile Include="Language\CommandHelpers.cs" />
8283
<Compile Include="Language\CompletionResults.cs" />

src/PowerShellEditorServices/PowerShellEditorServices.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Session\SessionPSHostUserInterface.cs" />
131131
<Compile Include="Session\SessionStateChangedEventArgs.cs" />
132132
<Compile Include="Templates\TemplateDetails.cs" />
133+
<Compile Include="Templates\TemplateResult.cs" />
133134
<Compile Include="Templates\TemplateService.cs" />
134135
<Compile Include="Utility\AsyncContextThread.cs" />
135136
<Compile Include="Utility\AsyncDebouncer.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.PowerShell.EditorServices.Templates
7+
{
8+
/// <summary>
9+
/// Provides details about the result of the creation of a file
10+
/// or project from a template.
11+
/// </summary>
12+
public class TemplateResult
13+
{
14+
/// <summary>
15+
/// Gets or sets a boolean which is true if creation was successful.
16+
/// </summary>
17+
public bool IsSuccess { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the template path which was used in creation.
21+
/// </summary>
22+
public string TemplatePath { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the destination path where the file (or files) were created.
26+
/// </summary>
27+
public string DestinationPath { get; set; }
28+
29+
/// <summary>
30+
/// Gets or sets the array of file paths that were created.
31+
/// </summary>
32+
public string[] CreatedFiles { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets the array of file paths that were updated.
36+
/// </summary>
37+
public string[] UpdatedFiles { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets the list of modules that will need to be installed for
41+
/// the created file or project to be fully functional.
42+
/// </summary>
43+
public string[] MissingModules { get; set; }
44+
}
45+
}

src/PowerShellEditorServices/Templates/TemplateService.cs

+35-11
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,27 @@ await this.powerShellContext.ExecuteCommand<PSObject>(
158158
/// </summary>
159159
/// <param name="templatePath">The folder path containing the template.</param>
160160
/// <param name="destinationPath">The folder path where the files will be created.</param>
161-
/// <returns>A boolean-returning Task which communicates success or failure.</returns>
162-
public async Task<bool> CreateFromTemplate(
161+
/// <returns>A TemplateResult object with details about the created file or files.</returns>
162+
public async Task<TemplateResult> CreateFromTemplate(
163163
string templatePath,
164164
string destinationPath)
165165
{
166166
Logger.Write(
167167
LogLevel.Verbose,
168168
$"Invoking Plaster...\n\n TemplatePath: {templatePath}\n DestinationPath: {destinationPath}");
169169

170-
PSCommand command = new PSCommand();
171-
command.AddCommand("Invoke-Plaster");
172-
command.AddParameter("TemplatePath", templatePath);
173-
command.AddParameter("DestinationPath", destinationPath);
170+
PSCommand psCommand = new PSCommand();
171+
psCommand
172+
.AddCommand("Invoke-Plaster")
173+
.AddParameter("TemplatePath", templatePath)
174+
.AddParameter("DestinationPath", destinationPath)
175+
.AddParameter("PassThru");
174176

175-
var errorString = new System.Text.StringBuilder();
176-
await this.powerShellContext.ExecuteCommand<PSObject>(
177-
command, errorString, false, true);
177+
var templateResult =
178+
(await this.powerShellContext.ExecuteCommand<PSObject>(
179+
psCommand, false, true)).FirstOrDefault();
178180

179-
// If any errors were written out, creation was not successful
180-
return errorString.Length == 0;
181+
return CreateTemplateResult(templateResult);
181182
}
182183

183184
#endregion
@@ -202,6 +203,29 @@ private static TemplateDetails CreateTemplateDetails(PSObject psObject)
202203
};
203204
}
204205

206+
private static TemplateResult CreateTemplateResult(PSObject psObject)
207+
{
208+
if (psObject != null)
209+
{
210+
return new TemplateResult
211+
{
212+
IsSuccess = (bool)psObject.Members["Success"].Value,
213+
TemplatePath = (string)psObject.Members["TemplatePath"].Value,
214+
DestinationPath = (string)psObject.Members["DestinationPath"].Value,
215+
CreatedFiles = (string[])psObject.Members["CreatedFiles"].Value,
216+
UpdatedFiles = (string[])psObject.Members["UpdatedFiles"].Value,
217+
MissingModules = (string[])psObject.Members["MissingModules"].Value
218+
};
219+
}
220+
else
221+
{
222+
return new TemplateResult
223+
{
224+
IsSuccess = false
225+
};
226+
}
227+
}
228+
205229
#endregion
206230
}
207231
}

0 commit comments

Comments
 (0)