private static bool AddToCompileUnit(INamespace n, Meta2ClassesTransformation t) { if (t == null) { return(true); } return(t.AddToCompileUnit(n)); }
private void GenerateCode() { var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation(); var stopWatch = new Stopwatch(); packageTransform.ForceGeneration = options.Force; packageTransform.CreateOperations = options.Operations; packageTransform.DefaultNamespace = options.OverallNamespace; LoadTypeMappings(); Dictionary <Uri, string> mappings = LoadNamespaceMappings(); var metaPackage = LoadPackageFromFiles(mappings); SetUri(metaPackage); Model model = metaPackage.Model; if (model == null) { throw new InvalidOperationException("The namespace was not loaded correctly."); } 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); OutputGeneratedCode(compileUnit); Console.WriteLine("Code generated successfully!"); }
private static void AddNamespace(INamespace ns, CodeCompileUnit unit, ITransformationContext context, Meta2ClassesTransformation t) { if (AddToCompileUnit(ns, t)) { var codeNamespace = context.Trace.ResolveIn(ns2ns, ns); unit.Namespaces.Add(codeNamespace); } foreach (var child in ns.ChildNamespaces) { AddNamespace(child, unit, context, t); } }
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!"); }
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!"); }
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!"); }