feat: add DSH IDE open-file references
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Prophet.CoopWithDsh
|
||||
{
|
||||
internal sealed class SnapshotPublisher : IDisposable
|
||||
{
|
||||
private readonly object gate = new object();
|
||||
internal string InstanceId { get; } = Guid.NewGuid().ToString("N");
|
||||
internal string FilePath { get; }
|
||||
|
||||
internal SnapshotPublisher(string? localAppData = null)
|
||||
{
|
||||
var root = localAppData ?? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
if (string.IsNullOrWhiteSpace(root)) throw new InvalidOperationException("LOCALAPPDATA is unavailable.");
|
||||
var directory = Path.Combine(root, "Prophet", "dsh-plugin-coop-with-vs", "v1", "instances");
|
||||
FilePath = Path.Combine(directory, $"visualstudio-{InstanceId}.json");
|
||||
}
|
||||
|
||||
internal void Publish(OpenFileSnapshot snapshot)
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
var directory = Path.GetDirectoryName(FilePath)!;
|
||||
Directory.CreateDirectory(directory);
|
||||
var temporary = FilePath + "." + Guid.NewGuid().ToString("N") + ".tmp";
|
||||
File.WriteAllText(temporary, JsonConvert.SerializeObject(snapshot) + Environment.NewLine, new UTF8Encoding(false));
|
||||
try
|
||||
{
|
||||
if (File.Exists(FilePath)) File.Replace(temporary, FilePath, null);
|
||||
else File.Move(temporary, FilePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
TryDelete(temporary);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() => TryDelete(FilePath);
|
||||
|
||||
private static void TryDelete(string path)
|
||||
{
|
||||
try { if (File.Exists(path)) File.Delete(path); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user