From 820f7d7ce5d694ca33d87314911455b9bfc8db66 Mon Sep 17 00:00:00 2001 From: Rustam Sayfutdinov Date: Sat, 29 May 2021 09:04:49 +0300 Subject: [PATCH] Fix --- .../PreviewInfoResolverTask.cs | 163 +++++++++--------- 1 file changed, 77 insertions(+), 86 deletions(-) diff --git a/src/AvaloniaProjectInfoResolver.PreviewTask/PreviewInfoResolverTask.cs b/src/AvaloniaProjectInfoResolver.PreviewTask/PreviewInfoResolverTask.cs index 44cbfa6..9a53c84 100644 --- a/src/AvaloniaProjectInfoResolver.PreviewTask/PreviewInfoResolverTask.cs +++ b/src/AvaloniaProjectInfoResolver.PreviewTask/PreviewInfoResolverTask.cs @@ -88,19 +88,16 @@ public PreviewInfoResolverTask() protected override bool ExecuteInner() { - var isSuccess = TryResolvePreviewInfoTfms(out var targetFrameworks); - if (!isSuccess) + if (!TryResolvePreviewInfoTfms(ProjectFile, out var targetFrameworks)) return false; - isSuccess = TryResolvePreviewInfoCommon(out var previewInfo, targetFrameworks); - if (!isSuccess) + if (!TryResolvePreviewInfoCommon(out var previewInfo, targetFrameworks)) return false; var appExecInfoCollection = new List(targetFrameworks.Length); foreach (var tfm in targetFrameworks) { - isSuccess = TryResolveAppExecInfo(out var appExecInfo, tfm); - if (!isSuccess) + if (!TryResolveAppExecInfo(out var appExecInfo, tfm)) return false; appExecInfoCollection.Add(appExecInfo); } @@ -110,65 +107,45 @@ protected override bool ExecuteInner() return true; } - private bool IsReferencesAvalonia(PreviewInfo previewInfo) + private bool TryResolvePreviewInfoTfms(string projectFile, out string[] targetFrameworks) { - var isPreviewContains = !string.IsNullOrEmpty(previewInfo.AvaloniaPreviewerNetCoreToolPath) - || !string.IsNullOrEmpty(previewInfo.AvaloniaPreviewerNetFullToolPath); - if (isPreviewContains) - return true; - - BuildEngine.LogErrorEvent(new BuildErrorEventArgs( - "APIR", - string.Empty, - ProjectFile, - 0, - 0, - 0, - 0, - "MSBuild project file does not reference AvaloniaUI", - string.Empty, - string.Empty)); - - return false; - } - - private bool TryResolvePreviewInfoTfms(out string[] targetFrameworks) - { - targetFrameworks = default!; var targetOutputs = new Dictionary(); - var isSuccess = BuildEngine.BuildProjectFile( - ProjectFile, TargetGetTfms, _globalPropertiesCommon, targetOutputs); - if (!isSuccess) - return false; - - targetFrameworks = targetOutputs.ResultFromArray(SelectInfoTargetFrameworks); + if (BuildEngine.BuildProjectFile(projectFile, TargetGetTfms, _globalPropertiesCommon, targetOutputs)) + { + targetFrameworks = targetOutputs.ResultFromArray(SelectInfoTargetFrameworks); + return true; + } - return true; + targetFrameworks = default!; + return false; } private bool TryResolvePreviewInfoCommon(out PreviewInfo previewInfo, string[] targetFrameworks) { - previewInfo = default!; var targetOutputs = new Dictionary(); var props = GetGlobalProperties(targetFrameworks[0]); - var isSuccess = BuildEngine.BuildProjectFile(ProjectFile, TargetNamesCommon, props, targetOutputs); - if (!isSuccess) + if (!BuildEngine.BuildProjectFile(ProjectFile, TargetNamesCommon, props, targetOutputs)) + { + previewInfo = default!; return false; + } previewInfo = SelectPreviewInfoCommon(targetOutputs); - if (!IsReferencesAvalonia(previewInfo)) + if (IsReferencesAvalonia(previewInfo)) { - previewInfo = default!; - return false; + var xamlFileInfoCollection = ResolveReferenceXamlFileInfoCollection( + previewInfo.XamlFileInfo.ProjectPath, props); + previewInfo.XamlFileInfo.ReferenceXamlFileInfoCollection = xamlFileInfoCollection; + + return true; } - var xamlFileInfoCollection = ResolveReferenceXamlFileInfoCollection( - previewInfo.XamlFileInfo.ProjectPath, props); - previewInfo.XamlFileInfo.ReferenceXamlFileInfoCollection = xamlFileInfoCollection; + LogProjectNotAvalonia(); - return true; + previewInfo = default!; + return false; } private List ResolveReferenceXamlFileInfoCollection( @@ -176,22 +153,22 @@ private List ResolveReferenceXamlFileInfoCollection( { var targetOutputs = new Dictionary(); var xamlFileInfoCollection = new List(); - var isSuccess = BuildEngine.BuildProjectFile( - parentProjectPath, TargetGetProjectReference, props, targetOutputs); - if (isSuccess) + if (BuildEngine.BuildProjectFile(parentProjectPath, TargetGetProjectReference, props, targetOutputs)) { var currentProjectDirectory = Directory.GetParent(parentProjectPath)!.FullName; var projectReferenceCollection = targetOutputs.ResultFromArray(SelectInfoProjectReference); - foreach (var project in projectReferenceCollection) + if (!string.IsNullOrEmpty(projectReferenceCollection[0])) { - if (string.IsNullOrEmpty(project)) - continue; - - var path = Path.Combine(currentProjectDirectory, project); - isSuccess = TryResolvePreviewInfoCommonProjectReference(path, out var xamlFileInfo); - if (isSuccess) - xamlFileInfoCollection.Add(xamlFileInfo); + foreach (var project in projectReferenceCollection) + { + if (string.IsNullOrEmpty(project)) + continue; + + var path = Path.Combine(currentProjectDirectory, project); + if (TryResolvePreviewInfoCommonProjectReference(path, out var xamlFileInfo)) + xamlFileInfoCollection.Add(xamlFileInfo); + } } } @@ -200,52 +177,66 @@ private List ResolveReferenceXamlFileInfoCollection( private bool TryResolvePreviewInfoCommonProjectReference(string projectReference, out XamlFileInfo xamlFileInfo) { - xamlFileInfo = default!; - - var isSuccess = TryResolvePreviewInfoTfms(out var targetFrameworks); - if (!isSuccess) + if (!TryResolvePreviewInfoTfms(projectReference, out var targetFrameworks)) + { + xamlFileInfo = default!; return false; + } var targetOutputs = new Dictionary(); var props = GetGlobalProperties(targetFrameworks[0]); - isSuccess = BuildEngine.BuildProjectFile(projectReference, TargetNamesCommon, props, targetOutputs); - if (!isSuccess) - return false; - - var projectPath = targetOutputs.ResultFromSingle(SelectInfoProjectPath); - // The project can be build, but in reality it cannot - if (string.IsNullOrEmpty(projectPath)) - return false; - - xamlFileInfo = new XamlFileInfo + if (BuildEngine.BuildProjectFile(projectReference, TargetNamesCommon, props, targetOutputs)) { - ProjectPath = projectPath, - AvaloniaResource = targetOutputs.ResultFromArrayAsSingleSkipNonXaml(SelectInfoAvaloniaResource), - AvaloniaXaml = targetOutputs.ResultFromArrayAsSingle(SelectInfoAvaloniaXaml), - }; + xamlFileInfo = new XamlFileInfo + { + ProjectPath = targetOutputs.ResultFromSingle(SelectInfoProjectPath), + AvaloniaResource = targetOutputs.ResultFromArrayAsSingleSkipNonXaml(SelectInfoAvaloniaResource), + AvaloniaXaml = targetOutputs.ResultFromArrayAsSingle(SelectInfoAvaloniaXaml), + }; - xamlFileInfo.ReferenceXamlFileInfoCollection = ResolveReferenceXamlFileInfoCollection( - xamlFileInfo.ProjectPath, props); + xamlFileInfo.ReferenceXamlFileInfoCollection = ResolveReferenceXamlFileInfoCollection( + xamlFileInfo.ProjectPath, props); - return true; + return true; + } + + xamlFileInfo = default!; + return false; } private bool TryResolveAppExecInfo(out AppExecInfo appExecInfo, string targetFramework = "") { - appExecInfo = default!; var targetOutputs = new Dictionary(); var props = GetGlobalProperties(targetFramework); - var isSuccess = BuildEngine.BuildProjectFile(ProjectFile, TargetNamesAppExecInfo, props, targetOutputs); - if (!isSuccess) - return false; - - appExecInfo = SelectAppExecInfo(targetOutputs); + if (BuildEngine.BuildProjectFile(ProjectFile, TargetNamesAppExecInfo, props, targetOutputs)) + { + appExecInfo = SelectAppExecInfo(targetOutputs); + return true; + } - return true; + appExecInfo = default!; + return false; } + private static bool IsReferencesAvalonia(PreviewInfo previewInfo) => + !string.IsNullOrEmpty(previewInfo.AvaloniaPreviewerNetCoreToolPath) + || !string.IsNullOrEmpty(previewInfo.AvaloniaPreviewerNetFullToolPath); + + private void LogProjectNotAvalonia() => + BuildEngine.LogErrorEvent(new BuildErrorEventArgs( + "APIR", + string.Empty, + ProjectFile, + 0, + 0, + 0, + 0, + "MSBuild project file does not reference AvaloniaUI", + string.Empty, + string.Empty)); + private Dictionary GetGlobalProperties(string targetFramework) => string.IsNullOrEmpty(targetFramework) ? _globalPropertiesCommon