3

在实现 microsoft.build.utilities.task 时,我如何访问构建的各种环境变量?

例如“目标路径”

我知道我可以将它作为任务 XML 的一部分传入

<MyTask TargetPath="$(TargetPath)" />

但是,如果我可以访问代码中的变量,我不想强​​迫任务的使用者必须这样做。

http://msdn.microsoft.com/en-us/library/microsoft.build.utilities.task.aspx

4

2 回答 2

5

我想出了如何做到这一点

public static class BuildEngineExtensions
{
    const BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public;

    public static IEnumerable GetEnvironmentVariable(this IBuildEngine buildEngine, string key,bool throwIfNotFound)
    {
        var projectInstance = GetProjectInstance(buildEngine);

        var items = projectInstance.Items
            .Where(x => string.Equals(x.ItemType, key, StringComparison.InvariantCultureIgnoreCase)).ToList();
        if (items.Count > 0)
        {
            return items.Select(x => x.EvaluatedInclude);
        }


        var properties = projectInstance.Properties
            .Where(x => string.Equals(x.Name, key, StringComparison.InvariantCultureIgnoreCase)).ToList();
        if (properties.Count > 0)
        {
            return properties.Select(x => x.EvaluatedValue);
        }

        if (throwIfNotFound)
        {
            throw new Exception(string.Format("Could not extract from '{0}' environmental variables.", key));
        }

        return Enumerable.Empty();
    }

    static ProjectInstance GetProjectInstance(IBuildEngine buildEngine)
    {
        var buildEngineType = buildEngine.GetType();
        var targetBuilderCallbackField = buildEngineType.GetField("targetBuilderCallback", bindingFlags);
        if (targetBuilderCallbackField == null)
        {
            throw new Exception("Could not extract targetBuilderCallback from " + buildEngineType.FullName);
        }
        var targetBuilderCallback = targetBuilderCallbackField.GetValue(buildEngine);
        var targetCallbackType = targetBuilderCallback.GetType();
        var projectInstanceField = targetCallbackType.GetField("projectInstance", bindingFlags);
        if (projectInstanceField == null)
        {
            throw new Exception("Could not extract projectInstance from " + targetCallbackType.FullName);
        }
        return (ProjectInstance)projectInstanceField.GetValue(targetBuilderCallback);
    }
}

而且可以这样使用

string targetPath = buildEngine.GetEnvironmentVariable("TargetPath", true).First();
string intermediateAssembly = buildEngine.GetEnvironmentVariable("IntermediateAssembly", true).First();
IEnumerable<string> referencePaths = buildEngine.GetEnvironmentVariable("ReferencePath", true);

是的,它是丑陋的黑魔法,但它有效。

于 2011-05-22T04:56:18.077 回答
2

你不能轻易做到这一点,你不应该这样做。任务不应该知道它的执行上下文,并且应该使用它的输入参数。

免责声明:不要这样做!

如果你真的想这样做,你需要用类似的东西重新解析项目文件。

public override bool Execute()
{
  string projectFile = BuildEngine.ProjectFileOfTaskNode;

  Engine buildEngine = new Engine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory());

  Project project = new Project(buildEngine);
  project.Load(projectFile);
  foreach(var o in project.EvaluatedProperties)
  {
    // Use properties
  }

  // Do what you want

  return true;
}
于 2010-06-15T08:39:37.320 回答