Exemplo n.º 1
0
        /// <summary>
        /// Generate enum file.
        /// </summary>
        /// <param name="inputJsonText">Input json text to generate the enum from</param>
        /// <param name="outputPath">Path where to save the generated enum to</param>
        /// <param name="outputType">Type of output to produce</param>
        /// <param name="collectionJPath">JPath to the collection in the input file</param>
        /// <param name="entryNameJPath">
        /// JPath to the name field in an entry in the input file</param>
        /// <param name="entryValueJPath">
        /// Optional JPath to the value field in an entry in the input file.
        /// </param>
        /// <param name="entryCommentJPath">
        /// Optional JPath to the comment field in an entry in the input file.
        /// </param>
        /// <param name="enumComment">
        /// Optional comment to add to the generated enum.
        /// </param>
        /// <param name="enumNamespace">
        /// Optional namespace to add the generated enum to.
        /// </param>
        /// <param name="headerMode">Mode to use when adding a header</param>
        /// <param name="indentMode">Mode to use when indenting text</param>
        /// <param name="indentSize">When indenting with spaces this controls how many</param>
        /// <param name="newlineMode">Mode to use when adding newlines to text</param>
        /// <param name="storageType">Storage type for the exported enum</param>
        /// <param name="curlyBracketMode">Mode to use when writing curly-brackets</param>
        /// <param name="logger">Optional logger for diagnostic output</param>
        public static void GenerateEnumToFile(
            string inputJsonText,
            string outputPath,
            OutputType outputType,
            string collectionJPath,
            string entryNameJPath,
            string entryValueJPath,
            string entryCommentJPath,
            string enumComment,
            string enumNamespace,
            HeaderMode headerMode,
            CodeBuilder.IndentMode indentMode,
            int indentSize,
            CodeBuilder.NewlineMode newlineMode,
            StorageType storageType,
            CurlyBracketMode curlyBracketMode,
            ILogger logger = null)
        {
            // Generate enum name.
            var enumName = GetEnumName(outputPath, logger);

            if (enumName == null)
            {
                return;
            }

            // Create mapping context.
            var context = Context.Create(
                collectionJPath,
                entryNameJPath,
                entryValueJPath,
                entryCommentJPath,
                logger);

            // Map enum.
            EnumDefinition enumDefinition = null;

            try
            {
                enumDefinition = context.MapEnum(inputJsonText, enumName, enumComment);
            }
            catch (JsonParsingFailureException)
            {
                logger?.LogCritical("Failed to parse input file: invalid json");
                return;
            }
            catch (MappingFailureException e)
            {
                logger?.LogCritical($"Failed to map enum: {e.InnerException.Message}");
                return;
            }

            // Export.
            byte[] output = null;
            switch (outputType)
            {
            case OutputType.CSharp:
                try
                {
                    output = Utf8NoBom.GetBytes(enumDefinition.ExportCSharp(
                                                    enumNamespace,
                                                    headerMode,
                                                    indentMode,
                                                    indentSize,
                                                    newlineMode,
                                                    storageType,
                                                    curlyBracketMode));
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to generate csharp: {e.Message}");
                    return;
                }

                break;

            case OutputType.FSharp:
                try
                {
                    output = Utf8NoBom.GetBytes(enumDefinition.ExportFSharp(
                                                    string.IsNullOrEmpty(enumNamespace) ? "Generated" : enumNamespace,
                                                    headerMode,
                                                    indentSize,
                                                    newlineMode,
                                                    storageType));
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to generate fsharp: {e.Message}");
                    return;
                }

                break;

            case OutputType.VisualBasic:
                try
                {
                    output = Utf8NoBom.GetBytes(enumDefinition.ExportVisualBasic(
                                                    enumNamespace,
                                                    headerMode,
                                                    indentMode,
                                                    indentSize,
                                                    newlineMode,
                                                    storageType));
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to generate visual-basic: {e.Message}");
                    return;
                }

                break;

            case OutputType.Cil:
                try
                {
                    output = Utf8NoBom.GetBytes(enumDefinition.ExportCil(
                                                    assemblyName: enumName,
                                                    enumNamespace,
                                                    headerMode,
                                                    indentMode,
                                                    indentSize,
                                                    newlineMode,
                                                    storageType,
                                                    curlyBracketMode));
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to generate cil: {e.Message}");
                    return;
                }

                break;

            case OutputType.ClassLibrary:
                try
                {
                    output = enumDefinition.ExportClassLibrary(
                        assemblyName: enumName,
                        enumNamespace,
                        storageType);
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to generate classlibrary: {e.Message}");
                    return;
                }

                break;
            }

            // Write the file.
            try
            {
                var fullPath = Path.Combine(UnityEngine.Application.dataPath, outputPath);
                if (!fullPath.EndsWith(GetRequiredExtension(outputType), StringComparison.OrdinalIgnoreCase))
                {
                    fullPath = $"{fullPath}{GetDesiredExtension(outputType)}";
                }

                var outputDir = Path.GetDirectoryName(fullPath);
                if (!Directory.Exists(outputDir))
                {
                    logger?.LogDebug($"Creating output directory: '{outputDir}'");
                    Directory.CreateDirectory(outputDir);
                }

                File.WriteAllBytes(fullPath, output);
                logger?.LogInformation($"Saved enum: '{fullPath}'");
            }
            catch (Exception e)
            {
                logger?.LogCritical($"Failed to save enum: {e.Message}");
            }
        }