Skip to content
Closed
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
8 changes: 8 additions & 0 deletions Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Editor/Release.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 135 additions & 0 deletions Assets/Editor/Release/SuiteVersionWindow.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
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<SuiteVersionWindow>(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<string, string> 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);
}
}
}
}
2 changes: 2 additions & 0 deletions Assets/Editor/Release/SuiteVersionWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Project Settings pages stay at `Project/DNExtensions/<Name>`, and GameObject create items at `GameObject/DNExtensions/<Name>`.
- **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.