public static void Rename(ILogger _logger, string solutionPath, string projectPath, List <PreRenameData> lstRenames, CancellationToken cancellationToken) { using (Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create()) { // Open the solution within the workspace. Microsoft.CodeAnalysis.Solution solution = workspace.OpenSolutionAsync(solutionPath).Result; bool isProjectFound = false; foreach (Microsoft.CodeAnalysis.ProjectId projectId in solution.ProjectIds) { // Look up the snapshot for the original project in the latest forked solution. Microsoft.CodeAnalysis.Project project = solution.GetProject(projectId); if (project.FilePath == projectPath) { isProjectFound = true; foreach (Microsoft.CodeAnalysis.DocumentId documentId in project.DocumentIds) { // Look up the snapshot for the original document in the latest forked solution. Microsoft.CodeAnalysis.Document document = solution.GetDocument(documentId); //tg.RelativePathToGeneratedFile // only in 'main' declaration files //TODO implement based on knowledge of generated file ??? if (Path.GetDirectoryName(document.FilePath).EndsWith("ViewModels")) { if (Path.GetExtension(document.FilePath) == "cs") { CodeAnalysisCSharp.Rename(_logger, solution, document, lstRenames, cancellationToken).Wait(); } else if (Path.GetExtension(document.FilePath) == "vb") { CodeAnalysisVisualBasic.Rename(solution, document, lstRenames, cancellationToken).Wait(); } else { throw new NotSupportedException(); } } } } } if (!isProjectFound) { throw new Exception("Project not found"); } } }
public static void Compile(ILogger _logger, string solutionPath, CancellationToken cancellationToken) { //var lstBuilds = Microsoft.Build.Locator.MSBuildLocator.QueryVisualStudioInstances().ToList(); //var build = lstBuilds[0]; //Microsoft.Build.Locator.MSBuildLocator.RegisterInstance(build); //Microsoft.Build.Locator.VisualStudioInstanceQueryOptions.Default = new Microsoft.Build.Locator.VisualStudioInstanceQueryOptions() { DiscoveryType = Microsoft.Build.Locator.DiscoveryType.DotNetSdk }; //Microsoft.Build.Locator.MSBuildLocator.RegisterDefaults(); var properties = new Dictionary <string, string> { //{ "Configuration", "Release" }, // Use the latest language version to force the full set of available analyzers to run on the project. { "LangVersion", "latest" }, }; using (Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(properties)) //https://gist.github.com/DustinCampbell/32cd69d04ea1c08a16ae5c4cd21dd3a3 //using (Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create()) { _logger.LogInformation("Compiling solution {0}".FilePos(), solutionPath); Microsoft.CodeAnalysis.Solution solution = workspace.OpenSolutionAsync(solutionPath).Result; if (workspace.Diagnostics.Count > 0) { var en = workspace.Diagnostics.GetEnumerator(); en.MoveNext(); if (en.Current.Kind == Microsoft.CodeAnalysis.WorkspaceDiagnosticKind.Failure) { throw new Exception(en.Current.Message); } } foreach (var project in solution.Projects) { var compilation = project.GetCompilationAsync().Result; var diag = compilation.GetDiagnostics(); var lst = from p in diag where p.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error select p; if (lst.Count() > 0) { throw new Exception("Compilation errors are found.\nSolution: " + solutionPath + "\nProject: " + project.FilePath); } } } }
private static int MainInner(string[] args) { Timing timing = new Timing(); while (args.Length != 0) { const int NumArgs = 3; if (args.Length < NumArgs) { throw new ArgumentException(); } string solutionBasePath = args[0]; string interfacesProjectBasePath = args[1]; string targetProjectName = args[2]; Array.Copy(args, NumArgs, args, 0, args.Length - NumArgs); Array.Resize(ref args, args.Length - NumArgs); string targetSolutionFilePath = FindFirstFile(solutionBasePath, ".sln"); string interfacesProjectName = Path.GetFileName(interfacesProjectBasePath); string interfacesSolutionFilePath = FindFirstFile(Path.GetDirectoryName(interfacesProjectBasePath), ".sln"); string templateProjectBasePath = Path.Combine(solutionBasePath, "Template"); // TODO: hardcoded string[] doNotUnload; string[] imports; Config[] configs = Config.LoadConfigs(Path.Combine(solutionBasePath, targetProjectName, "transform.xml"), out doNotUnload, out imports); timing.Mark("Init/Load"); Solution interfacesSolution, targetSolution; Project interfacesProject, targetProject; { Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace workspace = Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create(); // load interface project interfacesSolution = workspace.OpenSolutionAsync(interfacesSolutionFilePath).Result; interfacesProject = interfacesSolution.Projects.First(delegate(Project p) { return(String.Equals(p.Name, interfacesProjectName)); }); // load target project targetSolution = workspace.OpenSolutionAsync(targetSolutionFilePath).Result; targetProject = targetSolution.Projects.First(delegate(Project p) { return(String.Equals(p.Name, targetProjectName)); }); // remove all preexisting specializations since we'll be regenerating them and don't want multiply-defined errors foreach (DocumentId documentId in targetProject.DocumentIds) { if (Array.IndexOf(doNotUnload, targetProject.GetDocument(documentId).Name) < 0) { targetProject = targetProject.RemoveDocument(documentId); } } } Compilation targetCompilation = null; Compilation interfacesCompilation = null; foreach (Config config in configs) { string templateSourceFilePath = Path.Combine(templateProjectBasePath, Path.ChangeExtension(config.templateClassName, ".cs")); string targetFilePath = Path.Combine(solutionBasePath, targetProjectName, "Generated", Path.ChangeExtension(config.targetClassName, ".cs")); ConsoleColor oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(config.targetClassName); Console.ForegroundColor = oldColor; // lazy init if (targetCompilation == null) { Task <Compilation> targetCompiling = targetProject.GetCompilationAsync(); Task <Compilation> interfacesCompiling = interfacesProject.GetCompilationAsync(); Task.WaitAll(targetCompiling, interfacesCompiling); targetCompilation = targetCompiling.Result; interfacesCompilation = interfacesCompiling.Result; } Generate(config, targetCompilation, templateSourceFilePath, targetFilePath, interfacesCompilation, timing); } if (Debugger.IsAttached) // report after each command line argument group { timing.Finish(); timing.WriteReport(); } } timing.Finish(); timing.WriteReport(); Console.WriteLine("Finished"); if (Debugger.IsAttached) { Console.ReadLine(); } return(0); }