74 private Dictionary<Type, ILicenseCommentTemplate> DefaultCommentTemplates =
new()
82 DefaultCommentTemplates[typeof(T)] =
template;
86 public IEnumerable<ILicenseCommentTemplate> Templates => DefaultCommentTemplates.Values;
88 public Func<AbsolutePath, bool>? AllowDirectory {
get;
set; }
89 public Func<AbsolutePath, bool>? AllowFile {
get;
set; }
91 public string LicenseRegionFile {
get;
set; } =
"*LicenseRegion*";
92 public bool AllowDotFiles {
get;
set; } =
false;
93 public bool AllowDotDirectories {
get;
set; } =
false;
98 public static void ProcessLicenseRegion(
this INukeBuild
self, AbsolutePath root, LicenseCommentData licenseData,
LicenseRegion? options =
null)
101 var fileFilters = options.Templates.SelectMany(t => t.FileFilters).ToArray();
102 var files = root.GlobFiles(fileFilters)
103 .Where(f => options.AllowFile?.Invoke(f) ??
true)
104 .Where(f => options.AllowDotFiles || !f.Name.StartsWith(
'.'))
105 .Where(f => !Glob.IsMatch(f, options.LicenseRegionFile, GlobOptions.CaseInsensitive));
107 foreach (var file
in files)
109 var
template = options.Templates.First(t => t.FileFilters.Any(f => Glob.IsMatch(file, f, GlobOptions.CaseInsensitive)));
110 var fileText =
template.RemoveExistingComment(file.ReadAllText());
111 var license =
template.TransformLicenseText(licenseData.License);
112 var commentTemplate = Template.Parse(
template.LeadingCommentTemplate);
113 var commentText = commentTemplate.Render(licenseData with {License = license});
114 file.WriteAllText(commentText + Environment.NewLine + fileText);
117 root.GetDirectories()
118 .Where(d => options.AllowDirectory?.Invoke(d) ??
true)
119 .Where(d => options.AllowDotDirectories || !d.Name.StartsWith(
'.'))
120 .Where(d => d.GlobFiles(options.LicenseRegionFile).IsEmpty())
121 .ForEach(d =>
self.ProcessLicenseRegion(d, licenseData, options));