Skip to content

Commit

Permalink
use FwDataProject class for S&R Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Oct 25, 2024
1 parent 7926a9b commit d16eac3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
9 changes: 4 additions & 5 deletions backend/CrdtMerge/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,26 @@
var fwDataProject = new FwDataProject(projectCode, projectFolder); // TODO: use projectName (once we have it) instead of projectCode here
logger.LogDebug("crdtFile: {crdtFile}", crdtFile);
logger.LogDebug("fwDataFile: {fwDataFile}", fwDataProject.FilePath);
var crdtProjectName = projectCode;

if (File.Exists(fwDataProject.FilePath))
{
var srResult = srService.SendReceive(projectFolder, projectCode);
var srResult = srService.SendReceive(fwDataProject);
logger.LogInformation("Send/Receive result: {srResult}", srResult.Output);
}
else
{
var srResult = srService.Clone(projectFolder, projectCode);
var srResult = srService.Clone(fwDataProject);
logger.LogInformation("Send/Receive result: {srResult}", srResult.Output);
}
var fwdataApi = fwDataFactory.GetFwDataMiniLcmApi(fwDataProject, true);
// var crdtProject = projectsService.GetProject(crdtProjectName);
var crdtProject = File.Exists(crdtFile) ?
new CrdtProject(projectCode, crdtFile) : // TODO: use projectName (once we have it) instead of projectCode here
await projectsService.CreateProject(new(crdtProjectName, fwdataApi.ProjectId, SeedNewProjectData: false, Path: projectFolder));
await projectsService.CreateProject(new(projectCode, fwdataApi.ProjectId, SeedNewProjectData: false, Path: projectFolder));
var miniLcmApi = await services.OpenCrdtProject(crdtProject);
var result = await syncService.Sync(miniLcmApi, fwdataApi, dryRun);
logger.LogInformation("Sync result, CrdtChanges: {CrdtChanges}, FwdataChanges: {FwdataChanges}", result.CrdtChanges, result.FwdataChanges);
var srResult2 = srService.SendReceive(projectFolder, projectCode);
var srResult2 = srService.SendReceive(fwDataProject);
logger.LogInformation("Send/Receive result after CRDT sync: {srResult2}", srResult2.Output);
return result;
}
Expand Down
24 changes: 12 additions & 12 deletions backend/CrdtMerge/SendReceiveHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using FwDataMiniLcmBridge;
using SIL.Progress;

namespace CrdtMerge;
Expand All @@ -18,14 +19,14 @@ public record SendReceiveParams(string ProjectCode, string BaseUrl, string Dir)

public record LfMergeBridgeResult(string Output, string ProgressMessages);

public static LfMergeBridgeResult CallLfMergeBridge(string method, IDictionary<string, string> flexBridgeOptions)
private static LfMergeBridgeResult CallLfMergeBridge(string method, IDictionary<string, string> flexBridgeOptions)
{
var progress = new StringBuilderProgress();
LfMergeBridge.LfMergeBridge.Execute(method, progress, flexBridgeOptions.ToDictionary(), out var lfMergeBridgeOutputForClient);
return new LfMergeBridgeResult(lfMergeBridgeOutputForClient, progress.ToString());
}

