Beispiel #1
0
        public static void Main(string[] args)
        {
            var target          = Path.GetFullPath(args[0]);
            var outputDirectory = Path.GetFullPath(args[1]);
            var references      = File.ReadAllLines(Path.GetFullPath(args[2]))
                                  .Select(x => new Reference(x))
                                  .ToDictionary(x => x.Name);
            var sqlRootDirectory = Path.GetFullPath(args[3]);
            var sqlRootNamespace = args[4];
            var sqlSubDirectory  = args[5];
            var option           = new GeneratorOption(args[6]);

            var targetAssembly = Assembly.LoadFile(target);
            var context        = AssemblyLoadContext.GetLoadContext(targetAssembly);

            context.Resolving += (loadContext, name) =>
            {
                if (references.TryGetValue(name.Name, out var reference))
                {
                    return(context.LoadFromAssemblyPath(reference.FilePath));
                }

                return(null);
            };

            var assembly = context.LoadFromAssemblyPath(target);

            var loader    = new FileSqlLoader(sqlRootDirectory, sqlRootNamespace, sqlSubDirectory);
            var writer    = new FileSourceWriter(outputDirectory);
            var generator = new DataAccessorGenerator(loader, writer, option);

            generator.Generate(assembly.GetExportedTypes());

            var assemblySource = Path.Combine(outputDirectory, AssemblySource);

            WriteAssembly(targetAssembly, assemblySource, option);

            var newFiles = writer.NewFiles.Append(AssemblySource).ToHashSet();

            foreach (var file in Directory.GetFiles(outputDirectory).Select(Path.GetFileName))
            {
                if (!newFiles.Contains(file))
                {
                    File.Delete(Path.Combine(outputDirectory, file));
                }
            }
        }
Beispiel #2
0
        private static void WriteAssembly(Assembly assembly, string path, GeneratorOption option)
        {
            var sb = new StringBuilder();

            sb.AppendLine("// <auto-generated />");
            sb.AppendLine("// Option:");
            foreach (string key in option.Keys)
            {
                sb.AppendLine($"//   {key} : {option.GetValue(key)}");
            }
            sb.AppendLine("using System.Reflection;");
            sb.AppendLine();
            sb.AppendLine($"[assembly: AssemblyVersion(\"{assembly.GetName().Version}\")]");
            var fileVersion = assembly.GetCustomAttribute <AssemblyFileVersionAttribute>();

            if (fileVersion != null)
            {
                sb.AppendLine($"[assembly: AssemblyFileVersion(\"{fileVersion.Version}\")]");
            }
            var informationVersion = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>();

            if (informationVersion != null)
            {
                sb.AppendLine($"[assembly: AssemblyInformationalVersion(\"{informationVersion.InformationalVersion}\")]");
            }

            var source = sb.ToString();

            if (File.Exists(path))
            {
                var currentSource = File.ReadAllText(path);
                if (currentSource == source)
                {
                    return;
                }
            }

            File.WriteAllText(path, source);
        }