Skip to content

Commit

Permalink
Merge pull request #8 from idexus/vscode
Browse files Browse the repository at this point in the history
net8.0 update - VSCode
  • Loading branch information
idexus authored Feb 13, 2024
2 parents 43cdebd + 880775f commit a2f71c0
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 55 deletions.
6 changes: 5 additions & 1 deletion src/HotReloadKit.VSCode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

## 0.5.0

- Initial release
- Initial release (net7.0)

## 0.6.0

- Updated to net8.0
8 changes: 8 additions & 0 deletions src/HotReloadKit.VSCode/buildvsix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rm service.zip
cd ..
rm -r ./HotReloadKit.VSCodeService/bin
rm -r ./HotReloadKit.VSCodeService/obj
zip -r service.zip HotReloadKit.VSCodeService Shared
mv service.zip ./HotReloadKit.VSCode
cd HotReloadKit.VSCode
vsce package
2 changes: 1 addition & 1 deletion src/HotReloadKit.VSCode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"displayName": "HotReloadKit",
"description": "C# Hot Reload Kit",
"repository": "https://github.com/idexus/HotReloadKit",
"version": "0.5.0",
"version": "0.6.0",
"icon": "icon.png",
"engines": {
"vscode": "^1.85.0"
Expand Down
Binary file modified src/HotReloadKit.VSCode/service.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion src/HotReloadKit.VSCode/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export function delay(milliseconds: number): Promise<void> {
resolve();
}, milliseconds);
});
}
}
46 changes: 13 additions & 33 deletions src/HotReloadKit.VSCodeService/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public async Task<IActionResult> StartServiceAsync()
[HttpGet("checkService")]
public IActionResult CheckService()
{
return Ok($"HotReloadKit service is working");
if (hotReloadServer != null)
return Ok($"HotReloadKit service is working");
return BadRequest();
}

[HttpPost("debugStarted")]
Expand All @@ -46,40 +48,19 @@ public async Task<IActionResult> DebugStartedAsync([FromBody] DebugInfo debugInf
{
if (hotReloadServer != null)
{
var workspace = MSBuildWorkspace.Create();
var properties = new Dictionary<string, string>();

if (debugInfo.RuntimeIdentifier != "undefined") properties.Add("RuntimeIdentifier", debugInfo.RuntimeIdentifier);
if (debugInfo.TargetFramework != "undefined") properties.Add("TargetFramework", debugInfo.TargetFramework);

var workspace = MSBuildWorkspace.Create(properties);
var project = await workspace.OpenProjectAsync(debugInfo.ProjectPath);

if (debugInfo.Type == "maui")
{
string outputFilePath;

if (debugInfo.RuntimeIdentifier != "undefined")
{
var platform = GetPlatform(debugInfo);
var options = project.CompilationOptions!.WithPlatform(platform);
project = project.WithCompilationOptions(options);
outputFilePath = Path.Combine(Path.GetDirectoryName(debugInfo.ProjectPath)!, "bin", debugInfo.Configuration, debugInfo.TargetFramework, debugInfo.RuntimeIdentifier, project.AssemblyName + ".dll");
}
else
{
outputFilePath = Path.Combine(Path.GetDirectoryName(debugInfo.ProjectPath)!, "bin", debugInfo.Configuration, debugInfo.TargetFramework, project.AssemblyName + ".dll");
}

hotReloadServer.RegisterProject(new ProjectInfo
{
DebugInfo = debugInfo,
Project = project,
OutputFilePath = outputFilePath
});
}
else
hotReloadServer.RegisterProject(new ProjectInfo
{
hotReloadServer.RegisterProject(new ProjectInfo
{
DebugInfo = debugInfo,
Project = project,
});
}
DebugInfo = debugInfo,
Project = project,
});
}

return Ok($"Debug started");
Expand Down Expand Up @@ -123,7 +104,6 @@ public class ProjectInfo
public required DebugInfo DebugInfo { get; set; }
// public required MSBuildWorkspace Workspace { get; set; }
public required Project Project { get; set; }
public string? OutputFilePath { get; set; }
}

