Skip to content

Commit

Permalink
Change binding files generation location
Browse files Browse the repository at this point in the history
  • Loading branch information
Eastrall committed Sep 22, 2022
1 parent 2d9bffd commit 38ea7f2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
3 changes: 1 addition & 2 deletions Editor/Scripts/Generator/RosalinaBindingsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -78,7 +77,7 @@ public RosalinaGenerationResult Generate(UIDocumentAsset document, string output
.ToFullString();
string generatedCode = GeneratedCodeHeader + code;

return new RosalinaGenerationResult(generatedCode, Path.Combine(document.Path, outputFileName));
return new RosalinaGenerationResult(generatedCode, outputFileName);
}

private static MemberDeclarationSyntax CreateDocumentVariable()
Expand Down
2 changes: 1 addition & 1 deletion Editor/Scripts/Generator/RosalinaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public RosalinaGenerationResult Generate(UIDocumentAsset document, string output
.NormalizeWhitespace()
.ToFullString();

return new RosalinaGenerationResult(code, Path.Combine(document.Path, outputFileName));
return new RosalinaGenerationResult(code, outputFileName);
}
}

Expand Down
7 changes: 6 additions & 1 deletion Editor/Scripts/MenuItems/RosalinaGenerateBindingsMenuItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
Expand All @@ -19,7 +20,11 @@ private static void GenerateUIBindings()
EditorUtility.DisplayProgressBar("Rosalina", $"Generating {document.Name} UI bindings...", 50);
Debug.Log($"[Rosalina]: Generating UI bindings for {assetPath}");

RosalinaGenerationResult result = RosalinaGenerator.GenerateBindings(document, $"{document.Name}.g.cs");
string generatedBindingsScriptName = $"{document.Name}.g.cs";
string generatedBindingsScriptPath = RosalinaGenerator.BuildAutoGeneratedFilePath(generatedBindingsScriptName);
RosalinaGenerationResult result = RosalinaGenerator.GenerateBindings(document, generatedBindingsScriptPath);

RosalinaGenerator.DeleteGeneartedFile(Path.Combine(document.Path, generatedBindingsScriptName));
result.Save();

AssetDatabase.Refresh();
Expand Down
2 changes: 1 addition & 1 deletion Editor/Scripts/MenuItems/RosalinaGenerateScriptMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool GenerateUIScriptValidation()
private static bool TryGenerateBindings(UIDocumentAsset document)
{
string generatedBindingsScriptName = $"{document.Name}.g.cs";
string generatedBindingsScriptPath = Path.Combine(document.Path, generatedBindingsScriptName);
string generatedBindingsScriptPath = RosalinaGenerator.BuildAutoGeneratedFilePath(generatedBindingsScriptName);

if (!File.Exists(generatedBindingsScriptPath) && AskGenerateBindings())
{
Expand Down
6 changes: 5 additions & 1 deletion Editor/Scripts/RosalinaAssetProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del
EditorUtility.DisplayProgressBar("Rosalina", $"Generating {document.Name} bindings...", GeneratePercentage(i, uiFilesChanged.Length));
Debug.Log($"[Rosalina]: Generating UI bindings for {uiDocumentPath}");

RosalinaGenerationResult result = RosalinaGenerator.GenerateBindings(document, $"{document.Name}.g.cs");
string generatedBindingsScriptName = $"{document.Name}.g.cs";
string generatedBindingsScriptPath = RosalinaGenerator.BuildAutoGeneratedFilePath(generatedBindingsScriptName);
RosalinaGenerationResult result = RosalinaGenerator.GenerateBindings(document, generatedBindingsScriptPath);

RosalinaGenerator.DeleteGeneartedFile(Path.Combine(document.Path, generatedBindingsScriptName));
result.Save();

Debug.Log($"[Rosalina]: Done generating: {document.Name} (output: {result.OutputFilePath})");
Expand Down
2 changes: 1 addition & 1 deletion Editor/Scripts/RosalinaConstants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#if UNITY_EDITOR
public class RosalinaConstants
{
public const string Version = "1.2.1";
public const string Version = "1.3.0";
}
#endif
40 changes: 40 additions & 0 deletions Editor/Scripts/RosalinaGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
#if UNITY_EDITOR
using System.IO;

internal static class RosalinaGenerator
{
public const string RosalinaFolderName = "Rosalina";
public const string RosalinaGeneratedCodeFolderName = "AutoGenerated";

/// <summary>
/// Creates the auto generated code file path based on the given output file name.
/// </summary>
/// <param name="outputFileName">Output file name.</param>
/// <returns>Auto generated file path.</returns>
public static string BuildAutoGeneratedFilePath(string outputFileName)
{
string autogeneratedFolder = Path.Combine("Assets", RosalinaFolderName, RosalinaGeneratedCodeFolderName);

if (!Directory.Exists(autogeneratedFolder))
{
Directory.CreateDirectory(autogeneratedFolder);
}

return Path.Combine(autogeneratedFolder, outputFileName);
}

/// <summary>
/// Generates a C# script containing the bindings of the given UI document.
/// </summary>
Expand All @@ -22,5 +44,23 @@ public static RosalinaGenerationResult GenerateScript(UIDocumentAsset document,
{
return new RosalinaScriptGenerator().Generate(document, outputFileName);
}

/// <summary>
/// Deletes the given generated file.
/// </summary>
/// <param name="generatedFilePath">Generated file path.</param>
/// <remarks>
/// Prior to version 1.3.0, the UXML bindings were generated next to the .uxml document.
/// In newer versions, documents are generated in Assets/Rosalina/AutoGenerated.
/// This method is used in bindings generator to delete the previous generated files.
/// In the future, this method will be deleted.
/// </remarks>
public static void DeleteGeneartedFile(string generatedFilePath)
{
if (File.Exists(generatedFilePath))
{
File.Delete(generatedFilePath);
}
}
}
#endif
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.eastylabs.rosalina",
"version": "1.2.1",
"version": "1.3.0",
"type": "tool",
"displayName": "Rosalina",
"unity": "2021.2",
Expand Down

0 comments on commit 38ea7f2

Please sign in to comment.