Esempio n. 1
0
        private static void GenerateCodeFor(string input, string output, string name)
        {
            EPackage        package = EcoreInterop.LoadPackageFromFile(input);
            INamespace      nMeta   = EcoreInterop.Transform2Meta(package);
            CodeCompileUnit code    = MetaFacade.CreateCode(nMeta, name);

            MetaFacade.GenerateCode(code, new CSharpCodeProvider(), output, true);
        }
Esempio n. 2
0
        public static int GenerateAndCompile(INamespace ns, Action <string, int> afterCompileAction, out string error, out string log)
        {
            lock (codeGenLock)
            {
                var path = Path.GetTempFileName();
                File.Delete(path);
                Directory.CreateDirectory(path);

                try
                {
                    var projectFile = Path.Combine(path, "project.csproj");
                    var codeFile    = Path.Combine(path, "code.cs");

                    var code = MetaFacade.CreateCode(ns, "TemporaryGeneratedCode");
                    MetaFacade.GenerateCode(code, new Microsoft.CSharp.CSharpCodeProvider(), codeFile, false);

                    File.WriteAllText(projectFile, GenerateProjectFile(codeFile));

                    var startInfo = new ProcessStartInfo("dotnet.exe", "build " + projectFile);
                    startInfo.CreateNoWindow         = true;
                    startInfo.ErrorDialog            = false;
                    startInfo.UseShellExecute        = false;
                    startInfo.RedirectStandardError  = true;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.WorkingDirectory       = path;

                    var buildJob      = Process.Start(startInfo);
                    var outputBuilder = new StringBuilder();
                    var errorBuilder  = new StringBuilder();

                    buildJob.ErrorDataReceived += (o, e) =>
                    {
                        errorBuilder.AppendLine(e.Data);
                        Debug.WriteLine(e.Data);
                    };
                    var line = buildJob.StandardOutput.ReadLine();
                    while (line != null)
                    {
                        outputBuilder.AppendLine(line);
                        line = buildJob.StandardOutput.ReadLine();
                    }
                    buildJob.WaitForExit();
                    error = errorBuilder.ToString();
                    log   = outputBuilder.ToString();
                    afterCompileAction?.Invoke(path, buildJob.ExitCode);
                    return(buildJob.ExitCode);
                }
                finally
                {
                    try
                    {
                        Directory.Delete(path, true);
                    }
                    // In code coverage analysis, another process is usually accessing our temporary folder
                    catch (Exception) { }
                }
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var core = CreateCoreNamespace();
            var unit = MetaFacade.CreateCode(core, "FZI.SoftwareEngineering.DeepModeling");

            MetaFacade.GenerateCode(unit, new Microsoft.CSharp.CSharpCodeProvider(), @"..\..\..\DeepADL\DeepADL", true);
            using (var fs = File.Create(@"..\..\..\DeepADL.nmf"))
            {
                MetaRepository.Instance.Serializer.Serialize(core.Model, fs);
            }
        }
Esempio n. 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.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);
                }
            }
        }
Esempio n. 5
0
        public static int GenerateAndCompile(INamespace ns, out string error, out string log)
        {
            lock (codeGenLock)
            {
                var path = Path.GetTempFileName();
                File.Delete(path);
                Directory.CreateDirectory(path);

                try
                {
                    var projectFile = Path.Combine(path, "project.csproj");
                    var codeFile    = Path.Combine(path, "code.cs");

                    var code = MetaFacade.CreateCode(ns, "Test");
                    MetaFacade.GenerateCode(code, new Microsoft.CSharp.CSharpCodeProvider(), codeFile, false);

                    File.WriteAllText(projectFile, GenerateProjectFile(codeFile));

                    var startInfo = new ProcessStartInfo("MSBuild.exe", projectFile);
                    startInfo.CreateNoWindow         = true;
                    startInfo.ErrorDialog            = false;
                    startInfo.UseShellExecute        = false;
                    startInfo.RedirectStandardError  = true;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.WorkingDirectory       = path;

                    var    buildJob = Process.Start(startInfo);
                    string buildLog = null;

                    buildJob.ErrorDataReceived += (o, e) =>
                    {
                        buildLog += e.Data;
                        Debug.WriteLine(e.Data);
                    };
                    log = null;
                    var line = buildJob.StandardOutput.ReadLine();
                    while (line != null)
                    {
                        log += line;
                        line = buildJob.StandardOutput.ReadLine();
                    }
                    buildJob.WaitForExit();
                    error = buildLog;
                    return(buildJob.ExitCode);
                }
                finally
                {
                    Directory.Delete(path, true);
                }
            }
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            var uri          = new Uri("http://github.com/georghinkel/mediaStore/Repository");
            var mediaStoreL1 = CreateMediaStoreRepository(uri);
            var model        = new Model()
            {
                ModelUri = uri
            };

            model.RootElements.Add(mediaStoreL1);
            var unit = MetaFacade.CreateCode(mediaStoreL1, "FZI.SoftwareEngineering.DeepModeling");

            MetaFacade.GenerateCode(unit, new Microsoft.CSharp.CSharpCodeProvider(), @"..\..\..\MediaStoreRepository\MediaStoreRepository", true);
            using (var fs = File.Create(@"..\..\..\MediaStoreRepository.nmf"))
            {
                MetaRepository.Instance.Serializer.Serialize(mediaStoreL1, fs);
            }
        }
Esempio n. 7
0
        private static void GenerateMediaStoreAssembly(int?factor, string codeLocation, string metamodelLocation)
        {
            var uri = new Uri("http://github.com/georghinkel/mediaStore/Assembly" + (factor.HasValue ? "X" + factor.Value.ToString() : ""));

            var system = new MediaStoreSystem()
            {
                Name = "MediaStore" + (factor.HasValue ? "X" + factor.Value.ToString() : "")
            };
            var mediaStoreL2 = new Namespace()
            {
                Name   = "Assembly" + (factor.HasValue ? "X" + factor.Value.ToString() : ""),
                Uri    = uri,
                Prefix = "mediaStore" + (factor.HasValue ? "X" + factor.Value.ToString() : "")
            };

            mediaStoreL2.Types.Add(system);
            if (!factor.HasValue)
            {
                CreateMediaStoreAssembly(system, string.Empty);
            }
            else
            {
                for (int i = 1; i <= factor.Value; i++)
                {
                    CreateMediaStoreAssembly(system, i.ToString());
                }
            }
            var model = new Model()
            {
                ModelUri = uri
            };

            model.RootElements.Add(mediaStoreL2);
            var unit = MetaFacade.CreateCode(mediaStoreL2, "FZI.SoftwareEngineering.DeepModeling");

            MetaFacade.GenerateCode(unit, new Microsoft.CSharp.CSharpCodeProvider(), codeLocation, true);
            using (var fs = File.OpenWrite(metamodelLocation))
            {
                MetaRepository.Instance.Serializer.Serialize(mediaStoreL2, fs);
            }
        }
Esempio n. 8
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!");
        }