Skip to content

Commit

Permalink
Add TemplateResult type to pass along creation results to caller
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daviwil committed Jan 18, 2017
1 parent 2037bd1 commit 21f3f8a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static readonly
public class NewProjectFromTemplateResponse
{
public bool CreationSuccessful { get; set; }

public string DestinationPath { get; set; }
}

public class GetProjectTemplatesRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ private Task HandleNewProjectFromTemplateRequest(
await requestContext.SendResult(
new NewProjectFromTemplateResponse
{
CreationSuccessful = task.Result
CreationSuccessful = task.Result.IsSuccess,
DestinationPath = task.Result.DestinationPath
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Extensions\IEditorOperations.cs" />
<Compile Include="Templates\TemplateService.cs" />
<Compile Include="Templates\TemplateDetails.cs" />
<Compile Include="Templates\TemplateResult.cs" />
<Compile Include="Language\AstOperations.cs" />
<Compile Include="Language\CommandHelpers.cs" />
<Compile Include="Language\CompletionResults.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<Compile Include="Session\SessionPSHostUserInterface.cs" />
<Compile Include="Session\SessionStateChangedEventArgs.cs" />
<Compile Include="Templates\TemplateDetails.cs" />
<Compile Include="Templates\TemplateResult.cs" />
<Compile Include="Templates\TemplateService.cs" />
<Compile Include="Utility\AsyncContextThread.cs" />
<Compile Include="Utility\AsyncDebouncer.cs" />
Expand Down
45 changes: 45 additions & 0 deletions src/PowerShellEditorServices/Templates/TemplateResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices.Templates
{
/// <summary>
/// Provides details about the result of the creation of a file
/// or project from a template.
/// </summary>
public class TemplateResult
{
/// <summary>
/// Gets or sets a boolean which is true if creation was successful.
/// </summary>
public bool IsSuccess { get; set; }

/// <summary>
/// Gets or sets the template path which was used in creation.
/// </summary>
public string TemplatePath { get; set; }

/// <summary>
/// Gets or sets the destination path where the file (or files) were created.
/// </summary>
public string DestinationPath { get; set; }

/// <summary>
/// Gets or sets the array of file paths that were created.
/// </summary>
public string[] CreatedFiles { get; set; }

/// <summary>
/// Gets or sets the array of file paths that were updated.
/// </summary>
public string[] UpdatedFiles { get; set; }

/// <summary>
/// Gets or sets the list of modules that will need to be installed for
/// the created file or project to be fully functional.
/// </summary>
public string[] MissingModules { get; set; }
}
}
46 changes: 35 additions & 11 deletions src/PowerShellEditorServices/Templates/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,27 @@ await this.powerShellContext.ExecuteCommand<PSObject>(
/// </summary>
/// <param name="templatePath">The folder path containing the template.</param>
/// <param name="destinationPath">The folder path where the files will be created.</param>
/// <returns>A boolean-returning Task which communicates success or failure.</returns>
public async Task<bool> CreateFromTemplate(
/// <returns>A TemplateResult object with details about the created file or files.</returns>
public async Task<TemplateResult> CreateFromTemplate(
string templatePath,
string destinationPath)
{
Logger.Write(
LogLevel.Verbose,
$"Invoking Plaster...\n\n TemplatePath: {templatePath}\n DestinationPath: {destinationPath}");

PSCommand command = new PSCommand();
command.AddCommand("Invoke-Plaster");
command.AddParameter("TemplatePath", templatePath);
command.AddParameter("DestinationPath", destinationPath);
PSCommand psCommand = new PSCommand();
psCommand
.AddCommand("Invoke-Plaster")
.AddParameter("TemplatePath", templatePath)
.AddParameter("DestinationPath", destinationPath)
.AddParameter("PassThru");

var errorString = new System.Text.StringBuilder();
await this.powerShellContext.ExecuteCommand<PSObject>(
command, errorString, false, true);
var templateResult =
(await this.powerShellContext.ExecuteCommand<PSObject>(
psCommand, false, true)).FirstOrDefault();

// If any errors were written out, creation was not successful
return errorString.Length == 0;
return CreateTemplateResult(templateResult);
}

#endregion
Expand All @@ -202,6 +203,29 @@ private static TemplateDetails CreateTemplateDetails(PSObject psObject)
};
}

private static TemplateResult CreateTemplateResult(PSObject psObject)
{
if (psObject != null)
{
return new TemplateResult
{
IsSuccess = (bool)psObject.Members["Success"].Value,
TemplatePath = (string)psObject.Members["TemplatePath"].Value,
DestinationPath = (string)psObject.Members["DestinationPath"].Value,
CreatedFiles = (string[])psObject.Members["CreatedFiles"].Value,
UpdatedFiles = (string[])psObject.Members["UpdatedFiles"].Value,
MissingModules = (string[])psObject.Members["MissingModules"].Value
};
}
else
{
return new TemplateResult
{
IsSuccess = false
};
}
}

#endregion
}
}

0 comments on commit 21f3f8a

Please sign in to comment.