Ejemplo n.º 1
0
		/// <summary>
		///   Compiles the <paramref name="compilation" /> with the S# compiler and returns the resulting assembly that has been
		///   loaded into the app domain.
		/// </summary>
		/// <param name="compilation">The compilation that should be compiled.</param>
		/// <param name="output">The output that should be used to write test output.</param>
		public static Assembly CompileSafetySharp(Compilation compilation, TestTraceOutput output)
		{
			using (var workspace = new AdhocWorkspace())
			{
				var project = workspace
					.AddProject(compilation.AssemblyName, LanguageNames.CSharp)
					.AddMetadataReferences(compilation.References)
					.WithCompilationOptions(compilation.Options);

				foreach (var syntaxTree in compilation.SyntaxTrees)
					project = project.AddDocument(syntaxTree.FilePath, syntaxTree.GetRoot().GetText(Encoding.UTF8)).Project;

				var errorReporter = new TestErrorReporter(output);
				var compiler = new Compiler(errorReporter);

				try
				{
					var assembly = compiler.Compile(project);
					output.Trace("{0}", SyntaxTreesToString(compiler.Compilation));

					return assembly;
				}
				catch (CompilationException e)
				{
					throw new TestException("{0}\n\n{1}", e.Message, SyntaxTreesToString(compiler.Compilation));
				}
			}
		}
Ejemplo n.º 2
0
		/// <summary>
		///     Runs the compilation process.
		/// </summary>
		/// <param name="args">The compiler arguments passed via the command line.</param>
		private int Compile(string[] args)
		{
			var log = new ConsoleErrorReporter();

			using (var parser = new Parser(c => c.HelpWriter = null))
			{
				// Check the arguments for '--help' or '-h' as the command line parser library handles help in a strange
				// way. If so, output the help screen and successfully terminate the application.
				if (args.Any(arg => arg == "--help" || arg == "-h"))
				{
					log.Info("{0}", GenerateHelpMessage());
					return 0;
				}

				// If there was an error parsing the command line, show the help screen and terminate the application.
				if (!parser.ParseArguments(args, this))
				{
					log.Info("{0}", GenerateHelpMessage());
					log.Die("Invalid command line arguments.");
				}
			}

			log.Silent = Silent;
			log.Info("");

			log.Info("S# Compiler");
			log.Info("Copyright (c) 2014 Institute for Software & Systems Engineering");

			log.Info("");
			log.Info("This is free software. You may redistribute copies of it under the terms of");
			log.Info("the MIT license (see http://opensource.org/licenses/MIT).");

			log.Info("");

			// Start the compilation process.
			try
			{
				var compiler = new Compiler(log);
				if (!compiler.Compile(ProjectFile, Configuration, Platform))
					return -1;

				log.Info("Compilation completed successfully.");
				return 0;
			}
			catch (Exception e)
			{
				log.Error("A fatal compilation error occurred: {0}", e.Message);
#if DEBUG
				log.Error("StackTrace:\n{0}", e.StackTrace);
#endif
				return -1;
			}
		}