Skip to content

Commit

Permalink
Merge pull request #11 from semihcelek/bugfix/composition-revision
Browse files Browse the repository at this point in the history
Aspects can't generate to a read-only directory bug is solved.
  • Loading branch information
semihcelek committed Jul 8, 2023
2 parents 1270071 + 478a037 commit bba614d
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 27 deletions.
33 changes: 33 additions & 0 deletions Gum.Composer/AspectType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

namespace Gum.Composer
{
public readonly struct AspectType : IEquatable<AspectType>
{
public readonly int Value;

public AspectType(int value)
{
Value = value;
}

public bool Equals(AspectType other)
{
return Value == other.Value;
}

public override bool Equals(object obj)
{
return obj is AspectType other && Equals(other);
}

public override int GetHashCode()
{
return Value;
}

public static implicit operator int(AspectType aspectType) => aspectType.Value;

public static implicit operator AspectType(int value) => new AspectType(value);
}
}
11 changes: 7 additions & 4 deletions Gum.Composer/CodeGen/CompositionCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static class CompositionCodeGenerator
TAB + TAB + LINE + CTOR_CONTENT +
LINE + TAB + TAB + "}";
private const string ASPECT_TYPE_TEMPLATE =
TAB + TAB + "public const " + ASPECT_TYPE + " ASPECT_TYPE = " + ASPECT_TYPE + "." + OBJECT_NAME + ";" +
TAB + TAB + "public static readonly " + NAMESPACE + "." + ASPECT_TYPE + " ASPECT_TYPE = " + "(int)" + ASPECT_TYPE + "." + OBJECT_NAME + ";" +
LINE + LINE +
TAB + TAB + "public " + ASPECT_TYPE + " Type => ASPECT_TYPE;" + LINE + LINE;
TAB + TAB + "public " + NAMESPACE + "." + ASPECT_TYPE + " Type => ASPECT_TYPE;" + LINE + LINE;
private const string FIELD_TEMPLATE =
TAB + TAB + "public readonly " + TYPE + " " + FIELD_NAME + ";" + LINE + LINE;

Expand All @@ -57,15 +57,18 @@ public static void Run()
{
string aspectName = aspectPrototype.Name;
bodyStringBuilder.Append(ASPECT_TYPE_TEMPLATE
.Replace(OBJECT_NAME, aspectName));
.Replace(OBJECT_NAME, aspectName)
.Replace(NAMESPACE, UserConfig.NAMESPACE));

int argCounter = 0;
foreach (KeyValuePair<string, string> kvp in aspectPrototype.Fields)
{
string fieldName = kvp.Key;
string type = kvp.Value;
bodyStringBuilder.Append(FIELD_TEMPLATE
.Replace(TYPE, type)
.Replace(FIELD_NAME, fieldName));
.Replace(FIELD_NAME, fieldName)
.Replace(NAMESPACE, UserConfig.NAMESPACE));

string argName = $"arg{argCounter}";
argsStringBuilder.Append($"{type} {argName}, ");
Expand Down
1 change: 0 additions & 1 deletion Gum.Composer/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Runtime.CompilerServices;
using Gum.Composer.Exception;
using Gum.Composer.Generated;
using Gum.Composer.Internal;
using Gum.Pooling;
using Gum.Pooling.Collections;
Expand Down
6 changes: 0 additions & 6 deletions Gum.Composer/Generated/AspectType.cs

This file was deleted.

Empty file removed Gum.Composer/Generated/Aspects.cs
Empty file.
1 change: 0 additions & 1 deletion Gum.Composer/Generated/Types.gum

This file was deleted.

4 changes: 1 addition & 3 deletions Gum.Composer/IAspect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Gum.Composer.Generated;

