Skip to content

Commit a541191

Browse files
author
Vladislav Kalugin
committed
switch to json writer, set target to net462
1 parent bbea90e commit a541191

4 files changed

Lines changed: 58 additions & 69 deletions

File tree

.github/workflows/build.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Build dotnet package
22

3-
on: [push]
3+
on: [ push ]
44

55
jobs:
66
build:
@@ -17,6 +17,4 @@ jobs:
1717
uses: actions/upload-artifact@v3
1818
with:
1919
name: msbuild-database
20-
path: bin/Release/netstandard2.0/msbuild-database.dll
21-
22-
20+
path: bin/Release/net462

.github/workflows/publish_release.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ jobs:
2020
run: |
2121
export VERSION=$(date '+%Y.%-m').$GITHUB_RUN_NUMBER
2222
echo "VERSION=$VERSION" >> $GITHUB_ENV
23-
- uses: "marvinpinto/action-automatic-releases@latest"
23+
- name: Archive release
24+
run: zip -r msbuild-release.zip bin/Release/net462
25+
- name: Publish release
26+
uses: "marvinpinto/action-automatic-releases@latest"
2427
with:
2528
repo_token: "${{ secrets.GITHUB_TOKEN }}"
2629
automatic_release_tag: "${{ env.VERSION }}"
2730
prerelease: true
28-
files: bin/Release/netstandard2.0/msbuild-database.dll
31+
files: msbuild-release.zip

msbuild-database.cs

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.Web;
1010
using Microsoft.Build.Framework;
1111
using Microsoft.Build.Utilities;
12+
using System.Text.Json;
13+
1214

1315
/// <summary>
1416
/// An MsBuild logger that emit compile_commands.json and link_commands.json files from a C++ project build.
@@ -18,6 +20,20 @@
1820
/// </remarks>
1921
public class CompileDatabase : Logger
2022
{
23+
private class CompileCommand
24+
{
25+
public String command { get; set; }
26+
public String directory { get; set; }
27+
public String file { get; set; }
28+
}
29+
30+
private class LinkCommand
31+
{
32+
public String command { get; set; }
33+
public String directory { get; set; }
34+
public List<String> files { get; set; }
35+
}
36+
2137
public override void Initialize(IEventSource eventSource)
2238
{
2339
string compileOutputFilePath = "compile_commands.json";
@@ -28,11 +44,10 @@ public override void Initialize(IEventSource eventSource)
2844
const bool append = false;
2945
Encoding utf8WithoutBom = new UTF8Encoding(false);
3046
this.CompileStreamWriter = new StreamWriter(compileOutputFilePath, append, utf8WithoutBom);
31-
this.firstLine = true;
32-
CompileStreamWriter.WriteLine("[");
33-
3447
this.LinkStreamWriter = new StreamWriter(linkOutputFilePath, append, utf8WithoutBom);
35-
LinkStreamWriter.WriteLine("[");
48+
49+
compileCommands = new List<CompileCommand>();
50+
linkCommands = new List<LinkCommand>();
3651
}
3752
catch (Exception ex)
3853
{
@@ -246,9 +261,9 @@ private void ProcessCompileCommand(string compilerPath, string[] cmdArgs, String
246261
}
247262

248263
// simplify the compile command to avoid .. etc.
249-
string compileCommand = '"' + Path.GetFullPath(compilerPath) + "\" " + String.Join(" ", cmdArgs);
264+
string compileCommand = Path.GetFullPath(compilerPath) + " " + String.Join(" ", cmdArgs);
250265

251-
WriteCompileCommand(compileCommand, filenames, dirname);
266+
addCompileCommand(compileCommand, filenames, dirname);
252267
}
253268

254269
private void ProcessLinkCommand(string compilerPath, string[] cmdArgs, String dirname)
@@ -300,88 +315,58 @@ private void ProcessLinkCommand(string compilerPath, string[] cmdArgs, String di
300315
}
301316

302317
// simplify the compile command to avoid .. etc.
303-
string compileCommand = '"' + Path.GetFullPath(compilerPath) + "\" " + String.Join(" ", cmdArgs);
318+
string linkCommand = Path.GetFullPath(compilerPath) + " " + String.Join(" ", cmdArgs);
304319

305-
WriteLinkCommand(compileCommand, filenames, dirname);
320+
addLinkCommand(linkCommand, filenames, dirname);
306321
}
307322

