Skip to content

Commit

Permalink
vscode-ext: Enable logging (#242)
Browse files Browse the repository at this point in the history
* vscode-ext: Enable logging

* workaround xunit tracewriter
  • Loading branch information
ChristopherHX authored Oct 19, 2023
1 parent cdf376d commit 7a99cef
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/Sdk/AzurePipelines/AzureDevops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.C
if(fileContent == null) {
throw new Exception($"Couldn't read template {filenameAndRef} resolved to {finalFileName} ({finalRepository ?? "self"})");
}
context.TraceWriter?.Info("{0}", $"Parsing template {filenameAndRef} resolved to {finalFileName} ({finalRepository ?? "self"}) using Schema {schemaName ?? "pipeline-root"}");
context.TraceWriter?.Verbose("{0}", fileContent);

TemplateToken token;
using (var stringReader = new StringReader(fileContent))
Expand Down Expand Up @@ -591,6 +593,7 @@ public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.C

var evaluatedResult = TemplateEvaluator.Evaluate(templateContext, schemaName ?? "pipeline-root", pipelineroot, 0, fileId);
templateContext.Errors.Check();
context.TraceWriter?.Verbose("{0}", evaluatedResult.ToContextData().ToJToken().ToString());
return evaluatedResult.AssertMapping("root");
}

Expand Down
2 changes: 2 additions & 0 deletions src/azure-pipelines-vscode-ext/ext-core/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public static partial class Interop {
internal static partial Task Message(int type, string message);
[JSImport("sleep", "extension.js")]
internal static partial Task Sleep(int time);
[JSImport("log", "extension.js")]
internal static partial void Log(int type, string message);
}
35 changes: 34 additions & 1 deletion src/azure-pipelines-vscode-ext/ext-core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,45 @@ public async Task<string> ReadFile(string repositoryAndRef, string path)
}
}

public class TraceWriter : GitHub.DistributedTask.ObjectTemplating.ITraceWriter {
public void Error(string format, params object[] args)
{
if(args?.Length == 1 && args[0] is Exception ex) {
Interop.Log(5, string.Format("{0} {1}", format, ex.Message));
return;
}
try {
Interop.Log(5, args?.Length > 0 ? string.Format(format, args) : format);
} catch {
Interop.Log(5, format);
}
}

public void Info(string format, params object[] args)
{
try {
Interop.Log(3, args?.Length > 0 ? string.Format(format, args) : format);
} catch {
Interop.Log(3, format);
}
}

public void Verbose(string format, params object[] args)
{
try {
Interop.Log(2, args?.Length > 0 ? string.Format(format, args) : format);
} catch {
Interop.Log(2, format);
}
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static async Task<string> ExpandCurrentPipeline(JSObject handle, string currentFileName) {
try {
var context = new Runner.Server.Azure.Devops.Context {
FileProvider = new MyFileProvider(handle),
TraceWriter = new GitHub.DistributedTask.ObjectTemplating.EmptyTraceWriter(),
TraceWriter = new TraceWriter(),
Flags = GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives
};
var template = await AzureDevops.ReadTemplate(context, currentFileName);
Expand Down
38 changes: 33 additions & 5 deletions src/azure-pipelines-vscode-ext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ function activate(context) {
customImports["dotnet.runtime.js"] = require("./build/AppBundle/_framework/dotnet.runtime.js");
customImports["dotnet.native.js"] = require("./build/AppBundle/_framework/dotnet.native.js");

var logchannel = vscode.window.createOutputChannel("Azure Pipeline Evalation Log", { log: true });

var runtimePromise = vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: "Updating Runtime",
cancellable: true
}, async (progress, token) => {
logchannel.appendLine("Updating Runtime");
var items = 1;
var citem = 0;
var runtime = await dotnet.withOnConfigLoaded(async config => {
Expand Down Expand Up @@ -46,10 +49,10 @@ function activate(context) {
return null;
}
} else {
// Get current textEditor content for the entrypoint
var doc = handle.textEditor.document;
if(handle.filename === filename && doc) {
return doc.getText();
// Get current textEditor content for the entrypoint
var doc = handle.textEditor.document;
if(handle.filename === filename && doc) {
return doc.getText();
}
uri = handle.base.with({ path: handle.base.path + "/" + filename });
}
Expand All @@ -64,19 +67,43 @@ function activate(context) {
message: async (type, content) => {
switch(type) {
case 0:
logchannel.info(content);
await vscode.window.showInformationMessage(content);
break;
case 1:
logchannel.warn(content);
await vscode.window.showWarningMessage(content);
break;
case 2:
logchannel.error(content);
await vscode.window.showErrorMessage(content);
break;
}
},
sleep: time => new Promise((resolve, reject) => setTimeout(resolve), time)
sleep: time => new Promise((resolve, reject) => setTimeout(resolve), time),
log: (type, message) => {
switch(type) {
case 1:
logchannel.trace(message);
break;
case 2:
logchannel.debug(message);
break;
case 3:
logchannel.info(message);
break;
case 4:
logchannel.warn(message);
break;
case 5:
logchannel.error(message);
break;
}
}
});
logchannel.appendLine("Starting extension main to keep dotnet alive");
runtime.runMainAndExit("ext-core", []);
logchannel.appendLine("Runtime is now ready");
return runtime;
});

Expand Down Expand Up @@ -111,6 +138,7 @@ function activate(context) {
filename ??= current.path.substring(li + 1);
var result = await runtime.BINDING.bind_static_method("[ext-core] MyClass:ExpandCurrentPipeline")({ base: base, textEditor: textEditor, filename: filename, repositories: repositories }, filename);
if(result) {
logchannel.debug(result);
if(validate) {
await vscode.window.showInformationMessage("No issues found");
} else {
Expand Down

0 comments on commit 7a99cef

Please sign in to comment.