예제 #1
0
        public int Run()
        {
            if (options.ShouldShowHelp)
            {
                ShowHelpMessage();
                return(0);
            }

            if (!ValidateOptions())
            {
                ShowHelpMessage();
                return(1);
            }

            var bundlePath          = GenerateBundle();
            var schemaBundle        = SchemaBundle.FromJson(File.ReadAllText(bundlePath));
            var store               = new DetailsStore(schemaBundle);
            var workerGenerationJob = new WorkerGenerationJob(options.NativeOutputDirectory, options, fileSystem);
            var singleJob           = new SingleGenerationJob(options.NativeOutputDirectory, store, fileSystem);

            var runner = new JobRunner(fileSystem);

            runner.Run(singleJob, workerGenerationJob);
            return(0);
        }
예제 #2
0
        public void OneTimeSetup()
        {
            var json      = JsonParsingTests.GetBundleContents();
            var overrides = new List <string>
            {
                "global::Improbable.Gdk.Tests.SomeType;global::UserCode.SerializationExtensions.Type"
            };

            store = new DetailsStore(SchemaBundle.LoadBundle(json), overrides);
        }
예제 #3
0
        private static DetailsStore GetDetailsFromBundle(string bundleName)
        {
            var bundleResourceName =
                $"CodeGenerationLib.Tests.Model.SchemaBundleV1.Resources.{bundleName}.json";

            var assembly = Assembly.GetExecutingAssembly();
            var resource = assembly.GetManifestResourceStream(bundleResourceName);
            var json     = new StreamReader(resource).ReadToEnd();

            return(new DetailsStore(SchemaBundle.LoadBundle(json), new List <string>(), null));
        }
        public int Run()
        {
            if (options.ShouldShowHelp)
            {
                ShowHelpMessage();
                return(0);
            }

            if (!ValidateOptions())
            {
                ShowHelpMessage();
                return(1);
            }

            var bundlePath   = GenerateBundle();
            var schemaBundle = SchemaBundle.LoadBundle(File.ReadAllText(bundlePath));
            var fileTree     = new FileTree(options.SchemaInputDirs);
            var store        = new DetailsStore(schemaBundle, options.SerializationOverrides, fileTree);

            var jobs = AppDomain.CurrentDomain
                       .GetAssemblies()
                       .SelectMany(assembly =>
            {
                try
                {
                    return(assembly.GetTypes());
                }
                catch (ReflectionTypeLoadException e)
                {
                    Console.Error.WriteLine($"Failed to load assembly {assembly.FullName} with error {e}");
                    return(Enumerable.Empty <Type>());
                }
            })
                       .Where(type => typeof(CodegenJob).IsAssignableFrom(type))
                       .Where(type => !type.IsAbstract)
                       .Where(type => !type.GetCustomAttributes(typeof(IgnoreCodegenJobAttribute)).Any())
                       .Select(type =>
                               (CodegenJob)Activator.CreateInstance(type, options.NativeOutputDirectory, fileSystem, store))
                       .ToArray();

            new JobRunner(fileSystem).Run(jobs);
            return(0);
        }
예제 #5
0
        public DetailsStore(SchemaBundle bundle)
        {
            this.bundle = bundle;

            PopulateBlittableMaps();
            BlittableMap = ImmutableHashSet.CreateRange(blittableMap.Where(kv => kv.Value).Select(kv => kv.Key));

            var enums = bundle.BundleContents.EnumDefinitions
                        .Select(enumm => (enumm.EnumIdentifier, new UnityEnumDetails(enumm)))
                        .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2);

            Enums = new ReadOnlyDictionary <Identifier, UnityEnumDetails>(enums);

            var types = bundle.BundleContents.TypeDefinitions
                        .Select(type => (type.Identifier, new UnityTypeDetails(type)))
                        .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2);

            Types = new ReadOnlyDictionary <Identifier, UnityTypeDetails>(types);

            var components = bundle.BundleContents.ComponentDefinitions
                             .Select(component => (component.Identifier, new UnityComponentDetails(component, this)))
                             .ToDictionary(tuple => tuple.Item1, tuple => tuple.Item2);

            Components = new ReadOnlyDictionary <Identifier, UnityComponentDetails>(components);

            SchemaFiles = bundle.SourceMap.SourceReferences.Values
                          .Select(sourceRef => sourceRef.FilePath)
                          .Distinct()
                          .ToList()
                          .AsReadOnly();

            foreach (var kv in Types)
            {
                kv.Value.PopulateFields(this);
                kv.Value.PopulateChildren(this);
            }

            foreach (var kv in Components)
            {
                kv.Value.PopulateFields(this);
            }
        }
