Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change binding files generation location #22

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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