예제 #1
0
        private static void GenerateCode(Options options)
        {
            var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation();
            var stopWatch        = new Stopwatch();

            packageTransform.ForceGeneration  = options.Force;
            packageTransform.CreateOperations = options.Operations;
            packageTransform.DefaultNamespace = options.OverallNamespace;

            Dictionary <Uri, string> mappings = null;

            if (options.NamespaceMappings != null && options.NamespaceMappings.Count > 0)
            {
                mappings = new Dictionary <Uri, string>();
                foreach (var mapping in options.NamespaceMappings)
                {
                    if (string.IsNullOrEmpty(mapping))
                    {
                        continue;
                    }
                    var lastIdx = mapping.LastIndexOf('=');
                    if (lastIdx == -1)
                    {
                        Console.WriteLine("Namespace mapping {0} is missing required separator =", mapping);
                        continue;
                    }
                    Uri uri;
                    if (!Uri.TryCreate(mapping.Substring(0, lastIdx), UriKind.Absolute, out uri))
                    {
                        uri = new Uri(mapping.Substring(0, lastIdx), UriKind.Relative);
                    }
                    mappings.Add(uri, mapping.Substring(lastIdx + 1));
                }
            }

            var metaPackage = LoadPackageFromFiles(options.InputFiles, options.OverallNamespace, mappings);

            if (options.Uri != null)
            {
                Uri uri;
                if (Uri.TryCreate(options.Uri, UriKind.Absolute, out uri))
                {
                    metaPackage.Uri = uri;
                }
                else
                {
                    Console.Error.WriteLine("The provided string {0} could not be parsed as an absolute URI.", options.Uri);
                }
            }
            if (metaPackage.Uri == null)
            {
                Console.Error.WriteLine("Warning: There is no base Uri for the provided metamodels. Some features of the generated code will be disabled.");
            }

            var model = metaPackage.Model;

            if (model == null)
            {
                model = new Model();
                model.RootElements.Add(metaPackage);
            }
            model.ModelUri = metaPackage.Uri;
            if (options.NMeta != null)
            {
                using (var fs = File.Create(options.NMeta))
                {
                    MetaRepository.Instance.Serializer.Serialize(model, fs);
                }
            }

            stopWatch.Start();
            var compileUnit = TransformationEngine.Transform <INamespace, CodeCompileUnit>(metaPackage,
                                                                                           options.Parallel
                   ? (ITransformationEngineContext) new ParallelTransformationContext(packageTransform)
                   : new TransformationContext(packageTransform));

            stopWatch.Stop();

            Console.WriteLine("Operation took {0}ms", stopWatch.Elapsed.TotalMilliseconds);

            CodeDomProvider generator = null;

            switch (options.Language)
            {
            case SupportedLanguage.CS:
                generator = new Microsoft.CSharp.CSharpCodeProvider();
                break;

            case SupportedLanguage.VB:
                generator = new Microsoft.VisualBasic.VBCodeProvider();
                break;

            case SupportedLanguage.CPP:
                generator = new Microsoft.VisualC.CppCodeProvider();
                break;

            case SupportedLanguage.JS:
                generator = new Microsoft.JScript.JScriptCodeProvider();
                break;

            default:
                Console.WriteLine("Unknown language detected. Falling back to default C#");
                generator = new Microsoft.CSharp.CSharpCodeProvider();
                break;
            }

            var genOptions = new System.CodeDom.Compiler.CodeGeneratorOptions()
            {
                BlankLinesBetweenMembers = true,
                VerbatimOrder            = false,
                ElseOnClosing            = false,
                BracingStyle             = "C",
                IndentString             = "    "
            };

            if (options.UseFolders)
            {
                foreach (var file in MetaFacade.SplitCompileUnit(compileUnit))
                {
                    var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension);
                    CheckDirectoryExists(fileInfo.Directory);
                    using (var fs = fileInfo.Create())
                    {
                        using (var sw = new StreamWriter(fs))
                        {
                            generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions);
                        }
                    }
                }
            }
            else
            {
                using (var sw = new StreamWriter(options.OutputFile))
                {
                    generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions);
                }
            }

            Console.WriteLine("Code generated successfully!");
        }
