Exemplo n.º 1
0
        /// <summary>
        /// User has requested the generation of code given an assembly and specification file.
        /// </summary>
        /// <param name="specification"></param>
        /// <param name="assembly"></param>
        /// <param name="outputPath"></param>
        public static void GenerateCode(FileInfo specification, FileInfo assembly, string outputPath = default)
        {
            if (specification == null)
            {
                throw new ArgumentException(Resources.NoSpecificationFileProvided, nameof(specification));
            }

            // register the appropriate style class for its related type key. Each type in the specification needs to have a 'style' specified
            // for it to be able to generate the appropriate type of code for it.


            TypeGeneratorStore.Instance.Register(global::Birch.Generator.Styles.Extension.Style.Type, () => new global::Birch.Generator.Styles.Extension.Style());
            TypeGeneratorStore.Instance.Register(global::Birch.Generator.Styles.Class.Style.Type, () => new global::Birch.Generator.Styles.Class.Style());

            var job = JobReader.FromFile(Path.Join(specification.DirectoryName, specification.Name));

            var outputTypeStore = OutputTypesStore.Create(job.Configuration.OutputTypes);

            var typeStore = new TypeStore();

            typeStore.Load(Path.Join(assembly.DirectoryName, assembly.Name));

            var modelBuilder = new ModelBuilder(typeStore, outputTypeStore);
            var models       = modelBuilder.Create(job);

            var codeBuilder = new CodeBuilder(job);
            var output      = codeBuilder.Create(models);

            if (outputPath != null)
            {
                File.WriteAllText(outputPath, output);
                Console.WriteLine(Resources.CodeWrittenTo, outputPath);
            }
            else
            {
                Console.WriteLine(output);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// User has requested the generation of a specification file for an assembly.
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="outputPath"></param>
        /// <param name="typeFilter"></param>
        /// <param name="methodFilter"></param>
        /// <param name="templatesStyle"></param>
        /// <param name="templateExportStyle"></param>
        public static void GenerateSpecification(FileInfo assembly, string outputPath = default,
                                                 string typeFilter = default, string methodFilter = default, string templatesStyle = default,
                                                 TemplateExportStyle templateExportStyle = TemplateExportStyle.Embed)
        {
            var typeStore = new TypeStore();

            typeStore.Load(Path.Join(assembly.DirectoryName, assembly.Name));

            var g = new global::Birch.Generator.Specification.Generator(Path.Join(Directory.GetCurrentDirectory(), "\\Templates"));

            Func <ITypeDefinition, bool> typeFilterFunc;

            // lets see if we can convert the type filter into something that can be used at runtime...

            if (!string.IsNullOrEmpty(typeFilter))
            {
                try
                {
                    var parameter = System.Linq.Expressions.Expression.Parameter(typeof(ITypeDefinition), "typeDefinition");

                    var expression = DynamicExpressionParser.ParseLambda(new[] { parameter }, null, typeFilter);

                    var compiled = expression.Compile();

                    typeFilterFunc = (typeDefinition) => (bool)compiled.DynamicInvoke(typeDefinition);
                }
                catch (Exception exception)
                {
                    throw new ArgumentException($"Unable to parse type filter:'{typeFilter}'", exception);
                }
            }
            else
            {
                // just pull in everything
                typeFilterFunc = (s) => true;
            }

            Func <IMethod, bool> methodFilterFunc;

            if (!string.IsNullOrEmpty(methodFilter))
            {
                try
                {
                    var parameter = System.Linq.Expressions.Expression.Parameter(typeof(IMethod), "method");

                    var expression = DynamicExpressionParser.ParseLambda(new[] { parameter }, null, methodFilter);

                    var compiled = expression.Compile();

                    methodFilterFunc = (typeDefinition) => (bool)compiled.DynamicInvoke(typeDefinition);
                }
                catch (Exception exception)
                {
                    throw new ArgumentException($"Unable to method filter:'{methodFilter}'", exception);
                }
            }
            else
            {
                // just pull in everything
                methodFilterFunc = (s) => true;
            }


            var r = g.Generate(typeStore, typeFilterFunc, methodFilterFunc, templatesStyle);

            // if the user specified they want their templates to be individually saved, then we need to do that as well...
            if (templateExportStyle == TemplateExportStyle.File)
            {
                var outputDirectory = Path.GetDirectoryName(outputPath);

                var revisedTemplates = new List <Template>();

                foreach (var template in r.Configuration.Templates)
                {
                    var fileName = $"{template.Name}.{TemplateExtension}";

                    var fullPath = Path.Join(outputDirectory, fileName);

                    File.WriteAllText(fullPath, template.Content);

                    revisedTemplates.Add(new Template(template.Name, template.Type, TemplateLocation.File)
                    {
                        Path = fileName
                    });
                }

                r.Configuration.Templates = revisedTemplates;
            }

            var f      = new JsonFormatter();
            var output = f.Format(r);

            if (outputPath != null)
            {
                File.WriteAllText(outputPath, output);
                Console.WriteLine(Resources.SpecificationFileWritten, outputPath);
            }
            else
            {
                Console.WriteLine(output);
            }
        }