-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FwLiteProjectSync needs a Program class now
Now that we're not making it an exe, it's not allowed to use top-level statements, so we need to move those into a real Program class with a static Main method.
- Loading branch information
Showing
1 changed file
with
89 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,105 @@ | ||
using System.CommandLine; | ||
using FwDataMiniLcmBridge; | ||
using FwLiteProjectSync; | ||
using LcmCrdt; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using MiniLcm; | ||
|
||
namespace FwLiteProjectSync; | ||
|
||
var rootCommand = new RootCommand("CRDT to FwData sync tool"); | ||
var crdtOption = new Option<string>("--crdt", "CRDT sqlite file") { IsRequired = true }; | ||
rootCommand.AddGlobalOption(crdtOption); | ||
var fwDataOption = new Option<string>("--fwdata", "FwData file") { IsRequired = true }; | ||
rootCommand.AddGlobalOption(fwDataOption); | ||
var beforeSyncCommand = new Command("before-sync", "Run before sync"); | ||
beforeSyncCommand.SetHandler((crdtFile, fwDataFile) => | ||
{ | ||
Console.WriteLine($"crdtFile: {crdtFile}"); | ||
Console.WriteLine($"fwDataFile: {fwDataFile}"); | ||
}, | ||
crdtOption, | ||
fwDataOption); | ||
rootCommand.Add(beforeSyncCommand); | ||
var afterSyncCommand = new Command("after-sync", "Run after sync"); | ||
afterSyncCommand.SetHandler((crdtFile, fwDataFile) => | ||
{ | ||
Console.WriteLine($"crdtFile: {crdtFile}"); | ||
Console.WriteLine($"fwDataFile: {fwDataFile}"); | ||
}, | ||
crdtOption, | ||
fwDataOption); | ||
rootCommand.Add(afterSyncCommand); | ||
var syncCommand = new Command("sync", "Synchronize CRDT with FwData"); | ||
var createCrdtDirOption = new Option<bool>("--create-crdt-dir", "Create CRDT directory"); | ||
var dryRunOption = new Option<bool>("--dry-run", () => false, "Dry run"); | ||
syncCommand.Add(createCrdtDirOption); | ||
syncCommand.Add(dryRunOption); | ||
syncCommand.SetHandler(async (crdtFile, fwDataFile, createCrdtDir, dryRun) => | ||
public class Program | ||
{ | ||
public static Task<int> Main(string[] args) | ||
{ | ||
Console.WriteLine($"crdtFile: {crdtFile}"); | ||
Console.WriteLine($"fwDataFile: {fwDataFile}"); | ||
var fwProjectName = Path.GetFileNameWithoutExtension(fwDataFile); | ||
var crdtProjectName = Path.GetFileNameWithoutExtension(crdtFile); | ||
var rootCommand = new RootCommand("CRDT to FwData sync tool"); | ||
var crdtOption = new Option<string>("--crdt", "CRDT sqlite file") { IsRequired = true }; | ||
rootCommand.AddGlobalOption(crdtOption); | ||
var fwDataOption = new Option<string>("--fwdata", "FwData file") { IsRequired = true }; | ||
rootCommand.AddGlobalOption(fwDataOption); | ||
var beforeSyncCommand = new Command("before-sync", "Run before sync"); | ||
beforeSyncCommand.SetHandler((crdtFile, fwDataFile) => | ||
{ | ||
Console.WriteLine($"crdtFile: {crdtFile}"); | ||
Console.WriteLine($"fwDataFile: {fwDataFile}"); | ||
}, | ||
crdtOption, | ||
fwDataOption); | ||
rootCommand.Add(beforeSyncCommand); | ||
var afterSyncCommand = new Command("after-sync", "Run after sync"); | ||
afterSyncCommand.SetHandler((crdtFile, fwDataFile) => | ||
{ | ||
Console.WriteLine($"crdtFile: {crdtFile}"); | ||
Console.WriteLine($"fwDataFile: {fwDataFile}"); | ||
}, | ||
crdtOption, | ||
fwDataOption); | ||
rootCommand.Add(afterSyncCommand); | ||
var syncCommand = new Command("sync", "Synchronize CRDT with FwData"); | ||
var createCrdtDirOption = new Option<bool>("--create-crdt-dir", "Create CRDT directory"); | ||
var dryRunOption = new Option<bool>("--dry-run", () => false, "Dry run"); | ||
syncCommand.Add(createCrdtDirOption); | ||
syncCommand.Add(dryRunOption); | ||
syncCommand.SetHandler(async (crdtFile, fwDataFile, createCrdtDir, dryRun) => | ||
{ | ||
Console.WriteLine($"crdtFile: {crdtFile}"); | ||
Console.WriteLine($"fwDataFile: {fwDataFile}"); | ||
var fwProjectName = Path.GetFileNameWithoutExtension(fwDataFile); | ||
var crdtProjectName = Path.GetFileNameWithoutExtension(crdtFile); | ||
|
||
await using var serviceRoot = SyncServices(crdtFile, fwDataFile, createCrdtDir); | ||
await using var scope = serviceRoot.CreateAsyncScope(); | ||
var services = scope.ServiceProvider; | ||
var logger = services.GetRequiredService<ILogger<Program>>(); | ||
var fwdataApi = services.GetRequiredService<FwDataFactory>().GetFwDataMiniLcmApi(fwProjectName, true); | ||
var projectsService = services.GetRequiredService<ProjectsService>(); | ||
var crdtProject = projectsService.GetProject(crdtProjectName); | ||
if (crdtProject is null) | ||
{ | ||
crdtProject = await projectsService.CreateProject(new(crdtProjectName, fwdataApi.ProjectId, SeedNewProjectData: false)); | ||
} | ||
projectsService.SetProjectScope(crdtProject); | ||
await services.GetRequiredService<CurrentProjectService>().PopulateProjectDataCache(); | ||
var syncService = services.GetRequiredService<CrdtFwdataProjectSyncService>(); | ||
await using var serviceRoot = SyncServices(crdtFile, fwDataFile, createCrdtDir); | ||
await using var scope = serviceRoot.CreateAsyncScope(); | ||
var services = scope.ServiceProvider; | ||
var logger = services.GetRequiredService<ILogger<Program>>(); | ||
var fwdataApi = services.GetRequiredService<FwDataFactory>().GetFwDataMiniLcmApi(fwProjectName, true); | ||
var projectsService = services.GetRequiredService<ProjectsService>(); | ||
var crdtProject = projectsService.GetProject(crdtProjectName); | ||
if (crdtProject is null) | ||
{ | ||
crdtProject = await projectsService.CreateProject(new(crdtProjectName, fwdataApi.ProjectId, SeedNewProjectData: false)); | ||
} | ||
projectsService.SetProjectScope(crdtProject); | ||
await services.GetRequiredService<CurrentProjectService>().PopulateProjectDataCache(); | ||
var syncService = services.GetRequiredService<CrdtFwdataProjectSyncService>(); | ||
|
||
var result = await syncService.Sync(services.GetRequiredService<IMiniLcmApi>(), fwdataApi, dryRun); | ||
logger.LogInformation("Sync result, CrdtChanges: {CrdtChanges}, FwdataChanges: {FwdataChanges}", result.CrdtChanges, result.FwdataChanges); | ||
}, | ||
crdtOption, | ||
fwDataOption, | ||
createCrdtDirOption, | ||
dryRunOption); | ||
rootCommand.Add(syncCommand); | ||
await rootCommand.InvokeAsync(args); | ||
var result = await syncService.Sync(services.GetRequiredService<IMiniLcmApi>(), fwdataApi, dryRun); | ||
logger.LogInformation("Sync result, CrdtChanges: {CrdtChanges}, FwdataChanges: {FwdataChanges}", result.CrdtChanges, result.FwdataChanges); | ||
}, | ||
crdtOption, | ||
fwDataOption, | ||
createCrdtDirOption, | ||
dryRunOption); | ||
rootCommand.Add(syncCommand); | ||
return rootCommand.InvokeAsync(args); | ||
|
||
static ServiceProvider SyncServices(string crdtFile, string fwDataFile, bool createCrdtDir) | ||
{ | ||
if (!File.Exists(fwDataFile)) throw new InvalidOperationException("Could not find fwdata file " + fwDataFile); | ||
static ServiceProvider SyncServices(string crdtFile, string fwDataFile, bool createCrdtDir) | ||
{ | ||
if (!File.Exists(fwDataFile)) throw new InvalidOperationException("Could not find fwdata file " + fwDataFile); | ||
|
||
var fwDataProjectsFolder = Directory.GetParent(fwDataFile)?.Parent; | ||
if (fwDataProjectsFolder is null) throw new InvalidOperationException("Could not find parent folder of fwdata dir " + fwDataFile); | ||
var crdtFolder = Directory.GetParent(crdtFile); | ||
if (crdtFolder is null) throw new InvalidOperationException("Could not find parent folder of crdt file " + crdtFile); | ||
if (!crdtFolder.Exists && createCrdtDir) | ||
{ | ||
crdtFolder.Create(); | ||
} else if (!crdtFolder.Exists) | ||
{ | ||
throw new InvalidOperationException("Could not find crdt folder " + crdtFolder); | ||
} | ||
var fwDataProjectsFolder = Directory.GetParent(fwDataFile)?.Parent; | ||
if (fwDataProjectsFolder is null) throw new InvalidOperationException("Could not find parent folder of fwdata dir " + fwDataFile); | ||
var crdtFolder = Directory.GetParent(crdtFile); | ||
if (crdtFolder is null) throw new InvalidOperationException("Could not find parent folder of crdt file " + crdtFile); | ||
if (!crdtFolder.Exists && createCrdtDir) | ||
{ | ||
crdtFolder.Create(); | ||
} else if (!crdtFolder.Exists) | ||
{ | ||
throw new InvalidOperationException("Could not find crdt folder " + crdtFolder); | ||
} | ||
|
||
var crdtServices = new ServiceCollection() | ||
.AddLcmCrdtClient() | ||
.AddFwDataBridge() | ||
.AddFwLiteProjectSync() | ||
.Configure<FwDataBridgeConfig>(c => c.ProjectsFolder = fwDataProjectsFolder.FullName) | ||
.Configure<LcmCrdtConfig>(c => c.ProjectPath = crdtFolder.FullName) | ||
.AddLogging(builder => builder.AddConsole().AddDebug().AddConfiguration(new ConfigurationManager().AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
["Logging:LogLevel:Microsoft.EntityFrameworkCore"] = "Warning" | ||
}).Build())) | ||
.BuildServiceProvider(true); | ||
return crdtServices; | ||
var crdtServices = new ServiceCollection() | ||
.AddLcmCrdtClient() | ||
.AddFwDataBridge() | ||
.AddFwLiteProjectSync() | ||
.Configure<FwDataBridgeConfig>(c => c.ProjectsFolder = fwDataProjectsFolder.FullName) | ||
.Configure<LcmCrdtConfig>(c => c.ProjectPath = crdtFolder.FullName) | ||
.AddLogging(builder => builder.AddConsole().AddDebug().AddConfiguration(new ConfigurationManager().AddInMemoryCollection(new Dictionary<string, string?> | ||
{ | ||
["Logging:LogLevel:Microsoft.EntityFrameworkCore"] = "Warning" | ||
}).Build())) | ||
.BuildServiceProvider(true); | ||
return crdtServices; | ||
} | ||
} | ||
} |