namespace Gum.Composer
namespace Gum.Composer
{
public interface IAspect
{
Expand Down
2 changes: 1 addition & 1 deletion Gum.Composer/Internal/AspectDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Gum.Composer.Generated;
using Gum.Core.Utility;
using Gum.Composer;

namespace Gum.Composer.Internal
{
Expand Down
2 changes: 1 addition & 1 deletion Gum.Composer/Unity/Editor/AspectCreationEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void AddNewType()
TypeNameValidationResult typeNameValidationResult = ValidateTypeName(out string fullName);
if (typeNameValidationResult != TypeNameValidationResult.Success)
{
Debug.LogError(
Debug.LogError(
$"Error while trying to add type: {_typeName} Error code: {typeNameValidationResult}");
return;
}
Expand Down
31 changes: 26 additions & 5 deletions Gum.Composer/UserConfig.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using System;
using System.IO;
using System.IO;
using Gum.Core.Assert;

namespace Gum.Composer
{
public static class UserConfig
{
private static readonly string ProjectDirectory = GetProjectDirectory(Directory.GetCurrentDirectory());
public static readonly string AspectsDirectoryPath = $@"{ProjectDirectory}\Aspects";
public static readonly string OutputDirectoryPath = $@"{ProjectDirectory}\Generated";
public static readonly string AspectsDirectoryPath = $@"{ProjectDirectory}\{OUTPUT_FOLDER_NAME_PATTERN}\{ASPECT_FOLDER_NAME_PATTERN}";
public static readonly string OutputDirectoryPath = $@"{ProjectDirectory}\{OUTPUT_FOLDER_NAME_PATTERN}";

public const string NAMESPACE = @"Gum.Composer";
public const string FOLDER_SEARCH_PATTERN = "Gum.Composer";
public const string FOLDER_SEARCH_PATTERN = "Assets";
private const string OUTPUT_FOLDER_NAME_PATTERN = @"Gum\Composer\Generated";
private const string ASPECT_FOLDER_NAME_PATTERN = "Aspects";

private static string GetProjectDirectory(string directory)
{
if (TryGetProjectDirectory(directory, out string foundDirectory))
{
CreateRequiredFoldersIfNotExist(foundDirectory);

return foundDirectory;
}

Expand All @@ -42,6 +45,24 @@ private static bool TryGetProjectDirectory(string directory, out string foundDir
}

return false;

}

private static void CreateRequiredFoldersIfNotExist(string foundDirectory)
{
string[] directories;

try
{
directories = Directory.GetDirectories(foundDirectory, OUTPUT_FOLDER_NAME_PATTERN, SearchOption.AllDirectories);
}
catch (DirectoryNotFoundException directoryNotFoundException)

Check warning on line 59 in Gum.Composer/UserConfig.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'directoryNotFoundException' is declared but never used
{
string outputFolderNamePattern = $@"{foundDirectory}\{OUTPUT_FOLDER_NAME_PATTERN}";

Directory.CreateDirectory(outputFolderNamePattern);
Directory.CreateDirectory($@"{outputFolderNamePattern}\{ASPECT_FOLDER_NAME_PATTERN}");
}
}
}
}
9 changes: 4 additions & 5 deletions Tests/CompositionTests/TestAspects.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Gum.Composer;
using Gum.Composer.Generated;

namespace Tests.CompositionTests
{
public readonly struct BarAspect : IAspect
{
public const AspectType ASPECT_TYPE = 0;
public static readonly Gum.Composer.AspectType ASPECT_TYPE = 0;

public AspectType Type => ASPECT_TYPE;
public Gum.Composer.AspectType Type => ASPECT_TYPE;

public readonly int MyInt;

Expand All @@ -19,9 +18,9 @@ public BarAspect(int myInt)

public readonly struct FooAspect : IAspect
{
public const AspectType ASPECT_TYPE = (AspectType)1;
public static readonly Gum.Composer.AspectType ASPECT_TYPE = 1;

public AspectType Type => ASPECT_TYPE;
public Gum.Composer.AspectType Type => ASPECT_TYPE;

public readonly int MyInt;

Expand Down

0 comments on commit bba614d

Please sign in to comment.