Skip to content

Commit

Permalink
update based on comments and use lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
bbonaby committed Oct 4, 2024
1 parent 9e43c25 commit 85db8ad
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DevHome.Common.Models.ExtensionJsonData;
/// Root class that will contain the deserialized data located in the
/// src\Assets\Schemas\ExtensionInformation.schema.json file.
/// </summary>
public class DevHomeExtensionJsonData
public class DevHomeExtensionContentData
{
public List<Product> Products { get; set; } = new();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace DevHome.Common.Models.ExtensionJsonData;
[JsonSerializable(typeof(ProviderSpecificProperty))]
[JsonSerializable(typeof(Properties))]
[JsonSerializable(typeof(Product))]
[JsonSerializable(typeof(DevHomeExtensionJsonData))]
[JsonSerializable(typeof(DevHomeExtensionContentData))]
[JsonSerializable(typeof(DevHomeExtension))]
[JsonSerializable(typeof(JsonSchema))]
[JsonSerializable(typeof(EvaluationResults))]
Expand Down
2 changes: 1 addition & 1 deletion common/Services/IExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ public interface IExtensionService
/// Gets known extension information from internal extension json file.
/// </summary>
/// <returns>An object that holds a list of extension information based on the internal json file.</returns>
public Task<DevHomeExtensionJsonData?> GetExtensionJsonDataAsync();
public Task<DevHomeExtensionContentData?> GetExtensionJsonDataAsync();
}
1 change: 1 addition & 0 deletions src/Assets/Schemas/ExtensionInformation.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"required": [ "DisplayNameKey" ]
},
"ProviderType": {
"$comment": "Enum values should be kept in sync with the provider type enum values located in the Dev Home SDK.",
"type": "string",
"enum": [
"ComputeSystem",
Expand Down
27 changes: 18 additions & 9 deletions src/Services/ExtensionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public class ExtensionService : IExtensionService, IDisposable

private readonly IStringResource _stringResource;

private string? _localExtensionJsonSchemaAbsoluteFilePath;
private readonly Lazy<string> _localExtensionJsonSchemaAbsoluteFilePath;

private string? _localExtensionJsonAbsoluteFilePath;
private readonly Lazy<string> _localExtensionJsonAbsoluteFilePath;

public ExtensionService(ILocalSettingsService settingsService, IStringResource stringResource)
{
Expand All @@ -58,6 +58,18 @@ public ExtensionService(ILocalSettingsService settingsService, IStringResource s
_catalog.PackageUpdating += Catalog_PackageUpdating;
_localSettingsService = settingsService;
_stringResource = stringResource;
_localExtensionJsonSchemaAbsoluteFilePath = new Lazy<string>(GetExtensionJsonSchemaAbsoluteFilePath);
_localExtensionJsonAbsoluteFilePath = new Lazy<string>(GetExtensionJsonAbsoluteFilePath);
}

private string GetExtensionJsonSchemaAbsoluteFilePath()
{
return Path.Combine(_localSettingsService.GetPathToPackageLocation(), LocalExtensionJsonSchemaRelativeFilePath);
}

private string GetExtensionJsonAbsoluteFilePath()
{
return Path.Combine(_localSettingsService.GetPathToPackageLocation(), LocalExtensionJsonRelativeFilePath);
}

private void Catalog_PackageInstalling(PackageCatalog sender, PackageInstallingEventArgs args)
Expand Down Expand Up @@ -415,18 +427,15 @@ public async Task<bool> DisableExtensionIfWindowsFeatureNotAvailable(IExtensionW
return true;
}

public async Task<DevHomeExtensionJsonData?> GetExtensionJsonDataAsync()
public async Task<DevHomeExtensionContentData?> GetExtensionJsonDataAsync()
{
try
{
_localExtensionJsonSchemaAbsoluteFilePath ??= Path.Combine(_localSettingsService.GetPathToPackageLocation(), LocalExtensionJsonRelativeFilePath);
_localExtensionJsonAbsoluteFilePath ??= Path.Combine(_localSettingsService.GetPathToPackageLocation(), LocalExtensionJsonRelativeFilePath);

_log.Information($"Getting extension information from file: '{_localExtensionJsonAbsoluteFilePath}'");
var extensionJson = await File.ReadAllTextAsync(_localExtensionJsonAbsoluteFilePath);
_log.Information($"Getting extension information from file: '{_localExtensionJsonAbsoluteFilePath.Value}'");
var extensionJson = await File.ReadAllTextAsync(_localExtensionJsonAbsoluteFilePath.Value);
var serializerOptions = ExtensionJsonSerializerOptions;
serializerOptions.Converters.Add(new LocalizedPropertiesConverter(_stringResource));
return JsonSerializer.Deserialize<DevHomeExtensionJsonData>(extensionJson, serializerOptions);
return JsonSerializer.Deserialize<DevHomeExtensionContentData>(extensionJson, serializerOptions);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion test/FunctionalTests/ExtensionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public async Task ExtensionServiceReturnsValidExtensionJsonDataObject()
var extensionJsonData = await extensionService.GetExtensionJsonDataAsync();
Assert.IsNotNull(extensionJsonData);

Assert.IsInstanceOfType(extensionJsonData, typeof(DevHomeExtensionJsonData));
Assert.IsInstanceOfType(extensionJsonData, typeof(DevHomeExtensionContentData));
}
}

0 comments on commit 85db8ad

Please sign in to comment.