public void WriteCode(List <GeneratorOutput> outputs) { var outputPath = Options.OutputDir; if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } foreach (var output in outputs) { var fileBase = output.TranslationUnit.FileNameWithoutExtension; if (Options.GenerateName != null) { fileBase = Options.GenerateName(output.TranslationUnit); } foreach (var template in output.Templates) { var fileName = string.Format("{0}.{1}", fileBase, template.FileExtension); Diagnostics.EmitMessage("Generated '{0}'", fileName); var filePath = Path.Combine(outputPath, fileName); string file = Path.GetFullPath(filePath); File.WriteAllText(file, template.Generate()); Options.CodeFiles.Add(file); } } }
void OnFileParsed(string file, ParserResult result) { switch (result.Kind) { case ParserResultKind.Success: Diagnostics.EmitMessage(DiagnosticId.ParseResult, "Parsed '{0}'", file); break; case ParserResultKind.Error: Diagnostics.EmitError(DiagnosticId.ParseResult, "Error parsing '{0}'", file); break; case ParserResultKind.FileNotFound: Diagnostics.EmitError(DiagnosticId.ParseResult, "File '{0}' was not found", file); break; } foreach (var diag in result.Diagnostics) { if (Options.IgnoreParseWarnings && diag.Level == ParserDiagnosticLevel.Warning) { continue; } Diagnostics.EmitMessage(DiagnosticId.ParserDiagnostic, "{0}({1},{2}): {3}: {4}", diag.FileName, diag.LineNumber, diag.ColumnNumber, diag.Level.ToString().ToLower(), diag.Message); } }
public void CompileCode() { try { var assemblyFile = string.IsNullOrEmpty(Options.LibraryName) ? "out.dll" : Options.LibraryName + ".dll"; var docFile = Path.ChangeExtension(Path.GetFileName(assemblyFile), ".xml"); var compilerOptions = new StringBuilder(); compilerOptions.Append(" /doc:" + docFile); compilerOptions.Append(" /debug:pdbonly"); compilerOptions.Append(" /unsafe"); var compilerParameters = new CompilerParameters { GenerateExecutable = false, TreatWarningsAsErrors = false, OutputAssembly = assemblyFile, GenerateInMemory = false, CompilerOptions = compilerOptions.ToString() }; compilerParameters.ReferencedAssemblies.Add(typeof(object).Assembly.Location); var location = Assembly.GetExecutingAssembly().Location; var locationRuntime = Path.Combine(Path.GetDirectoryName(location), "CppSharp.Runtime.dll"); compilerParameters.ReferencedAssemblies.Add(locationRuntime); var codeProvider = new CSharpCodeProvider( new Dictionary <string, string> { { "CompilerVersion", "v4.0" } }); var compilerResults = codeProvider.CompileAssemblyFromFile( compilerParameters, Options.CodeFiles.ToArray()); var errors = compilerResults.Errors.Cast <CompilerError>(); foreach (var error in errors.Where(error => !error.IsWarning)) { Diagnostics.EmitError(error.ToString()); } } catch (Exception exception) { Diagnostics.EmitError("Could not compile the generated source code"); Diagnostics.EmitMessage(exception.ToString()); } }