|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="DowngradeProjects.cs" company="Andrew Arnott"> |
| 3 | +// Copyright (c) Andrew Arnott. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace DotNetOpenAuth.BuildTasks { |
| 8 | + using System; |
| 9 | + using System.Collections.Generic; |
| 10 | + using System.IO; |
| 11 | + using System.Linq; |
| 12 | + using System.Text; |
| 13 | + using Microsoft.Build.BuildEngine; |
| 14 | + using Microsoft.Build.Framework; |
| 15 | + using Microsoft.Build.Utilities; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Downgrades Visual Studio 2010 solutions and projects so that they load in Visual Studio 2008. |
| 19 | + /// </summary> |
| 20 | + public class DowngradeProjects : Task { |
| 21 | + /// <summary> |
| 22 | + /// Gets or sets the projects and solutions to downgrade. |
| 23 | + /// </summary> |
| 24 | + [Required] |
| 25 | + public ITaskItem[] Projects { get; set; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Executes this instance. |
| 29 | + /// </summary> |
| 30 | + public override bool Execute() { |
| 31 | + foreach (ITaskItem taskItem in this.Projects) { |
| 32 | + switch (GetClassification(taskItem)) { |
| 33 | + case ProjectClassification.VS2010Project: |
| 34 | + this.Log.LogMessage(MessageImportance.Low, "Downgrading project \"{0}\".", taskItem.ItemSpec); |
| 35 | + Project project = new Project(); |
| 36 | + project.Load(taskItem.ItemSpec); |
| 37 | + project.DefaultToolsVersion = "3.5"; |
| 38 | + |
| 39 | + string projectTypeGuids = project.GetEvaluatedProperty("ProjectTypeGuids"); |
| 40 | + if (!string.IsNullOrEmpty(projectTypeGuids)) { |
| 41 | + projectTypeGuids = projectTypeGuids.Replace("{F85E285D-A4E0-4152-9332-AB1D724D3325}", "{603c0e0b-db56-11dc-be95-000d561079b0}"); |
| 42 | + project.SetProperty("ProjectTypeGuids", projectTypeGuids); |
| 43 | + } |
| 44 | + |
| 45 | + // Web projects usually have an import that includes these substrings |
| 46 | + foreach (Import import in project.Imports) { |
| 47 | + import.ProjectPath = import.ProjectPath |
| 48 | + .Replace("$(MSBuildExtensionsPath32)", "$(MSBuildExtensionsPath)") |
| 49 | + .Replace("VisualStudio\\v10.0", "VisualStudio\\v9.0"); |
| 50 | + } |
| 51 | + |
| 52 | + // VS2010 won't let you have a System.Core reference, but VS2008 requires it. |
| 53 | + BuildItemGroup references = project.GetEvaluatedItemsByName("Reference"); |
| 54 | + if (!references.Cast<BuildItem>().Any(item => item.FinalItemSpec.StartsWith("System.Core", StringComparison.OrdinalIgnoreCase))) { |
| 55 | + project.AddNewItem("Reference", "System.Core"); |
| 56 | + } |
| 57 | + |
| 58 | + project.Save(taskItem.ItemSpec); |
| 59 | + break; |
| 60 | + case ProjectClassification.VS2010Solution: |
| 61 | + this.Log.LogMessage(MessageImportance.Low, "Downgrading solution \"{0}\".", taskItem.ItemSpec); |
| 62 | + string[] contents = File.ReadAllLines(taskItem.ItemSpec); |
| 63 | + if (contents[1] != "Microsoft Visual Studio Solution File, Format Version 11.00" || |
| 64 | + contents[2] != "# Visual Studio 2010") { |
| 65 | + this.Log.LogError("Unrecognized solution file header in \"{0}\".", taskItem.ItemSpec); |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + contents[1] = "Microsoft Visual Studio Solution File, Format Version 10.00"; |
| 70 | + contents[2] = "# Visual Studio 2008"; |
| 71 | + |
| 72 | + for (int i = 3; i < contents.Length; i++) { |
| 73 | + contents[i] = contents[i].Replace("TargetFrameworkMoniker = \".NETFramework,Version%3Dv", "TargetFramework = \""); |
| 74 | + } |
| 75 | + |
| 76 | + File.WriteAllLines(taskItem.ItemSpec, contents); |
| 77 | + break; |
| 78 | + default: |
| 79 | + this.Log.LogWarning("Unrecognized project type for \"{0}\".", taskItem.ItemSpec); |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return !this.Log.HasLoggedErrors; |
| 85 | + } |
| 86 | + |
| 87 | + private static ProjectClassification GetClassification(ITaskItem taskItem) { |
| 88 | + if (Path.GetExtension(taskItem.ItemSpec).EndsWith("proj", StringComparison.OrdinalIgnoreCase)) { |
| 89 | + return ProjectClassification.VS2010Project; |
| 90 | + } else if (Path.GetExtension(taskItem.ItemSpec).Equals(".sln", StringComparison.OrdinalIgnoreCase)) { |
| 91 | + return ProjectClassification.VS2010Solution; |
| 92 | + } else { |
| 93 | + return ProjectClassification.Unrecognized; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private enum ProjectClassification { |
| 98 | + VS2010Project, |
| 99 | + VS2010Solution, |
| 100 | + Unrecognized, |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments