Skip to content

Commit 8fb6d81

Browse files
authored
Merge pull request #188 from Nfactor26/simplify-clone-and-publish
Clone and Publish are separate actions now for project and prefab manager
2 parents 6349f21 + c5998ce commit 8fb6d81

File tree

10 files changed

+189
-116
lines changed

10 files changed

+189
-116
lines changed

src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionManagerViewModel.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,20 @@ public class PrefabVersionManagerViewModel : Screen , IVersionManager
2222
private readonly PrefabProject prefabProject;
2323
private readonly ApplicationSettings applicationSettings;
2424

25+
/// <summary>
26+
/// Available version of Prefab
27+
/// </summary>
2528
public BindableCollection<PrefabVersionViewModel> AvailableVersions { get; set; } = new BindableCollection<PrefabVersionViewModel>();
2629

27-
30+
/// <summary>
31+
/// constructor
32+
/// </summary>
33+
/// <param name="prefabProject"></param>
34+
/// <param name="workspaceManagerFactory"></param>
35+
/// <param name="referenceManagerFactory"></param>
36+
/// <param name="serializer"></param>
37+
/// <param name="prefabDataManager"></param>
38+
/// <param name="applicationSettings"></param>
2839
public PrefabVersionManagerViewModel(PrefabProject prefabProject, IWorkspaceManagerFactory workspaceManagerFactory,
2940
IReferenceManagerFactory referenceManagerFactory, ISerializer serializer,
3041
IPrefabDataManager prefabDataManager, ApplicationSettings applicationSettings)
@@ -44,29 +55,23 @@ public PrefabVersionManagerViewModel(PrefabProject prefabProject, IWorkspaceMana
4455
}
4556

