private List <MetadataReference> GetApplicationReferences() { var references = new List <MetadataReference>(); var export = _libraryManager.GetAllExports(_environment.ApplicationName); foreach (var metadataReference in export.MetadataReferences) { var fileMetadataReference = metadataReference as IMetadataFileReference; if (fileMetadataReference != null) { references.Add(CreateMetadataFileReference(fileMetadataReference.Path)); } else { var roslynReference = metadataReference as IRoslynMetadataReference; if (roslynReference != null) { references.Add(roslynReference.MetadataReference); } } } return(references); }
//Todo: This is using application references to compile the template, //perhaps that's not right, we should use the dependencies of the caller //who calls the templating API. private List <MetadataReference> GetApplicationReferences() { var references = new List <MetadataReference>(); // Todo: When the target app references scaffolders as nuget packages rather than project references, // since the target app does not actully use code from scaffolding framework, our dlls // are not in app dependencies closure. However we need them for compiling the // generated razor template. So we are just using the known references for now to make things work. // Note that this model breaks for custom scaffolders because they may be using // some other references which are not available in any of these closures. // As the above comment, the right thing to do here is to use the dependency closure of // the assembly which has the template. var baseProjects = new string[] { _environment.ApplicationName, "Microsoft.Framework.CodeGeneration" }; foreach (var baseProject in baseProjects) { var export = _libraryManager.GetAllExports(baseProject); foreach (var metadataReference in export.MetadataReferences) { references.Add(ConvertMetadataReference(metadataReference)); } } return(references); }
public static IEnumerable <CompilationReference> GetProjectsInApp( [NotNull] this ILibraryManager libraryManager, [NotNull] IApplicationEnvironment environment) { var export = libraryManager.GetAllExports(environment.ApplicationName); return(export.MetadataReferences .OfType <IMetadataProjectReference>() .OfType <IRoslynMetadataReference>() .Select(reference => reference.MetadataReference as CompilationReference) .Where(compilation => !_frameworkProjectNames.Contains(compilation.Compilation.AssemblyName))); }
private List <MetadataReference> GetApplicationReferences() { var references = new List <MetadataReference>(); var export = _libraryManager.GetAllExports(_environment.ApplicationName); foreach (var metadataReference in export.MetadataReferences) { // Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/... // Microsoft.Framework.Runtime.Roslyn/RoslynCompiler.cs#L164 // We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies // than the view engine needs (Microsoft.Framework.Runtime) for example references.Add(ConvertMetadataReference(metadataReference)); } return(references); }
public ProjectMetadata GetProjectMetadata(string name) { Project project; if (!_projectResolver.TryResolveProject(name, out project)) { return(null); } var export = _libraryManager.GetAllExports(name); if (export == null) { return(null); } return(new ProjectMetadata(project, export)); }
public void Main(string[] args) { // Get the export for the specified project var exports = _libraries.GetAllExports(_env.ApplicationName); var projectExport = exports.MetadataReferences.SingleOrDefault(m => m.Name.Equals(_env.ApplicationName)) as IMetadataProjectReference; var others = exports.MetadataReferences.Where(m => m != projectExport); RuntimeProject runtimeProject; if (!_projectResolver.TryResolveProject(_env.ApplicationName, out runtimeProject)) { throw new Exception("Where the project at?"); } // Collect the references var references = new List <MetadataReference>(); foreach (var reference in others) { var converted = ConvertMetadataReference(reference); Console.WriteLine($"Using Reference: {converted.Display}"); references.Add(converted); } // Create the project var runtimeSettings = ToCompilationSettings(runtimeProject.GetCompilerOptions(_env.RuntimeFramework, _env.Configuration), _env.RuntimeFramework); var prjId = ProjectId.CreateNewId(); var projectInfo = ProjectInfo.Create( prjId, VersionStamp.Create(), runtimeProject.Name, runtimeProject.Name, runtimeProject.LanguageServices?.Name ?? "C#", compilationOptions: runtimeSettings.CompilationOptions, parseOptions: new CSharpParseOptions( languageVersion: runtimeSettings.LanguageVersion, preprocessorSymbols: runtimeSettings.Defines), documents: CreateDocuments(prjId, projectExport.GetSources()), metadataReferences: references); // Create the workspace var ws = new AdhocWorkspace(); var project = ws.AddProject(projectInfo); }
private List <MetadataReference> GetApplicationReferences() { var references = new List <MetadataReference>(); // Get the MetadataReference for the executing application. If it's a Roslyn reference, // we can copy the references created when compiling the application to the Razor page being compiled. // This avoids performing expensive calls to MetadataReference.CreateFromImage. var libraryExport = _libraryManager.GetLibraryExport(_environment.ApplicationName); if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0) { Debug.Assert(libraryExport.MetadataReferences.Count == 1, "Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count); var roslynReference = libraryExport.MetadataReferences[0] as IRoslynMetadataReference; var compilationReference = roslynReference?.MetadataReference as CompilationReference; if (compilationReference != null) { references.AddRange(compilationReference.Compilation.References); references.Add(roslynReference.MetadataReference); return(references); } } var export = _libraryManager.GetAllExports(_environment.ApplicationName); foreach (var metadataReference in export.MetadataReferences) { // Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/... // Microsoft.Framework.Runtime.Roslyn/RoslynCompiler.cs#L164 // We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies // than the view engine needs (Microsoft.Framework.Runtime) for example references.Add(ConvertMetadataReference(metadataReference)); } return(references); }
public ILibraryExport GetAllExports(string name) { return(_libraryManager.GetAllExports(name)); }