Skip to content

Commit

Permalink
feat: Add ability to send null old content to overwrite file
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Sep 20, 2024
1 parent 7203c83 commit 2cd7ad4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,32 +438,29 @@ private async Task ProcessUpdateFile(UpdateFile message)

try
{
var (result, error) = DoUpdateFile();
var (result, error) = message switch
{
{ FilePath: null or { Length: 0 } } => (FileUpdateResult.BadRequest, "Invalid request (file path is empty)"),
{ OldText: not null, NewText: not null } => DoUpdate(message.OldText, message.NewText),
{ OldText: null, NewText: not null } => DoWrite(message.NewText),
{ NewText: null, IsCreateDeleteAllowed: true } => DoDelete(),
_ => (FileUpdateResult.BadRequest, "Invalid request")
};
if ((int)result < 300 && !message.IsForceHotReloadDisabled)
{
await RequestHotReloadToIde(hotReload.Id);
}

await _remoteControlServer.SendFrame(new UpdateFileResponse(message.RequestId, message.FilePath, result, error, hotReload.Id));
await _remoteControlServer.SendFrame(new UpdateFileResponse(message.RequestId, message.FilePath ?? "", result, error, hotReload.Id));
}
catch (Exception ex)
{
await hotReload.Complete(HotReloadServerResult.InternalError, ex);
await _remoteControlServer.SendFrame(new UpdateFileResponse(message.RequestId, message.FilePath, FileUpdateResult.Failed, ex.Message));
await _remoteControlServer.SendFrame(new UpdateFileResponse(message.RequestId, message.FilePath ?? "", FileUpdateResult.Failed, ex.Message));
}

(FileUpdateResult, string?) DoUpdateFile()
(FileUpdateResult, string?) DoUpdate(string oldText, string newText)
{
if (message?.IsValid() is not true)
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Got an invalid update file frame ({message}) [{message?.RequestId}].");
}

return (FileUpdateResult.BadRequest, "Invalid request");
}

if (!File.Exists(message.FilePath))
{
if (this.Log().IsEnabled(LogLevel.Debug))
Expand All @@ -474,21 +471,16 @@ private async Task ProcessUpdateFile(UpdateFile message)
return (FileUpdateResult.FileNotFound, $"Requested file '{message.FilePath}' does not exists.");
}

if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Apply Changes to {message.FilePath} [{message.RequestId}].");
}

var originalContent = File.ReadAllText(message.FilePath);
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().LogTrace($"Original content: {message.FilePath} [{message.RequestId}].");
this.Log().LogTrace($"Original content: {originalContent} [{message.RequestId}].");
}

var updatedContent = originalContent.Replace(message.OldText, message.NewText);
var updatedContent = originalContent.Replace(oldText, newText);

Check notice on line 480 in src/Uno.UI.RemoteControl.Server.Processors/HotReload/ServerHotReloadProcessor.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RemoteControl.Server.Processors/HotReload/ServerHotReloadProcessor.cs#L480

Change this call to 'originalContent.Replace' to an overload that accepts a 'StringComparison' as a parameter.
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().LogTrace($"Updated content: {message.FilePath} [{message.RequestId}].");
this.Log().LogTrace($"Updated content: {updatedContent} [{message.RequestId}].");
}

if (updatedContent == originalContent)
Expand All @@ -504,6 +496,43 @@ private async Task ProcessUpdateFile(UpdateFile message)
File.WriteAllText(message.FilePath, updatedContent);
return (FileUpdateResult.Success, null);
}

(FileUpdateResult, string?) DoWrite(string newText)
{
if (!message.IsCreateDeleteAllowed && !File.Exists(message.FilePath))
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Requested file '{message.FilePath}' does not exists [{message.RequestId}].");
}

return (FileUpdateResult.FileNotFound, $"Requested file '{message.FilePath}' does not exists.");
}

if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().LogTrace($"Write content: {newText} [{message.RequestId}].");
}

File.WriteAllText(message.FilePath, newText);
return (FileUpdateResult.Success, null);
}

(FileUpdateResult, string?) DoDelete()
{
if (!File.Exists(message.FilePath))
{
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().LogDebug($"Requested file '{message.FilePath}' does not exists [{message.RequestId}].");
}

return (FileUpdateResult.FileNotFound, $"Requested file '{message.FilePath}' does not exists.");
}

File.Delete(message.FilePath);
return (FileUpdateResult.Success, null);
}
}

private async Task<bool> RequestHotReloadToIde(long sequenceId)
Expand Down
20 changes: 18 additions & 2 deletions src/Uno.UI.RemoteControl/HotReload/Messages/UpdateFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@ public class UpdateFile : IMessage
[JsonProperty]
public string FilePath { get; set; } = string.Empty;

/// <summary>
/// The old text to replace in the file, or `null` to create a new file (only if <see cref="IsCreateDeleteAllowed"/> is true).
/// </summary>
[JsonProperty]
public string? OldText { get; set; }

/// <summary>
/// The new text to replace in the file, or `null` to delete the file (only if <see cref="IsCreateDeleteAllowed"/> is true).
/// </summary>
[JsonProperty]
public string OldText { get; set; } = string.Empty;
public string? NewText { get; set; }

/// <summary>
/// Indicates if the file can be created or deleted.
/// </summary>
[JsonProperty]
public string NewText { get; set; } = string.Empty;
public bool IsCreateDeleteAllowed { get; set; }

/// <summary>
/// Disable the forced hot-reload requested on VS after the file has been modified.
Expand All @@ -37,6 +49,10 @@ public class UpdateFile : IMessage
[JsonIgnore]
string IMessage.Name => Name;

/// <summary>
/// LEGACY, indicates if valid for the legacy processor to handle it.
/// </summary>
/// <returns></returns>
[MemberNotNullWhen(true, nameof(FilePath), nameof(OldText), nameof(NewText))]
public bool IsValid()
=> !FilePath.IsNullOrEmpty() &&
Expand Down

0 comments on commit 2cd7ad4

Please sign in to comment.