4657
/// <summary>
47-
/// Create a copy of selected version and mark the selected version as published.
58+
/// Set the state of version as published.
4859
/// </summary>
4960
/// <returns></returns>
50-
public async Task CloneAndPublishAsync(PrefabVersionViewModel prefabVersionViewModel)
61+
public async Task PublishAsync(PrefabVersionViewModel prefabVersionViewModel)
5162
{
5263
try
53-
{
54-
logger.Information($"Trying to clone version {prefabVersionViewModel.Version} for Prefab : {this.prefabProject.PrefabName}");
55-
56-
//Create a new active version from selected version
57-
PrefabVersion newVersion = await prefabVersionViewModel.CloneAsync(this.prefabDataManager);
58-
IPrefabFileSystem fileSystem = new PrefabFileSystem(serializer, applicationSettings);
59-
fileSystem.Initialize(this.prefabProject, newVersion);
60-
61-
//After cloning, we mark the curret version as published
64+
{
6265
if (!prefabVersionViewModel.IsPublished)
63-
{
66+
{
67+
bool isLatestActieVersion = prefabProject.LatestActiveVersion.Version.Equals(prefabVersionViewModel.Version);
6468
await prefabVersionViewModel.PublishAsync(this.workspaceManagerFactory, this.prefabDataManager);
65-
logger.Information($"Version {prefabVersionViewModel.Version} for Prefab : {this.prefabProject.PrefabName} is published now.");
66-
}
67-
68-
this.AvailableVersions.Add(new PrefabVersionViewModel(this.prefabProject, newVersion, fileSystem, referenceManagerFactory));
69-
69+
logger.Information("Version {0} for project : {1} is published now.", prefabVersionViewModel.Version, this.prefabProject.PrefabName);
70+
if (isLatestActieVersion)
71+
{
72+
await CloneAsync(prefabVersionViewModel);
73+
}
74+
}
7075
}
7176
catch (Exception ex)
7277
{
@@ -75,17 +80,20 @@ public async Task CloneAndPublishAsync(PrefabVersionViewModel prefabVersionViewM
7580
}
7681

7782
/// <summary>
78-
/// Create a copy of selected version and mark the selected version as published.
83+
/// Create a copy of selected version. Only published versions can be cloned.
7984
/// </summary>
8085
/// <returns></returns>
81-
public async Task PublishAsync(PrefabVersionViewModel prefabVersionViewModel)
86+
public async Task CloneAsync(PrefabVersionViewModel prefabVersionViewModel)
8287
{
8388
try
8489
{
85-
if (prefabVersionViewModel.CanPublish)
86-
{
87-
await prefabVersionViewModel.PublishAsync(this.workspaceManagerFactory, this.prefabDataManager);
88-
logger.Information($"Version {prefabVersionViewModel.Version} for project : {this.prefabProject.PrefabName} is published now.");
90+
if (prefabVersionViewModel.IsPublished)
91+
{
92+
PrefabVersion newVersion = await prefabVersionViewModel.CloneAsync(this.prefabDataManager);
93+
IPrefabFileSystem fileSystem = new PrefabFileSystem(serializer, applicationSettings);
94+
fileSystem.Initialize(this.prefabProject, newVersion);
95+
this.AvailableVersions.Add(new PrefabVersionViewModel(this.prefabProject, newVersion, fileSystem, referenceManagerFactory));
96+
logger.Information("Version {0} for project : {1} is cloned from {2}.", newVersion.Version, this.prefabProject.PrefabName, prefabVersionViewModel.Version);
8997
}
9098
}
9199
catch (Exception ex)
@@ -94,6 +102,10 @@ public async Task PublishAsync(PrefabVersionViewModel prefabVersionViewModel)
94102
}
95103
}
96104

105+
/// <summary>
106+
/// Close Prefab version manager screen
107+
/// </summary>
108+
/// <returns></returns>
97109
public async Task CloseAsync()
98110
{
99111
await this.TryCloseAsync(false);

src/Pixel.Automation.AppExplorer.ViewModels/Prefab/PrefabVersionViewModel.cs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class PrefabVersionViewModel : PropertyChangedBase
2323
public Version Version
2424
{
2525
get => prefabVersion.Version;
26-
set => prefabVersion.Version = value;
2726
}
2827

2928
public bool IsPublished
@@ -49,8 +48,16 @@ public bool IsActive
4948
}
5049
}
5150

52-
public bool CanPublish { get; private set; }
51+
public bool CanPublish
52+
{
53+
get => !this.IsPublished;
54+
}
55+
5356

57+
public bool CanClone
58+
{
59+
get => this.IsPublished;
60+
}
5461

5562
public string PrefabAssembly
5663
{
@@ -71,8 +78,7 @@ public PrefabVersionViewModel(PrefabProject prefabProject, PrefabVersion prefabV
7178
this.prefabProject = Guard.Argument(prefabProject, nameof(prefabProject)).NotNull();
7279
this.prefabVersion = Guard.Argument(prefabVersion, nameof(prefabVersion)).NotNull();
7380
this.fileSystem = Guard.Argument(fileSystem).NotNull().Value;
74-
this.referenceManager = new Lazy<IReferenceManager>(() => { return referenceManagerFactory.CreateReferenceManager( this.prefabProject.PrefabId, this.prefabVersion.ToString(), this.fileSystem); });
75-
this.CanPublish = !prefabVersion.IsPublished && !prefabVersion.Version.Equals(prefabProject.LatestActiveVersion.Version);
81+
this.referenceManager = new Lazy<IReferenceManager>(() => { return referenceManagerFactory.CreateReferenceManager( this.prefabProject.PrefabId, this.prefabVersion.ToString(), this.fileSystem); });
7682
}
7783

7884
public async Task<PrefabVersion> CloneAsync(IPrefabDataManager prefabDataManager)
@@ -93,10 +99,7 @@ public async Task<PrefabVersion> CloneAsync(IPrefabDataManager prefabDataManage
9399
{
94100
IsActive = true
95101
};
96-
}
97-
98-
//Download the prefab data given it might not be locally available and then add new version by cloning from current version
99-
await prefabDataManager.DownloadPrefabDataAsync(this.prefabProject, this.prefabVersion);
102+
}
100103
await prefabDataManager.AddPrefabVersionAsync(this.prefabProject, newVersion, this.prefabVersion);
101104
return newVersion;
102105
}
@@ -110,36 +113,39 @@ public async Task PublishAsync(IWorkspaceManagerFactory workspaceFactory, IPrefa
110113
{
111114
if(!this.IsPublished)
112115
{
113-
this.fileSystem.Initialize(this.prefabProject, this.prefabVersion);
114-
logger.Information($"Prefab file system has been initialized.");
116+
//Download the prefab data given it might not be locally available
117+
await prefabDataManager.DownloadPrefabDataAsync(this.prefabProject, this.prefabVersion);
115118

119+
this.fileSystem.Initialize(this.prefabProject, this.prefabVersion);
116120
string prefabProjectName = this.prefabProject.PrefabName;
117121
ICodeWorkspaceManager workspaceManager = workspaceFactory.CreateCodeWorkspaceManager(this.fileSystem.DataModelDirectory);
118122
workspaceManager.WithAssemblyReferences(this.referenceManager.Value.GetCodeEditorReferences());
119123
workspaceManager.AddProject(prefabProjectName, this.prefabProject.Namespace, Array.Empty<string>());
124+
120125
string[] existingDataModelFiles = Directory.GetFiles(this.fileSystem.DataModelDirectory, "*.cs");
121-
if (existingDataModelFiles.Any())
126+
if (!existingDataModelFiles.Any())
127+
{
128+
throw new FileNotFoundException($"Missing data model files in path : {this.fileSystem.DataModelDirectory}");
129+
}
130+
131+
//This will add all the documents to workspace so that they are available during compilation
132+
foreach (var dataModelFile in existingDataModelFiles)
122133
{
123-
//This will add all the documents to workspace so that they are available during compilation
124-
foreach (var dataModelFile in existingDataModelFiles)
134+
string documentName = Path.GetFileName(dataModelFile);
135+
if (!workspaceManager.HasDocument(documentName, prefabProjectName))
125136
{
126-
string documentName = Path.GetFileName(dataModelFile);
127-
if (!workspaceManager.HasDocument(documentName, prefabProjectName))
128-
{
129-
workspaceManager.AddDocument(documentName, prefabProjectName, File.ReadAllText(dataModelFile));
130-
}
137+
workspaceManager.AddDocument(documentName, prefabProjectName, File.ReadAllText(dataModelFile));
131138
}
132139
}
133140

134-
string assemblyName = $"{this.prefabProject.Namespace}.v{Version.Major}";
141+
string assemblyName = $"{this.prefabProject.Namespace}.v{Version.Major}.{Version.Minor}";
135142
using (var compilationResult = workspaceManager.CompileProject(prefabProjectName, assemblyName))
136143
{
137144
compilationResult.SaveAssemblyToDisk(this.fileSystem.ReferencesDirectory);
138145
}
139146
logger.Information($"Workspace prepared and compilation done.");
140147

141-
this.IsActive = false;
142-
this.CanPublish = false;
148+
this.IsActive = false;
143149
this.PrefabAssembly = $"{assemblyName}.dll";
144150

145151
logger.Information($"Data model assembly name is : {this.PrefabAssembly}");
@@ -159,6 +165,7 @@ public async Task PublishAsync(IWorkspaceManagerFactory workspaceFactory, IPrefa
159165
await prefabDataManager.SavePrefabDataAsync(this.prefabProject, this.prefabVersion);
160166

161167
NotifyOfPropertyChange(() => CanPublish);
168+
NotifyOfPropertyChange(() => CanClone);
162169
}
163170
}
164171

src/Pixel.Automation.AppExplorer.Views/Prefab/PrefabVersionManagerView.xaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,19 @@
5151
<DataGridTemplateColumn Header="">
5252
<DataGridTemplateColumn.CellTemplate>
5353
<DataTemplate>
54-
<Button x:Name="CloneAndPublish" Content="Clone and Publish" cal:Action.TargetWithoutContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
55-
cal:Message.Attach="[Event Click] = [Action CloneAndPublishAsync($dataContext)]" Margin="5,2,5,2" HorizontalAlignment="Center" ToolTip="Create a copy with incremented version and mark this version published." />
54+
<Button x:Name="Publish" Content="Publish" IsEnabled="{Binding CanPublish}" cal:Action.TargetWithoutContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
55+
cal:Message.Attach="[Event Click] = [Action PublishAsync($dataContext)]" Margin="5,2,5,2" HorizontalAlignment="Center"
56+
ToolTip="Set the state of version as published" />
5657
</DataTemplate>
5758
</DataGridTemplateColumn.CellTemplate>
5859
</DataGridTemplateColumn>
5960
<DataGridTemplateColumn Header="">
6061
<DataGridTemplateColumn.CellTemplate>
6162
<DataTemplate>
62-
<Button x:Name="Publish" Content="Publish" IsEnabled="{Binding CanPublish}"
63+
<Button x:Name="Clone" Content="Clone" IsEnabled="{Binding CanClone}"
6364
cal:Action.TargetWithoutContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"
64-
cal:Message.Attach="[Event Click] = [Action PublishAsync($dataContext)]" Margin="5,2,5,2" HorizontalAlignment="Center"
65-
ToolTip="Mark the version as published. Published versions can be used in automation." />
65+
cal:Message.Attach="[Event Click] = [Action CloneAsync($dataContext)]" Margin="5,2,5,2" HorizontalAlignment="Center"
66+
ToolTip="Create an incremented version by cloning this version" />
6667
</DataTemplate>
6768
</DataGridTemplateColumn.CellTemplate>
6869
</DataGridTemplateColumn>

src/Pixel.Automation.Designer.ViewModels/VersionManager/ProjectVersionManagerViewModel.cs

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ public class ProjectVersionManagerViewModel: Caliburn.Micro.Screen, IVersionMana
2222
private readonly AutomationProject automationProject;
2323
private readonly ApplicationSettings applicationSettings;
2424

25-
private bool wasPublished;
25+
private bool wasPublishedOrCloned;
2626

2727
public BindableCollection<ProjectVersionViewModel> AvailableVersions { get; set; } = new BindableCollection<ProjectVersionViewModel>();
2828

29+
/// <summary>
30+
/// constructor
31+
/// </summary>
32+
/// <param name="automationProject"></param>
33+
/// <param name="workspaceManagerFactory"></param>
34+
/// <param name="referenceManagerFactory"></param>
35+
/// <param name="serializer"></param>
36+
/// <param name="projectDataManager"></param>
37+
/// <param name="applicationSettings"></param>
2938
public ProjectVersionManagerViewModel(AutomationProject automationProject, IWorkspaceManagerFactory workspaceManagerFactory,
3039
IReferenceManagerFactory referenceManagerFactory, ISerializer serializer, IProjectDataManager projectDataManager, ApplicationSettings applicationSettings)
3140
{
@@ -44,61 +53,63 @@ public ProjectVersionManagerViewModel(AutomationProject automationProject, IWork
4453
}
4554

4655
/// <summary>
47-
/// Create a copy of selected version and mark the selected version as Published.
56+
/// Set the state of version as published.
4857
/// </summary>
4958
/// <returns></returns>
50-
public async Task CloneAndPublishAsync(ProjectVersionViewModel projectVersionViewModel)
59+
public async Task PublishAsync(ProjectVersionViewModel projectVersionViewModel)
5160
{
5261
try
5362
{
54-
logger.Information($"Trying to clone version {projectVersionViewModel.Version} for Project : {this.automationProject.Name}");
55-
56-
//Create a new active version from selected version
57-
ProjectVersion newVersion = await projectVersionViewModel.CloneAsync(this.projectDataManager);
58-
59-
IProjectFileSystem projectFileSystem = new ProjectFileSystem(serializer, this.applicationSettings);
60-
projectFileSystem.Initialize(this.automationProject, newVersion);
61-
62-
//After cloning, we mark the curret version as published
63-
if(!projectVersionViewModel.IsPublished)
63+
if (!projectVersionViewModel.IsPublished)
6464
{
65-
await projectVersionViewModel.PublishAsync(this.workspaceManagerFactory, this.projectDataManager);
66-
logger.Information($"Version {projectVersionViewModel.Version} for project : {this.automationProject.Name} is published now.");
67-
}
68-
69-
this.AvailableVersions.Add(new ProjectVersionViewModel(this.automationProject, newVersion, projectFileSystem, referenceManagerFactory));
70-
this.wasPublished = true;
65+
bool isLatestActieVersion = automationProject.LatestActiveVersion.Version.Equals(projectVersionViewModel.Version);
66+
await projectVersionViewModel.PublishAsync(this.workspaceManagerFactory, this.projectDataManager);
67+
if (isLatestActieVersion)
68+
{
69+
await CloneAsync(projectVersionViewModel);
70+
}
71+
this.wasPublishedOrCloned = true;
72+
logger.Information("Version {0} for project : {1} is published now.", projectVersionViewModel.Version, this.automationProject.Name);
73+
}
7174
}
7275
catch (Exception ex)
7376
{
74-
logger.Error(ex, ex.Message);
75-
}
77+
logger.Error(ex, ex.Message);
78+
}
7679
}
7780

81+
7882
/// <summary>
79-
/// Create a copy of selected version and mark the selected version as Published.
83+
/// Create a copy of selected version. Only published versions can be cloned.
8084
/// </summary>
8185
/// <returns></returns>
82-
public async Task PublishAsync(ProjectVersionViewModel projectVersionViewModel)
86+
public async Task CloneAsync(ProjectVersionViewModel projectVersionViewModel)
8387
{
8488
try
85-
{
86-
if (projectVersionViewModel.CanPublish)
89+
{
90+
if (projectVersionViewModel.IsPublished)
8791
{
88-
await projectVersionViewModel.PublishAsync(this.workspaceManagerFactory, this.projectDataManager);
89-
logger.Information($"Version {projectVersionViewModel.Version} for project : {this.automationProject.Name} is published now.");
90-
}
92+
//Create a new active version from selected version
93+
ProjectVersion newVersion = await projectVersionViewModel.CloneAsync(this.projectDataManager);
94+
IProjectFileSystem projectFileSystem = new ProjectFileSystem(serializer, this.applicationSettings);
95+
projectFileSystem.Initialize(this.automationProject, newVersion);
96+
this.AvailableVersions.Add(new ProjectVersionViewModel(this.automationProject, newVersion, projectFileSystem, referenceManagerFactory));
97+
this.wasPublishedOrCloned = true;
98+
}
9199
}
92100
catch (Exception ex)
93101
{
94-
logger.Error(ex, ex.Message);
95-
}
102+
logger.Error(ex, ex.Message);
103+
}
96104
}
97105

98-
106+
/// <summary>
107+
/// Close Project version manager screen
108+
/// </summary>
109+
/// <returns></returns>
99110
public async Task CloseAsync()
100111
{
101-
await this.TryCloseAsync(this.wasPublished);
112+
await this.TryCloseAsync(this.wasPublishedOrCloned);
102113
}
103114
}
104115
}

0 commit comments

Comments
 (0)