Skip to content

Commit

Permalink
fix(versionumber): the updateversionfrompackagejson function should n…
Browse files Browse the repository at this point in the history
…ot use a static path (#103)

* change to a dynamic path

it now use a dynamic path based on the assembly definition

* fix: because the folder structure have changed
  • Loading branch information
Morgan-6Freedom authored Jan 22, 2024
1 parent 9aeff4a commit 31029b9
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions package/Editor/Plugins/PluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Linq;

namespace Scenario
{
public class PluginSettings : EditorWindow
{
private static string assemblyDefinitionFileName = "com.scenarioinc.scenario.editor";
private string apiKey;
private string secretKey;
private string saveFolder;
Expand All @@ -18,7 +20,7 @@ public class PluginSettings : EditorWindow
private readonly string[] imageFormats = { "JPEG", "PNG" };
private readonly string[] imageFormatExtensions = { "jpeg", "png" };

private static string vnumber = "";
private static string vnumber => GetVersionFromPackageJson();
private static string version => $"Scenario Beta Version {vnumber}";

[System.Serializable]
Expand All @@ -27,14 +29,35 @@ private class PackageInfo
public string version;
}

[MenuItem("Scenario/Update Version")]
public static void UpdateVersionFromPackageJson()
/// <summary>
/// Get the correct version number from the package JSON
/// </summary>
/// <returns>The version of the plugin, as a string</returns>
private static string GetVersionFromPackageJson()
{
string packageJsonPath = "Assets/Scenario/package.json";
string packageJsonContent = File.ReadAllText(packageJsonPath);
vnumber = JsonUtility.FromJson<PackageInfo>(packageJsonContent).version;
//Find the assembly Definition which should be at package/Editor/ folder because it's a unique file.
string[] guids = AssetDatabase.FindAssets($"{assemblyDefinitionFileName} t:assemblydefinitionasset");

if (guids.Length > 1)
{
Debug.LogError($"it seems that you have multiple file '{assemblyDefinitionFileName}.asmdef'. Please delete one");
return "0";
}

EditorWindow.GetWindow<PluginSettings>().Repaint();
if (guids.Length == 0)
{
Debug.LogError($"It seems that you don't have the file '{assemblyDefinitionFileName}.asmdef'. Please redownload the plugin from the asset store.");
return "0";
}

//find the folder of that file
string folderPath = AssetDatabase.GUIDToAssetPath(guids[0]);
folderPath = folderPath.Remove(folderPath.IndexOf($"Editor/{assemblyDefinitionFileName}.asmdef"));

//find the package.json inside this folder
string packageJsonPath = $"{folderPath}/package.json";
string packageJsonContent = File.ReadAllText(packageJsonPath);
return JsonUtility.FromJson<PackageInfo>(packageJsonContent).version;
}

public static string EncodedAuth
Expand Down Expand Up @@ -62,15 +85,15 @@ public static void ShowWindow()

private void OnEnable()
{
UpdateVersionFromPackageJson();
GetVersionFromPackageJson();
LoadSettings();
}

private void OnGUI()
{
Color backgroundColor = new Color32(18, 18, 18, 255);
EditorGUI.DrawRect(new Rect(0, 0, Screen.width, Screen.height), backgroundColor);

GUILayout.Space(10);

apiKey = EditorGUILayout.TextField("API Key", apiKey);
Expand Down

0 comments on commit 31029b9

Please sign in to comment.