-
Notifications
You must be signed in to change notification settings - Fork 551
[xma] Added targets and tasks to copy modified dll and pdb files back to Windows #22677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
051aaae
Fixed Zip task to not copy inputs remotely and also to not create emp…
mauroa 7d80423
[xma] Added targets and tasks to copy modified dll and pdb files back…
mauroa 477104c
[xma][test] Augment RemoteTest to include validations over the output…
mauroa 45b11cb
Auto-format source code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
msbuild/Xamarin.MacDev.Tasks/Tasks/AnalyzeFileChanges.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Reflection.Metadata; | ||
| using System.Reflection.PortableExecutable; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using Xamarin.Localization.MSBuild; | ||
| using Xamarin.Messaging.Build.Client; | ||
|
|
||
| namespace Xamarin.MacDev.Tasks { | ||
| public class AnalyzeFileChanges : XamarinTask, ITaskCallback { | ||
| [Required] | ||
| public string WorkingDirectory { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public ITaskItem? ReportFile { get; set; } | ||
|
|
||
| [Output] | ||
| public ITaskItem [] ChangedFiles { get; set; } = []; | ||
|
|
||
| public IEnumerable<ITaskItem> GetAdditionalItemsToBeCopied () => []; | ||
|
|
||
| //We need the ReportFile input to be copied to the Mac. Since it's the only ITaskItem input, it's safe to return true | ||
| public bool ShouldCopyToBuildServer (ITaskItem item) => true; | ||
|
|
||
| //In case it's a remote execution, we don't want empty output files to be copied since we need the real files to be copied | ||
| public bool ShouldCreateOutputFile (ITaskItem item) => false; | ||
|
|
||
| public override bool Execute () | ||
| { | ||
| if (ShouldExecuteRemotely ()) { | ||
| return new TaskRunner (SessionId, BuildEngine4).RunAsync (this).Result; | ||
| } | ||
|
|
||
| if (!Directory.Exists (WorkingDirectory)) { | ||
| Log.LogError (MSBStrings.E7146 /* Unable to analyze file changes in '{0}'. Directory not found. */, WorkingDirectory); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| if (!File.Exists (ReportFile!.ItemSpec)) { | ||
| Log.LogError (MSBStrings.E7147 /* Unable to analyze file changes in '{0}'. The report file '{1}' does not exist. */, WorkingDirectory, ReportFile.ItemSpec); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| var changedFiles = new List<ITaskItem> (); | ||
| //Gets a dictionary of file names, lengths and MVIDs from the ReportFile | ||
| IDictionary<string, (long length, Guid mvid)> reportFileList = GetReportFileList (); | ||
| IEnumerable<string> files = Directory.GetFiles (WorkingDirectory, "*.dll", SearchOption.TopDirectoryOnly); | ||
|
|
||
| foreach (string file in files) { | ||
| //If there is a new assembly in the remote side not present in the report file, we register it for copying back | ||
| if (!reportFileList.TryGetValue (Path.GetFileName (file), out (long length, Guid mvid) localInfo)) { | ||
| changedFiles.Add (new TaskItem (file)); | ||
| TryAddPdbFile (file, changedFiles); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| var fileInfo = new FileInfo (file); | ||
|
|
||
| //If the file lengths differ, it means local and remote versions are different | ||
| if (fileInfo.Length != localInfo.length) { | ||
| changedFiles.Add (new TaskItem (file)); | ||
| TryAddPdbFile (file, changedFiles); | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| using Stream stream = fileInfo.OpenRead (); | ||
| using var peReader = new PEReader (stream); | ||
| MetadataReader metadataReader = peReader.GetMetadataReader (); | ||
| Guid mvid = metadataReader.GetGuid (metadataReader.GetModuleDefinition ().Mvid); | ||
|
|
||
| //If the MVID from the report file (local MVID) is different than the calculated MVID of the file, it means local and remote versions are different | ||
| if (mvid != localInfo.mvid) { | ||
| changedFiles.Add (new TaskItem (file)); | ||
| TryAddPdbFile (file, changedFiles); | ||
| } | ||
| } | ||
|
|
||
| ChangedFiles = [.. changedFiles]; | ||
|
|
||
| return true; | ||
| } catch (Exception ex) { | ||
| Log.LogError (MSBStrings.E7148 /* Unable to analyze file changes in '{0}'. An unexpected error occurred. */, WorkingDirectory); | ||
| Log.LogErrorFromException (ex); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| IDictionary<string, (long length, Guid mvid)> GetReportFileList () | ||
| { | ||
| var reportFileList = new Dictionary<string, (long length, Guid mvid)> (); | ||
|
|
||
| //Expected format of the report file lines (defined in the CalculateAssembliesReport task): Foo.dll/23189/768C814C-05C3-4563-9B53-35FEF571968E | ||
| foreach (var line in File.ReadLines (ReportFile!.ItemSpec)) { | ||
| string [] lineParts = line.Split (['/'], StringSplitOptions.RemoveEmptyEntries); | ||
|
|
||
| // Skip lines that don't match the expected format | ||
| if (lineParts.Length == 3 && long.TryParse (lineParts [1], out long fileLength) && Guid.TryParse (lineParts [2], out Guid mvid)) { | ||
| // Adds file name, length and MVID to the dictionary | ||
| reportFileList.Add (lineParts [0], (fileLength, mvid)); | ||
| } | ||
| } | ||
|
|
||
| return reportFileList; | ||
| } | ||
|
|
||
| bool TryAddPdbFile (string file, List<ITaskItem> changedFiles) | ||
| { | ||
| var pdbFile = Path.ChangeExtension (file, ".pdb"); | ||
|
|
||
| if (!File.Exists (pdbFile)) { | ||
| return false; | ||
| } | ||
|
|
||
| changedFiles.Add (new TaskItem (pdbFile)); | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
msbuild/Xamarin.MacDev.Tasks/Tasks/CalculateAssembliesReport.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Reflection.Metadata; | ||
| using System.Reflection.PortableExecutable; | ||
| using System.Text; | ||
| using Microsoft.Build.Framework; | ||
| using Xamarin.Localization.MSBuild; | ||
| using Xamarin.Messaging.Build.Client; | ||
|
|
||
| namespace Xamarin.MacDev.Tasks { | ||
| public class CalculateAssembliesReport : XamarinTask { | ||
| [Required] | ||
| public string WorkingDirectory { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string TargetReportFile { get; set; } = string.Empty; | ||
|
|
||
| public override bool Execute () | ||
| { | ||
| if (ShouldExecuteRemotely ()) { | ||
| return new TaskRunner (SessionId, BuildEngine4).RunAsync (this).Result; | ||
| } | ||
|
|
||
| if (!Directory.Exists (WorkingDirectory)) { | ||
| Log.LogError (MSBStrings.E7149 /* Unable to calculate the assemblies report from '{0}'. Directory not found. */, WorkingDirectory); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| try { | ||
| var reportEntriesBuilder = new StringBuilder (); | ||
| IEnumerable<string> files = Directory.GetFiles (WorkingDirectory, "*.dll", SearchOption.TopDirectoryOnly); | ||
|
|
||
| foreach (var file in files) { | ||
| try { | ||
| var fileInfo = new FileInfo (file); | ||
| using Stream stream = fileInfo.OpenRead (); | ||
| using var peReader = new PEReader (stream); | ||
| MetadataReader metadataReader = peReader.GetMetadataReader (); | ||
| Guid mvid = metadataReader.GetGuid (metadataReader.GetModuleDefinition ().Mvid); | ||
|
|
||
| //Appending the file name, length and mvid like: Foo.dll/23189/768C814C-05C3-4563-9B53-35FEF571968E | ||
| reportEntriesBuilder.AppendLine ($"{Path.GetFileName (file)}/{fileInfo.Length}/{mvid}"); | ||
| } catch (Exception) { | ||
| Log.LogWarning (MSBStrings.W7145 /* Unable to retrieve information from '{0}'. The file may not be a valid PE file."\ */, Path.GetFileName (file)); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| //Creates or overwrites the report file | ||
| File.WriteAllText (TargetReportFile, reportEntriesBuilder.ToString ()); | ||
|
|
||
| return true; | ||
| } catch (Exception ex) { | ||
| Log.LogError (MSBStrings.E7150 /* Unable to calculate assemblies report from '{0}'. An unexpected error occurred. */, WorkingDirectory); | ||
| Log.LogErrorFromException (ex); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.