Skip to content

Commit

Permalink
Changing prefab version should not require application restart
Browse files Browse the repository at this point in the history
All the published prefabs have same data model assembly name. As a result, if we change the version, we can't load the assembly with same name from different path. While publishing the prefab, we compile the assembly with major version included in the name. This avoids the issue with loading when changing version.
  • Loading branch information
Nfactor26 committed Oct 30, 2022
1 parent a72f38f commit 783a3bf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public class PrefabVersionManagerViewModel : Screen , IVersionManager
private readonly PrefabProject prefabProject;
private readonly ApplicationSettings applicationSettings;

private bool wasPublished = false;

public BindableCollection<PrefabVersionViewModel> AvailableVersions { get; set; } = new BindableCollection<PrefabVersionViewModel>();


Expand Down Expand Up @@ -84,9 +82,7 @@ public async Task CloneAndPublishAsync(PrefabVersionViewModel prefabVersionViewM
await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, new PrefabVersion(prefabVersionViewModel.Version) { IsActive = false });
await this.applicationDataManager.AddOrUpdatePrefabDataFilesAsync(this.prefabProject, newVersion);

this.AvailableVersions.Insert(indexToInsert, new PrefabVersionViewModel(this.prefabProject, newVersion, fileSystem, referenceManagerFactory));
this.wasPublished = true;

this.AvailableVersions.Insert(indexToInsert, new PrefabVersionViewModel(this.prefabProject, newVersion, fileSystem, referenceManagerFactory));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -118,7 +114,7 @@ public async Task PublishAsync(PrefabVersionViewModel prefabVersionViewModel)

public async Task CloseAsync()
{
await this.TryCloseAsync(this.wasPublished);
await this.TryCloseAsync(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void Publish(IWorkspaceManagerFactory workspaceFactory)
}
}

string assemblyName = this.prefabProject.Namespace;
string assemblyName = $"{this.prefabProject.Namespace}.v{Version.Major}";
using (var compilationResult = workspaceManager.CompileProject(prefabProjectName, assemblyName))
{
compilationResult.SaveAssemblyToDisk(this.fileSystem.ReferencesDirectory);
Expand All @@ -164,10 +164,10 @@ public void Publish(IWorkspaceManagerFactory workspaceFactory)
this.PrefabAssembly = $"{assemblyName}.dll";

//Replace the assemly name in the process and template file
UpdateAssemblyReference(this.fileSystem.PrefabFile, assemblyName);
UpdateAssemblyReference(this.fileSystem.PrefabFile, this.prefabProject.Namespace, assemblyName);
if (File.Exists(this.fileSystem.TemplateFile))
{
UpdateAssemblyReference(this.fileSystem.TemplateFile, assemblyName);
UpdateAssemblyReference(this.fileSystem.TemplateFile, this.prefabProject.Namespace, assemblyName);
}

logger.Information($"Assembly references updated in process and template files.");
Expand All @@ -181,13 +181,13 @@ public void Publish(IWorkspaceManagerFactory workspaceFactory)
/// </summary>
/// <param name="processFile"></param>
/// <param name="assemblyName"></param>
private void UpdateAssemblyReference(string processFile, string assemblyName)
private void UpdateAssemblyReference(string processFile, string assemblyName, string replaceWith)
{
string fileContents = File.ReadAllText(processFile);
Regex regex = new Regex($"({assemblyName})(_\\d+)");
fileContents = regex.Replace(fileContents, (m) =>
{
return assemblyName;
return replaceWith;
});
File.WriteAllText(processFile, fileContents);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="90"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
</Grid.RowDefinitions>

Expand Down Expand Up @@ -54,17 +54,10 @@
</DataGrid>

<StackPanel Grid.Row="1" x:Name="WarningPanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="{StaticResource ControlMargin}"
Background="{DynamicResource MahApps.Brushes.ValidationSummary3}" Orientation="Vertical">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="5">
<iconPacks:PackIconMaterial Kind="Alert" Margin="5" VerticalAlignment="Center"/>
<TextBlock x:Name="WarningMessage" Margin="5" VerticalAlignment="Center"
Background="{DynamicResource MahApps.Brushes.ValidationSummary3}" Orientation="Horizontal">
<iconPacks:PackIconMaterial Kind="Alert" Margin="10,0,0,0" VerticalAlignment="Center"/>
<TextBlock x:Name="WarningMessage" Margin="10,0,0,0" VerticalAlignment="Center"
Text="Upgrading Prefab version can be a breaking change. You might need to redo input and output mapping script for Prefab."/>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="5">
<iconPacks:PackIconMaterial Kind="Alert" Margin="5" VerticalAlignment="Center"/>
<TextBlock x:Name="RestartMessage" Margin="5" VerticalAlignment="Center"
Text="Any changes will take effect after application restart."/>
</StackPanel>
</StackPanel>
<DockPanel Grid.Row="2" LastChildFill="False">
<Border DockPanel.Dock="Top" BorderThickness="1" Height="1" HorizontalAlignment="Stretch" Width="{Binding Path=Width,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DockPanel}}}" BorderBrush="{DynamicResource MahApps.Brushes.Accent}"/>
Expand Down

0 comments on commit 783a3bf

Please sign in to comment.