asmdef name domain separator class
This commit is contained in:
parent
5e63d32268
commit
363ed7914f
@ -0,0 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode {
|
||||
public class DomainGroup {
|
||||
readonly Dictionary<string, List<DomainUnit>> dict;
|
||||
|
||||
public DomainGroup() {
|
||||
dict = new Dictionary<string, List<DomainUnit>>();
|
||||
}
|
||||
|
||||
public void Create(IEnumerable<string> 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<DomainUnit> { unit });
|
||||
} else {
|
||||
if (dict.TryGetValue(unit.TopDomain, out var list)) {
|
||||
list.Add(unit);
|
||||
} else {
|
||||
dict.Add(unit.TopDomain, new List<DomainUnit> { 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<DomainUnit> { new DomainUnit(unit.FullName, '\0') });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetTopDomains() => dict.Keys;
|
||||
public IEnumerable<string> GetSoloDomains() => dict
|
||||
.Where(x => x.Value.Count == 1)
|
||||
.Select(x => x.Key);
|
||||
|
||||
public IEnumerable<string> GetTopDomainsWithSomeSubDomains() => dict
|
||||
.Keys
|
||||
.Except(GetSoloDomains());
|
||||
|
||||
public IEnumerable<string> GetSubDomains(string topDomain) {
|
||||
if (dict.TryGetValue(topDomain, out var list))
|
||||
{
|
||||
return list.Select(x => x.SubDomain);
|
||||
}
|
||||
return new string[0];
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d9fe84ab1b8c44b0a841738f0ecb37d3
|
||||
timeCreated: 1606659209
|
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2927a4439ddc45a0a386b64e05220d53
|
||||
timeCreated: 1606658272
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ccc87caf20a4985a4e3c94bfc1c12c6
|
||||
timeCreated: 1606660230
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ecc6823b62f94fd3a910f5a885a201dc
|
||||
timeCreated: 1606658361
|
Loading…
x
Reference in New Issue
Block a user