8
8
using Java . Interop . Tools . TypeNameMappings ;
9
9
using Microsoft . Android . Build . Tasks ;
10
10
using Microsoft . Build . Framework ;
11
+ using Microsoft . Build . Utilities ;
11
12
using Mono . Cecil ;
12
13
using MonoDroid . Tuner ;
13
14
using Xamarin . Android . Tools ;
@@ -78,10 +79,6 @@ public override bool RunTask ()
78
79
ReadSymbols = ReadSymbols ,
79
80
} ;
80
81
81
- var writerParameters = new WriterParameters {
82
- DeterministicMvid = Deterministic ,
83
- } ;
84
-
85
82
Dictionary < AndroidTargetArch , Dictionary < string , ITaskItem > > perArchAssemblies = MonoAndroidHelper . GetPerArchAssemblies ( ResolvedAssemblies , Array . Empty < string > ( ) , validate : false ) ;
86
83
87
84
AssemblyPipeline ? pipeline = null ;
@@ -122,7 +119,7 @@ public override bool RunTask ()
122
119
123
120
Directory . CreateDirectory ( Path . GetDirectoryName ( destination . ItemSpec ) ) ;
124
121
125
- RunPipeline ( pipeline ! , source , destination , writerParameters ) ;
122
+ RunPipeline ( pipeline ! , source , destination ) ;
126
123
}
127
124
128
125
pipeline ? . Dispose ( ) ;
@@ -141,9 +138,26 @@ protected virtual void BuildPipeline (AssemblyPipeline pipeline, MSBuildLinkCont
141
138
142
139
findJavaObjectsStep . Initialize ( context ) ;
143
140
pipeline . Steps . Add ( findJavaObjectsStep ) ;
141
+
142
+ // SaveChangedAssemblyStep
143
+ var writerParameters = new WriterParameters {
144
+ DeterministicMvid = Deterministic ,
145
+ } ;
146
+
147
+ var saveChangedAssemblyStep = new SaveChangedAssemblyStep ( Log , writerParameters ) ;
148
+ pipeline . Steps . Add ( saveChangedAssemblyStep ) ;
149
+
150
+ // FindTypeMapObjectsStep - this must be run after the assembly has been saved, as saving changes the MVID
151
+ var findTypeMapObjectsStep = new FindTypeMapObjectsStep ( Log ) {
152
+ ErrorOnCustomJavaObject = ErrorOnCustomJavaObject ,
153
+ Debug = Debug ,
154
+ } ;
155
+
156
+ findTypeMapObjectsStep . Initialize ( context ) ;
157
+ pipeline . Steps . Add ( findTypeMapObjectsStep ) ;
144
158
}
145
159
146
- void RunPipeline ( AssemblyPipeline pipeline , ITaskItem source , ITaskItem destination , WriterParameters writerParameters )
160
+ void RunPipeline ( AssemblyPipeline pipeline , ITaskItem source , ITaskItem destination )
147
161
{
148
162
var assembly = pipeline . Resolver . GetAssembly ( source . ItemSpec ) ;
149
163
@@ -157,28 +171,47 @@ void RunPipeline (AssemblyPipeline pipeline, ITaskItem source, ITaskItem destina
157
171
IsUserAssembly = ResolvedUserAssemblies . Any ( a => a . ItemSpec == source . ItemSpec ) ,
158
172
} ;
159
173
160
- var changed = pipeline . Run ( assembly , context ) ;
161
-
162
- if ( changed ) {
163
- Log . LogDebugMessage ( $ "Saving modified assembly: { destination . ItemSpec } ") ;
164
- Directory . CreateDirectory ( Path . GetDirectoryName ( destination . ItemSpec ) ) ;
165
- writerParameters . WriteSymbols = assembly . MainModule . HasSymbols ;
166
- assembly . Write ( destination . ItemSpec , writerParameters ) ;
167
- } else {
168
- // If we didn't write a modified file, copy the original to the destination
169
- CopyIfChanged ( source , destination ) ;
170
- }
174
+ pipeline . Run ( assembly , context ) ;
171
175
}
172
176
173
- void CopyIfChanged ( ITaskItem source , ITaskItem destination )
177
+ class SaveChangedAssemblyStep : IAssemblyModifierPipelineStep
174
178
{
175
- if ( MonoAndroidHelper . CopyAssemblyAndSymbols ( source . ItemSpec , destination . ItemSpec ) ) {
176
- Log . LogDebugMessage ( $ "Copied: { destination . ItemSpec } ") ;
177
- } else {
178
- Log . LogDebugMessage ( $ "Skipped unchanged file: { destination . ItemSpec } ") ;
179
+ public TaskLoggingHelper Log { get ; set ; }
180
+
181
+ public WriterParameters WriterParameters { get ; set ; }
182
+
183
+ public SaveChangedAssemblyStep ( TaskLoggingHelper log , WriterParameters writerParameters )
184
+ {
185
+ Log = log ;
186
+ WriterParameters = writerParameters ;
187
+ }
179
188
180
- // NOTE: We still need to update the timestamp on this file, or this target would run again
181
- File . SetLastWriteTimeUtc ( destination . ItemSpec , DateTime . UtcNow ) ;
189
+ public void ProcessAssembly ( AssemblyDefinition assembly , StepContext context )
190
+ {
191
+ if ( context . IsAssemblyModified ) {
192
+ Log . LogDebugMessage ( $ "Saving modified assembly: { context . Destination . ItemSpec } ") ;
193
+ Directory . CreateDirectory ( Path . GetDirectoryName ( context . Destination . ItemSpec ) ) ;
194
+ WriterParameters . WriteSymbols = assembly . MainModule . HasSymbols ;
195
+ assembly . Write ( context . Destination . ItemSpec , WriterParameters ) ;
196
+ } else {
197
+ // If we didn't write a modified file, copy the original to the destination
198
+ CopyIfChanged ( context . Source , context . Destination ) ;
199
+ }
200
+
201
+ // We just saved the assembly, so it is no longer modified
202
+ context . IsAssemblyModified = false ;
203
+ }
204
+
205
+ void CopyIfChanged ( ITaskItem source , ITaskItem destination )
206
+ {
207
+ if ( MonoAndroidHelper . CopyAssemblyAndSymbols ( source . ItemSpec , destination . ItemSpec ) ) {
208
+ Log . LogDebugMessage ( $ "Copied: { destination . ItemSpec } ") ;
209
+ } else {
210
+ Log . LogDebugMessage ( $ "Skipped unchanged file: { destination . ItemSpec } ") ;
211
+
212
+ // NOTE: We still need to update the timestamp on this file, or this target would run again
213
+ File . SetLastWriteTimeUtc ( destination . ItemSpec , DateTime . UtcNow ) ;
214
+ }
182
215
}
183
216
}
184
217
}
0 commit comments