using Amazon.Lambda.Annotations.SourceGenerator.FileIO;
using System;
using System.Xml;
namespace Amazon.Lambda.Annotations.SourceGenerator
{
///
/// Utilities for working with project (.csproj) files
///
public class ProjectFileHandler
{
private const string OptOutNodeXpath = "//PropertyGroup/AWSSuppressLambdaAnnotationsTelemetry";
///
/// Determines if the project has opted out of any Lambda Annotations telemetry
///
/// Path to a .csproj file
/// FileManager instance used to read the csproj contents
/// True if opted out of telemetry, false otherwise
public static bool IsTelemetrySuppressed(string projectFilePath, IFileManager fileManager)
{
// If we were unable to find the csproj file, treat as if not opted out
if (string.IsNullOrEmpty(projectFilePath))
{
return false;
}
var projectfileContent = fileManager.ReadAllText(projectFilePath);
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(projectfileContent);
var optOutNode = xmlDoc.SelectSingleNode(OptOutNodeXpath) as XmlElement;
if (optOutNode != null && !string.IsNullOrEmpty(optOutNode.InnerText))
{
if (string.Equals("true", optOutNode.InnerText, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
}
}