private static void TranslateToExe(SpecSharpOptions commandLineOptions) //^ requires commandLineOptions.FileNames.Count > 0; { HostEnvironment hostEnvironment = new HostEnvironment(); hostEnvironment.Errors += hostEnvironment.HandleErrors; hostEnvironment.displayFileName = true; List<IAssemblyReference> assemblyReferences = GetAssemblyReferences(commandLineOptions, hostEnvironment); List<IModuleReference> moduleReferences = new List<IModuleReference>(); List<SpecSharpSourceDocument> programSources = new List<SpecSharpSourceDocument>(1); IName name = hostEnvironment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(commandLineOptions.FileNames[0])); SpecSharpAssembly assem = new SpecSharpAssembly(name, Path.GetFullPath(name.Value), hostEnvironment, commandLineOptions, assemblyReferences, moduleReferences, programSources); SpecSharpCompilationHelper helper = new SpecSharpCompilationHelper(assem.Compilation); foreach (string fileName in commandLineOptions.FileNames) { name = hostEnvironment.NameTable.GetNameFor(fileName); StreamReader instream = File.OpenText(fileName); programSources.Add(new SpecSharpSourceDocument(helper, name, Path.GetFullPath(fileName), instream)); } if (assem.Compilation.HasErrors) return; var sourceLocationProvider = assem.Compilation.SourceLocationProvider; var localScopeProvider = assem.Compilation.LocalScopeProvider; using (var pdbWriter = new PdbWriter(Path.ChangeExtension(assem.Location, "pdb"), sourceLocationProvider)) { PeWriter.WritePeToStream(assem, hostEnvironment, File.Create(Path.ChangeExtension(assem.Location, "exe")), sourceLocationProvider, localScopeProvider, pdbWriter); } }
public Assembly CompileProject(string projectFileName) { Assembly existing; if (Compilations.TryGetValue(Path.GetFullPath(projectFileName), out existing)) { return existing; } var project = new Microsoft.Build.BuildEngine.Project(); project.Load(projectFileName); var projectName = Environment.NameTable.GetNameFor(project.EvaluatedProperties["AssemblyName"].Value); var projectPath = project.FullFileName; var compilerOptions = new SpecSharpOptions(); var assemblyReferences = new List<IAssemblyReference>(); var moduleReferences = new List<IModuleReference>(); var programSources = new List<SpecSharpSourceDocument>(); var assembly = new SpecSharpAssembly(projectName, projectPath, Environment, compilerOptions, assemblyReferences, moduleReferences, programSources); var helper = new SpecSharpCompilationHelper(assembly.Compilation); Compilations[Path.GetFullPath(projectFileName)] = assembly; assemblyReferences.Add(Environment.LoadAssembly(Environment.CoreAssemblySymbolicIdentity)); project.Build("ResolveAssemblyReferences"); foreach (BuildItem item in project.GetEvaluatedItemsByName("ReferencePath")) { var assemblyName = new System.Reflection.AssemblyName(item.GetEvaluatedMetadata("FusionName")); var name = Environment.NameTable.GetNameFor(assemblyName.Name); var culture = assemblyName.CultureInfo != null ? assemblyName.CultureInfo.Name : ""; var version = assemblyName.Version == null ? new Version(0, 0) : assemblyName.Version; var token = assemblyName.GetPublicKeyToken(); if (token == null) token = new byte[0]; var location = item.FinalItemSpec; var identity = new AssemblyIdentity(name, culture, version, token, location); var reference = Environment.LoadAssembly(identity); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("ProjectReference")) { var name = Environment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(item.FinalItemSpec)); var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var reference = CompileProject(location); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("Compile")) { var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var name = Environment.NameTable.GetNameFor(location); var programSource = new SpecSharpSourceDocument(helper, name, location, File.ReadAllText(location)); programSources.Add(programSource); } return assembly; }
private static int RunTest(HostEnvironment hostEnvironment, string suiteName, string test, StringBuilder actualOutput, List<string> compilerParameters, List<string> testCaseParameters) { hostEnvironment.hasError = false; IName name = hostEnvironment.NameTable.GetNameFor(suiteName); SpecSharpOptions options = new SpecSharpOptions(); //TODO: extract from params List<IAssemblyReference> assemblyReferences = new List<IAssemblyReference>(); List<IModuleReference> moduleReferences = new List<IModuleReference>(); assemblyReferences.Add(hostEnvironment.LoadAssembly(hostEnvironment.CoreAssemblySymbolicIdentity)); IUnit unit; SpecSharpAssembly/*?*/ assem = null; SpecSharpCompilationHelper helper; if (hostEnvironment.previousDocument != null && compilerParameters.Contains("/incremental")) { unit = hostEnvironment.GetIncrementalUnit(test); helper = (SpecSharpCompilationHelper)hostEnvironment.previousDocument.SpecSharpCompilationPart.Helper; } else { List<SpecSharpSourceDocument> programSources = new List<SpecSharpSourceDocument>(1); assem = new SpecSharpAssembly(name, "", hostEnvironment, options, assemblyReferences, moduleReferences, programSources); helper = new SpecSharpCompilationHelper(assem.Compilation); programSources.Add(hostEnvironment.previousDocument = new SpecSharpSourceDocument(helper, name, "", test)); unit = assem; } if (assem != null && assem.Compilation.HasErrors) return 0; if (assem != null && assem.EntryPoint.ResolvedMethod != Dummy.Method) { var memStream = new MemoryStream(); PeWriter.WritePeToStream(assem, hostEnvironment, memStream); if (hostEnvironment.hasError) return 0; var runtimeAssembly = System.Reflection.Assembly.Load(memStream.ToArray()); var result = runtimeAssembly.EntryPoint.Invoke(null, null); if (result is int) return (int)result; return 0; } BaseCodeTraverser traverser = new BaseCodeTraverser(); unit.Dispatch(traverser); return 0; }
public Assembly CompileProject(string projectFileName) { Assembly existing; if (Compilations.TryGetValue(Path.GetFullPath(projectFileName), out existing)) { return(existing); } var project = new Microsoft.Build.BuildEngine.Project(); project.Load(projectFileName); var projectName = Environment.NameTable.GetNameFor(project.EvaluatedProperties["AssemblyName"].Value); var projectPath = project.FullFileName; var compilerOptions = new SpecSharpOptions(); var assemblyReferences = new List <IAssemblyReference>(); var moduleReferences = new List <IModuleReference>(); var programSources = new List <SpecSharpSourceDocument>(); var assembly = new SpecSharpAssembly(projectName, projectPath, Environment, compilerOptions, assemblyReferences, moduleReferences, programSources); var helper = new SpecSharpCompilationHelper(assembly.Compilation); Compilations[Path.GetFullPath(projectFileName)] = assembly; assemblyReferences.Add(Environment.LoadAssembly(Environment.CoreAssemblySymbolicIdentity)); project.Build("ResolveAssemblyReferences"); foreach (BuildItem item in project.GetEvaluatedItemsByName("ReferencePath")) { var assemblyName = new System.Reflection.AssemblyName(item.GetEvaluatedMetadata("FusionName")); var name = Environment.NameTable.GetNameFor(assemblyName.Name); var culture = assemblyName.CultureInfo != null ? assemblyName.CultureInfo.Name : ""; var version = assemblyName.Version == null ? new Version(0, 0) : assemblyName.Version; var token = assemblyName.GetPublicKeyToken(); if (token == null) { token = new byte[0]; } var location = item.FinalItemSpec; var identity = new AssemblyIdentity(name, culture, version, token, location); var reference = Environment.LoadAssembly(identity); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("ProjectReference")) { var name = Environment.NameTable.GetNameFor(Path.GetFileNameWithoutExtension(item.FinalItemSpec)); var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var reference = CompileProject(location); assemblyReferences.Add(reference); } foreach (BuildItem item in project.GetEvaluatedItemsByName("Compile")) { var location = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullFileName), item.FinalItemSpec)); var name = Environment.NameTable.GetNameFor(location); var programSource = new SpecSharpSourceDocument(helper, name, location, File.ReadAllText(location)); programSources.Add(programSource); } return(assembly); }