diff --git a/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabProjectViewModel.cs b/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabProjectViewModel.cs index ebcff3d4..68d56e22 100644 --- a/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabProjectViewModel.cs +++ b/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabProjectViewModel.cs @@ -52,9 +52,9 @@ public string GroupName } } - public IEnumerable DeployedVersions - { - get => this.prefabProject.AvailableVersions.Where(a => a.IsDeployed).ToList(); + public IEnumerable PublishedVersion + { + get => this.prefabProject.PublishedVersions; } public PrefabProjectViewModel(PrefabProject prefabProject) diff --git a/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionManagerViewModel.cs b/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionManagerViewModel.cs index 66698d1a..b312eb82 100644 --- a/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionManagerViewModel.cs +++ b/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionManagerViewModel.cs @@ -22,7 +22,7 @@ public class PrefabVersionManagerViewModel : Screen , IVersionManager private readonly PrefabProject prefabProject; private readonly ApplicationSettings applicationSettings; - private bool wasDeployed = false; + private bool wasPublished = false; public BindableCollection AvailableVersions { get; set; } = new BindableCollection(); @@ -30,7 +30,7 @@ public class PrefabVersionManagerViewModel : Screen , IVersionManager public PrefabVersionManagerViewModel(PrefabProject prefabProject, IWorkspaceManagerFactory workspaceManagerFactory, IReferenceManagerFactory referenceManagerFactory, ISerializer serializer, IApplicationDataManager applicationDataManager, ApplicationSettings applicationSettings) { - this.DisplayName = "Manage & Deploy Versions"; + this.DisplayName = "Manage Prefab Versions"; this.workspaceManagerFactory = Guard.Argument(workspaceManagerFactory, nameof(workspaceManagerFactory)).NotNull().Value; this.referenceManagerFactory = Guard.Argument(referenceManagerFactory, nameof(referenceManagerFactory)).NotNull().Value; this.serializer = Guard.Argument(serializer, nameof(serializer)).NotNull().Value; @@ -45,36 +45,70 @@ public PrefabVersionManagerViewModel(PrefabProject prefabProject, IWorkspaceMana } /// - /// Create a copy of selected version and deploy the selected version. + /// Create a copy of selected version and mark the selected version as published. /// /// - public async Task Deploy(PrefabVersionViewModel prefabVersionViewModel) + public async Task CloneAndPublishAsync(PrefabVersionViewModel prefabVersionViewModel) { try { - if (prefabVersionViewModel?.IsActive == true) + logger.Information($"Trying to clone version {prefabVersionViewModel.Version} for Prefab : {this.prefabProject.PrefabName}"); + + //Create a new active version from selected version + PrefabVersion newVersion = prefabVersionViewModel.Clone(); + IPrefabFileSystem fileSystem = new PrefabFileSystem(serializer, applicationSettings); + fileSystem.Initialize(this.prefabProject, newVersion); + + //Publish the selected version + if(!prefabVersionViewModel.IsPublished) { - //Create a new active version from selected version - PrefabVersion newVersion = prefabVersionViewModel.Clone(); + prefabVersionViewModel.Publish(this.workspaceManagerFactory); + logger.Information($"Version {prefabVersionViewModel.Version} for Prefab : {this.prefabProject.PrefabName} is published now."); + } - IPrefabFileSystem fileSystem = new PrefabFileSystem(serializer, applicationSettings); - fileSystem.Initialize(this.prefabProject, newVersion); + int indexToInsert = 0; + foreach (var version in this.prefabProject.AvailableVersions.Select(s => s.Version)) + { + if (version < newVersion.Version) + { + indexToInsert++; + continue; + } + break; + } - //Deploy the selected version - prefabVersionViewModel.Deploy(this.workspaceManagerFactory); + this.prefabProject.AvailableVersions.Insert(indexToInsert, newVersion); + serializer.Serialize(fileSystem.PrefabDescriptionFile, this.prefabProject); - this.prefabProject.AvailableVersions.Add(newVersion); - serializer.Serialize(fileSystem.PrefabDescriptionFile, this.prefabProject); + await this.applicationDataManager.AddOrUpdatePrefabAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsActive = false }); + await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsActive = false }); + await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, newVersion); - await this.applicationDataManager.AddOrUpdatePrefabAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsDeployed = true, IsActive = false }); - await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsDeployed = true, IsActive = false }); - await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, newVersion); - - this.AvailableVersions.Add(new PrefabVersionViewModel(this.prefabProject, newVersion, fileSystem, referenceManagerFactory)); + this.AvailableVersions.Insert(indexToInsert, new PrefabVersionViewModel(this.prefabProject, newVersion, fileSystem, referenceManagerFactory)); + this.wasPublished = true; - this.wasDeployed = true; - } + } + catch (Exception ex) + { + logger.Error(ex, ex.Message); + } + } + /// + /// Create a copy of selected version and mark the selected version as published. + /// + /// + public async Task PublishAsync(PrefabVersionViewModel prefabVersionViewModel) + { + try + { + if (prefabVersionViewModel.CanPublish) + { + prefabVersionViewModel.Publish(this.workspaceManagerFactory); + await this.applicationDataManager.AddOrUpdatePrefabAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsActive = false }); + await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsActive = false }); + logger.Information($"Version {prefabVersionViewModel.Version} for project : {this.prefabProject.PrefabName} is published now."); + } } catch (Exception ex) { @@ -84,7 +118,7 @@ public async Task Deploy(PrefabVersionViewModel prefabVersionViewModel) public async Task CloseAsync() { - await this.TryCloseAsync(this.wasDeployed); + await this.TryCloseAsync(this.wasPublished); } } } diff --git a/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionViewModel.cs b/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionViewModel.cs index df0bde6d..dd57c6c4 100644 --- a/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionViewModel.cs +++ b/src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionViewModel.cs @@ -5,6 +5,7 @@ using Pixel.Scripting.Editor.Core.Contracts; using Pixel.Scripting.Reference.Manager; using Pixel.Scripting.Reference.Manager.Contracts; +using Serilog; using System.IO; using System.Text.RegularExpressions; @@ -12,6 +13,8 @@ namespace Pixel.Automation.AppExplorer.ViewModels.Prefab { public class PrefabVersionViewModel : PropertyChangedBase { + private readonly ILogger logger = Log.ForContext(); + private readonly PrefabProject prefabProject; private readonly PrefabVersion prefabVersion; private readonly IPrefabFileSystem fileSystem; @@ -23,18 +26,9 @@ public Version Version set => prefabVersion.Version = value; } - public bool IsDeployed + public bool IsPublished { - get => prefabVersion.IsDeployed; - set - { - //can be only set to true when version is not already deployed - if (!prefabVersion.IsDeployed && value) - { - prefabVersion.IsDeployed = value; - } - NotifyOfPropertyChange(() => IsDeployed); - } + get => prefabVersion.IsPublished; } /// @@ -51,16 +45,19 @@ public bool IsActive prefabVersion.IsActive = value; } NotifyOfPropertyChange(() => IsActive); + NotifyOfPropertyChange(() => IsPublished); } } + public bool CanPublish { get; private set; } + public string PrefabAssembly { get => prefabVersion.DataModelAssembly; private set { - if (prefabVersion.IsDeployed && !string.IsNullOrEmpty(value)) + if (prefabVersion.IsPublished && !string.IsNullOrEmpty(value)) { prefabVersion.DataModelAssembly = value; } @@ -75,16 +72,28 @@ public PrefabVersionViewModel(PrefabProject prefabProject, PrefabVersion prefabV this.prefabVersion = Guard.Argument(prefabVersion, nameof(prefabVersion)).NotNull(); this.fileSystem = Guard.Argument(fileSystem).NotNull().Value; this.referenceManager = new Lazy(() => { return referenceManagerFactory.CreateForPrefabProject(prefabProject, prefabVersion); }); + this.CanPublish = !prefabVersion.IsPublished && !prefabVersion.Version.Equals(prefabProject.LatestActiveVersion.Version); } public PrefabVersion Clone() { - ////Increment active version for project - PrefabVersion newVersionInfo = new PrefabVersion(new Version(this.prefabVersion.Version.Major + 1, 0, 0, 0)) + PrefabVersion newVersionInfo; + if (!this.prefabProject.AvailableVersions.Any(a => a.Version.Major.Equals(this.prefabVersion.Version.Major + 1))) { - IsActive = true, - IsDeployed = false - }; + newVersionInfo = new PrefabVersion(new Version(this.prefabVersion.Version.Major + 1, 0, 0, 0)) + { + IsActive = true + }; + } + else + { + var versionsWithSameMajor = this.prefabProject.AvailableVersions.Select(s => s.Version).Where(v => v.Major.Equals(this.prefabVersion.Version.Major)); + int nextMinor = versionsWithSameMajor.Select(v => v.Minor).Max() + 1; + newVersionInfo = new PrefabVersion(new Version(this.prefabVersion.Version.Major, nextMinor, 0, 0)) + { + IsActive = true + }; + } this.fileSystem.Initialize(this.prefabProject, this.prefabVersion); var currentWorkingDirectory = new DirectoryInfo(this.fileSystem.WorkingDirectory); @@ -109,7 +118,7 @@ void CopyAll(DirectoryInfo source, DirectoryInfo target) CopyAll(diSourceSubDir, nextTargetSubDir); } } - + logger.Information($"Completed cloning version : {this.prefabVersion} of Prefab: {this.prefabProject.PrefabName}. Cloned version is : {newVersionInfo}"); return newVersionInfo; } @@ -118,45 +127,53 @@ void CopyAll(DirectoryInfo source, DirectoryInfo target) /// Copy last compiled dll from temp folder to References folder. /// Set IsDeployed to true and set the assembly name /// - public void Deploy(IWorkspaceManagerFactory workspaceFactory) + public void Publish(IWorkspaceManagerFactory workspaceFactory) { - - this.fileSystem.Initialize(this.prefabProject, this.prefabVersion); - - string prefabProjectName = this.prefabProject.PrefabName; - ICodeWorkspaceManager workspaceManager = workspaceFactory.CreateCodeWorkspaceManager(this.fileSystem.DataModelDirectory); - workspaceManager.WithAssemblyReferences(this.referenceManager.Value.GetCodeEditorReferences()); - workspaceManager.AddProject(prefabProjectName, this.prefabProject.Namespace, Array.Empty()); - string[] existingDataModelFiles = Directory.GetFiles(this.fileSystem.DataModelDirectory, "*.cs"); - if (existingDataModelFiles.Any()) + if(!this.IsPublished) { - //This will add all the documents to workspace so that they are available during compilation - foreach (var dataModelFile in existingDataModelFiles) + this.fileSystem.Initialize(this.prefabProject, this.prefabVersion); + logger.Information($"Prefab file system has been initialized."); + + string prefabProjectName = this.prefabProject.PrefabName; + ICodeWorkspaceManager workspaceManager = workspaceFactory.CreateCodeWorkspaceManager(this.fileSystem.DataModelDirectory); + workspaceManager.WithAssemblyReferences(this.referenceManager.Value.GetCodeEditorReferences()); + workspaceManager.AddProject(prefabProjectName, this.prefabProject.Namespace, Array.Empty()); + string[] existingDataModelFiles = Directory.GetFiles(this.fileSystem.DataModelDirectory, "*.cs"); + if (existingDataModelFiles.Any()) { - string documentName = Path.GetFileName(dataModelFile); - if (!workspaceManager.HasDocument(documentName, prefabProjectName)) + //This will add all the documents to workspace so that they are available during compilation + foreach (var dataModelFile in existingDataModelFiles) { - workspaceManager.AddDocument(documentName, prefabProjectName, File.ReadAllText(dataModelFile)); + string documentName = Path.GetFileName(dataModelFile); + if (!workspaceManager.HasDocument(documentName, prefabProjectName)) + { + workspaceManager.AddDocument(documentName, prefabProjectName, File.ReadAllText(dataModelFile)); + } } } - } - string assemblyName = this.prefabProject.Namespace; - using (var compilationResult = workspaceManager.CompileProject(prefabProjectName, assemblyName)) - { - compilationResult.SaveAssemblyToDisk(this.fileSystem.ReferencesDirectory); - } - this.IsDeployed = true; - this.IsActive = false; - this.PrefabAssembly = $"{assemblyName}.dll"; + string assemblyName = this.prefabProject.Namespace; + using (var compilationResult = workspaceManager.CompileProject(prefabProjectName, assemblyName)) + { + compilationResult.SaveAssemblyToDisk(this.fileSystem.ReferencesDirectory); + } + logger.Information($"Workspace prepared and compilation done."); - //Replace the assemly name in the process and template file - UpdateAssemblyReference(this.fileSystem.PrefabFile, assemblyName); - if(File.Exists(this.fileSystem.TemplateFile)) - { - UpdateAssemblyReference(this.fileSystem.TemplateFile, assemblyName); - } + this.IsActive = false; + this.CanPublish = false; + this.PrefabAssembly = $"{assemblyName}.dll"; + + //Replace the assemly name in the process and template file + UpdateAssemblyReference(this.fileSystem.PrefabFile, assemblyName); + if (File.Exists(this.fileSystem.TemplateFile)) + { + UpdateAssemblyReference(this.fileSystem.TemplateFile, assemblyName); + } + + logger.Information($"Assembly references updated in process and template files."); + NotifyOfPropertyChange(() => CanPublish); + } } /// diff --git a/src/Pixel.Automation.AppExplorer.ViewModels/PrefabDropHandler/PrefabVersionSelectorViewModel.cs b/src/Pixel.Automation.AppExplorer.ViewModels/PrefabDropHandler/PrefabVersionSelectorViewModel.cs index 36048416..8a2d74df 100644 --- a/src/Pixel.Automation.AppExplorer.ViewModels/PrefabDropHandler/PrefabVersionSelectorViewModel.cs +++ b/src/Pixel.Automation.AppExplorer.ViewModels/PrefabDropHandler/PrefabVersionSelectorViewModel.cs @@ -100,7 +100,7 @@ public PrefabVersionSelectorViewModel(IProjectFileSystem projectFileSystem, IPre { this.prefabReferences = new PrefabReferences(); } - this.AvailableVersions = prefabProject.DeployedVersions; + this.AvailableVersions = prefabProject.PublishedVersions; this.CanChangeVersion = !prefabReferences.HasReference(prefabProject); if(!this.CanChangeVersion) { diff --git a/src/Pixel.Automation.AppExplorer.Views/Prefab/PrefabVersionManagerView.xaml b/src/Pixel.Automation.AppExplorer.Views/Prefab/PrefabVersionManagerView.xaml index d824382f..f203b128 100644 --- a/src/Pixel.Automation.AppExplorer.Views/Prefab/PrefabVersionManagerView.xaml +++ b/src/Pixel.Automation.AppExplorer.Views/Prefab/PrefabVersionManagerView.xaml @@ -41,20 +41,28 @@ - + - + - + - -