diff --git a/Assets/Editor.meta b/Assets/Editor.meta new file mode 100644 index 0000000..c575312 --- /dev/null +++ b/Assets/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 31a23fb00560476095419a4b8a39c13d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Release.meta b/Assets/Editor/Release.meta new file mode 100644 index 0000000..6c57634 --- /dev/null +++ b/Assets/Editor/Release.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 241fba2679c7418b976b07245409efac +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Editor/Release/SuiteVersionWindow.cs b/Assets/Editor/Release/SuiteVersionWindow.cs new file mode 100644 index 0000000..d5d7645 --- /dev/null +++ b/Assets/Editor/Release/SuiteVersionWindow.cs @@ -0,0 +1,135 @@ +using System; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using DNExtensions.Utilities; +using UnityEditor; +using UnityEditor.PackageManager; +using UnityEngine; + +namespace DNExtensions.Release +{ + /// + /// Sets one version on every DNExtensions package: each package.json, the com.danielnoam dependencies between them, + /// the lock file, the README install URLs and a new dated CHANGELOG section. Tag the merged commit vX.Y.Z afterwards. + /// + internal class SuiteVersionWindow : EditorWindow + { + private const string PackagePrefix = "com.danielnoam."; + + private static readonly Regex SemVer = new Regex(@"^\d+\.\d+\.\d+$"); + private static readonly Regex VersionLine = new Regex(@"^(\s*""version"":\s*"")([^""]+)("")", RegexOptions.Multiline); + private static readonly Regex DependencyVersion = new Regex(@"(""com\.danielnoam\.[a-z]+"":\s*"")\d+\.\d+\.\d+("")"); + private static readonly Regex ReadmeTag = new Regex(@"(DNExtensions\.git\?path=/Packages/com\.danielnoam\.[a-z]+)#v\d+\.\d+\.\d+"); + + private string _currentVersion; + private string _newVersion; + + private static string ProjectRoot => Directory.GetParent(Application.dataPath).FullName; + private static string PackagesFolder => Path.Combine(ProjectRoot, "Packages"); + + [MenuItem(DNExtensionsMenu.Root + "Release/Set Suite Version")] + public static void ShowWindow() + { + var window = GetWindow(true, "Set Suite Version"); + window.minSize = new Vector2(320f, 130f); + window.maxSize = new Vector2(320f, 130f); + window.ShowUtility(); + } + + private void OnEnable() + { + _currentVersion = ReadCurrentVersion(); + _newVersion = _currentVersion; + } + + private void OnGUI() + { + EditorGUILayout.LabelField("Current", _currentVersion); + _newVersion = EditorGUILayout.TextField("New", _newVersion).Trim(); + + var isValid = SemVer.IsMatch(_newVersion) && _newVersion != _currentVersion; + if (!SemVer.IsMatch(_newVersion)) + { + EditorGUILayout.HelpBox("Use MAJOR.MINOR.PATCH, e.g. 1.3.0.", MessageType.Warning); + } + + using (new EditorGUI.DisabledScope(!isValid)) + { + if (GUILayout.Button("Apply")) + { + Apply(_newVersion); + Close(); + } + } + } + + private static string[] GetPackageJsonPaths() + { + return Directory.GetDirectories(PackagesFolder, PackagePrefix + "*") + .Select(folder => Path.Combine(folder, "package.json")) + .Where(File.Exists) + .OrderBy(path => path) + .ToArray(); + } + + private static string ReadCurrentVersion() + { + var path = GetPackageJsonPaths().FirstOrDefault(); + if (path == null) + { + return "?"; + } + + var match = VersionLine.Match(File.ReadAllText(path)); + return match.Success ? match.Groups[2].Value : "?"; + } + + private static void Apply(string version) + { + foreach (var path in GetPackageJsonPaths()) + { + RewriteFile(path, text => + { + text = VersionLine.Replace(text, "${1}" + version + "${3}", 1); + return DependencyVersion.Replace(text, "${1}" + version + "${2}"); + }); + } + + RewriteFile(Path.Combine(PackagesFolder, "packages-lock.json"), text => DependencyVersion.Replace(text, "${1}" + version + "${2}")); + RewriteFile(Path.Combine(ProjectRoot, "README.md"), text => ReadmeTag.Replace(text, "${1}#v" + version)); + RewriteFile(Path.Combine(ProjectRoot, "CHANGELOG.md"), text => AddChangelogSection(text, version)); + + Client.Resolve(); + Debug.Log($"DNExtensions set to {version}. Commit, merge, then tag the merged commit v{version} and push the tag."); + } + + private static string AddChangelogSection(string text, string version) + { + const string unreleased = "## [Unreleased]"; + if (!text.Contains(unreleased) || text.Contains($"## [{version}]")) + { + return text; + } + + var index = text.IndexOf(unreleased, StringComparison.Ordinal) + unreleased.Length; + return text.Insert(index, $"\n\n## [{version}] - {DateTime.Now:yyyy-MM-dd}"); + } + + private static void RewriteFile(string path, Func rewrite) + { + if (!File.Exists(path)) + { + Debug.LogWarning($"Set Suite Version: {path} not found, skipped."); + return; + } + + var text = File.ReadAllText(path); + var newText = rewrite(text); + if (newText != text) + { + File.WriteAllText(path, newText); + } + } + } +} diff --git a/Assets/Editor/Release/SuiteVersionWindow.cs.meta b/Assets/Editor/Release/SuiteVersionWindow.cs.meta new file mode 100644 index 0000000..700b04d --- /dev/null +++ b/Assets/Editor/Release/SuiteVersionWindow.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: ce524c11b96642c5b52133b0db383351 diff --git a/CLAUDE.md b/CLAUDE.md index fe3516c..4b47540 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -7,6 +7,7 @@ - Project Settings pages stay at `Project/DNExtensions/`, and GameObject create items at `GameObject/DNExtensions/`. - **Versions** are shared: every package carries the same `version`, and the `com.danielnoam.*` dependency versions match it. To release, set them all (and the same entries in `Packages/packages-lock.json`) to the new version, move - `[Unreleased]` in CHANGELOG.md into a dated section, update the `#vX.Y.Z` in the README URLs, then tag the merged + `[Unreleased]` in CHANGELOG.md into a dated section, update the `#vX.Y.Z` in the README URLs (**DNExtensions → + Release → Set Suite Version** in this project does all of that), then tag the merged commit `vX.Y.Z` and push the tag. Never move or reuse a tag. The Release Tag Check workflow fails a tag that disagrees with the package versions.