@@ -10,6 +10,7 @@ namespace DotNetOpenAuth.BuildTasks {
1010 using System . IO ;
1111 using System . Linq ;
1212 using System . Text ;
13+ using System . Text . RegularExpressions ;
1314 using Microsoft . Build . BuildEngine ;
1415 using Microsoft . Build . Framework ;
1516 using Microsoft . Build . Utilities ;
@@ -24,6 +25,9 @@ public class DowngradeProjects : Task {
2425 [ Required ]
2526 public ITaskItem [ ] Projects { get ; set ; }
2627
28+ [ Output ]
29+ public ITaskItem [ ] DowngradedProjects { get ; set ; }
30+
2731 /// <summary>
2832 /// Gets or sets a value indicating whether ASP.NET MVC 2 projects are downgraded to MVC 1.0.
2933 /// </summary>
@@ -33,11 +37,26 @@ public class DowngradeProjects : Task {
3337 /// Executes this instance.
3438 /// </summary>
3539 public override bool Execute ( ) {
40+ var newProjectToOldProjectMapping = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
41+ var createdProjectFiles = new List < TaskItem > ( ) ;
42+
43+ foreach ( ITaskItem taskItem in this . Projects ) {
44+ switch ( GetClassification ( taskItem ) ) {
45+ case ProjectClassification . VS2010Project :
46+ case ProjectClassification . VS2010Solution :
47+ string projectNameForVS2008 = Path . Combine (
48+ Path . GetDirectoryName ( taskItem . ItemSpec ) ,
49+ Path . GetFileNameWithoutExtension ( taskItem . ItemSpec ) + "-vs2008" + Path . GetExtension ( taskItem . ItemSpec ) ) ;
50+ newProjectToOldProjectMapping [ taskItem . ItemSpec ] = projectNameForVS2008 ;
51+ break ;
52+ }
53+ }
54+
3655 foreach ( ITaskItem taskItem in this . Projects ) {
3756 switch ( GetClassification ( taskItem ) ) {
3857 case ProjectClassification . VS2010Project :
3958 this . Log . LogMessage ( MessageImportance . Low , "Downgrading project \" {0}\" ." , taskItem . ItemSpec ) ;
40- Project project = new Project ( ) ;
59+ var project = new Project ( ) ;
4160 project . Load ( taskItem . ItemSpec ) ;
4261 project . DefaultToolsVersion = "3.5" ;
4362
@@ -66,7 +85,18 @@ public override bool Execute() {
6685 project . AddNewItem ( "Reference" , "System.Core" ) ;
6786 }
6887
69- project . Save ( taskItem . ItemSpec ) ;
88+ // Rewrite ProjectReferences to other renamed projects.
89+ BuildItemGroup projectReferences = project . GetEvaluatedItemsByName ( "ProjectReference" ) ;
90+ foreach ( var mapping in newProjectToOldProjectMapping ) {
91+ string oldName = Path . GetFileName ( mapping . Key ) ;
92+ string newName = Path . GetFileName ( mapping . Value ) ;
93+ foreach ( BuildItem projectReference in projectReferences ) {
94+ projectReference . Include = Regex . Replace ( projectReference . Include , oldName , newName , RegexOptions . IgnoreCase ) ;
95+ }
96+ }
97+
98+ project . Save ( newProjectToOldProjectMapping [ taskItem . ItemSpec ] ) ;
99+ createdProjectFiles . Add ( new TaskItem ( taskItem ) { ItemSpec = newProjectToOldProjectMapping [ taskItem . ItemSpec ] } ) ;
70100 break ;
71101 case ProjectClassification . VS2010Solution :
72102 this . Log . LogMessage ( MessageImportance . Low , "Downgrading solution \" {0}\" ." , taskItem . ItemSpec ) ;
@@ -84,14 +114,25 @@ public override bool Execute() {
84114 contents [ i ] = contents [ i ] . Replace ( "TargetFrameworkMoniker = \" .NETFramework,Version%3Dv" , "TargetFramework = \" " ) ;
85115 }
86116
87- File . WriteAllLines ( taskItem . ItemSpec , contents ) ;
117+ foreach ( var mapping in newProjectToOldProjectMapping ) {
118+ string oldName = Path . GetFileName ( mapping . Key ) ;
119+ string newName = Path . GetFileName ( mapping . Value ) ;
120+ for ( int i = 0 ; i < contents . Length ; i ++ ) {
121+ contents [ i ] = Regex . Replace ( contents [ i ] , oldName , newName , RegexOptions . IgnoreCase ) ;
122+ }
123+ }
124+
125+ File . WriteAllLines ( newProjectToOldProjectMapping [ taskItem . ItemSpec ] , contents ) ;
126+ createdProjectFiles . Add ( new TaskItem ( taskItem ) { ItemSpec = newProjectToOldProjectMapping [ taskItem . ItemSpec ] } ) ;
88127 break ;
89128 default :
90129 this . Log . LogWarning ( "Unrecognized project type for \" {0}\" ." , taskItem . ItemSpec ) ;
91130 break ;
92131 }
93132 }
94133
134+ this . DowngradedProjects = createdProjectFiles . ToArray ( ) ;
135+
95136 return ! this . Log . HasLoggedErrors ;
96137 }
97138
0 commit comments