Create task to parse json files
This commit is contained in:
parent
445175df6d
commit
406f7ed423
1 changed files with 54 additions and 0 deletions
54
src/core-sdk-tasks/JsonPropertyParser.cs
Normal file
54
src/core-sdk-tasks/JsonPropertyParser.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Build.Framework;
|
||||
using Microsoft.Build.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.Build
|
||||
{
|
||||
public class JsonPropertyParser : Task
|
||||
{
|
||||
[Required]
|
||||
public string Filename
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[Required]
|
||||
public string Path
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
[Output]
|
||||
public string Value
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public override bool Execute()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var sr = new StreamReader(Filename))
|
||||
{
|
||||
var json = sr.ReadToEnd();
|
||||
var o = JObject.Parse(json);
|
||||
Value = o.SelectToken(Path).Value<string>();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.LogErrorFromException(e);
|
||||
}
|
||||
|
||||
return !Log.HasLoggedErrors;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue