2016-03-07 20:20:50 +00:00
|
|
|
using System;
|
2016-03-16 21:39:02 +00:00
|
|
|
using System.Linq;
|
2016-03-07 20:20:50 +00:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Build.Framework
|
|
|
|
{
|
|
|
|
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
|
|
|
|
public class EnvironmentAttribute : TargetConditionAttribute
|
|
|
|
{
|
|
|
|
private string _envVar;
|
2016-03-07 22:52:41 +00:00
|
|
|
private string[] _expectedVals;
|
2016-03-07 20:20:50 +00:00
|
|
|
|
2016-03-07 22:52:41 +00:00
|
|
|
public EnvironmentAttribute(string envVar, params string[] expectedVals)
|
2016-03-07 20:20:50 +00:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(envVar))
|
|
|
|
{
|
2016-03-16 21:39:02 +00:00
|
|
|
throw new ArgumentNullException(nameof(envVar));
|
|
|
|
}
|
|
|
|
if (expectedVals == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(expectedVals));
|
2016-03-07 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_envVar = envVar;
|
2016-03-07 22:52:41 +00:00
|
|
|
_expectedVals = expectedVals;
|
2016-03-07 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override bool EvaluateCondition()
|
|
|
|
{
|
|
|
|
var actualVal = Environment.GetEnvironmentVariable(_envVar);
|
|
|
|
|
2016-03-16 21:39:02 +00:00
|
|
|
if (_expectedVals.Any())
|
2016-03-07 20:20:50 +00:00
|
|
|
{
|
2016-03-16 21:39:02 +00:00
|
|
|
return _expectedVals.Any(ev => string.Equals(actualVal, ev, StringComparison.Ordinal));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return !string.IsNullOrEmpty(actualVal);
|
2016-03-07 20:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|