public class DebugContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Microsoft.Build.Locator" Version="1.7.1" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
2 changes: 0 additions & 2 deletions src/HotReloadKit.VSCodeService/HotReloadServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,7 @@ async Task CompileAndEmitChangesAsync(SlimClient client, DebugContext context, s
codeCompilation = new CodeCompilation
{
AdditionalTypeNames = additionaTypeNames,
Solution = null, //Solution,
Project = context.ProjectInfo.Project,
OutputFilePath = context.ProjectInfo.OutputFilePath,
RequestedFilePaths = context.changedFilePaths.ToList()
};
context.changedFilePaths.Clear();
Expand Down
16 changes: 8 additions & 8 deletions src/HotReloadKit/HotReloader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Net;
using System.Net;
using System.Reflection;
using System.Text.Json;
using System.Threading;
Expand All @@ -8,10 +8,10 @@

namespace HotReloadKit;

public class HotReloadTypeData
{
public Type Type { get; set; }
public bool IsFromChangedFile { get; set; }
public class HotReloadTypeData
{
public Type Type { get; set; }
public bool IsFromChangedFile { get; set; }
}

public static class HotReloader
Expand Down Expand Up @@ -54,7 +54,7 @@ static async Task ConnectAsync(IPAddress[] serverIPs, int timeout)
var messageData = JsonSerializer.Deserialize<HotReloadServerConnectionData>(message);
if (messageData?.Token == HotReloadServerConnectionData.DefaultToken)
{
Debug.WriteLine($"HotReloadKit connected - address: {serverIP.ToString()} port: {serverPort} server version: {messageData.Version} guid: {messageData.Guid}");
Debug.WriteLine($"HotReloadKit connected - address: {serverIP.ToString()} port: {serverPort} protocol ver: {messageData.Version} guid: {messageData.Guid}");
_ = ClientRunLoop(client);
return;
}
Expand Down Expand Up @@ -100,8 +100,8 @@ static async Task ClientRunLoop(SlimClient client)
{
var type = assembly.GetType(typeName);
var isFromChangedFile = false;
if (hotReloadData.ChangedTypeNames != null)
isFromChangedFile = hotReloadData.ChangedTypeNames.Contains(typeName);
if (hotReloadData.ChangedTypeNames != null)
isFromChangedFile = hotReloadData.ChangedTypeNames.Contains(typeName);
if (type != null) typeDataList.Add(new HotReloadTypeData { Type = type, IsFromChangedFile = isFromChangedFile });
}
UpdateApplication?.Invoke(typeDataList.ToArray());
Expand Down
14 changes: 7 additions & 7 deletions src/Shared/Builder/CodeCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public class CodeCompilation

// -- public properties --

public Solution Solution { get; set; }
public Project Project { get; set; }
public string OutputFilePath { get; set; }
public string[] AdditionalTypeNames { get; set; }

public IEnumerable<string> RequestedFilePaths { get; set; }
Expand All @@ -36,8 +34,7 @@ public class CodeCompilation
public async Task CompileAsync()
{
// ------ Microsoft.CodeAnalysis projects ------
var solution = Solution ?? Project.Solution;
var referencedProjects = Project.ProjectReferences?.Select(e => solution.Projects.FirstOrDefault(x => x.Id == e.ProjectId));
var referencedProjects = Project.ProjectReferences?.Select(e => Project.Solution.Projects.FirstOrDefault(x => x.Id == e.ProjectId));
var generators = Project.AnalyzerReferences.SelectMany(e => e.GetGeneratorsForAllLanguages());
var includedProjects = referencedProjects?.ToList() ?? new List<Microsoft.CodeAnalysis.Project>();
includedProjects.Add(Project);
Expand Down Expand Up @@ -123,9 +120,12 @@ public async Task CompileAsync()
syntaxTreeList.AddRange(partialSyntaxTrees);

// --------- metadata reference ---------
var mainPath = Path.GetDirectoryName(Project.OutputFilePath)!;

List<MetadataReference> metadataReferences = new List<MetadataReference>();
metadataReferences.AddRange(includedProjects.Select(e => MetadataReference.CreateFromFile(OutputFilePath ?? Project.OutputFilePath)));
metadataReferences.AddRange(compilation.References);
var metaDatas = includedProjects.Select(e => MetadataReference.CreateFromFile(Path.Combine(mainPath, Path.GetFileName(e!.OutputFilePath!))));
metadataReferences.AddRange(metaDatas);
metadataReferences.AddRange(Project.MetadataReferences);

// --------- new compilation ------------

Expand All @@ -145,7 +145,7 @@ public async Task EmitDataAsync(Func<string[], string[], byte[], byte[], Task> s
{
using (var dllStream = new MemoryStream())
using (var pdbStream = new MemoryStream())
{
{
var emitResult = newCompilation.Emit(dllStream, pdbStream);
if (emitResult.Success)
{
Expand Down

0 comments on commit a2f71c0

Please sign in to comment.