Skip to content

Enable saving remote files opened with psedit #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public static readonly
EventType<TextDocumentIdentifier>.Create("textDocument/didClose");
}

public class DidSaveTextDocumentNotification
{
public static readonly
EventType<DidSaveTextDocumentParams> Type =
EventType<DidSaveTextDocumentParams>.Create("textDocument/didSave");
}

public class DidSaveTextDocumentParams
{
public TextDocumentIdentifier TextDocument { get; set; }
}

public class DidChangeTextDocumentNotification
{
public static readonly
Expand Down
18 changes: 18 additions & 0 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ protected override void Initialize()

this.SetEventHandler(DidOpenTextDocumentNotification.Type, this.HandleDidOpenTextDocumentNotification);
this.SetEventHandler(DidCloseTextDocumentNotification.Type, this.HandleDidCloseTextDocumentNotification);
this.SetEventHandler(DidSaveTextDocumentNotification.Type, this.HandleDidSaveTextDocumentNotification);
this.SetEventHandler(DidChangeTextDocumentNotification.Type, this.HandleDidChangeTextDocumentNotification);
this.SetEventHandler(DidChangeConfigurationNotification<LanguageServerSettingsWrapper>.Type, this.HandleDidChangeConfigurationNotification);

Expand Down Expand Up @@ -516,6 +517,23 @@ protected async Task HandleDidCloseTextDocumentNotification(

Logger.Write(LogLevel.Verbose, "Finished closing document.");
}
protected async Task HandleDidSaveTextDocumentNotification(
DidSaveTextDocumentParams saveParams,
EventContext eventContext)
{
ScriptFile savedFile =
this.editorSession.Workspace.GetFile(
saveParams.TextDocument.Uri);

if (savedFile != null)
{
if (this.editorSession.RemoteFileManager.IsUnderRemoteTempPath(savedFile.FilePath))
{
await this.editorSession.RemoteFileManager.SaveRemoteFile(
savedFile.FilePath);
}
}
}

protected Task HandleDidChangeTextDocumentNotification(
DidChangeTextDocumentParams textChangeParams,
Expand Down
65 changes: 65 additions & 0 deletions src/PowerShellEditorServices/Session/RemoteFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Session
Expand Down Expand Up @@ -77,6 +78,15 @@ public class RemoteFileManager
Get-EventSubscriber -SourceIdentifier PSESRemoteSessionOpenFile -EA Ignore | Remove-Event
";

private const string SetRemoteContentsScript = @"
param(
[string] $RemoteFilePath,
[byte[]] $Content
)
Set-Content -Path $RemoteFilePath -Value $Content -Encoding Byte -Force 2>&1
";

#endregion

#region Constructors
Expand Down Expand Up @@ -185,6 +195,61 @@ public async Task<string> FetchRemoteFile(
return localFilePath;
}

/// <summary>
/// Saves the contents of the file under the temporary local
/// file cache to its corresponding remote file.
/// </summary>
/// <param name="localFilePath">
/// The local file whose contents will be saved. It is assumed
/// that the editor has saved the contents of the local cache
/// file to disk before this method is called.
/// </param>
/// <returns>A Task to be awaited for completion.</returns>
public async Task SaveRemoteFile(string localFilePath)
{
string remoteFilePath =
this.GetMappedPath(
localFilePath,
this.powerShellContext.CurrentRunspace);

Logger.Write(
LogLevel.Verbose,
$"Saving remote file {remoteFilePath} (local path: {localFilePath})");

byte[] localFileContents = null;
try
{
localFileContents = File.ReadAllBytes(localFilePath);
}
catch (IOException e)
{
Logger.WriteException(
"Failed to read contents of local copy of remote file",
e);

return;
}

PSCommand saveCommand = new PSCommand();
saveCommand
.AddScript(SetRemoteContentsScript)
.AddParameter("RemoteFilePath", remoteFilePath)
.AddParameter("Content", localFileContents);

StringBuilder errorMessages = new StringBuilder();

await this.powerShellContext.ExecuteCommand<object>(
saveCommand,
errorMessages,
false,
false);

if (errorMessages.Length > 0)
{
Logger.Write(LogLevel.Error, $"Remote file save failed due to an error:\r\n\r\n{errorMessages}");
}
}

/// <summary>
/// Creates a temporary file with the given name and contents
/// corresponding to the specified runspace.
Expand Down