Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/Types/Types/Program.cs @ 14731

Last change on this file since 14731 was 14731, checked in by gkronber, 7 years ago

#2520: added tool program which adds StorableType attributes to interfaces and enums

File size: 3.0 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Text.RegularExpressions;
8using System.Threading.Tasks;
9using HeuristicLab.Common;
10
11namespace Types {
12  class Program {
13    private static string path = @"D:\hl\branches\PersistenceOverhaul";
14    static void Main(string[] args) {
15      InsertStorableTypeGuids();
16
17      // foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
18      // {
19      //   Console.WriteLine(asm);
20      //   foreach (var type in asm.GetTypes().Where(t => typeof(IEqualityComparer).IsAssignableFrom(t)))
21      //   {
22      //     Console.WriteLine("RegisterType(new Guid(\"{0}\"), allTypes[\"{2}\"]); ", Guid.NewGuid().ToString().ToUpper(), type.GetPrettyName(), type.FullName);
23      //   }
24      //   foreach (var type in asm.GetTypes().Where(t => typeof(IEqualityComparer<>).IsAssignableFrom(t))) {
25      //     Console.WriteLine("RegisterType(new Guid(\"{0}\"), allTypes[\"{2}\"]); ", Guid.NewGuid().ToString().ToUpper(), type.GetPrettyName(), type.FullName);
26      //   }
27      // }
28    }
29
30    private static void InsertStorableTypeGuids() {
31      var regex = new Regex(@"(public\s+interface|private\s+interface|protected\s+interface|internal\s+interface|public\s+enum|private\s+enum|protected\s+enum|internal\s+enum)");
32      foreach (var file in Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories)) {
33        if (file.Contains("HeuristicLab.ExtLibs")) continue;
34        if (file.Contains(".Views")) continue;
35        if (file.Contains("HeuristicLab.MainForm")) continue;
36        if (file.Contains("HeuristicLab.DataPreprocessing")) continue;
37        //Console.WriteLine(file);
38        Encoding encoding = Encoding.ASCII;
39        using (StreamReader reader = new StreamReader(file, encoding, true)) {
40          reader.Peek(); // you need this!
41          encoding = reader.CurrentEncoding;
42        }
43        var lines = File.ReadAllLines(file, encoding).ToList();
44        using (var stream = new FileStream(file, FileMode.Create)) {
45          using (var writer = new StreamWriter(stream, encoding)) {
46            for (int i = 0; i < lines.Count; i++) {
47
48              var match = regex.Match(lines[i]);
49              var idx = match.Index;
50              if (match.Success) {
51                writer.WriteLine(string.Join("", Enumerable.Repeat(" ", idx)) + "[HeuristicLab.Persistence.Default.CompositeSerializers.Storable.StorableType(\"{0}\")]", Guid.NewGuid().ToString().ToUpper());
52              }
53              else if (lines[i].IndexOf("interface") >= 0 || lines[i].IndexOf("enum") >= 0) // write lines which contain 'interface' or 'enum' but are not matched by the regex
54              {   
55                Console.WriteLine(lines[i]);             
56              }
57              writer.WriteLine(lines[i]);
58            }
59            writer.Flush();
60            stream.Flush();
61          }
62        }
63      }
64    }
65  }
66}
Note: See TracBrowser for help on using the repository browser.