|
16 | 16 |
|
17 | 17 | namespace Coverlet.MSbuild.Tasks |
18 | 18 | { |
19 | | - public class InstrumentationTask : BaseTask |
20 | | - { |
21 | | - private readonly MSBuildLogger _logger; |
| 19 | + public class InstrumentationTask : BaseTask |
| 20 | + { |
| 21 | + private readonly MSBuildLogger _logger; |
22 | 22 |
|
23 | | - [Required] |
24 | | - public string Path { get; set; } |
| 23 | + [Required] |
| 24 | + public string Path { get; set; } |
25 | 25 |
|
26 | | - public string Include { get; set; } |
| 26 | + public string Include { get; set; } |
27 | 27 |
|
28 | | - public string IncludeDirectory { get; set; } |
| 28 | + public string IncludeDirectory { get; set; } |
29 | 29 |
|
30 | | - public string Exclude { get; set; } |
| 30 | + public string Exclude { get; set; } |
31 | 31 |
|
32 | | - public string ExcludeByFile { get; set; } |
| 32 | + public string ExcludeByFile { get; set; } |
33 | 33 |
|
34 | | - public string ExcludeByAttribute { get; set; } |
| 34 | + public string ExcludeByAttribute { get; set; } |
35 | 35 |
|
36 | | - public bool IncludeTestAssembly { get; set; } |
| 36 | + public bool IncludeTestAssembly { get; set; } |
37 | 37 |
|
38 | | - public bool SingleHit { get; set; } |
| 38 | + public bool SingleHit { get; set; } |
39 | 39 |
|
40 | | - public string MergeWith { get; set; } |
| 40 | + public string MergeWith { get; set; } |
41 | 41 |
|
42 | | - public bool UseSourceLink { get; set; } |
| 42 | + public bool UseSourceLink { get; set; } |
43 | 43 |
|
44 | | - public bool SkipAutoProps { get; set; } |
| 44 | + public bool SkipAutoProps { get; set; } |
45 | 45 |
|
46 | | - public string DoesNotReturnAttribute { get; set; } |
| 46 | + public string DoesNotReturnAttribute { get; set; } |
47 | 47 |
|
48 | | - public bool DeterministicReport { get; set; } |
| 48 | + public bool DeterministicReport { get; set; } |
49 | 49 |
|
50 | | - public string ExcludeAssembliesWithoutSources { get; set; } |
| 50 | + public string ExcludeAssembliesWithoutSources { get; set; } |
51 | 51 |
|
52 | | - [Output] |
53 | | - public ITaskItem InstrumenterState { get; set; } |
| 52 | + [Output] |
| 53 | + public ITaskItem InstrumenterState { get; set; } |
54 | 54 |
|
55 | | - public InstrumentationTask() |
56 | | - { |
57 | | - _logger = new MSBuildLogger(Log); |
58 | | - } |
| 55 | + public InstrumentationTask() |
| 56 | + { |
| 57 | + _logger = new MSBuildLogger(Log); |
| 58 | + } |
59 | 59 |
|
60 | | - private void AttachDebugger() |
61 | | - { |
62 | | - if (int.TryParse(Environment.GetEnvironmentVariable("COVERLET_MSBUILD_INSTRUMENTATIONTASK_DEBUG"), out int result) && result == 1) |
63 | | - { |
64 | | - Debugger.Launch(); |
65 | | - Debugger.Break(); |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - public override bool Execute() |
| 60 | + private void AttachDebugger() |
| 61 | + { |
| 62 | + if (int.TryParse(Environment.GetEnvironmentVariable("COVERLET_MSBUILD_INSTRUMENTATIONTASK_DEBUG"), out int result) && result == 1) |
| 63 | + { |
| 64 | + Debugger.Launch(); |
| 65 | + Debugger.Break(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public override bool Execute() |
| 70 | + { |
| 71 | + AttachDebugger(); |
| 72 | + |
| 73 | + IServiceCollection serviceCollection = new ServiceCollection(); |
| 74 | + serviceCollection.AddTransient<IProcessExitHandler, ProcessExitHandler>(); |
| 75 | + serviceCollection.AddTransient<IFileSystem, FileSystem>(); |
| 76 | + serviceCollection.AddTransient<IAssemblyAdapter, AssemblyAdapter>(); |
| 77 | + serviceCollection.AddTransient<IConsole, SystemConsole>(); |
| 78 | + serviceCollection.AddTransient<ILogger, MSBuildLogger>(_ => _logger); |
| 79 | + serviceCollection.AddTransient<IRetryHelper, RetryHelper>(); |
| 80 | + // We cache resolutions |
| 81 | + serviceCollection.AddSingleton<ISourceRootTranslator, SourceRootTranslator>(serviceProvider => |
| 82 | + new SourceRootTranslator(Path, serviceProvider.GetRequiredService<ILogger>(), serviceProvider.GetRequiredService<IFileSystem>(), serviceProvider.GetRequiredService<IAssemblyAdapter>())); |
| 83 | + // We need to keep singleton/static semantics |
| 84 | + serviceCollection.AddSingleton<IInstrumentationHelper, InstrumentationHelper>(); |
| 85 | + serviceCollection.AddSingleton<ICecilSymbolHelper, CecilSymbolHelper>(); |
| 86 | + |
| 87 | + ServiceProvider = serviceCollection.BuildServiceProvider(); |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + IFileSystem fileSystem = ServiceProvider.GetService<IFileSystem>(); |
| 92 | + |
| 93 | + var parameters = new CoverageParameters |
70 | 94 | { |
71 | | - AttachDebugger(); |
72 | | - |
73 | | - IServiceCollection serviceCollection = new ServiceCollection(); |
74 | | - serviceCollection.AddTransient<IProcessExitHandler, ProcessExitHandler>(); |
75 | | - serviceCollection.AddTransient<IFileSystem, FileSystem>(); |
76 | | - serviceCollection.AddTransient<IAssemblyAdapter, AssemblyAdapter>(); |
77 | | - serviceCollection.AddTransient<IConsole, SystemConsole>(); |
78 | | - serviceCollection.AddTransient<ILogger, MSBuildLogger>(_ => _logger); |
79 | | - serviceCollection.AddTransient<IRetryHelper, RetryHelper>(); |
80 | | - // We cache resolutions |
81 | | - serviceCollection.AddSingleton<ISourceRootTranslator, SourceRootTranslator>(serviceProvider => |
82 | | - new SourceRootTranslator(Path, serviceProvider.GetRequiredService<ILogger>(), serviceProvider.GetRequiredService<IFileSystem>(), serviceProvider.GetRequiredService<IAssemblyAdapter>())); |
83 | | - // We need to keep singleton/static semantics |
84 | | - serviceCollection.AddSingleton<IInstrumentationHelper, InstrumentationHelper>(); |
85 | | - serviceCollection.AddSingleton<ICecilSymbolHelper, CecilSymbolHelper>(); |
86 | | - |
87 | | - BaseTask.ServiceProvider = serviceCollection.BuildServiceProvider(); |
88 | | - |
89 | | - try |
90 | | - { |
91 | | - IFileSystem fileSystem = ServiceProvider.GetService<IFileSystem>(); |
92 | | - |
93 | | - var parameters = new CoverageParameters |
94 | | - { |
95 | | - IncludeFilters = Include?.Split(','), |
96 | | - IncludeDirectories = IncludeDirectory?.Split(','), |
97 | | - ExcludeFilters = Exclude?.Split(','), |
98 | | - ExcludedSourceFiles = ExcludeByFile?.Split(','), |
99 | | - ExcludeAttributes = ExcludeByAttribute?.Split(','), |
100 | | - IncludeTestAssembly = IncludeTestAssembly, |
101 | | - SingleHit = SingleHit, |
102 | | - MergeWith = MergeWith, |
103 | | - UseSourceLink = UseSourceLink, |
104 | | - SkipAutoProps = SkipAutoProps, |
105 | | - DeterministicReport = DeterministicReport, |
106 | | - ExcludeAssembliesWithoutSources = ExcludeAssembliesWithoutSources, |
107 | | - DoesNotReturnAttributes = DoesNotReturnAttribute?.Split(',') |
108 | | - }; |
109 | | - |
110 | | - var coverage = new Coverage(Path, |
111 | | - parameters, |
112 | | - _logger, |
113 | | - ServiceProvider.GetService<IInstrumentationHelper>(), |
114 | | - ServiceProvider.GetService<IFileSystem>(), |
115 | | - ServiceProvider.GetService<ISourceRootTranslator>(), |
116 | | - ServiceProvider.GetService<ICecilSymbolHelper>()); |
117 | | - |
118 | | - CoveragePrepareResult prepareResult = coverage.PrepareModules(); |
119 | | - string randomPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()); |
120 | | - InstrumenterState = new TaskItem(randomPath); |
121 | | - using Stream instrumentedStateFile = fileSystem.NewFileStream(InstrumenterState.ItemSpec, FileMode.CreateNew, FileAccess.Write); |
122 | | - using Stream serializedState = CoveragePrepareResult.Serialize(prepareResult); |
123 | | - serializedState.CopyTo(instrumentedStateFile); |
124 | | - } |
125 | | - catch (Exception ex) |
126 | | - { |
127 | | - _logger.LogError(ex); |
128 | | - return false; |
129 | | - } |
130 | | - |
131 | | - return true; |
132 | | - } |
| 95 | + IncludeFilters = Include?.Split(','), |
| 96 | + IncludeDirectories = IncludeDirectory?.Split(','), |
| 97 | + ExcludeFilters = Exclude?.Split(','), |
| 98 | + ExcludedSourceFiles = ExcludeByFile?.Split(','), |
| 99 | + ExcludeAttributes = ExcludeByAttribute?.Split(','), |
| 100 | + IncludeTestAssembly = IncludeTestAssembly, |
| 101 | + SingleHit = SingleHit, |
| 102 | + MergeWith = MergeWith, |
| 103 | + UseSourceLink = UseSourceLink, |
| 104 | + SkipAutoProps = SkipAutoProps, |
| 105 | + DeterministicReport = DeterministicReport, |
| 106 | + ExcludeAssembliesWithoutSources = ExcludeAssembliesWithoutSources, |
| 107 | + DoesNotReturnAttributes = DoesNotReturnAttribute?.Split(',') |
| 108 | + }; |
| 109 | + |
| 110 | + var coverage = new Coverage(Path, |
| 111 | + parameters, |
| 112 | + _logger, |
| 113 | + ServiceProvider.GetService<IInstrumentationHelper>(), |
| 114 | + ServiceProvider.GetService<IFileSystem>(), |
| 115 | + ServiceProvider.GetService<ISourceRootTranslator>(), |
| 116 | + ServiceProvider.GetService<ICecilSymbolHelper>()); |
| 117 | + |
| 118 | + CoveragePrepareResult prepareResult = coverage.PrepareModules(); |
| 119 | + string randomPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName()); |
| 120 | + InstrumenterState = new TaskItem(randomPath); |
| 121 | + using Stream instrumentedStateFile = fileSystem.NewFileStream(InstrumenterState.ItemSpec, FileMode.CreateNew, FileAccess.Write); |
| 122 | + using Stream serializedState = CoveragePrepareResult.Serialize(prepareResult); |
| 123 | + serializedState.CopyTo(instrumentedStateFile); |
| 124 | + } |
| 125 | + catch (Exception ex) |
| 126 | + { |
| 127 | + _logger.LogError(ex); |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
133 | 132 | } |
| 133 | + } |
134 | 134 | } |
0 commit comments