105 lines
3.8 KiB
C#
105 lines
3.8 KiB
C#
|
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using Microsoft.Extensions.Configuration;
|
||
|
|
|
||
|
|
namespace Sozsoft.Platform.Data.Seeds;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Runtime'da duzenlenebilen seed dosyalarinin (SqlData / PostgresData / WizardData) klasorunu cozer.
|
||
|
|
/// <para>
|
||
|
|
/// Kod ile birlikte versiyonlanan sabit seed'ler (ornegin TenantDataSeeder'in okudugu
|
||
|
|
/// TenantData.json) buraya dahil degildir; onlar kendi projelerinin ciktisinda kalir.
|
||
|
|
/// </para>
|
||
|
|
/// <para>
|
||
|
|
/// Klasor bilincli olarak hicbir projenin icinde degildir: depoda <c>configs/seeds</c> altinda durur ve
|
||
|
|
/// konteynere bind mount ile baglanir. Boylece bir seed dosyasi degistiginde ne api ne de migrator
|
||
|
|
/// imaji yeniden derlenir; deploy sirasindaki <c>git pull</c> yeterlidir. Ayni klasor File Manager
|
||
|
|
/// uzerinden de duzenlenebildigi icin yapilan degisiklik dogrudan host diskinde kalici olur.
|
||
|
|
/// </para>
|
||
|
|
/// <para>
|
||
|
|
/// Cozum sirasi:
|
||
|
|
/// 1) <c>App:SeedsPath</c> ayari (konteynerde mount noktasi),
|
||
|
|
/// 2) calisma dizininden yukari dogru <c>configs/seeds</c> aramasi (local gelistirme),
|
||
|
|
/// 3) calisma dizininin altindaki <c>Seeds</c> klasoru (son care).
|
||
|
|
/// </para>
|
||
|
|
/// </summary>
|
||
|
|
public static class SeedPathResolver
|
||
|
|
{
|
||
|
|
public const string ConfigurationKey = "App:SeedsPath";
|
||
|
|
|
||
|
|
/// <summary>SQL Server saglayicisinin script klasoru.</summary>
|
||
|
|
public const string SqlDataFolder = "SqlData";
|
||
|
|
|
||
|
|
/// <summary>PostgreSQL saglayicisinin script klasoru.</summary>
|
||
|
|
public const string PostgresDataFolder = "PostgresData";
|
||
|
|
|
||
|
|
/// <summary>Host veritabani kapsamindaki scriptler.</summary>
|
||
|
|
public const string HostDataFolder = "HostData";
|
||
|
|
|
||
|
|
/// <summary>Tenant veritabani kapsamindaki scriptler.</summary>
|
||
|
|
public const string TenantDataFolder = "TenantData";
|
||
|
|
|
||
|
|
/// <summary>Yalnizca olusturulan/guncellenen nesne scriptleri.</summary>
|
||
|
|
public const string ObjectDataFolder = "ObjectData";
|
||
|
|
|
||
|
|
/// <summary>Olusturulan, ayrica tum migration'lar bittikten sonra calistirilan scriptler.</summary>
|
||
|
|
public const string ExecuteDataFolder = "ExecuteData";
|
||
|
|
|
||
|
|
/// <summary>Wizard'in urettigi konfigurasyon dosyalari.</summary>
|
||
|
|
public const string WizardDataFolder = "WizardData";
|
||
|
|
|
||
|
|
private const string RepositorySeedsFolder = "seeds";
|
||
|
|
private const string RepositoryConfigsFolder = "configs";
|
||
|
|
private const string FallbackFolderName = "Seeds";
|
||
|
|
|
||
|
|
/// <summary>Seed kok klasorunun tam yolunu dondurur. Klasorun var olmasi garanti edilmez.</summary>
|
||
|
|
public static string GetRootPath(IConfiguration? configuration)
|
||
|
|
{
|
||
|
|
var configuredPath = configuration?[ConfigurationKey];
|
||
|
|
if (!string.IsNullOrWhiteSpace(configuredPath))
|
||
|
|
{
|
||
|
|
return Path.GetFullPath(configuredPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
var repositoryPath = FindRepositorySeedsPath();
|
||
|
|
if (repositoryPath != null)
|
||
|
|
{
|
||
|
|
return repositoryPath;
|
||
|
|
}
|
||
|
|
|
||
|
|
return Path.Combine(Directory.GetCurrentDirectory(), FallbackFolderName);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Seed kok klasorunun altindaki bir alt yolu dondurur.</summary>
|
||
|
|
public static string GetPath(IConfiguration? configuration, params string[] subPaths)
|
||
|
|
{
|
||
|
|
var path = GetRootPath(configuration);
|
||
|
|
|
||
|
|
foreach (var subPath in subPaths)
|
||
|
|
{
|
||
|
|
path = Path.Combine(path, subPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
return path;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Calisma dizininden yukari dogru cikarak depodaki configs/seeds klasorunu arar.</summary>
|
||
|
|
private static string? FindRepositorySeedsPath()
|
||
|
|
{
|
||
|
|
var dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||
|
|
|
||
|
|
while (dir != null)
|
||
|
|
{
|
||
|
|
var candidate = Path.Combine(dir.FullName, RepositoryConfigsFolder, RepositorySeedsFolder);
|
||
|
|
if (Directory.Exists(candidate))
|
||
|
|
{
|
||
|
|
return candidate;
|
||
|
|
}
|
||
|
|
|
||
|
|
dir = dir.Parent;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|