public static IServiceProvider CreateServices(string testAppName) { #if RELEASE var applicationInfo = new ApplicationInfo("TestApp", Directory.GetCurrentDirectory(), "Release"); #else var applicationInfo = new ApplicationInfo("TestApp", Directory.GetCurrentDirectory(), "Debug"); #endif // When the tests are run the applicationInfo points to test project. // Change the app applicationInfo to point to the test application to be used // by test. var originalAppBase = applicationInfo.ApplicationBasePath; ////Microsoft.VisualStudio.Web.CodeGeneration.Core.FunctionalTest #if NET451 var testAppPath = Path.GetFullPath(Path.Combine(originalAppBase, "..", "..", "..", "..", "..", "TestApps", testAppName)); #else var testAppPath = Path.GetFullPath(Path.Combine(originalAppBase, "..", "TestApps", testAppName)); #endif var testEnvironment = new TestApplicationInfo(applicationInfo, testAppPath, testAppName); var context = ProjectContext.CreateContextForEachFramework(testAppPath).First(); var exporter = new LibraryExporter(context, testEnvironment); var workspace = new ProjectJsonWorkspace(context); return(new WebHostBuilder() .UseServer(new DummyServer()) .UseStartup <ModelTypesLocatorTestWebApp.Startup>() .ConfigureServices(services => { services.AddSingleton <IApplicationInfo>(testEnvironment); services.AddSingleton <ILibraryExporter>(exporter); services.AddSingleton <CodeAnalysis.Workspace>(workspace); }) .Build() .Services); }
private void Do(string path) { // This is so meta! var workspace = new ProjectJsonWorkspace(path); var thisDocument = workspace.CurrentSolution.GetDocumentIdsWithFilePath(Path.Combine(path, "Program.cs")).First(); var project = workspace.CurrentSolution.GetProject(thisDocument.ProjectId); var program = project.GetCompilationAsync().Result.Assembly.GetTypeByMetadataName("Program"); Console.WriteLine(program.GetDocumentationCommentXml()); }
private Project GetProjectJsonProject(string path) { try { Logger.LogVerbose($"Loading project {path}", file: path); var workspace = new ProjectJsonWorkspace(path); return(workspace.CurrentSolution.Projects.FirstOrDefault(p => p.FilePath == Path.GetFullPath(path))); } catch (Exception e) { Logger.Log(LogLevel.Warning, $"Error opening project {path}: {e.Message}. Ignored."); return(null); } }
static Tuple <Assembly, MetadataReference> Build(string assmName, string source, MetadataReference schema = null) { var cwd = System.IO.Directory.GetCurrentDirectory(); // Console.WriteLine("project.json: {0}", Path.Combine(cwd, "src/ConsoleApp/project.json")); var references = new ProjectJsonWorkspace(Path.Combine(cwd, "src/ConsoleApp/project.json")).CurrentSolution.Projects.SelectMany(p => p.MetadataReferences); var currentAssembly = typeof(Program).GetTypeInfo().Assembly; var fileUri = "file:///"; // pretty dumb test for windows platform if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TEMP"))) { fileUri = "file://"; } var asmPath = Path.GetFullPath(currentAssembly.CodeBase.Substring(fileUri.Length)); var compilerOptions = new CSharpCompilationOptions(outputKind: OutputKind.DynamicallyLinkedLibrary); var trees = new SyntaxTree[] { CSharpSyntaxTree.ParseText(source), }; var compilation = CSharpCompilation.Create(assmName) .WithOptions(compilerOptions) .WithReferences(references.Concat(new [] { MetadataReference.CreateFromFile(asmPath) }.Concat(schema != null ? new [] { schema } : new MetadataReference[] {} ))) .AddSyntaxTrees(trees); var stream = new MemoryStream(); var compilationResult = compilation.Emit(stream, options: new EmitOptions()); foreach (var diag in compilationResult.Diagnostics) { if (diag.Severity == DiagnosticSeverity.Error) { Console.WriteLine("Error: {0}", diag.GetMessage()); } } stream.Position = 0; var asm = LibraryLoader.LoadFromStream(stream); stream.Position = 0; var metaRef = MetadataReference.CreateFromStream(stream); return(Tuple.Create(asm, metaRef as MetadataReference)); }
public static Compilation LoadWorkspace(string filePath) { var projectWorkspace = new ProjectJsonWorkspace(filePath); var project = projectWorkspace.CurrentSolution.Projects.FirstOrDefault(); var compilation = project.GetCompilationAsync().Result; /*foreach (var tree in compilation.SyntaxTrees) * { * var source = tree.GetRoot().DescendantNodes(); * var classDeclarations = source.OfType<ClassDeclarationSyntax>().Where(x => x.Identifier.Text.Contains("MessagingConfig")).ToList(); * if (classDeclarations.Any()) * { * * } * }*/ return(compilation); }
private async Task <Project> GetProjectAsync(string path) { try { string name = Path.GetFileName(path); if (name.Equals("project.json", StringComparison.OrdinalIgnoreCase)) { var workspace = new ProjectJsonWorkspace(path); return(workspace.CurrentSolution.Projects.FirstOrDefault(p => p.FilePath == Path.GetFullPath(path))); } return(await _workspace.Value.OpenProjectAsync(path)); } catch (Exception e) { Logger.Log(LogLevel.Warning, $"Error opening project {path}: {e.Message}. Ignored."); return(null); } }
public static void Main(string[] args) { var solution = new ProjectJsonWorkspace(projectPath).CurrentSolution; solution = RenameNamespace("FastQuant", "SmartQuant", solution); solution = RenameClass("Message", "Message_", solution); solution = RenameClass("Command", "Command_", solution); solution = RenameClass("Response", "Response_", solution); var pids = solution.Projects.Where(p => p.Name.StartsWith("FastQuant")).Select(p => p.Id); foreach (var id in pids) { solution = solution.WithProjectAssemblyName(id, NewAssemblyName); } foreach (var p in solution.Projects) { GenerateDll(p); } Console.WriteLine("Succeed!"); }
private static void CompileProject(CompilerOptions options) { Project project; Workspace workspace; var handlebarsFiles = new List <string>(); if (options.JSONProject) { workspace = new ProjectJsonWorkspace(options.ProjectFile); project = workspace.CurrentSolution.Projects.First(); handlebarsFiles.AddRange(ScrapeDirectoryForHandlebarsFiles(new DirectoryInfo(Path.GetDirectoryName(project.FilePath)), options)); } else {//Old project files. Accessible via MSBuildWorkspace var properties = new Dictionary <string, string>() { { "AdditionalFileItemNames", "none" } }; workspace = MSBuildWorkspace.Create(properties); project = (workspace as MSBuildWorkspace).OpenProjectAsync(options.ProjectFile).Result; handlebarsFiles.AddRange(project.AdditionalDocuments.Where(x => Path.GetExtension(x.FilePath).Equals(".hbs")).Select(x => x.FilePath)); } CompileHandlebarsFiles(project, workspace, handlebarsFiles, options); }
private static async Task <List <string> > GenerateMetadataFromProjectListCoreAsync(List <string> projectFiles, string outputFolder) { if (projectFiles == null || projectFiles.Count == 0) { return(null); } List <string> outputFilePathList = new List <string>(); if (!string.IsNullOrEmpty(outputFolder)) { if (Directory.Exists(outputFolder)) { ParseResult.WriteToConsole(ResultLevel.Warn, "{0} directory already exists.", outputFolder); } else { Directory.CreateDirectory(outputFolder); } } HashSet <ProjectId> tempMapping = new HashSet <ProjectId>(); foreach (var projectFile in projectFiles) { List <Project> projects = new List <Project>(); var fileExtension = Path.GetExtension(projectFile); ParseResult.WriteToConsole(ResultLevel.Info, "Generating documentation metadata from {0} ...", projectFile); if (fileExtension == ".sln") { var solution = await Workspace.OpenSolutionAsync(projectFile); projects = solution.Projects.ToList(); } else if (fileExtension == ".csproj" || fileExtension == ".vbproj") { var project = await Workspace.OpenProjectAsync(projectFile); projects.Add(project); } else if (Path.GetFileName(projectFile) == "project.json") { var workspace = new ProjectJsonWorkspace(projectFile); projects.AddRange(workspace.CurrentSolution.Projects.Where(p => p.FilePath == projectFile)); } else { ParseResult.WriteToConsole(ResultLevel.Warn, "Project type {0} is currently not supported, ignored", fileExtension); continue; } if (projects.Count == 0) { ParseResult.WriteToConsole(ResultLevel.Warn, "No project is found under {0}, ignored", projectFile); continue; } foreach (var project in projects) { if (tempMapping.Contains(project.Id)) { ParseResult.WriteToConsole(ResultLevel.Info, "Metadata for project {0} already generated.", project.Name); continue; } else { tempMapping.Add(project.Id); } string projectFilePath; var compilation = await project.GetCompilationAsync(); var projectMetadata = GenerateYamlMetadata(compilation); var result = TryExportYamlMetadataFile(projectMetadata, outputFolder, UriKind.Absolute, out projectFilePath); if (result.ResultLevel == ResultLevel.Success) { outputFilePathList.Add(projectFilePath); } result.WriteToConsole(); } } return(outputFilePathList); }