Beispiel #1
0
        public static string GenerateResolver(IReadOnlyList <string> sourcePaths, string resolverName, string namespaceRoot, Utf8JsonGenerateArguments generateArguments)
        {
            if (sourcePaths == null)
            {
                throw new ArgumentNullException(nameof(sourcePaths));
            }
            if (string.IsNullOrEmpty(resolverName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(resolverName));
            }
            if (string.IsNullOrEmpty(namespaceRoot))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(namespaceRoot));
            }

            string resolver            = Utf8JsonUniversalCodeGeneratorUtility.Generate(sourcePaths, resolverName, namespaceRoot, generateArguments);
            CompilationUnitSyntax unit = SyntaxFactory.ParseCompilationUnit(resolver);

            unit = CodeGenerateEditorUtility.AddGeneratedCodeLeadingTrivia(unit);

            return(unit.ToFullString());
        }
Beispiel #2
0
        public static string GenerateResolver(Utf8JsonResolverAssetInfo info, ICodeGenerateContainerValidation validation = null, Compilation compilation = null, SyntaxGenerator generator = null)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (validation == null)
            {
                validation = CodeGenerateContainerAssetEditorUtility.DefaultValidation;
            }
            if (compilation == null)
            {
                compilation = CodeAnalysisEditorUtility.ProjectCompilation;
            }
            if (generator == null)
            {
                generator = CodeAnalysisEditorUtility.Generator;
            }

            var    sourcePaths   = new List <string>();
            string resolverName  = Utf8JsonEditorUtility.FormatResolverName(info.ResolverName);
            string namespaceRoot = info.NamespaceRoot;
            string externalsTemp = string.Empty;
            Type   attributeType = null;

            var generateArguments = new Utf8JsonGenerateArguments
            {
                IgnoreReadOnly         = info.IgnoreReadOnly,
                IgnoreEmpty            = info.IgnoreEmpty,
                IsTypeRequireAttribute = info.AttributeRequired,
            };

            if (info.AttributeRequired && info.TryGetAttributeType(out attributeType))
            {
                generateArguments.TypeRequiredAttributeShortName = attributeType.Name;
            }

            if (info.Sources.Count > 0)
            {
                var externals = new List <string>();

                for (int i = 0; i < info.Sources.Count; i++)
                {
                    TextAsset asset = info.Sources[i];
                    string    path  = AssetDatabase.GetAssetPath(asset);

                    if (!string.IsNullOrEmpty(path))
                    {
                        if (path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase))
                        {
                            sourcePaths.Add(path);
                        }
                        else if (path.EndsWith(Utf8JsonExternalTypeEditorUtility.EXTERNAL_TYPE_ASSET_EXTENSION, StringComparison.OrdinalIgnoreCase))
                        {
                            externals.Add(path);
                        }
                    }
                }

                if (externals.Count > 0)
                {
                    externalsTemp = Utf8JsonExternalTypeEditorUtility.GenerateExternalSources(externals, sourcePaths, attributeType, validation, compilation, generator);
                }
            }

            string source = Utf8JsonEditorUtility.GenerateResolver(sourcePaths, resolverName, namespaceRoot, generateArguments);

            if (!string.IsNullOrEmpty(externalsTemp))
            {
                FileUtil.DeleteFileOrDirectory(externalsTemp);
            }

            if (info.ResolverAsset)
            {
                source = AppendResolverAsset(source, resolverName, namespaceRoot, compilation, generator);
            }

            return(source);
        }