Skip to content

Commit 7f530ff

Browse files
committed
Download latest initialize environment script before editing it
1 parent 782a4b7 commit 7f530ff

File tree

9 files changed

+72
-19
lines changed

9 files changed

+72
-19
lines changed

src/Pixel.Automation.Designer.ViewModels/AutomationBuilder/AutomationEditorViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public async Task EditScriptAsync()
161161
{
162162
try
163163
{
164+
await this.projectManager.DownloadFileByNameAsync(Constants.InitializeEnvironmentScript);
164165
var entityManager = this.EntityManager;
165166
var fileSystem = entityManager.GetCurrentFileSystem();
166167
var scriptFile = Path.Combine(fileSystem.ScriptsDirectory, Constants.InitializeEnvironmentScript);

src/Pixel.Automation.Designer.ViewModels/AutomationBuilder/AutomationProjectManager.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ public class AutomationProjectManager : ProjectManager, IAutomationProjectManage
2525
private AutomationProject activeProject;
2626
private VersionInfo loadedVersion;
2727

28+
/// <summary>
29+
/// constructor
30+
/// </summary>
31+
/// <param name="serializer"></param>
32+
/// <param name="entityManager"></param>
33+
/// <param name="projectFileSystem"></param>
34+
/// <param name="typeProvider"></param>
35+
/// <param name="argumentTypeProvider"></param>
36+
/// <param name="codeEditorFactory"></param>
37+
/// <param name="scriptEditorFactory"></param>
38+
/// <param name="scriptEngineFactory"></param>
39+
/// <param name="codeGenerator"></param>
40+
/// <param name="applicationDataManager"></param>
41+
/// <param name="referenceManagerFactory"></param>
42+
/// <param name="projectDataManager"></param>
43+
/// <param name="prefabDataManager"></param>
44+
/// <param name="projectAssetDataManager"></param>
2845
public AutomationProjectManager(ISerializer serializer, IEntityManager entityManager, IProjectFileSystem projectFileSystem, ITypeProvider typeProvider, IArgumentTypeProvider argumentTypeProvider,
2946
ICodeEditorFactory codeEditorFactory, IScriptEditorFactory scriptEditorFactory, IScriptEngineFactory scriptEngineFactory, ICodeGenerator codeGenerator, IApplicationDataManager applicationDataManager,
3047
IReferenceManagerFactory referenceManagerFactory, IProjectDataManager projectDataManager, IPrefabDataManager prefabDataManager, IProjectAssetsDataManager projectAssetDataManager)
@@ -37,7 +54,7 @@ public AutomationProjectManager(ISerializer serializer, IEntityManager entityMan
3754
this.prefabDataManager = Guard.Argument(prefabDataManager, nameof(prefabDataManager)).NotNull().Value;
3855
}
3956

40-
#region Load Project
57+
#region methods
4158

4259
public async Task<Entity> Load(AutomationProject activeProject, VersionInfo versionToLoad)
4360
{
@@ -141,9 +158,7 @@ private Entity DeserializeProject()
141158
{
142159
var entity = this.Load<Entity>(this.projectFileSystem.ProcessFile);
143160
return entity;
144-
}
145-
146-
#endregion Load Project
161+
}
147162

148163
/// <summary>
149164
/// Save and load project again. Update services to use new data model . One time registration of services is skipped unlike load.
@@ -154,35 +169,43 @@ private Entity DeserializeProject()
154169
/// <param name="entityManager"></param>
155170
/// <returns></returns>
156171
public override async Task Reload()
157-
{
172+
{
158173

159174
logger.Information($"{this.GetProjectName()} will be re-loaded");
160175
var reference = this.fileSystem.LoadFile<ProjectReferences>(this.fileSystem.ReferencesFile);
161176
this.referenceManager.SetProjectReferences(reference);
162177
var dataModel = CompileAndCreateDataModel(Constants.AutomationProcessDataModelName);
163178
ConfigureScriptEngine(this.referenceManager, dataModel);
164-
ConfigureScriptEditor(this.referenceManager, dataModel);
179+
ConfigureScriptEditor(this.referenceManager, dataModel);
165180
this.entityManager.Arguments = dataModel;
166181
SetupInitializationScriptProject(dataModel);
167-
await ExecuteInitializationScript();
182+
await ExecuteInitializationScript();
168183
ConfigureArgumentTypeProvider(this.entityManager.Arguments.GetType().Assembly);
169184
this.RootEntity.ResetHierarchy();
170-
serializer.Serialize(this.projectFileSystem.ProcessFile, this.RootEntity, typeProvider.GetKnownTypes());
171-
185+
serializer.Serialize(this.projectFileSystem.ProcessFile, this.RootEntity, typeProvider.GetKnownTypes());
186+
172187
var rootEntity = DeserializeProject();
173188
//we don't want any launched applications to be lost. Copy over ApplicationDetails from each ApplicationEntity in to newly loaded root entity.
174-
foreach(var applicationEntity in this.entityManager.RootEntity.GetComponentsOfType<ApplicationEntity>(SearchScope.Descendants))
189+
foreach (var applicationEntity in this.entityManager.RootEntity.GetComponentsOfType<ApplicationEntity>(SearchScope.Descendants))
175190
{
176191
var newApplicationEntity = rootEntity.GetComponentById(applicationEntity.Id, SearchScope.Descendants) as IApplicationEntity;
177192
newApplicationEntity.SetTargetApplicationDetails(applicationEntity.GetTargetApplicationDetails());
178-
}
193+
}
179194
this.RootEntity = rootEntity;
180195
RestoreParentChildRelation(this.RootEntity);
181196
await Task.CompletedTask;
182197
logger.Information($"Reload completed for project {this.GetProjectName()}");
183198

184199
}
185200

201+
///<inheritdoc/>
202+
public async Task DownloadFileByNameAsync(string fileName)
203+
{
204+
await this.projectDataManager.DownloadProjectDataFileByNameAsync(this.activeProject, this.loadedVersion as ProjectVersion, fileName);
205+
}
206+
207+
#endregion methods
208+
186209
#region overridden methods
187210

188211
///<inheritdoc/>

src/Pixel.Automation.Editor.Core/Interfaces/IProjectManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public interface IProjectManager
6464

6565
public interface IAutomationProjectManager: IProjectManager
6666
{
67-
Task<Entity> Load(AutomationProject activeProject, VersionInfo versionToLoad);
67+
Task<Entity> Load(AutomationProject activeProject, VersionInfo versionToLoad);
68+
69+
Task DownloadFileByNameAsync(string fileName);
6870
}
6971

7072

src/Pixel.Persistence.Respository/FilesRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public async Task<ProjectDataFile> GetFileAsync(string projectId, string project
112112
ProjectId = projectId,
113113
ProjectVersion = projectVersion,
114114
FileName = fileInfo.Filename,
115-
FilePath = fileInfo.Metadata[this.filePathField].AsString
115+
FilePath = fileInfo.Metadata[this.filePathField].AsString,
116+
Bytes = fileData
116117
};
117118
}
118119
}

