public void Dispose()
 {
     if (mWorkspace != null)
     {
         mWorkspace.Dispose();
     }
 }
        ImmutableArray <Project> IProjectReader.ReadAllSourceCodeFiles(string solutionFilePath, string stylecopJsonFile)
        {
            if (!File.Exists(solutionFilePath))
            {
                Console.Error.WriteLine($"Could not find a solution file '{solutionFilePath}'");
                return(ImmutableArray <Project> .Empty);
            }

            if (!MSBuildLocator.IsRegistered)
            {
                MSBuildLocator.RegisterDefaults();
            }

            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            var solution = workspace.OpenSolutionAsync(solutionFilePath).Result;

            var projects = ImmutableArray.CreateBuilder <Project>(solution.Projects.Count());

            foreach (var project in solution.Projects)
            {
                if (!string.IsNullOrEmpty(stylecopJsonFile) && File.Exists(stylecopJsonFile))
                {
                    projects.Add(project.AddAdditionalDocument("stylecop.json", SourceText.From(File.ReadAllText(stylecopJsonFile))).Project);
                }
            }

            workspace.Dispose();

            return(projects.ToImmutable());
        }
        ImmutableArray <Project> IProjectReader.ReadAllSourceCodeFiles(string csprojFilePath, string stylecopJsonFile)
        {
            if (!File.Exists(csprojFilePath))
            {
                Console.Error.WriteLine($"Could not find a csproj file '{csprojFilePath}'");
                return(ImmutableArray <Project> .Empty);
            }

            if (!MSBuildLocator.IsRegistered)
            {
                MSBuildLocator.RegisterDefaults();
            }

            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            var project = workspace.OpenProjectAsync(csprojFilePath).Result;

            if (!string.IsNullOrEmpty(stylecopJsonFile) && File.Exists(stylecopJsonFile))
            {
                project = project.AddAdditionalDocument("stylecop.json", SourceText.From(File.ReadAllText(stylecopJsonFile)))
                          .Project;
            }

            workspace.Dispose();

            return(ImmutableArray.Create(project));
        }
 public void Cleanup()
 {
     _workspace?.Dispose();
     _workspace = null;
     _solution  = null;
     _type      = null;
 }
Beispiel #5
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 public void Dispose()
 {
     if (_workspace != null)
     {
         _workspace.Dispose();
     }
 }
        private bool disposedValue = false; // To detect redundant calls


        private void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    _workspace?.Dispose();
                }


                disposedValue = true;
            }
        }
Beispiel #7
0
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    _workspace.Dispose();
                    MSBuildLocator.Unregister();
                }


                disposedValue = true;
            }
        }