308-
private void WriteCompileCommand(string compileCommand, List<string> files, string dirname)
323+
private void addCompileCommand(string compileCommand, List<string> files, string dirname)
309324
{
310325
foreach (string filename in files)
311326
{
312-
// Terminate the preceding entry
313-
if (firstLine)
327+
CompileCommand commandVal = new CompileCommand
314328
{
315-
firstLine = false;
316-
}
317-
else
318-
{
319-
CompileStreamWriter.WriteLine(",");
320-
}
329+
command = compileCommand,
330+
directory = dirname,
331+
file = filename
332+
};
321333

322-
// Write one entry
323-
CompileStreamWriter.WriteLine(" {");
324-
CompileStreamWriter.WriteLine(String.Format(
325-
" \"command\": \"{0}\",",
326-
HttpUtility.JavaScriptStringEncode(compileCommand)));
327-
CompileStreamWriter.WriteLine(String.Format(
328-
" \"file\": \"{0}\",",
329-
HttpUtility.JavaScriptStringEncode(filename)));
330-
CompileStreamWriter.WriteLine(String.Format(
331-
" \"directory\": \"{0}\"",
332-
HttpUtility.JavaScriptStringEncode(dirname)));
333-
CompileStreamWriter.Write(" }");
334+
compileCommands.Add(commandVal);
334335
}
335336
}
336337

337-
private void WriteLinkCommand(string linkCommand, List<string> files, string dirname)
338+
private void addLinkCommand(string linkCommand, List<string> files, string dirname)
338339
{
339-
LinkStreamWriter.WriteLine(" {");
340-
LinkStreamWriter.WriteLine(String.Format(
341-
" \"directory\": \"{0}\",",
342-
HttpUtility.JavaScriptStringEncode(dirname)));
343-
LinkStreamWriter.WriteLine(String.Format(
344-
" \"command\": \"{0}\",",
345-
HttpUtility.JavaScriptStringEncode(linkCommand)));
346-
LinkStreamWriter.WriteLine(" \"files\": [");
347-
bool fl = true;
348-
foreach (string filename in files)
340+
LinkCommand commandVal = new LinkCommand()
349341
{
350-
if (fl)
351-
{
352-
fl = false;
353-
}
354-
else
355-
{
356-
LinkStreamWriter.WriteLine(",");
357-
}
358-
359-
LinkStreamWriter.Write(String.Format(" \"{0}\"",
360-
HttpUtility.JavaScriptStringEncode(filename)));
361-
}
342+
command = linkCommand,
343+
directory = dirname,
344+
files = files
345+
};
362346

363-
LinkStreamWriter.WriteLine();
364-
LinkStreamWriter.WriteLine(" ]");
365-
LinkStreamWriter.WriteLine(" }");
347+
linkCommands.Add(commandVal);
366348
}
367349

368350
public override void Shutdown()
369351
{
370-
if (!firstLine)
352+
JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions
371353
{
372-
CompileStreamWriter.WriteLine();
373-
}
374-
375-
CompileStreamWriter.WriteLine("]");
354+
WriteIndented = true,
355+
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
356+
};
357+
CompileStreamWriter.WriteLine(JsonSerializer.Serialize(compileCommands, jsonSerializerOptions));
376358
CompileStreamWriter.Close();
377359

378-
LinkStreamWriter.WriteLine("]");
360+
361+
LinkStreamWriter.WriteLine(JsonSerializer.Serialize(linkCommands, jsonSerializerOptions));
379362
LinkStreamWriter.Close();
380363

381364
base.Shutdown();
382365
}
383366

367+
368+
private List<CompileCommand> compileCommands;
369+
private List<LinkCommand> linkCommands;
384370
private StreamWriter CompileStreamWriter;
385371
private StreamWriter LinkStreamWriter;
386-
private bool firstLine;
387372
}

msbuild-database.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
4+
<TargetFrameworks>net462</TargetFrameworks>
55
<Product />
66
<Description>An MsBuild logger that emit compile_commands.json and link_commands.json files from a C++</Description>
77
<PackageLicenseExpression>MIT</PackageLicenseExpression>
8+
<SelfContained>true</SelfContained>
9+
<PublishSingleFile>true</PublishSingleFile>
810
</PropertyGroup>
911
<ItemGroup>
1012
<PackageReference Include="Microsoft.Build" Version="14.3.0" PrivateAssets="All" />
1113
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="14.3.0" PrivateAssets="All" />
14+
<PackageReference Include="System.Text.Json" Version="7.0.0" />
1215
</ItemGroup>
1316

1417
</Project>

0 commit comments

Comments
 (0)