How to query MSBUILD file for list of supported targets?

后端 未结 6 446
遥遥无期
遥遥无期 2020-12-14 01:43

Is there any way to ask msbuild what build targets provided msbuild file support? If there is no way to do it in command prompt? May be it could be done programmatically?

6条回答
  •  被撕碎了的回忆
    2020-12-14 02:35

    Here is the code snippet to get all targets in the order their execution.

        static void Main(string[] args)
        {
            Project project = new Project(@"build.core.xml");
            var orderedTargets = GetAllTargetsInOrderOfExecution(project.Targets, project.Targets["FinalTargetInTheDependencyChain"]).ToList();
            File.WriteAllText(@"orderedTargets.txt", orderedTargets.Select(x => x.Name).Aggregate((a, b) => a + "\r\n" + b));
        }
    
        /// 
        /// Gets all targets in the order of their execution by traversing through the dependent targets recursively
        /// 
        /// 
        /// 
        /// 
        public static List GetAllTargetsInOrderOfExecution(IDictionary allTargetsInfo, ProjectTargetInstance target)
        {
            var orderedTargets = new List();
    
            var dependentTargets =
                target
                .DependsOnTargets
                .Split(';')
                .Where(allTargetsInfo.ContainsKey)
                .Select(x => allTargetsInfo[x])
                .ToList();
    
            foreach (var dependentTarget in dependentTargets)
            {
                orderedTargets = orderedTargets.Union(GetAllTargetsInOrderOfExecution(allTargetsInfo, dependentTarget)).ToList();
            }
    
            orderedTargets.Add(target);
    
            return orderedTargets;
        }
    

提交回复
热议问题