예제 #1
0
        public void CreateContainer()
        {
            var info = new CodeGenerateContainerInfo
            {
                TypeName = typeof(TestTargetContainerExternal).AssemblyQualifiedName,
                Members  =
                {
                    new CodeGenerateContainerInfo.MemberInfo
                    {
                        Name = "Field"
                    },
                    new CodeGenerateContainerInfo.MemberInfo
                    {
                        Active = false,
                        Name   = "Field2"
                    },
                    new CodeGenerateContainerInfo.MemberInfo
                    {
                        Name = "Property"
                    }
                }
            };

            CodeGenerateContainer container = CodeGenerateContainerInfoEditorUtility.CreateContainer(info, m_validation);

            Assert.NotNull(container);
            Assert.AreEqual(2, container.Fields.Count);
            Assert.True(container.Fields.Exists(x => x.Name == "Field"));
            Assert.False(container.Fields.Exists(x => x.Name == "Field2"));
            Assert.True(container.Fields.Exists(x => x.Name == "Property"));
        }
예제 #2
0
        public void CreateUnit()
        {
            CodeGenerateContainerInfo info = CodeGenerateContainerInfoEditorUtility.CreateInfo(typeof(Target), m_validation);

            Assert.NotNull(info);

            SyntaxNode unit = CodeGenerateContainerInfoEditorUtility.CreateUnit(info, m_validation);

            Assert.NotNull(unit);

            string result   = unit.NormalizeWhitespace().ToFullString();
            string expected = File.ReadAllText(m_target);

            Assert.AreEqual(expected, result);
        }
예제 #3
0
        public void CreateInfo()
        {
            CodeGenerateContainerInfo info = CodeGenerateContainerInfoEditorUtility.CreateInfo(typeof(TestTargetContainerExternal), m_validation);

            Assert.NotNull(info);

            bool result1 = info.TryGetTargetType(out Type type);

            Assert.True(result1);
            Assert.NotNull(type);
            Assert.AreEqual(typeof(TestTargetContainerExternal), type);
            Assert.AreEqual(3, info.Members.Count);
            Assert.True(info.Members.Exists(x => x.Name == "Field"));
            Assert.True(info.Members.Exists(x => x.Name == "Field2"));
            Assert.True(info.Members.Exists(x => x.Name == "Property"));
        }
예제 #4
0
        public static string GenerateExternalSources(IReadOnlyList <string> externalPaths, ICollection <string> sourcePaths, Type attributeType = null, ICodeGenerateContainerValidation validation = null, Compilation compilation = null, SyntaxGenerator generator = null)
        {
            if (externalPaths == null)
            {
                throw new ArgumentNullException(nameof(externalPaths));
            }
            if (sourcePaths == null)
            {
                throw new ArgumentNullException(nameof(sourcePaths));
            }
            if (validation == null)
            {
                validation = CodeGenerateContainerAssetEditorUtility.DefaultValidation;
            }
            if (compilation == null)
            {
                compilation = CodeAnalysisEditorUtility.ProjectCompilation;
            }
            if (generator == null)
            {
                generator = CodeAnalysisEditorUtility.Generator;
            }

            string externalsTempPath = FileUtil.GetUniqueTempPathInProject();
            var    types             = new HashSet <Type>();
            CSharpSyntaxRewriter rewriterAddAttribute = null;

            if (attributeType != null && compilation.TryConstructTypeSymbol(attributeType, out ITypeSymbol typeSymbol))
            {
                rewriterAddAttribute = GetAttributeRewriter(compilation, generator, typeSymbol);
            }

            Directory.CreateDirectory(externalsTempPath);

            for (int i = 0; i < externalPaths.Count; i++)
            {
                string externalPath = externalPaths[i];
                var    info         = AssetInfoEditorUtility.LoadInfo <CodeGenerateContainerInfo>(externalPath);

                if (info.TryGetTargetType(out Type type))
                {
                    if (types.Add(type))
                    {
                        SyntaxNode unit = CodeGenerateContainerInfoEditorUtility.CreateUnit(info, validation, compilation, generator);

                        if (rewriterAddAttribute != null)
                        {
                            unit = rewriterAddAttribute.Visit(unit);
                        }

                        string sourcePath = $"{externalsTempPath}/{Guid.NewGuid():N}.cs";
                        string source     = unit.NormalizeWhitespace().ToFullString();

                        File.WriteAllText(sourcePath, source);

                        sourcePaths.Add(sourcePath);
                    }
                    else
                    {
                        Debug.LogWarning($"The specified type already generated: '{type}'.");
                    }
                }
            }

            return(externalsTempPath);
        }