예제 #6
0
            public static CodegenStub GetCleanInstance()
            {
                var json = JsonParsingTests.GetBundleContents();

                return(new CodegenStub("", new MockFileSystem(), new DetailsStore(SchemaBundle.LoadBundle(json), new List <string>(), new MockFileTree()), false));
            }
예제 #7
0
        public int Run()
        {
            if (options.ShouldShowHelp)
            {
                ShowHelpMessage();
                return(0);
            }

            var optionErrors = options.GetValidationErrors().ToList();

            foreach (var optionError in optionErrors)
            {
                Console.WriteLine(optionError);
            }

            if (optionErrors.Any())
            {
                ShowHelpMessage();
                return(1);
            }

            logger.Info("Starting code generation.");

            logger.Info("Gathering schema information.");
            var bundlePath = GenerateBundle();

            logger.Info("Loading schema bundle from json.");
            var schemaBundle = SchemaBundle.LoadBundle(File.ReadAllText(bundlePath));

            logger.Info("Setting up schema file tree.");
            var fileTree = new FileTree(options.SchemaInputDirs);

            logger.Info("Initialising DetailsStore.");
            var store = new DetailsStore(schemaBundle, options.SerializationOverrides, fileTree);

            logger.Info("Setting up code generation jobs.");
            var jobs = AppDomain.CurrentDomain
                       .GetAssemblies()
                       .SelectMany(assembly =>
            {
                try
                {
                    return(assembly.GetTypes());
                }
                catch (ReflectionTypeLoadException e)
                {
                    logger.Error(e, $"Failed to load assembly {assembly.FullName}.");
                    return(Enumerable.Empty <Type>());
                }
            })
                       .Where(type => typeof(CodegenJob).IsAssignableFrom(type))
                       .Where(type => !type.IsAbstract)
                       .Where(type => !type.GetCustomAttributes(typeof(IgnoreCodegenJobAttribute)).Any())
                       .Select(type =>
            {
                logger.Info($"Creating instance of {type}.");
                return((CodegenJob)Activator.CreateInstance(type, options.NativeOutputDirectory, fileSystem, store, options.Force));
            })
                       .ToArray();

            logger.Info("Calling JobRunner.");
            new JobRunner(fileSystem).Run(jobs);

            logger.Info("Finished code generation.");
            return(0);
        }
예제 #8
0
        public DetailsStore(SchemaBundle bundle, List <string> serializationOverrides)
        {
            this.bundle = bundle;

            var overrideMap = serializationOverrides.Select(@override =>
            {
                var parts = @override.Split(";");

                if (parts.Length != 2)
                {
                    throw new ArgumentException($"Serialization override malformed: {@override}");
                }

                return(parts[0], parts[1]);
            }).ToDictionary(pair => pair.Item1, pair => pair.Item2);

            PopulateBlittableMaps();
            BlittableSet = ImmutableHashSet.CreateRange(blittableMap.Where(kv => kv.Value).Select(kv => kv.Key));

            var enums      = new Dictionary <string, UnityEnumDetails>();
            var types      = new Dictionary <string, UnityTypeDetails>();
            var components = new Dictionary <string, UnityComponentDetails>();

            foreach (var file in bundle.SchemaFiles)
            {
                foreach (var enumm in file.Enums)
                {
                    enums.Add(enumm.QualifiedName, new UnityEnumDetails(file.Package.Name, enumm));
                }

                foreach (var type in file.Types)
                {
                    var typeDetails = new UnityTypeDetails(file.Package.Name, type);

                    if (overrideMap.TryGetValue(typeDetails.FullyQualifiedTypeName, out var staticClassFqn))
                    {
                        typeDetails.SerializationOverride = new SerializationOverride(staticClassFqn);
                    }

                    types.Add(type.QualifiedName, typeDetails);
                }

                foreach (var component in file.Components)
                {
                    components.Add(component.QualifiedName, new UnityComponentDetails(file.Package.Name, component, this));
                }
            }

            Enums      = new ReadOnlyDictionary <string, UnityEnumDetails>(enums);
            Types      = new ReadOnlyDictionary <string, UnityTypeDetails>(types);
            Components = new ReadOnlyDictionary <string, UnityComponentDetails>(components);

            SchemaFiles = bundle.SchemaFiles
                          .Select(file => file.CanonicalPath)
                          .ToList().AsReadOnly();

            foreach (var kv in Types)
            {
                kv.Value.PopulateFields(this);
                kv.Value.PopulateChildren(this);
            }

            foreach (var kv in Components)
            {
                kv.Value.PopulateFields(this);
            }

            RemoveRecursiveOptions();
        }
