Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override LcmCache LoadCache(FwDataProject project)
return cache;
}

public override LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs)
public override LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs, string uiWs = "en")
{
Init();
var lcmDirectories = new LcmDirectories(project.ProjectsPath, TemplatesFolder);
Expand All @@ -28,7 +28,7 @@ public override LcmCache NewProject(FwDataProject project, string analysisWs, st
new SimpleProjectId(BackendProviderType.kMemoryOnly, Path.GetFullPath(project.FilePath)),
analysisWs,
vernacularWs,
null,
uiWs,
new LfLcmUi(progress.SynchronizeInvoke),
lcmDirectories,
new LcmSettings());
Expand Down
49 changes: 43 additions & 6 deletions backend/FwLite/FwDataMiniLcmBridge/LcmUtils/ProjectLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public interface IProjectLoader
/// <returns></returns>
LcmCache LoadCache(FwDataProject project);

LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs);
LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs, string uiWs = "en");

/// <summary>
/// Like the single-WS overload, but the new project also gets every analysis/vernacular writing
/// system in the given lists. The first item of each list is the default of that type.
/// </summary>
LcmCache NewProject(FwDataProject project, IReadOnlyList<string> analysisWss, IReadOnlyList<string> vernacularWss, string uiWs = "en");
}

public class ProjectLoader(IOptions<FwDataBridgeConfig> config) : IProjectLoader
Expand Down Expand Up @@ -75,27 +81,58 @@ public virtual LcmCache LoadCache(FwDataProject project)
return cache;
}

public virtual LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs)
public virtual LcmCache NewProject(FwDataProject project, string analysisWs, string vernacularWs, string uiWs = "en")
{
return NewProject(project, [analysisWs], [vernacularWs], uiWs);
}

public virtual LcmCache NewProject(FwDataProject project,
IReadOnlyList<string> analysisWss,
IReadOnlyList<string> vernacularWss,
string uiWs = "en")
{
Init();
var lcmDirectories = new LcmDirectories(project.ProjectsPath, TemplatesFolder);
var progress = new LcmThreadedProgress();
var analysisDefinitions = analysisWss.Select(CreateWritingSystemDefinition).ToList();
var vernacularDefinitions = vernacularWss.Select(CreateWritingSystemDefinition).ToList();
NewProject(progress,
project.Name,
lcmDirectories,
progress.SynchronizeInvoke,
new CoreWritingSystemDefinition(analysisWs) { Id = analysisWs },
new CoreWritingSystemDefinition(vernacularWs) { Id = vernacularWs });
analysisDefinitions[0],
vernacularDefinitions[0],
uiWs,
AdditionalWritingSystems(analysisDefinitions),
AdditionalWritingSystems(vernacularDefinitions));
return LoadCache(project);
}

private static CoreWritingSystemDefinition CreateWritingSystemDefinition(string ws) => new(ws) { Id = ws };

// The default writing system (the first of the list) is passed to CreateNewLangProj on its own, so
// the "additional" set is the whole list as a hash set with that default removed. The new project
// ends up with them in list order (NewProjectWritingSystemsTests checks this).
private static HashSet<CoreWritingSystemDefinition> AdditionalWritingSystems(List<CoreWritingSystemDefinition> definitions)
{
var additional = definitions.ToHashSet();
additional.Remove(definitions[0]);
return additional;
}

private static void NewProject(IThreadedProgress progress,
string projectName,
ILcmDirectories lcmDirectories,
ISynchronizeInvoke syncInvoke,
CoreWritingSystemDefinition analysisWs,
CoreWritingSystemDefinition vernacularWs)
CoreWritingSystemDefinition vernacularWs,
string uiWs,
HashSet<CoreWritingSystemDefinition> additionalAnalysisWss,
HashSet<CoreWritingSystemDefinition> additionalVernacularWss)
{
LcmCache.CreateNewLangProj(progress, [projectName, lcmDirectories, syncInvoke, analysisWs, vernacularWs]);
// After the default analysis/vernacular definitions and the string UI writing system,
// CreateNewLangProj takes the additional analysis then additional vernacular writing systems.
LcmCache.CreateNewLangProj(progress,
[projectName, lcmDirectories, syncInvoke, analysisWs, vernacularWs, uiWs, additionalAnalysisWss, additionalVernacularWss]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using FwDataMiniLcmBridge;
using FwDataMiniLcmBridge.LcmUtils;
using FwLiteProjectSync.Tests.Fixtures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace FwLiteProjectSync.Tests;

/// <summary>
/// ProjectLoader.NewProject with several writing systems of each type, against the real SIL.LCModel template.
/// </summary>
public class NewProjectWritingSystemsTests : IAsyncLifetime
{
private const string ProjectFolder = nameof(NewProjectWritingSystemsTests);
private readonly ServiceProvider _rootServiceProvider;
private readonly AsyncServiceScope _scope;
private IServiceProvider Services => _scope.ServiceProvider;

public NewProjectWritingSystemsTests()
{
_rootServiceProvider = new ServiceCollection()
.AddSyncServices(ProjectFolder, mockFwProjectLoader: false)
.BuildServiceProvider();
_scope = _rootServiceProvider.CreateAsyncScope();
}

public Task InitializeAsync()
{
if (Directory.Exists(ProjectFolder)) Directory.Delete(ProjectFolder, true);
Directory.CreateDirectory(Services.GetRequiredService<IOptions<FwDataBridgeConfig>>().Value.ProjectsFolder);
return Task.CompletedTask;
}

public async Task DisposeAsync()
{
await _scope.DisposeAsync();
await _rootServiceProvider.DisposeAsync();
}

[Fact]
public async Task NewProjectAddsEveryRequestedWritingSystemToTheCorrectListInOrder()
{
var fwDataBridgeConfig = Services.GetRequiredService<IOptions<FwDataBridgeConfig>>().Value;
// The SIL.LCModel template ships next to the test assembly; point liblcm at it (this project,
// unlike FwDataMiniLcmBridge.Tests, doesn't otherwise configure TemplatesFolder).
fwDataBridgeConfig.TemplatesFolder = Path.Combine(AppContext.BaseDirectory, "Templates");
var fwDataProject = new FwDataProject("multi-ws-source", fwDataBridgeConfig.ProjectsFolder);
// First of each list is the default; the rest go through CreateNewLangProj's additional-WS sets.
// Distinct codes per type so a swapped analysis/vernacular argument would fail this test, and
// several additional ones per type because CreateNewLangProj takes those as an unordered set.
string[] analysis = ["en", "de", "pt", "ru"];
string[] vernacular = ["fr", "es", "it", "nl"];
Services.GetRequiredService<IProjectLoader>().NewProject(fwDataProject, analysis, vernacular, uiWs: "en").Dispose();

using var api = Services.GetRequiredService<FwDataFactory>().GetFwDataMiniLcmApi(fwDataProject, false);
var writingSystems = await api.GetWritingSystems();
writingSystems.Analysis.Select(ws => ws.WsId.Code).Should().Equal(analysis);
writingSystems.Vernacular.Select(ws => ws.WsId.Code).Should().Equal(vernacular);
}
}
Loading