|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text.Json; |
| 5 | +using Semmle.Util; |
| 6 | +using Semmle.Util.Logging; |
| 7 | + |
| 8 | +namespace Semmle.Extraction.CSharp |
| 9 | +{ |
| 10 | + public interface IOverlayInfo |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// True, if the extractor is running in overlay mode. |
| 14 | + /// </summary> |
| 15 | + bool IsOverlayMode { get; } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Returns true, if the given file is not in the set of changed files. |
| 19 | + /// </summary> |
| 20 | + /// <param name="filePath">A source file path</param> |
| 21 | + bool OnlyMakeScaffold(string filePath); |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// An instance of this class is used when overlay is not enabled. |
| 27 | + /// </summary> |
| 28 | + public class TrivialOverlayInfo : IOverlayInfo |
| 29 | + { |
| 30 | + public TrivialOverlayInfo() { } |
| 31 | + |
| 32 | + public bool IsOverlayMode { get; } = false; |
| 33 | + |
| 34 | + public bool OnlyMakeScaffold(string filePath) => false; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// An instance of this class is used for detecting |
| 39 | + /// (1) Whether overlay is enabled. |
| 40 | + /// (2) Fetch the changed files that should be fully extracted as a part |
| 41 | + /// of the overlay extraction. |
| 42 | + /// </summary> |
| 43 | + public class OverlayInfo : IOverlayInfo |
| 44 | + { |
| 45 | + private readonly ILogger logger; |
| 46 | + private readonly HashSet<string> changedFiles; |
| 47 | + |
| 48 | + public OverlayInfo(ILogger logger, string json) |
| 49 | + { |
| 50 | + this.logger = logger; |
| 51 | + changedFiles = ParseJson(json); |
| 52 | + } |
| 53 | + |
| 54 | + public bool IsOverlayMode { get; } = true; |
| 55 | + |
| 56 | + public bool OnlyMakeScaffold(string filePath) => !changedFiles.Contains(filePath); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Private type only used to parse overlay changes JSON files. |
| 60 | + /// |
| 61 | + /// The content of such a file has the format |
| 62 | + /// { |
| 63 | + /// "changes": [ |
| 64 | + /// "app/controllers/about_controller.xyz", |
| 65 | + /// "app/models/about.xyz" |
| 66 | + /// ] |
| 67 | + /// } |
| 68 | + /// </summary> |
| 69 | + public record ChangedFiles |
| 70 | + { |
| 71 | + public string[]? Changes { get; set; } |
| 72 | + } |
| 73 | + |
| 74 | + private HashSet<string> ParseJson(string json) |
| 75 | + { |
| 76 | + try |
| 77 | + { |
| 78 | + var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; |
| 79 | + var obj = JsonSerializer.Deserialize<ChangedFiles>(json, options); |
| 80 | + return obj?.Changes is string[] changes |
| 81 | + ? new HashSet<string>(changes) |
| 82 | + : []; |
| 83 | + } |
| 84 | + catch (JsonException) |
| 85 | + { |
| 86 | + logger.LogError("Overlay: Unable to parse the JSON content from the overlay changes file."); |
| 87 | + return []; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static class OverlayInfoFactory |
| 93 | + { |
| 94 | + /// <summary> |
| 95 | + /// The returned object is used to decide, whether |
| 96 | + /// (1) The extractor is running in overlay mode. |
| 97 | + /// (2) Which files to only extract scaffolds for (unchanged files) |
| 98 | + /// </summary> |
| 99 | + /// <param name="mode">The extraction mode</param> |
| 100 | + /// <param name="logger">A logger</param> |
| 101 | + /// <returns>An overlay information object.</returns> |
| 102 | + public static IOverlayInfo Make(ILogger logger) |
| 103 | + { |
| 104 | + if (EnvironmentVariables.GetOverlayChangesFilePath() is string path) |
| 105 | + { |
| 106 | + try |
| 107 | + { |
| 108 | + var json = File.ReadAllText(path); |
| 109 | + return new OverlayInfo(logger, json); |
| 110 | + } |
| 111 | + catch |
| 112 | + { |
| 113 | + logger.LogError("Overlay: Unexpected error while reading the overlay changes file."); |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + return new TrivialOverlayInfo(); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments