1
+ using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
4
+ using System . IO ;
5
+ using System . Linq ;
6
+ using Microsoft . Build . Framework ;
7
+ using Microsoft . Build . Utilities ;
8
+ using Newtonsoft . Json ;
9
+
10
+ namespace Microsoft . SourceIndexer . Tasks
11
+ {
12
+ public class LiveReferenceData
13
+ {
14
+ public string Project { get ; set ; }
15
+ public string TargetPath { get ; set ; }
16
+ }
17
+
18
+ public class GenerateLiveReferenceCache : Task
19
+ {
20
+ [ Required ]
21
+ public ITaskItem [ ] CandidateReferenceProjects { get ; set ; }
22
+
23
+ public string SetProperties { get ; set ; } = string . Empty ;
24
+
25
+ public string UndefineProperties { get ; set ; } = string . Empty ;
26
+
27
+ [ Required ]
28
+ public string LiveReferenceCacheFile { get ; set ; }
29
+
30
+ public override bool Execute ( )
31
+ {
32
+ try
33
+ {
34
+ return ExecuteCore ( ) ;
35
+ }
36
+ catch ( Exception ex )
37
+ {
38
+ LogException ( ex ) ;
39
+ return false ;
40
+ }
41
+ }
42
+
43
+ private void LogException ( Exception ex )
44
+ {
45
+ var agg = ex as AggregateException ;
46
+ if ( agg != null )
47
+ {
48
+ foreach ( var inner in agg . InnerExceptions )
49
+ {
50
+ LogException ( inner ) ;
51
+ }
52
+ }
53
+ else
54
+ {
55
+ Log . LogErrorFromException ( ex , true ) ;
56
+ }
57
+ }
58
+
59
+ private bool ExecuteCore ( )
60
+ {
61
+ var properties = new Dictionary < string , string > ( ) ;
62
+ foreach ( var prop in SetProperties . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) )
63
+ {
64
+ var key = prop . Substring ( 0 , prop . IndexOf ( '=' ) ) . Trim ( ) ;
65
+ var value = prop . Substring ( prop . IndexOf ( '=' ) + 1 ) . Trim ( ) ;
66
+ if ( ! string . IsNullOrEmpty ( key ) )
67
+ {
68
+ properties [ key ] = value ;
69
+ }
70
+ }
71
+ var projectFiles = CandidateReferenceProjects
72
+ . Select ( p => p . GetMetadata ( "FullPath" ) )
73
+ . ToArray ( ) ;
74
+ var propertyArray = new IDictionary [ projectFiles . Length ] ;
75
+ for ( int i = 0 ; i < projectFiles . Length ; i ++ )
76
+ {
77
+ propertyArray [ i ] = properties ;
78
+ }
79
+ var removePropertiesArray = Enumerable . Repeat ( ( IList < string > ) UndefineProperties . Split ( new [ ] { ';' } ) . Select ( p => p . Trim ( ) ) . Where ( s => ! string . IsNullOrEmpty ( s ) ) . ToArray ( ) , propertyArray . Length ) . ToArray ( ) ;
80
+ BuildEngineResult result = BuildEngine3 . BuildProjectFilesInParallel (
81
+ projectFiles ,
82
+ Enumerable . Repeat ( "GetTargetPath" , projectFiles . Length ) . ToArray ( ) ,
83
+ propertyArray ,
84
+ removePropertiesArray ,
85
+ new string [ projectFiles . Length ] ,
86
+ true
87
+ ) ;
88
+ if ( ! result . Result )
89
+ {
90
+ Log . LogError ( "Building 'GetTargetPath' Failed." ) ;
91
+ return false ;
92
+ }
93
+ var assemblyNameToProject = new Dictionary < string , LiveReferenceData > ( ) ;
94
+ for ( int i = 0 ; i < projectFiles . Length ; i ++ )
95
+ {
96
+ string projectFile = projectFiles [ i ] ;
97
+ IDictionary < string , ITaskItem [ ] > targetOutputs = result . TargetOutputsPerProject [ i ] ;
98
+ ITaskItem targetPath = targetOutputs [ "GetTargetPath" ] . First ( ) ;
99
+ string assemblyName = targetPath . GetMetadata ( "FileName" ) ;
100
+ assemblyNameToProject [ assemblyName ] = new LiveReferenceData
101
+ {
102
+ Project = projectFile ,
103
+ TargetPath = targetPath . GetMetadata ( "FullPath" )
104
+ } ;
105
+ }
106
+ File . WriteAllText ( LiveReferenceCacheFile , JsonConvert . SerializeObject ( assemblyNameToProject , Formatting . Indented ) ) ;
107
+ return true ;
108
+ }
109
+ }
110
+ }
0 commit comments