Refactor folder structure
This commit is contained in:
9
src/Editor/Ghost.Data/AssemblyInfo.cs
Normal file
9
src/Editor/Ghost.Data/AssemblyInfo.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Ghost.Core.Attributes;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Ghost.Editor")]
|
||||
[assembly: InternalsVisibleTo("Ghost.Editor.Core")]
|
||||
[assembly: InternalsVisibleTo("Ghost.UnitTest")]
|
||||
[assembly: InternalsVisibleTo("Ghost.MicroTest")]
|
||||
|
||||
[assembly: EngineAssembly]
|
||||
BIN
src/Editor/Ghost.Data/Assets/ProjectTemplates/Empty.zip
Normal file
BIN
src/Editor/Ghost.Data/Assets/ProjectTemplates/Empty.zip
Normal file
Binary file not shown.
33
src/Editor/Ghost.Data/Ghost.Data.csproj
Normal file
33
src/Editor/Ghost.Data/Ghost.Data.csproj
Normal file
@@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<IsAotCompatible>True</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<IsAotCompatible>True</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="System.Data.SQLite" Version="1.0.119" />
|
||||
<PackageReference Include="System.Drawing.Common" Version="4.7.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Assets\ProjectTemplates\Empty.zip">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ghost.Core\Ghost.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
11
src/Editor/Ghost.Data/JsonContext.cs
Normal file
11
src/Editor/Ghost.Data/JsonContext.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Ghost.Data.Models;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ghost.Data;
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(TemplateInfo))]
|
||||
[JsonSerializable(typeof(ProjectMetadata))]
|
||||
internal partial class JsonContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
22
src/Editor/Ghost.Data/Models/ProjectInfo.cs
Normal file
22
src/Editor/Ghost.Data/Models/ProjectInfo.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Ghost.Data.Models;
|
||||
|
||||
internal class ProjectInfo
|
||||
{
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int ID
|
||||
{
|
||||
get; internal set;
|
||||
}
|
||||
|
||||
public required string Name
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required string MetadataPath
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
53
src/Editor/Ghost.Data/Models/ProjectMetadata.cs
Normal file
53
src/Editor/Ghost.Data/Models/ProjectMetadata.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
namespace Ghost.Data.Models;
|
||||
|
||||
public class ProjectMetadata
|
||||
{
|
||||
public const string PROJECT_FILE_EXTENSION_NAME = "gproj";
|
||||
|
||||
public Guid ID
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public Version EngineVersion
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public DateTime CreatedAt
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public DateTime LastOpened
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public ProjectMetadata(string name, Version engineVersion)
|
||||
{
|
||||
ID = Guid.NewGuid();
|
||||
Name = name;
|
||||
EngineVersion = engineVersion;
|
||||
CreatedAt = DateTime.UtcNow;
|
||||
LastOpened = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
// Parameterless constructor for deserialization
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
|
||||
public ProjectMetadata()
|
||||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct ProjectMetadataInfo(string path, ProjectMetadata metadata)
|
||||
{
|
||||
public readonly string Path => path;
|
||||
public readonly ProjectMetadata Metadata => metadata;
|
||||
}
|
||||
44
src/Editor/Ghost.Data/Models/TemplateInfo.cs
Normal file
44
src/Editor/Ghost.Data/Models/TemplateInfo.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
namespace Ghost.Data.Models;
|
||||
|
||||
public class TemplateInfo
|
||||
{
|
||||
public required string Name
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public string? Description
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required Version TemplateVersion
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public required Version EngineVersion
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
|
||||
public struct TemplateData(string templatePath, TemplateInfo info)
|
||||
{
|
||||
private const string _ICON_NAME = "icon.png";
|
||||
private const string _PREVIEW_NAME = "preview.png";
|
||||
|
||||
public string directory = Path.GetDirectoryName(templatePath)!;
|
||||
|
||||
public readonly TemplateInfo Info => info;
|
||||
|
||||
public readonly Uri GetIconURI()
|
||||
{
|
||||
return new Uri(Path.Combine(directory, _ICON_NAME));
|
||||
}
|
||||
|
||||
public readonly Uri GetPreviewURI()
|
||||
{
|
||||
return new Uri(Path.Combine(directory, _PREVIEW_NAME));
|
||||
}
|
||||
}
|
||||
172
src/Editor/Ghost.Data/Repository/ProjectRepository.cs
Normal file
172
src/Editor/Ghost.Data/Repository/ProjectRepository.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using Ghost.Data.Models;
|
||||
using Ghost.Data.Resources;
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace Ghost.Data.Repository;
|
||||
|
||||
internal static class ProjectRepository
|
||||
{
|
||||
private static class Command
|
||||
{
|
||||
public const string CONNECTION_STRING = "Data Source={0}\\projects.db;Version=3;";
|
||||
public const string CREATE_PROJECT_TABLE_STRING = "CREATE TABLE IF NOT EXISTS Projects (ID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, MetadataPath TEXT);";
|
||||
public const string SELECT_PROJECT_STRING = "SELECT * FROM Projects";
|
||||
public const string INSERT_PROJECT_STRING = "INSERT INTO Projects (Name, MetadataPath) VALUES (@Name, @MetadataPath);";
|
||||
public const string REMOVE_PROJECT_STRING = "DELETE FROM Projects WHERE ID = @ID;";
|
||||
public const string UPDATE_PROJECT_STRING = "UPDATE Projects SET Name = @Name, MetadataPath = @MetadataPath WHERE ID = @ID;";
|
||||
}
|
||||
|
||||
private static async Task EnsureTableCreatedAsync(SQLiteConnection connection)
|
||||
{
|
||||
using var createCommand = connection.CreateCommand();
|
||||
createCommand.CommandText = Command.CREATE_PROJECT_TABLE_STRING;
|
||||
await createCommand.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public static async IAsyncEnumerable<ProjectInfo> GetAllProjectsAsync()
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.SELECT_PROJECT_STRING;
|
||||
|
||||
using var reader = command.ExecuteReader();
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
var project = new ProjectInfo
|
||||
{
|
||||
ID = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
MetadataPath = reader.GetString(2),
|
||||
};
|
||||
|
||||
yield return project;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<ProjectInfo?> GetProjectByIdAsync(int id)
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.SELECT_PROJECT_STRING + " WHERE ID = @ID;";
|
||||
|
||||
command.Parameters.AddWithValue("@ID", id);
|
||||
|
||||
using var reader = await command.ExecuteReaderAsync();
|
||||
|
||||
if (await reader.ReadAsync())
|
||||
{
|
||||
return new ProjectInfo
|
||||
{
|
||||
ID = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
MetadataPath = reader.GetString(2),
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static async Task<ProjectInfo?> GetProjectByNameAsync(string name)
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.SELECT_PROJECT_STRING + " WHERE Name = @Name;";
|
||||
|
||||
command.Parameters.AddWithValue("@Name", name);
|
||||
|
||||
using var reader = await command.ExecuteReaderAsync();
|
||||
if (await reader.ReadAsync())
|
||||
{
|
||||
return new ProjectInfo
|
||||
{
|
||||
ID = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
MetadataPath = reader.GetString(2),
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static async Task<ProjectInfo?> GetProjectByMetadataPathAsync(string metadataPath)
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.SELECT_PROJECT_STRING + " WHERE MetadataPath = @MetadataPath;";
|
||||
|
||||
command.Parameters.AddWithValue("@MetadataPath", metadataPath);
|
||||
|
||||
using var reader = await command.ExecuteReaderAsync();
|
||||
if (await reader.ReadAsync())
|
||||
{
|
||||
return new ProjectInfo
|
||||
{
|
||||
ID = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
MetadataPath = reader.GetString(2),
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static async Task AddProjectAsync(ProjectInfo project)
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
await EnsureTableCreatedAsync(connection);
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.INSERT_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@Name", project.Name);
|
||||
command.Parameters.AddWithValue("@MetadataPath", project.MetadataPath);
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public static async Task RemoveProjectAsync(ProjectInfo project)
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.REMOVE_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@ID", project.ID);
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
public static async Task UpdateProjectAsync(ProjectInfo project)
|
||||
{
|
||||
using var connection = new SQLiteConnection(string.Format(Command.CONNECTION_STRING, DataPath.s_applicationDataFolder));
|
||||
connection.Open();
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = Command.UPDATE_PROJECT_STRING;
|
||||
|
||||
command.Parameters.AddWithValue("@Name", project.Name);
|
||||
command.Parameters.AddWithValue("@MetadataPath", project.MetadataPath);
|
||||
command.Parameters.AddWithValue("@ID", project.ID);
|
||||
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
8
src/Editor/Ghost.Data/Resources/AssetsPath.cs
Normal file
8
src/Editor/Ghost.Data/Resources/AssetsPath.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Ghost.Data.Resources;
|
||||
|
||||
public static class AssetsPath
|
||||
{
|
||||
public const string ASSETS_FOLDER = "Assets";
|
||||
|
||||
public readonly static string s_appIconPath = Path.Combine(AppContext.BaseDirectory, $"{ASSETS_FOLDER}/Icon-256.ico");
|
||||
}
|
||||
9
src/Editor/Ghost.Data/Resources/DataPath.cs
Normal file
9
src/Editor/Ghost.Data/Resources/DataPath.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Ghost.Data.Resources;
|
||||
|
||||
public class DataPath
|
||||
{
|
||||
public const string ENGINE_DATA_FOLDER_NAME = "GhostEngine";
|
||||
|
||||
public readonly static string s_applicationDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ENGINE_DATA_FOLDER_NAME);
|
||||
public readonly static string s_projectTemplateFolder = Path.Combine(s_applicationDataFolder, "ProjectTemplates");
|
||||
}
|
||||
226
src/Editor/Ghost.Data/Services/ProjectService.cs
Normal file
226
src/Editor/Ghost.Data/Services/ProjectService.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Data.Models;
|
||||
using Ghost.Data.Repository;
|
||||
using Ghost.Data.Resources;
|
||||
using System.IO.Compression;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Ghost.Data.Services;
|
||||
|
||||
internal partial class ProjectService
|
||||
{
|
||||
private const string _TEMPLATE_CONTENT_FILE = "content.zip";
|
||||
|
||||
public const string ASSETS_FOLDER = "Assets";
|
||||
public const string CACHE_FOLDER = "Caches";
|
||||
public const string CONFIG_FOLDER = "Configs";
|
||||
|
||||
public static ProjectMetadataInfo CurrentProject
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public static void EnsureDefaultTemplate()
|
||||
{
|
||||
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
|
||||
if (templates.Length > 0)
|
||||
{
|
||||
return; // Default template already exists
|
||||
}
|
||||
|
||||
var defaultTemplatePath = Path.Combine(AppContext.BaseDirectory, "Assets/ProjectTemplates/Empty.zip");
|
||||
ZipFile.ExtractToDirectory(defaultTemplatePath, DataPath.s_projectTemplateFolder, true);
|
||||
}
|
||||
|
||||
public static async IAsyncEnumerable<(string path, TemplateInfo info)> GetProjectTemplatesAsync()
|
||||
{
|
||||
var templatesFolder = DataPath.s_projectTemplateFolder;
|
||||
if (!Directory.Exists(templatesFolder))
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
var templates = Directory.GetFiles(DataPath.s_projectTemplateFolder, "template.json", SearchOption.AllDirectories);
|
||||
foreach (var templatePath in templates)
|
||||
{
|
||||
var fileStream = File.OpenRead(templatePath);
|
||||
var templateInfo = await JsonSerializer.DeserializeAsync<TemplateInfo>(fileStream, JsonContext.Default.TemplateInfo);
|
||||
if (templateInfo == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return (templatePath, templateInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task CreateMetadataFileAsync(string path, ProjectMetadata metadata)
|
||||
{
|
||||
await using var fileStream = File.Create(path);
|
||||
await JsonSerializer.SerializeAsync(fileStream, metadata, JsonContext.Default.ProjectMetadata);
|
||||
}
|
||||
|
||||
public static async Task<ProjectMetadata?> LoadMetadataAsync(string ghostprojPath)
|
||||
{
|
||||
if (!File.Exists(ghostprojPath))
|
||||
{
|
||||
throw new FileNotFoundException("Project metadata file not found.", ghostprojPath);
|
||||
}
|
||||
|
||||
await using var fileStream = File.OpenRead(ghostprojPath);
|
||||
return await JsonSerializer.DeserializeAsync<ProjectMetadata>(fileStream, JsonContext.Default.ProjectMetadata);
|
||||
}
|
||||
|
||||
public static async Task<Result<ProjectMetadataInfo>> ValidateProjectDirectoryAsync(string? projectDirectory)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(projectDirectory) || !Directory.Exists(projectDirectory))
|
||||
{
|
||||
return Result<ProjectMetadataInfo>.Failure("Project directory is invalid or does not exist.");
|
||||
}
|
||||
|
||||
var projectAssetsPath = Path.Combine(projectDirectory, ASSETS_FOLDER);
|
||||
var projectConfigPath = Path.Combine(projectDirectory, CONFIG_FOLDER);
|
||||
if (!Directory.Exists(projectAssetsPath) || !Directory.Exists(projectConfigPath))
|
||||
{
|
||||
return Result<ProjectMetadataInfo>.Failure("Project folder structure is invalid.");
|
||||
}
|
||||
|
||||
var metadataPath = Directory.GetFiles(projectDirectory, $"*.{ProjectMetadata.PROJECT_FILE_EXTENSION_NAME}", SearchOption.TopDirectoryOnly).FirstOrDefault();
|
||||
if (string.IsNullOrWhiteSpace(metadataPath) || !File.Exists(metadataPath))
|
||||
{
|
||||
return Result<ProjectMetadataInfo>.Failure("Project metadata file not found.");
|
||||
}
|
||||
|
||||
var metadata = await LoadMetadataAsync(metadataPath);
|
||||
if (metadata == null)
|
||||
{
|
||||
return Result<ProjectMetadataInfo>.Failure("Project metadata file is corrupted or invalid.");
|
||||
}
|
||||
|
||||
return new ProjectMetadataInfo(metadataPath, metadata);
|
||||
}
|
||||
|
||||
private static async ValueTask SetupRequestFolderAsync(string projectDirectory, string templateDirectory)
|
||||
{
|
||||
var projectAssetsPath = Path.Combine(projectDirectory, ASSETS_FOLDER);
|
||||
var projectConfigPath = Path.Combine(projectDirectory, CONFIG_FOLDER);
|
||||
var templateContentPath = Path.Combine(templateDirectory, _TEMPLATE_CONTENT_FILE);
|
||||
|
||||
Directory.CreateDirectory(projectAssetsPath);
|
||||
if (File.Exists(templateContentPath))
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
ZipFile.ExtractToDirectory(templateContentPath, projectAssetsPath);
|
||||
});
|
||||
}
|
||||
|
||||
Directory.CreateDirectory(projectConfigPath);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class ProjectService
|
||||
{
|
||||
public Task AddProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return ProjectRepository.AddProjectAsync(project);
|
||||
}
|
||||
|
||||
public async Task<ProjectInfo> AddProjectAsync(string name, string path)
|
||||
{
|
||||
var project = new ProjectInfo
|
||||
{
|
||||
Name = name,
|
||||
MetadataPath = path,
|
||||
};
|
||||
await ProjectRepository.AddProjectAsync(project);
|
||||
|
||||
return project;
|
||||
}
|
||||
|
||||
public Task RemoveProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return ProjectRepository.RemoveProjectAsync(project);
|
||||
}
|
||||
|
||||
public Task UpdateProjectAsync(ProjectInfo project)
|
||||
{
|
||||
return ProjectRepository.UpdateProjectAsync(project);
|
||||
}
|
||||
|
||||
public async Task<bool> HasProjectAsync(string path)
|
||||
{
|
||||
return await ProjectRepository.GetProjectByMetadataPathAsync(path) != null;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ProjectInfo> GetAllProjectAsync()
|
||||
{
|
||||
var badProjectList = new List<ProjectInfo>();
|
||||
await foreach (var project in ProjectRepository.GetAllProjectsAsync())
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(project.MetadataPath) || !File.Exists(project.MetadataPath))
|
||||
{
|
||||
badProjectList.Add(project);
|
||||
continue;
|
||||
}
|
||||
|
||||
yield return project;
|
||||
}
|
||||
|
||||
foreach (var badProject in badProjectList)
|
||||
{
|
||||
await ProjectRepository.RemoveProjectAsync(badProject);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<ProjectMetadataInfo>> CreateProjectAsync(string projectName, string projectDirectory, Version engineVersion, string templatePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var projectPath = Path.Combine(projectDirectory, projectName);
|
||||
if (!Directory.Exists(projectPath))
|
||||
{
|
||||
Directory.CreateDirectory(projectPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if folder is empty
|
||||
if (Directory.EnumerateFiles(projectPath, "*", SearchOption.AllDirectories).Any())
|
||||
{
|
||||
return Result.Failure("Directory is not empty");
|
||||
}
|
||||
}
|
||||
|
||||
var metadata = new ProjectMetadata(projectName, engineVersion);
|
||||
var metadataPath = Path.Combine(projectPath, $"{projectName}.{ProjectMetadata.PROJECT_FILE_EXTENSION_NAME}");
|
||||
await CreateMetadataFileAsync(metadataPath, metadata);
|
||||
await SetupRequestFolderAsync(projectPath, templatePath);
|
||||
|
||||
var info = await AddProjectAsync(projectName, metadataPath);
|
||||
return Result.Success(new ProjectMetadataInfo(metadataPath, metadata));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return Result.Failure($"Failed to create project: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<ProjectMetadataInfo>> AddProjectFromDirectoryAsync(string projectDirectory)
|
||||
{
|
||||
var result = await ValidateProjectDirectoryAsync(projectDirectory);
|
||||
if (result.IsFailure)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
if (await HasProjectAsync(result.Value.Path))
|
||||
{
|
||||
return Result.Failure("Project already exists.");
|
||||
}
|
||||
|
||||
await AddProjectAsync(result.Value.Metadata.Name, result.Value.Path);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user