Tweak task to make it more general

This commit is contained in:
Forgind 2023-07-26 15:14:04 -07:00
parent 1a59a90685
commit 9606060377

View file

@ -1,8 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework; using Microsoft.Build.Framework;
using Microsoft.Build.Utilities; using Microsoft.Build.Utilities;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -12,21 +9,21 @@ namespace Microsoft.DotNet.Cli.Build
public class JsonPropertyParser : Task public class JsonPropertyParser : Task
{ {
[Required] [Required]
public string Filename public string[] JFilenames
{ {
get; get;
set; set;
} }
[Required] [Required]
public string Path public string[] JPaths
{ {
get; get;
set; set;
} }
[Output] [Output]
public string Value public ITaskItem[] Value
{ {
get; get;
private set; private set;
@ -34,19 +31,29 @@ namespace Microsoft.DotNet.Cli.Build
public override bool Execute() public override bool Execute()
{ {
Value = new TaskItem[JFilenames.Length];
for (var i = 0; i < JFilenames.Length; i++)
{
Value[i] = new TaskItem(JFilenames[i]);
try try
{ {
using (var sr = new StreamReader(Filename)) using (var sr = new StreamReader(JFilenames[i]))
{ {
var json = sr.ReadToEnd(); var json = sr.ReadToEnd();
var o = JObject.Parse(json); var o = JObject.Parse(json);
Value = o.SelectToken(Path).Value<string>(); foreach (var path in JPaths)
{
var lastDot = path.LastIndexOf('.');
var name = lastDot == -1 ? path : path.Substring(lastDot + 1);
Value[i].SetMetadata(name, o.SelectToken(path).Value<string>());
}
} }
} }
catch (Exception e) catch (Exception e)
{ {
Log.LogErrorFromException(e); Log.LogErrorFromException(e);
} }
}
return !Log.HasLoggedErrors; return !Log.HasLoggedErrors;
} }