Skip to content

Commit

Permalink
Merge pull request #228 from c3-hoge-fuga-piyo/support_assembly_root_…
Browse files Browse the repository at this point in the history
…namespace

Support asmdef Root Namespace option
  • Loading branch information
hadashiA authored May 19, 2021
2 parents b9c24fe + aebf6b5 commit 0619d38
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions VContainer/Assets/VContainer/Editor/ScriptTemplateModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@
using UnityEditor;
using UnityEngine;

#if UNITY_2020_2_OR_NEWER
using UnityEditor.Compilation;
#endif

namespace VContainer.Editor
{
public sealed class ScriptTemplateProcessor : UnityEditor.AssetModificationProcessor
{
#if UNITY_2020_2_OR_NEWER
const string RootNamespaceBeginTag = "#ROOTNAMESPACEBEGIN#";
const string RootNamespaceEndTag = "#ROOTNAMESPACEEND#";
#endif

const string MonoInstallerTemplate =
"using VContainer;\n" +
"using VContainer.Unity;\n" +
"\n" +
#if UNITY_2020_2_OR_NEWER
RootNamespaceBeginTag + "\n" +
#endif
"public class #SCRIPTNAME# : LifetimeScope\n" +
"{\n" +
" protected override void Configure(IContainerBuilder builder)\n" +
" {\n" +
" }\n" +
"}\n";
"}\n" +
#if UNITY_2020_2_OR_NEWER
RootNamespaceEndTag + "\n" +
#endif
"";

public static void OnWillCreateAsset(string metaPath)
{
Expand All @@ -33,18 +49,59 @@ public static void OnWillCreateAsset(string metaPath)
return;
}

if (!scriptPath.EndsWith("LifetimeScope.cs"))
{
return;
}

var content = MonoInstallerTemplate.Replace("#SCRIPTNAME#", basename);

#if UNITY_2020_2_OR_NEWER
{
var rootNamespace = CompilationPipeline.GetAssemblyRootNamespaceFromScriptPath(scriptPath);
content = RemoveOrInsertNamespaceSimple(content, rootNamespace);
}
#endif

if (scriptPath.StartsWith("Assets/"))
{
scriptPath = scriptPath.Substring("Assets/".Length);
}

var fullPath = Path.Combine(Application.dataPath, scriptPath);
if (scriptPath.EndsWith("LifetimeScope.cs"))
File.WriteAllText(fullPath, content);
AssetDatabase.Refresh();
}

#if UNITY_2020_2_OR_NEWER
// https://github.com/Unity-Technologies/UnityCsReference/blob/2020.2/Editor/Mono/ProjectWindow/ProjectWindowUtil.cs#L495-L550
static string RemoveOrInsertNamespaceSimple(string content, string rootNamespace)
{
const char eol = '\n';

if (string.IsNullOrWhiteSpace(rootNamespace))
{
return content
.Replace(RootNamespaceBeginTag + eol, "")
.Replace(RootNamespaceEndTag + eol, "");
}

var lines = content.Split(eol);

var startAt = ArrayUtility.IndexOf(lines, RootNamespaceBeginTag);
var endAt = ArrayUtility.IndexOf(lines, RootNamespaceEndTag);

lines[startAt] = $"namespace {rootNamespace}\n{{";
{
var content = MonoInstallerTemplate.Replace("#SCRIPTNAME#", basename);
File.WriteAllText(fullPath, content);
AssetDatabase.Refresh();
for (var i = startAt + 1; i < endAt; ++i)
{
lines[i] = $" {lines[i]}";
}
}
lines[endAt] = "}";

return string.Join(eol.ToString(), lines);
}
#endif
}
}

1 comment on commit 0619d38

@vercel
Copy link

@vercel vercel bot commented on 0619d38 May 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.