예제 #2
0
파일: Program.cs 프로젝트: FrederikP/NMF
        private static void GenerateCode(Options options)
        {
            var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation();
            var stopWatch = new Stopwatch();

            packageTransform.ForceGeneration = options.Force;
            packageTransform.CreateOperations = options.Operations;
            packageTransform.DefaultNamespace = options.OverallNamespace;

            var metaPackage = LoadPackageFromFiles(options.InputFiles, options.OverallNamespace);

            var model = metaPackage.Model;
            if (model == null)
            {
                model = new Model();
                model.RootElements.Add(metaPackage);
            }
            model.ModelUri = metaPackage.Uri;
            if (options.NMeta != null)
            {
                using (var fs = File.OpenWrite(options.NMeta))
                {
                    MetaRepository.Instance.Serializer.Serialize(model, fs);
                }
            }

            stopWatch.Start();
            var compileUnit = TransformationEngine.Transform<INamespace, CodeCompileUnit>(metaPackage, 
                options.Parallel
                   ? (ITransformationEngineContext)new ParallelTransformationContext(packageTransform)
                   : new TransformationContext(packageTransform));
            stopWatch.Stop();

            Console.WriteLine("Operation took {0}ms", stopWatch.Elapsed.TotalMilliseconds);

            CodeDomProvider generator = null;

            switch (options.Language)
            {
                case SupportedLanguage.CS:
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
                case SupportedLanguage.VB:
                    generator = new Microsoft.VisualBasic.VBCodeProvider();
                    break;
                case SupportedLanguage.FS:
                    generator = new Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider();
                    break;
                case SupportedLanguage.CPP:
                    generator = new Microsoft.VisualC.CppCodeProvider();
                    break;
                case SupportedLanguage.JS:
                    generator = new Microsoft.JScript.JScriptCodeProvider();
                    break;
                default:
                    Console.WriteLine("Unknown language detected. Falling back to default C#");
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
            }

            var genOptions = new System.CodeDom.Compiler.CodeGeneratorOptions()
                {
                    BlankLinesBetweenMembers = true,
                    VerbatimOrder = false,
                    ElseOnClosing = false,
                    BracingStyle = "C",
                    IndentString = "    "
                };
            if (options.UseFolders)
            {
                foreach (var file in MetaFacade.SplitCompileUnit(compileUnit))
                {
                    var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension);
                    CheckDirectoryExists(fileInfo.Directory);
                    using (var fs = fileInfo.Create())
                    {
                        using (var sw = new StreamWriter(fs))
                        {
                            generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions);
                        }
                    }
                }
            }
            else
            {
                using (var sw = new StreamWriter(options.OutputFile))
                {
                    generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions);
                }
            }

            Console.WriteLine("Code generated successfully!");
        }
예제 #3
0
 internal void createProvider()
 {
     if (this.isCPPProject)
         provider = new Microsoft.VisualC.CppCodeProvider();
     else {
         if (isVB)
             provider = new Microsoft.VisualBasic.VBCodeProvider();
         else
             provider = new Microsoft.CSharp.CSharpCodeProvider();
     }
 }
예제 #4
0
        private void OutputGeneratedCode(CodeCompileUnit compileUnit)
        {
            CodeDomProvider generator = null;

            switch (options.Language)
            {
            case SupportedLanguage.CS:
                generator = new Microsoft.CSharp.CSharpCodeProvider();
                break;

            case SupportedLanguage.VB:
                generator = new Microsoft.VisualBasic.VBCodeProvider();
                break;

            case SupportedLanguage.CPP:
                generator = new Microsoft.VisualC.CppCodeProvider();
                break;

            case SupportedLanguage.JS:
                generator = new Microsoft.JScript.JScriptCodeProvider();
                break;

            case SupportedLanguage.PY:
                generator = new PythonProvider();
                break;

            default:
                Console.WriteLine("Unknown language detected. Falling back to default C#");
                generator = new Microsoft.CSharp.CSharpCodeProvider();
                break;
            }

            var genOptions = new CodeGeneratorOptions()
            {
                BlankLinesBetweenMembers = true,
                VerbatimOrder            = false,
                ElseOnClosing            = false,
                BracingStyle             = "C",
                IndentString             = "    "
            };

            if (options.UseFolders)
            {
                foreach (var file in MetaFacade.SplitCompileUnit(compileUnit))
                {
                    var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension);
                    CheckDirectoryExists(fileInfo.Directory);
                    using (var sw = new StreamWriter(fileInfo.Create()))
                    {
                        generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions);
                    }
                }
            }
            else
            {
                using (var sw = new StreamWriter(options.OutputFile))
                {
                    generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions);
                }
            }
        }
