// Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using System.Collections.Generic; using Microsoft.Build.Evaluation; using Microsoft.Build.Construction; using Microsoft.DotNet.ProjectModel; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli; using System.Linq; using System.IO; using Newtonsoft.Json; namespace Microsoft.DotNet.ProjectJsonMigration { public class AddPropertyTransform : ConditionalTransform { public string PropertyName { get; } private readonly ProjectRootElement _propertyObjectGenerator = ProjectRootElement.Create(); private readonly string _propertyValue; private readonly Func _propertyValueFunc; public AddPropertyTransform(string propertyName, string propertyValue, Func condition) : base(condition) { PropertyName = propertyName; _propertyValue = propertyValue; } public AddPropertyTransform(string propertyName, Func propertyValueFunc, Func condition) : base(condition) { PropertyName = propertyName; _propertyValueFunc = propertyValueFunc; } public override ProjectPropertyElement ConditionallyTransform(T source) { string propertyValue = GetPropertyValue(source); var property = _propertyObjectGenerator.CreatePropertyElement(PropertyName); property.Value = propertyValue; return property; } public string GetPropertyValue(T source) { return _propertyValue ?? _propertyValueFunc(source); } } }