Example #1
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);
        }
Example #2
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);
        }