Skip to content

Commit

Permalink
Don't show Update Yarn Commands when source generators are available
Browse files Browse the repository at this point in the history
  • Loading branch information
desplesda committed Mar 2, 2023
1 parent 7495bf2 commit 31430c6
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions Editor/Analysis/ActionSourceCodeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#if UNITY_2021_2_OR_NEWER
// Source generators are only available on Unity 2021.2 and later.
#define SOURCE_GENERATOR_AVAILABLE
#endif

using UnityEngine;
using Yarn.Unity;
using UnityEditor;
Expand All @@ -8,14 +13,31 @@ namespace Yarn.Unity.Editor
public static class ActionSourceCodeGenerator
{

/// <summary>
/// Get a path in the current project that can be used for storing
/// manually-generated Yarn Action registration code.
/// </summary>
/// <remarks>
/// This property checks to see if a file exists in the Assets folder
/// that is both named "YarnActionRegistration.cs", and contains a
/// marker indicating that it was generated by Yarn Spinner's code
/// generation systems. If this is found, the path to the file is
/// returned. Otherwise, the path
/// <c>Assets/YarnActionRegistration.cs</c> is returned.
/// </remarks>
public static string GeneratedSourcePath
{
get
{
const string YarnRegistrationFileName = "YarnActionRegistration.cs";
const string DefaultOutputFilePath = "Assets/" + YarnRegistrationFileName;

// Note the lack of a closing parenthesis in this string - we
// only want to check to see if it was generated by
// "YarnActionAnalyzer", not any specific version of that
// analyzer
const string YarnGeneratedCodeSignature = "GeneratedCode(\"YarnActionAnalyzer\"";

var existingFile = System.IO.Directory.EnumerateFiles(System.Environment.CurrentDirectory, YarnRegistrationFileName, System.IO.SearchOption.AllDirectories).FirstOrDefault();

if (existingFile == null)
Expand All @@ -27,10 +49,14 @@ public static string GeneratedSourcePath
try
{
var text = System.IO.File.ReadAllText(existingFile);
return text.Contains(YarnGeneratedCodeSignature) ? existingFile : DefaultOutputFilePath;
return text.Contains(YarnGeneratedCodeSignature)
? existingFile
: DefaultOutputFilePath;
}
catch (System.Exception e)
{
// Something happened while checking the file. Return
// our default, and log that we encountered a problem.
Debug.LogWarning($"Can't check to see if {existingFile} is a valid action registration script, using {DefaultOutputFilePath} instead: {e}");
return DefaultOutputFilePath;
}
Expand All @@ -39,7 +65,32 @@ public static string GeneratedSourcePath
}
}

#if SOURCE_GENERATOR_AVAILABLE
#pragma warning disable IDE0051 // private member is unused

// On versions of Unity where source generators are not available, we
// provide a menu item that manually generates the appropriate code that
// registers actions when the domain reloads.
//
// We don't need to do this when source generators ARE available, so we
// don't offer this item in this case.
[MenuItem("Window/Yarn Spinner/Update Yarn Commands")]
private static void OnUpdateYarnCommands()
{
GenerateYarnActionSourceCode();
}
#pragma warning restore IDE0051
#endif

/// <summary>
/// Generates and imports a C# source code file in the project
/// containing Yarn Action registration code at the path indicated by
/// <see cref="GeneratedSourcePath"/>.
/// </summary>
/// <remarks>
/// This method should not be called in projects where Unity has support
/// for source generators (i.e. Unity 2021.2 and later).
/// </remarks>
public static void GenerateYarnActionSourceCode()
{
var analysis = new Yarn.Unity.ActionAnalyser.Analyser("Assets");
Expand Down

0 comments on commit 31430c6

Please sign in to comment.