Beispiel #1
0
        public void Emit(CompiledProject project, CompiledProjects projects)
        {
            Console.WriteLine($"Emitting {project.Name} ...");

            var compileDirPath = Path.Combine(project.ProjectDirectory, ".forge-cache");

            DirectoryUtil.SafeDelete(compileDirPath);

            var targetDirPath = Path.Combine(project.ProjectDirectory, "targets", "debug");

            DirectoryUtil.SafeDelete(targetDirPath);

            Directory.CreateDirectory(compileDirPath);
            var cppSource     = compiler.EmitCpp(project.Package);
            var cppSourceName = project.Name + ".cpp";

            CreateFile(compileDirPath, cppSourceName, cppSource);
            if (project.Package.IsApp)
            {
                // write out the runtime
                CreateFile(compileDirPath, CppRuntime.FileName, CppRuntime.Source);
                foreach (var dependency in projects)
                {
                    var dependencyCppFileName = dependency.Name + ".cpp";
                    var dependencyCppFilePath = Path.Combine(dependency.ProjectDirectory, ".forge-cache", dependencyCppFileName);
                    File.Copy(dependencyCppFilePath, Path.Combine(compileDirPath, dependencyCppFileName));
                }
                Directory.CreateDirectory(targetDirPath);
                var result = CppCompiler.Invoke(Path.Combine(compileDirPath, cppSourceName), Path.Combine(targetDirPath, project.Name + ".exe"));
                if (result.ExitCode != 0)
                {
                    result.WriteOutputToConsole();
                }
            }
        }
        public void Test(TestCaseConfig config, TextReader reader)
        {
            var dependencies = config.Runtime ? new[] { runtimeDependency } : Enumerable.Empty <PackageReferenceSyntax>();
            var package      = new PackageSyntax($"Adamant.Exploratory.Compiler.Tests.{config.TestName}", true, dependencies);
            var unit         = compiler.Parse(package, new SourceReader(config.FileName, reader));

            package = package.With(new[] { unit });
            if (package.Diagnostics.Count > 0)
            {
                Assert.Fail(ToString(package.Diagnostics));
            }
            var compiledPackage = compiler.Compile(package, Enumerable.Empty <Package>());

            if (compiledPackage.Diagnostics.Count > 0)
            {
                Assert.Fail(ToString(compiledPackage.Diagnostics));
            }

            var cppSource     = compiler.EmitCpp(compiledPackage);
            var cppSourceName = compiledPackage.Name + ".cpp";

            CreateFile(cppSourceName, cppSource);
            CreateFile(CppRuntime.FileName, CppRuntime.Source);
            var targetPath = Path.Combine(WorkPath, compiledPackage.Name + ".exe");
            var result     = CppCompiler.Invoke(Path.Combine(WorkPath, cppSourceName), targetPath);

            if (result.ExitCode != 0)
            {
                result.WriteOutputToConsole();
                Assert.Fail("C++ compiler error");
            }

            // Execute the app
            using (var process = new Process())
            {
                process.StartInfo.FileName               = targetPath;
                process.StartInfo.WorkingDirectory       = Path.GetDirectoryName(WorkPath);
                process.StartInfo.CreateNoWindow         = true;
                process.StartInfo.UseShellExecute        = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                var outputBuffer = new StringBuilder();
                var errorBuffer  = new StringBuilder();
                process.OutputDataReceived += (s, e) => outputBuffer.AppendLine(e.Data);
                process.ErrorDataReceived  += (s, e) => errorBuffer.AppendLine(e.Data);
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();

                if (config.Result != null)
                {
                    Assert.AreEqual(config.Result, process.ExitCode, "Exit Code");
                }
                if (config.VerifyConsoleOutput)
                {
                    Assert.AreEqual(config.ExpectedConsoleOutput, outputBuffer.ToString());
                }
            }
        }