예제 #5
0
파일: Program.cs 프로젝트: NMFCode/NMF
        private static void GenerateCode(Options options)
        {
            var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation();
            var stopWatch = new Stopwatch();

            packageTransform.ForceGeneration = options.Force;
            packageTransform.CreateOperations = options.Operations;
            packageTransform.DefaultNamespace = options.OverallNamespace;

            Dictionary<Uri, string> mappings = null;
            if (options.NamespaceMappings != null && options.NamespaceMappings.Count > 0)
            {
                mappings = new Dictionary<Uri, string>();
                foreach (var mapping in options.NamespaceMappings)
                {
                    if (string.IsNullOrEmpty(mapping)) continue;
                    var lastIdx = mapping.LastIndexOf('=');
                    if (lastIdx == -1)
                    {
                        Console.WriteLine("Namespace mapping {0} is missing required separator =", mapping);
                        continue;
                    }
                    Uri uri;
                    if (!Uri.TryCreate(mapping.Substring(0, lastIdx), UriKind.Absolute, out uri))
                    {
                        uri = new Uri(mapping.Substring(0, lastIdx), UriKind.Relative);
                    }
                    mappings.Add(uri, mapping.Substring(lastIdx + 1));
                }
            }

            var metaPackage = LoadPackageFromFiles(options.InputFiles, options.OverallNamespace, mappings);
            if (options.Uri != null)
            {
                Uri uri;
                if (Uri.TryCreate(options.Uri, UriKind.Absolute, out uri))
                {
                    metaPackage.Uri = uri;
                }
                else
                {
                    Console.Error.WriteLine("The provided string {0} could not be parsed as an absolute URI.", options.Uri);
                }
            }
            if (metaPackage.Uri == null)
            {
                Console.Error.WriteLine("Warning: There is no base Uri for the provided metamodels. Some features of the generated code will be disabled.");
            }

            var model = metaPackage.Model;
            if (model == null)
            {
                model = new Model();
                model.RootElements.Add(metaPackage);
            }
            model.ModelUri = metaPackage.Uri;
            if (options.NMeta != null)
            {
                using (var fs = File.Create(options.NMeta))
                {
                    MetaRepository.Instance.Serializer.Serialize(model, fs);
                }
            }

            stopWatch.Start();
            var compileUnit = TransformationEngine.Transform<INamespace, CodeCompileUnit>(metaPackage,
                options.Parallel
                   ? (ITransformationEngineContext)new ParallelTransformationContext(packageTransform)
                   : new TransformationContext(packageTransform));
            stopWatch.Stop();

            Console.WriteLine("Operation took {0}ms", stopWatch.Elapsed.TotalMilliseconds);

            CodeDomProvider generator = null;

            switch (options.Language)
            {
                case SupportedLanguage.CS:
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
                case SupportedLanguage.VB:
                    generator = new Microsoft.VisualBasic.VBCodeProvider();
                    break;
                case SupportedLanguage.CPP:
                    generator = new Microsoft.VisualC.CppCodeProvider();
                    break;
                case SupportedLanguage.JS:
                    generator = new Microsoft.JScript.JScriptCodeProvider();
                    break;
                default:
                    Console.WriteLine("Unknown language detected. Falling back to default C#");
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
            }

            var genOptions = new System.CodeDom.Compiler.CodeGeneratorOptions()
                {
                    BlankLinesBetweenMembers = true,
                    VerbatimOrder = false,
                    ElseOnClosing = false,
                    BracingStyle = "C",
                    IndentString = "    "
                };
            if (options.UseFolders)
            {
                foreach (var file in MetaFacade.SplitCompileUnit(compileUnit))
                {
                    var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension);
                    CheckDirectoryExists(fileInfo.Directory);
                    using (var fs = fileInfo.Create())
                    {
                        using (var sw = new StreamWriter(fs))
                        {
                            generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions);
                        }
                    }
                }
            }
            else
            {
                using (var sw = new StreamWriter(options.OutputFile))
                {
                    generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions);
                }
            }

            Console.WriteLine("Code generated successfully!");
        }