Esempio n. 1
0
        /// <summary>
        /// Generate treescheme json file for a given type.
        /// </summary>
        /// <param name="rootAliasTypeName">Fullname of the type to use as the root of the tree</param>
        /// <param name="fieldSource">Enum to indicator how to find fields on types</param>
        /// <param name="outputPath">Path to save the output file relative to the Assets directory</param>
        /// <param name="typeIgnorePattern">Optional regex pattern to ignore types</param>
        /// <param name="nodeCommentProvider">Optional provider of node-comments</param>
        /// <param name="logger">Optional logger for diagnostic output</param>
        public static void GenerateSchemeToFile(
            string rootAliasTypeName,
            FieldSource fieldSource,
            string outputPath,
            Regex typeIgnorePattern = null,
            INodeCommentProvider nodeCommentProvider = null,
            Core.ILogger logger = null)
        {
            // Generate the json.
            var json = GenerateScheme(
                rootAliasTypeName, fieldSource, typeIgnorePattern, nodeCommentProvider, logger);

            if (json != null)
            {
                // Write the file.
                try
                {
                    var fullPath  = Path.Combine(UnityEngine.Application.dataPath, outputPath);
                    var outputDir = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(outputDir))
                    {
                        logger?.LogDebug($"Creating output directory: '{outputDir}'");
                        Directory.CreateDirectory(outputDir);
                    }

                    File.WriteAllText(fullPath, json);
                    logger?.LogInformation($"Saved scheme: '{outputPath}'");
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to save file: {e.Message.ToDistinctLines()}");
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Generate treescheme json for a given type.
        /// </summary>
        /// <param name="rootAliasTypeName">Fullname of the type to use as the root of the tree</param>
        /// <param name="fieldSource">Enum to indicator how to find fields on types</param>
        /// <param name="typeIgnorePattern">Optional regex pattern to ignore types</param>
        /// <param name="nodeCommentProvider">Optional provider of node-comments</param>
        /// <param name="logger">Optional logger for diagnostic output</param>
        /// <returns>Json string representing the scheme</returns>
        public static string GenerateScheme(
            string rootAliasTypeName,
            FieldSource fieldSource,
            Regex typeIgnorePattern = null,
            INodeCommentProvider nodeCommentProvider = null,
            Core.ILogger logger = null)
        {
            try
            {
                // Gather all the types.
                var typeCollection = TypeCollection.Create(AppDomain.CurrentDomain.GetAssemblies(), logger);

                // Create mapping context.
                var context = Context.Create(
                    typeCollection,
                    fieldSource,
                    typeIgnorePattern,
                    nodeCommentProvider,
                    logger);

                // Map the tree.
                var tree = TreeMapper.MapTree(context, rootAliasTypeName);

                // Serialize the scheme.
                return(JsonSerializer.ToJson(tree, JsonSerializer.Mode.Pretty));
            }
            catch (Exception e)
            {
                logger?.LogCritical($"Failed to generate scheme: {e.Message.ToDistinctLines()}");
                return(null);
            }
        }
        private static string ValidateFilePath(string path, Core.ILogger logger = null)
        {
            // Determine full-path.
            string fullPath;

            try
            {
                fullPath = Path.GetFullPath(path);
            }
            catch
            {
                logger?.LogCritical($"Unable to determine absolute path for: '{path}'");
                return(null);
            }

            // Validate file existence.
            if (!File.Exists(fullPath))
            {
                logger?.LogCritical($"No file found at path: '{fullPath}'");
                return(null);
            }

            return(fullPath);
        }
        /// <summary>
        /// Attempt to load a <see cref="ITypeCollection"/> from a Assembly path.
        /// </summary>
        /// <param name="assemblyPath">Path to the assembly file</param>
        /// <param name="dependencyDirectories">List of directories to search for dependant assemblies</param>
        /// <param name="logger">Optional logger to provide diagnostic output</param>
        /// <returns><see cref="ITypeCollection"/> if successfull otherwise null</returns>
        internal static ITypeCollection TryLoad(
            string assemblyPath,
            IEnumerable <string> dependencyDirectories,
            Core.ILogger logger = null)
        {
            if (assemblyPath == null)
            {
                throw new ArgumentNullException(nameof(assemblyPath));
            }
            if (dependencyDirectories == null)
            {
                throw new ArgumentNullException(nameof(dependencyDirectories));
            }

            // Verify that dependency paths exists.
            foreach (var dependencyDirectory in dependencyDirectories)
            {
                if (!Directory.Exists(dependencyDirectory))
                {
                    logger?.LogCritical($"Dependency directory '{dependencyDirectory}' cannot be found");
                    return(null);
                }
            }

            // Create load context.
            var loadContext = CreateLoadContext(assemblyPath, logger);

            if (loadContext == null)
            {
                return(null);
            }

            // Load assemblies.
            var assemblies = LoadAssemblyFromPath(loadContext, assemblyPath, dependencyDirectories, logger);

            logger?.LogDebug($"Loaded {assemblies.Count} assemblies");

            // Load types.
            var typeCollection = TypeCollection.Create(assemblies, logger);

            logger?.LogDebug($"Loaded {typeCollection.TypeCount} types");
            return(typeCollection);
        }