diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainGroup.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainGroup.cs new file mode 100644 index 0000000..35a45b2 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainGroup.cs @@ -0,0 +1,68 @@ +using System.Collections.Generic; +using System.Linq; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode { + public class DomainGroup { + readonly Dictionary> dict; + + public DomainGroup() { + dict = new Dictionary>(); + } + + public void Create(IEnumerable all) { + dict.Clear(); + foreach (var str in all) { + var unit = new DomainUnit(str, '.'); + if (!unit.HasSubDomain()) { + unit = new DomainUnit(str, '-'); + } + if (!unit.HasSubDomain()) { + unit = new DomainUnit(str, '_'); + } + + if (!unit.HasSubDomain()) { + dict.Add(unit.FullName, new List { unit }); + } else { + if (dict.TryGetValue(unit.TopDomain, out var list)) { + list.Add(unit); + } else { + dict.Add(unit.TopDomain, new List { unit }); + } + } + } + // 1つしかなかったものを単独とする + var soloKeys = GetSoloDomains().ToArray(); + foreach (var key in soloKeys) { + var unit = dict[key].FirstOrDefault(); + if (unit == null || key == unit.FullName) { + continue; + } + + dict.Remove(key); + var newKey = unit.FullName; + if (dict.ContainsKey(newKey)) { + dict[newKey].Add(unit); + } else { + dict.Add(newKey, new List { new DomainUnit(unit.FullName, '\0') }); + } + } + } + + public IEnumerable GetTopDomains() => dict.Keys; + public IEnumerable GetSoloDomains() => dict + .Where(x => x.Value.Count == 1) + .Select(x => x.Key); + + public IEnumerable GetTopDomainsWithSomeSubDomains() => dict + .Keys + .Except(GetSoloDomains()); + + public IEnumerable GetSubDomains(string topDomain) { + if (dict.TryGetValue(topDomain, out var list)) + { + return list.Select(x => x.SubDomain); + } + return new string[0]; + } + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainGroup.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainGroup.cs.meta new file mode 100644 index 0000000..fe937f9 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainGroup.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d9fe84ab1b8c44b0a841738f0ecb37d3 +timeCreated: 1606659209 \ No newline at end of file diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainUnit.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainUnit.cs new file mode 100644 index 0000000..d2969bd --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainUnit.cs @@ -0,0 +1,27 @@ + + +using System; +using System.Linq; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode { + public class DomainUnit { + public readonly string FullName; + public readonly string TopDomain; + public readonly string SubDomain; + + public DomainUnit(string fullName, char separator) { + this.FullName = fullName; + if (string.IsNullOrEmpty(FullName)) return; + var split = fullName.Split(separator).Where(x => !string.IsNullOrEmpty(x)).ToArray(); + if (split.Length < 2) { + TopDomain = fullName.Replace(separator.ToString(), ""); + SubDomain = string.Empty; + } else { + TopDomain = split[0]; + SubDomain = split.Skip(1).Aggregate((a, b) => $"{a}{separator}{b}"); + } + } + + public bool HasSubDomain() => !string.IsNullOrEmpty(SubDomain); + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainUnit.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainUnit.cs.meta new file mode 100644 index 0000000..d3c5030 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/DomainUnit.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2927a4439ddc45a0a386b64e05220d53 +timeCreated: 1606658272 \ No newline at end of file diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainGroupTest.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainGroupTest.cs new file mode 100644 index 0000000..cd6d2b7 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainGroupTest.cs @@ -0,0 +1,52 @@ +using System.Collections; +using System.Linq; +using NUnit.Framework; +using UnityEditor; +using UnityEngine.TestTools; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests { + public class DomainGroupTest { + + readonly string[] inputs ={ + "unity.hoge", + "unity.fuga", + "unity.foo.bar", + "UniRx", + "UniRx.Async", + "zenject", + "zenject-test-framework", + "MyAsmdef", + "MyAsmdef2_Test", + "MyAsmdef3.hoge", + "MyAsmdef3-hoge", + }; + + [Test] + public void TestDomainGroup() { + var group = new DomainGroup(); + group.Create(inputs); + Assert.That(group.GetTopDomains().Count(), Is.EqualTo(6)); + Assert.That(group.GetSubDomains("unity").Count(), Is.EqualTo(3)); + Assert.That(group.GetSubDomains("UniRx").Count(), Is.EqualTo(2)); + Assert.That(group.GetSubDomains("zenject").Count(), Is.EqualTo(2)); + Assert.That(group.GetSubDomains("MyAsmdef").Count(), Is.EqualTo(1)); + Assert.That(group.GetSubDomains("MyAsmdef2_Test").Count(), Is.EqualTo(1)); + Assert.That(group.GetSubDomains("MyAsmdef3").Count(), Is.EqualTo(2)); + + Assert.That(group.GetTopDomainsWithSomeSubDomains().Count(), Is.EqualTo(4)); + Assert.That(group.GetSoloDomains().Count(), Is.EqualTo(2)); + + Assert.That(group.GetSubDomains("unity").Any(x => x == "hoge"), Is.True); + Assert.That(group.GetSubDomains("unity").Any(x => x == "fuga"), Is.True); + Assert.That(group.GetSubDomains("unity").Any(x => x == "foo.bar"), Is.True); + Assert.That(group.GetSubDomains("UniRx").Any(x => x == ""), Is.True); + Assert.That(group.GetSubDomains("UniRx").Any(x => x == "Async"), Is.True); + Assert.That(group.GetSubDomains("zenject").Any(x => x == ""), Is.True); + Assert.That(group.GetSubDomains("zenject").Any(x => x == "test-framework"), Is.True); + Assert.That(group.GetSubDomains("MyAsmdef").Any(x => x == ""), Is.True); + Assert.That(group.GetSubDomains("MyAsmdef2_Test").Any(x => x == ""), Is.True); + Assert.That(group.GetSubDomains("MyAsmdef3").Any(x => x == "hoge"), Is.True); + Assert.That(group.GetSubDomains("MyAsmdef3").Count(x => x == "hoge"), Is.EqualTo(2)); + } + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainGroupTest.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainGroupTest.cs.meta new file mode 100644 index 0000000..be3c792 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainGroupTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1ccc87caf20a4985a4e3c94bfc1c12c6 +timeCreated: 1606660230 \ No newline at end of file diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainUnitTest.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainUnitTest.cs new file mode 100644 index 0000000..95540d4 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainUnitTest.cs @@ -0,0 +1,72 @@ +using System.Collections; +using NUnit.Framework; +using UnityEditor; +using UnityEngine.TestTools; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests { + public class DomainUnitTest { + + [Test] + public void TestDomainUnit_HasTopDomain() { + var unit = new DomainUnit("abc.def.ghi.jk", '.'); + Assert.That(unit.FullName, Is.EqualTo("abc.def.ghi.jk")); + Assert.That(unit.TopDomain, Is.EqualTo("abc")); + Assert.That(unit.SubDomain, Is.EqualTo("def.ghi.jk")); + Assert.That(unit.HasSubDomain, Is.True); + } + + [Test] + public void TestDomainUnit_HasNoTopDomain() { + var unit = new DomainUnit("abcdefghijk", '.'); + Assert.That(unit.FullName, Is.EqualTo("abcdefghijk")); + Assert.That(unit.TopDomain, Is.EqualTo("abcdefghijk")); + Assert.That(unit.SubDomain, Is.Empty); + Assert.That(unit.HasSubDomain, Is.False); + } + + [Test] + public void TestDomainUnit_StartWithSeparator() { + var unit = new DomainUnit(".abc", '.'); + Assert.That(unit.FullName, Is.EqualTo(".abc")); + Assert.That(unit.TopDomain, Is.EqualTo("abc")); + Assert.That(unit.SubDomain, Is.Empty); + Assert.That(unit.HasSubDomain, Is.False); + } + + [Test] + public void TestDomainUnit_EndWithSeparator() { + var unit = new DomainUnit("abc.", '.'); + Assert.That(unit.FullName, Is.EqualTo("abc.")); + Assert.That(unit.TopDomain, Is.EqualTo("abc")); + Assert.That(unit.SubDomain, Is.Empty); + Assert.That(unit.HasSubDomain, Is.False); + } + + [Test] + public void TestDomainUnit_StartAndEndWithSeparator() { + var unit = new DomainUnit(".abc.def.", '.'); + Assert.That(unit.FullName, Is.EqualTo(".abc.def.")); + Assert.That(unit.TopDomain, Is.EqualTo("abc")); + Assert.That(unit.SubDomain, Is.EqualTo("def")); + Assert.That(unit.HasSubDomain, Is.True); + } + + [Test] + public void TestDomainUnit_OnlySeparator() { + var unit = new DomainUnit(".", '.'); + Assert.That(unit.FullName, Is.EqualTo(".")); + Assert.That(unit.TopDomain, Is.Empty); + Assert.That(unit.SubDomain, Is.Empty); + Assert.That(unit.HasSubDomain, Is.False); + } + + [Test] + public void TestDomainUnit_OnlySeparators() { + var unit = new DomainUnit("...", '.'); + Assert.That(unit.FullName, Is.EqualTo("...")); + Assert.That(unit.TopDomain, Is.Empty); + Assert.That(unit.SubDomain, Is.Empty); + Assert.That(unit.HasSubDomain, Is.False); + } + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainUnitTest.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainUnitTest.cs.meta new file mode 100644 index 0000000..3e0f845 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/DomainUnitTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ecc6823b62f94fd3a910f5a885a201dc +timeCreated: 1606658361 \ No newline at end of file