Beispiel #8
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    _workspace.Dispose();
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
        ImmutableArray <Project> IProjectReader.ReadAllSourceCodeFiles(string directory, string stylecopJsonFile)
        {
            if (!Directory.Exists(directory))
            {
                Console.Error.WriteLine($"Could not find a part of the path '{directory}'");
                return(ImmutableArray <Project> .Empty);
            }

            var files = new DirectoryInfo(directory)?.GetFiles("*.cs", SearchOption.AllDirectories);

            if (files == null)
            {
                Console.Error.WriteLine($"Could not find csharp source files in '{directory}'");
                return(ImmutableArray <Project> .Empty);
            }

            var syntaxTrees = new List <SyntaxTree>();

            if (!MSBuildLocator.IsRegistered)
            {
                MSBuildLocator.RegisterDefaults();
            }

            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            var solution = workspace.CurrentSolution;
            var project  = solution.AddProject("Temp", "Temp", "C#");

            var lockObj = new object();

            Parallel.ForEach(files, (f) =>
            {
                var fileText = CSharpSyntaxTree.ParseText(File.ReadAllText(f.FullName), null, f.FullName);
                lock (lockObj)
                {
                    project = project.AddDocument(f.Name, fileText.GetRoot(), null, f.FullName)
                              .Project;
                }
            });

            if (!string.IsNullOrEmpty(stylecopJsonFile) && File.Exists(stylecopJsonFile))
            {
                project = project.AddAdditionalDocument("stylecop.json", SourceText.From(File.ReadAllText(stylecopJsonFile)))
                          .Project;
            }

            workspace.Dispose();
            return(ImmutableArray.Create(project));
        }
        ImmutableArray <Project> IProjectReader.ReadAllSourceCodeFiles(string file, string stylecopJsonFile)
        {
            if (!File.Exists(file))
            {
                Console.Error.WriteLine($"Could not find a part of the path '{file}'");
                return(ImmutableArray <Project> .Empty);
            }

            var fileInfo = new FileInfo(file);

            if (fileInfo == null)
            {
                Console.Error.WriteLine($"Could not find csharp source files in '{file}'");
                return(ImmutableArray <Project> .Empty);
            }

            if (!MSBuildLocator.IsRegistered)
            {
                MSBuildLocator.RegisterDefaults();
            }

            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            var solution = workspace.CurrentSolution;
            var project  = solution.AddProject("Temp", "Temp", "C#");

            var fileText = CSharpSyntaxTree.ParseText(File.ReadAllText(fileInfo.FullName), null, fileInfo.FullName);

            project = project.AddDocument(fileInfo.Name, fileText.GetRoot(), null, fileInfo.FullName)
                      .Project;

            if (!string.IsNullOrEmpty(stylecopJsonFile) && File.Exists(stylecopJsonFile))
            {
                project = project.AddAdditionalDocument("stylecop.json", SourceText.From(File.ReadAllText(stylecopJsonFile)))
                          .Project;
            }
            workspace.Dispose();

            return(ImmutableArray.Create(project));
        }
Beispiel #11
0
 public void Dispose()
 {
     _workspace?.Dispose();
     _workspace = null;
     ProjectCollection.Dispose();
 }
Beispiel #12
0
 public void Dispose()
 {
     workspace.Dispose();
 }
        public async Task <CommandResult> ExecuteAsync(string path, string msbuildPath = null, IEnumerable <string> properties = null)
        {
            MSBuildWorkspace workspace = null;

            try
            {
                workspace = CreateMSBuildWorkspace(msbuildPath, properties);

                if (workspace == null)
                {
                    return(CommandResult.Fail);
                }

                workspace.WorkspaceFailed += WorkspaceFailed;

                var cts = new CancellationTokenSource();
                Console.CancelKeyPress += (sender, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                CancellationToken cancellationToken = cts.Token;

                try
                {
                    CommandResult result = await ExecuteAsync(path, workspace, ConsoleProgressReporter.Default, cancellationToken);

                    if (result.Kind != CommandResultKind.None)
                    {
                        return(result);
                    }

                    ProjectOrSolution projectOrSolution = await OpenProjectOrSolutionAsync(path, workspace, ConsoleProgressReporter.Default, cancellationToken);

                    if (projectOrSolution != default)
                    {
                        return(await ExecuteAsync(projectOrSolution, cancellationToken));
                    }
                }
                catch (OperationCanceledException ex)
                {
                    OperationCanceled(ex);
                }
                catch (AggregateException ex)
                {
                    OperationCanceledException operationCanceledException = ex.GetOperationCanceledException();

                    if (operationCanceledException != null)
                    {
                        OperationCanceled(operationCanceledException);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            finally
            {
                workspace?.Dispose();
            }

            return(CommandResult.Fail);
        }
Beispiel #14
0
 public void Dispose()
 {
     _workspace.WorkspaceFailed -= WorkspaceOnWorkspaceFailed;
     _workspace?.Dispose();
 }
Beispiel #15
0
 public void Cleanup()
 {
     _workspace?.Dispose();
     _workspace = null;
 }
Beispiel #16
0
 public void IterationCleanup()
 {
     _workspace.Dispose();
     _workspace = null;
 }
Beispiel #17
0
 public void Dispose() => workspace.Dispose();