예제 #9
0
        public DetailsStore(SchemaBundle bundle, IEnumerable <string> serializationOverrides, IFileTree fileTree)
        {
            this.bundle = bundle;
            FileTree    = fileTree;

            Logger.Trace("Loading serialization overrides.");
            var overrideMap = serializationOverrides.Select(@override =>
            {
                var parts = @override.Split(";");

                if (parts.Length != 2)
                {
                    throw new ArgumentException($"Serialization override malformed: {@override}");
                }

                Logger.Info($"Found serialization override {parts[1]} for {parts[0]}.");

                return(parts[0], parts[1]);
            }).ToDictionary(pair => pair.Item1, pair => pair.Item2);

            Logger.Trace($"Found {overrideMap.Count} serialization {(overrideMap.Count == 1 ? "override" : "overrides")}.");

            PopulateBlittableMaps();
            BlittableSet = ImmutableHashSet.CreateRange(blittableMap.Where(kv => kv.Value).Select(kv => kv.Key));

            var enums      = new Dictionary <string, UnityEnumDetails>();
            var types      = new Dictionary <string, UnityTypeDetails>();
            var components = new Dictionary <string, UnityComponentDetails>();

            Logger.Trace("Processing schema files.");
            foreach (var file in bundle.SchemaFiles)
            {
                Logger.Trace($"Initialising details from {file.CanonicalPath}.");

                foreach (var rawEnum in file.Enums)
                {
                    enums.Add(rawEnum.QualifiedName, new UnityEnumDetails(file.Package.Name, rawEnum));
                }

                foreach (var type in file.Types)
                {
                    var typeDetails = new UnityTypeDetails(file.Package.Name, type);

                    if (overrideMap.TryGetValue(typeDetails.FullyQualifiedName, out var staticClassFqn))
                    {
                        typeDetails.SerializationOverride = new SerializationOverride(staticClassFqn);
                        Logger.Trace($"Adding serialization override {staticClassFqn} for {typeDetails.QualifiedName}.");
                    }

                    types.Add(type.QualifiedName, typeDetails);
                }

                foreach (var component in file.Components)
                {
                    components.Add(component.QualifiedName, new UnityComponentDetails(file.Package.Name, component, this));
                }

                Logger.Trace($"Enums added: {file.Enums.Count}.");
                Logger.Trace($"Types added: {file.Types.Count}.");
                Logger.Trace($"Components added: {file.Components.Count}.");
            }

            Logger.Info($"Processed {bundle.SchemaFiles.Count} schema files.");

            Enums      = new ReadOnlyDictionary <string, UnityEnumDetails>(enums);
            Types      = new ReadOnlyDictionary <string, UnityTypeDetails>(types);
            Components = new ReadOnlyDictionary <string, UnityComponentDetails>(components);

            SchemaFiles = bundle.SchemaFiles
                          .Select(file => file.CanonicalPath)
                          .ToList().AsReadOnly();
            Logger.Trace($"Retrieved canonical paths of {SchemaFiles.Count} schema files.");

            Logger.Trace("Populating all type details.");
            foreach (var kv in Types)
            {
                kv.Value.Populate(this);
            }

            Logger.Trace($"Populated details of {Types.Count} types.");

            Logger.Trace($"Populating all component field details.");
            foreach (var kv in Components)
            {
                kv.Value.PopulateFields(this);
            }

            Logger.Trace($"Populated field details of {Components.Count} components.");

            Logger.Trace("Removing all recursive options.");
            var numFieldsRemoved = RemoveRecursiveOptions();

            if (numFieldsRemoved > 0)
            {
                Logger.Trace($"Removed {numFieldsRemoved} recursive options.");
            }
        }
예제 #10
0
 public void ParsingSchemaBundle_does_not_throw()
 {
     Assert.DoesNotThrow(() => SchemaBundle.LoadBundle(GetBundleContents()));
 }
예제 #11
0
        public void OneTimeSetup()
        {
            var bundle = JsonParsingTests.GetBundleContents();

            store = new DetailsStore(SchemaBundle.FromJson(bundle));
        }
예제 #12
0
 public void ParsingSchemaBundleV1_does_not_throw()
 {
     Assert.DoesNotThrow(() => SchemaBundle.FromJson(GetBundleContents()));
 }