src/Pixel.Persistence.Services.Api/Controllers/FilesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public async Task<ActionResult> GetFileByName(string projectId, string projectve
6868
var file = await filesRepository.GetFileAsync(projectId, projectversion, fileName);
6969
if(file != null)
7070
{
71-
return File(file.Bytes, file.Type, file.FileName);
71+
return Ok(file);
7272
}
7373
return NotFound();
7474

src/Pixel.Persistence.Services.Client/DataManagers/ProjectDataManager.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.IO.Compression;
1212
using System.Linq;
1313
using System.Threading.Tasks;
14+
using static System.Net.Mime.MediaTypeNames;
1415

1516
namespace Pixel.Persistence.Services.Client;
1617

@@ -104,6 +105,28 @@ public async Task DownloadProjectDataFilesAsync(AutomationProject automationProj
104105
}
105106
}
106107

108+
/// <inheritdoc/>
109+
public async Task DownloadProjectDataFileByNameAsync(AutomationProject automationProject, ProjectVersion projectVersion, string fileName)
110+
{
111+
Guard.Argument(automationProject, nameof(automationProject)).NotNull();
112+
Guard.Argument(projectVersion, nameof(projectVersion)).NotNull();
113+
Guard.Argument(fileName, nameof(fileName)).NotNull().NotEmpty();
114+
115+
if (IsOnlineMode)
116+
{
117+
var file = await this.filesClient.DownProjectDataFile(automationProject.ProjectId, projectVersion.ToString(), fileName);
118+
using (MemoryStream ms = new MemoryStream(file.Bytes))
119+
{
120+
using (FileStream fs = new FileStream(file.FilePath, FileMode.CreateNew))
121+
{
122+
ms.Seek(0, SeekOrigin.Begin);
123+
ms.CopyTo(fs);
124+
}
125+
}
126+
logger.Information("File {0} was downloaded.", file.FilePath);
127+
}
128+
}
129+
107130
/// <inheritdoc/>
108131
public async Task DownloadDataModelFilesAsync(AutomationProject automationProject, ProjectVersion projectVersion)
109132
{

src/Pixel.Persistence.Services.Client/FilesRepositoryClient.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,15 @@ public async Task<byte[]> DownloadProjectDataFilesOfType(string projectId, strin
9494
}
9595

9696
/// <inheritdoc/>
97-
public async Task<byte[]> DownProjectDataFile(string projectId, string projectVersion, string fileName)
97+
public async Task<ProjectDataFile> DownProjectDataFile(string projectId, string projectVersion, string fileName)
9898
{
9999
Guard.Argument(projectId, nameof(projectId)).NotNull().NotEmpty();
100100
Guard.Argument(projectVersion, nameof(projectVersion)).NotNull().NotEmpty();
101101
Guard.Argument(fileName, nameof(fileName)).NotNull().NotEmpty();
102102

103103
RestRequest restRequest = new RestRequest($"{baseUrl}/{projectId}/{projectVersion}/name/{fileName}");
104104
var client = this.clientFactory.GetOrCreateClient();
105-
var result = await client.ExecuteGetAsync(restRequest);
106-
result.EnsureSuccess();
107-
return result.RawBytes;
105+
return await client.GetAsync<ProjectDataFile>(restRequest);
108106
}
109107

110108
/// <inheritdoc/>

src/Pixel.Persistence.Services.Client/Interfaces/IFilesRepositoryClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface IFilesRepositoryClient
3131
/// <param name="projectVersion"></param>
3232
/// <param name="fileName"></param>
3333
/// <returns></returns>
34-
Task<byte[]> DownProjectDataFile(string projectId, string projectVersion, string fileName);
34+
Task<ProjectDataFile> DownProjectDataFile(string projectId, string projectVersion, string fileName);
3535

3636
/// <summary>
3737
/// Download data files matching any of the specified tags for a given version of project

src/Pixel.Persistence.Services.Client/Interfaces/IProjectDataManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public interface IProjectDataManager
6464
/// <returns></returns>
6565
Task DownloadProjectsAsync();
6666

67+
/// <summary>
68+
/// Download a file with specified nme for the version of AutomtionProject being managed
69+
/// </summary>
70+
/// <returns></returns>
71+
Task DownloadProjectDataFileByNameAsync(AutomationProject automationProject, ProjectVersion projectVersion, string fileName);
6772

6873
/// <summary>
6974
/// Download all the data files belonging to the version of AutomationProject being managed

0 commit comments

Comments
 (0)