public static Uri BuildSendReceiveUrl(string baseUrl, string projectCode, SendReceiveAuth? auth)
private static Uri BuildSendReceiveUrl(string baseUrl, string projectCode, SendReceiveAuth? auth)
{
var baseUri = new Uri(baseUrl);
var projectUri = new Uri(baseUri, projectCode);
Expand All @@ -46,14 +47,14 @@ public static Uri BuildSendReceiveUrl(string baseUrl, string projectCode, SendRe
return builder.Uri;
}

public static LfMergeBridgeResult SendReceive(string fwdataPath, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? projectCode = null, string? commitMessage = null)
public static LfMergeBridgeResult SendReceive(FwDataProject project, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? commitMessage = null)
{
// If projectCode not given, calculate it from the fwdataPath
var fwdataInfo = new FileInfo(fwdataPath);
if (fwdataInfo.Directory is null) throw new ArgumentException("Not allowed to Send/Receive root-level directories like C:\\", nameof(fwdataPath));
projectCode ??= Path.GetFileNameWithoutExtension(fwdataPath);
var fwdataInfo = new FileInfo(project.FilePath);
if (fwdataInfo.Directory is null) throw new InvalidOperationException(
$"Not allowed to Send/Receive root-level directories like C:\\, was '{project.FilePath}'");

var repoUrl = BuildSendReceiveUrl(baseUrl, projectCode, auth);
var repoUrl = BuildSendReceiveUrl(baseUrl, project.Name, auth);

var flexBridgeOptions = new Dictionary<string, string>
{
Expand All @@ -69,14 +70,13 @@ public static LfMergeBridgeResult SendReceive(string fwdataPath, string baseUrl
return CallLfMergeBridge("Language_Forge_Send_Receive", flexBridgeOptions);
}

public static LfMergeBridgeResult CloneProject(string fwdataPath, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072", string? projectCode = null)
public static LfMergeBridgeResult CloneProject(FwDataProject project, string baseUrl = "http://localhost", SendReceiveAuth? auth = null, string fdoDataModelVersion = "7000072")
{
// If projectCode not given, calculate it from the fwdataPath
var fwdataInfo = new FileInfo(fwdataPath);
if (fwdataInfo.Directory is null) throw new ArgumentException("Not allowed to Send/Receive root-level directories like C:\\", nameof(fwdataPath));
projectCode ??= Path.GetFileNameWithoutExtension(fwdataPath);
var fwdataInfo = new FileInfo(project.FilePath);
if (fwdataInfo.Directory is null) throw new InvalidOperationException($"Not allowed to Send/Receive root-level directories like C:\\ '{project.FilePath}'");

var repoUrl = BuildSendReceiveUrl(baseUrl, projectCode, auth);
var repoUrl = BuildSendReceiveUrl(baseUrl, project.Name, auth);

var flexBridgeOptions = new Dictionary<string, string>
{
Expand Down
17 changes: 6 additions & 11 deletions backend/CrdtMerge/SendReceiveService.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
using FwDataMiniLcmBridge;
using Microsoft.Extensions.Options;

namespace CrdtMerge;

public class SendReceiveService(IOptions<CrdtMergeConfig> config)
{
public SendReceiveHelpers.LfMergeBridgeResult SendReceive(string projectFolder, string projectCode, string? commitMessage = null)
public SendReceiveHelpers.LfMergeBridgeResult SendReceive(FwDataProject project, string? commitMessage = null)
{
var fwdataName = $"{projectCode}.fwdata";
var fwdataPath = Path.Join(projectFolder, projectCode, fwdataName);
return SendReceiveHelpers.SendReceive(
fwdataPath: fwdataPath,
project: project,
baseUrl: config.Value.HgWebUrl,
auth: new SendReceiveHelpers.SendReceiveAuth(config.Value),
fdoDataModelVersion: config.Value.FdoDataModelVersion,
projectCode: projectCode,
commitMessage: commitMessage
);
}

public SendReceiveHelpers.LfMergeBridgeResult Clone(string projectFolder, string projectCode)
public SendReceiveHelpers.LfMergeBridgeResult Clone(FwDataProject project)
{
var fwdataName = $"{projectCode}.fwdata";
var fwdataPath = Path.Join(projectFolder, projectCode, fwdataName);
return SendReceiveHelpers.CloneProject(
fwdataPath: fwdataPath,
project: project,
baseUrl: config.Value.HgWebUrl,
auth: new SendReceiveHelpers.SendReceiveAuth(config.Value),
fdoDataModelVersion: config.Value.FdoDataModelVersion,
projectCode: projectCode
fdoDataModelVersion: config.Value.FdoDataModelVersion
);
}
}

0 comments on commit d16eac3

Please sign in to comment.