Esempio n. 1
0
        public static void Run(Enumeration enumeration, ToolWriter toolWriter)
        {
            var values = enumeration.Values.ToArray();

            for (var i = 0; i + 1 < values.Length; i++)
            {
                values[i] += ",";
            }

            string GetIdentifier(string value)
            => value.Aggregate(
                new StringBuilder(!char.IsLetter(value[index: 0]) ? "_" : string.Empty),
                (sb, c) => sb.Append(char.IsLetterOrDigit(c) ? c : '_'),
                sb => sb.ToString());

            toolWriter
            .WriteLine($"#region {enumeration.Name}")
            .WriteSummary(enumeration)
            .WriteLine("[PublicAPI]")
            .WriteLine("[Serializable]")
            .WriteObsoleteAttributeWhenObsolete(enumeration)
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine($"[TypeConverter(typeof(TypeConverter<{enumeration.Name}>))]")
            .WriteLine($"public partial class {enumeration.Name} : Enumeration")
            .WriteBlock(w => w.ForEach(enumeration.Values,
                                       x => w.WriteLine(
                                           $"public static {enumeration.Name} {GetIdentifier(x).EscapeProperty()} = new {enumeration.Name} {{ Value = {x.DoubleQuote()} }};")))
            .WriteLine("#endregion");
        }
Esempio n. 2
0
        private static void WriteGenericTask(this ToolWriter writer)
        {
            var tool       = writer.Tool;
            var parameters = new[]
            {
                "string arguments",
                "string workingDirectory = null",
                "IReadOnlyDictionary<string, string> environmentVariables = null",
                "int? timeout = null",
                "bool redirectOutput = false",
                "Func<string, string> outputFilter = null"
            };
            var arguments = new[]
            {
                $"{tool.Name}Path",
                "arguments",
                "workingDirectory",
                "environmentVariables",
                "timeout",
                "redirectOutput",
                "outputFilter"
            };

            writer
            .WriteSummary(tool.Help)
            .WriteLine($"public static IEnumerable<string> {tool.Name}({parameters.JoinComma()})")
            .WriteBlock(w => w
                        .WriteLine($"var process = ProcessTasks.StartProcess({arguments.JoinComma()});")
                        .WriteLine("process.AssertZeroExitCode();")
                        .WriteLine("return process.HasOutput ? process.Output.Select(x => x.Text) : null;"));
        }
Esempio n. 3
0
        public static void Run(DataClass dataClass, ToolWriter toolWriter)
        {
            if (dataClass.OmitDataClass)
            {
                return;
            }

            if (dataClass.IsToolSettingsClass)
            {
                foreach (var property in dataClass.Properties)
                {
                    CheckMissingValue(property);
                    CheckMissingSecret(property);
                }
            }

            var writer   = new DataClassWriter(dataClass, toolWriter);
            var baseType = dataClass.BaseClass ?? (dataClass.Name.EndsWith("Settings") ? "ToolSettings" : "ISettingsEntity");

            writer
            .WriteLine($"#region {dataClass.Name}")
            .WriteSummary(dataClass)
            .WriteLine("[PublicAPI]")
            .WriteObsoleteAttributeWhenObsolete(dataClass)
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine("[Serializable]")
            .WriteLine($"public partial class {dataClass.Name} : {baseType}")
            .WriteBlock(w => w
                        .WriteToolPath()
                        .WriteLogger()
                        .ForEach(dataClass.Properties, WritePropertyDeclaration)
                        .WriteConfigureArguments())
            .WriteLine("#endregion");
        }
Esempio n. 4
0
        private static ToolWriter WriteToolPath(this ToolWriter writer)
        {
            var tool      = writer.Tool;
            var resolvers = new List <string>();

            if (tool.PackageId != null)
            {
                resolvers.Add("ToolPathResolver.GetPackageExecutable(" +
                              $"{tool.PackageId.DoubleQuote()}, " +
                              $"{tool.PackageExecutable?.DoubleQuote() ?? "GetPackageExecutable()"})");
            }

            if (tool.PathExecutable != null)
            {
                resolvers.Add($"ToolPathResolver.GetPathExecutable({tool.PathExecutable.DoubleQuote()})");
            }

            if (resolvers.Count == 0)
            {
                resolvers.Add("GetToolPath()");
            }

            Trace.Assert(resolvers.Count == 1, "resolvers.Count == 1");

            return(writer
                   .WriteSummary($"Path to the {tool.Name} executable.")
                   .WriteLine($"public static string {tool.Name}Path =>")
                   .WriteLine($"    ToolPathResolver.TryGetEnvironmentExecutable(\"{tool.Name.ToUpperInvariant()}_EXE\") ??")
                   .WriteLine($"    {resolvers.Single()};"));
        }
Esempio n. 5
0
        private static void WriteGenericTask(this ToolWriter writer)
        {
            var tool       = writer.Tool;
            var parameters = new[]
            {
                "string arguments",
                "string workingDirectory = null",
                "IReadOnlyDictionary<string, string> environmentVariables = null",
                "int? timeout = null",
                "bool logOutput = true",
                "Func<string, string> outputFilter = null"
            };
            var arguments = new[]
            {
                $"{tool.Name}Path",
                "arguments",
                "workingDirectory",
                "environmentVariables",
                "timeout",
                "logOutput",
                $"{tool.Name}Logger",
                "outputFilter"
            };

            writer
            .WriteSummary(tool.Help)
            .WriteObsoleteAttributeWhenObsolete(tool)
            .WriteLine($"public static IReadOnlyCollection<Output> {tool.Name}({parameters.JoinComma()})")
            .WriteBlock(w => w
                        .WriteLine($"var process = ProcessTasks.StartProcess({arguments.JoinComma()});")
                        .WriteLine("process.AssertZeroExitCode();")
                        .WriteLine("return process.Output;"));
        }
Esempio n. 6
0
        private static void WriteGenericTask(this ToolWriter writer)
        {
            var tool       = writer.Tool;
            var parameters = new[]
            {
                "string arguments",
                "string workingDirectory = null",
                "ProcessSettings processSettings = null"
            };
            var arguments = new[]
            {
                $"{tool.Name}Path",
                "arguments",
                "workingDirectory",
                "processSettings?.EnvironmentVariables",
                "processSettings?.ExecutionTimeout",
                "processSettings?.RedirectOutput ?? true"
            };

            writer
            .WriteSummary(tool.Help)
            .WriteLine($"public static IEnumerable<string> {tool.Name}({parameters.JoinComma()})")
            .WriteBlock(w => w
                        .WriteLine($"var process = ProcessTasks.StartProcess({arguments.JoinComma()});")
                        .WriteLine("process.AssertZeroExitCode();")
                        .WriteLine("return process.Output.Select(x => x.Text);"));
        }
Esempio n. 7
0
        public static void Run(Tool tool, [CanBeNull] string @namespace, StreamWriter streamWriter)
        {
            using (var writer = new ToolWriter(tool, streamWriter))
            {
                writer
                // TODO [3]: extract license from dotsettings file
                .WriteLine($"// Copyright Matthias Koch, Sebastian Karasek {DateTime.Now.Year}.")
                .WriteLine("// Distributed under the MIT License.")
                .WriteLine("// https://github.com/nuke-build/nuke/blob/master/LICENSE")
                .WriteLine(string.Empty)
                .WriteLine($"// Generated with {s_assembly.GetName().Name}, Version: {s_assembly.GetVersionText()}.")
                .WriteLineIfTrue(tool.RepositoryUrl != null, $"// Generated from {tool.RepositoryUrl}.")
                .WriteLine(string.Empty)
                .ForEach(GetNamespaceImports(), x => writer.WriteLine($"using {x};"))
                .WriteLine(string.Empty)
                .WriteLineIfTrue(@namespace != null, $"namespace {@namespace}");

                if (!string.IsNullOrEmpty(@namespace))
                {
                    writer.WriteBlock(x => x.WriteAll());
                }
                else
                {
                    writer.WriteAll();
                }
            }
        }
Esempio n. 8
0
 private static ToolWriter WriteAll(this ToolWriter w)
 {
     return(w
            .WriteAlias()
            .WriteDataClasses()
            .WriteEnumerations());
 }
Esempio n. 9
0
        public static void Run(Tool tool, ToolWriter toolWriter)
        {
            if (tool.Tasks.Count == 0 && !tool.CustomExecutable && tool.PathExecutable == null && tool.PackageId == null)
            {
                return;
            }

            toolWriter
            .WriteSummary(tool)
            .WriteLine("[PublicAPI]")
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine($"public static partial class {tool.GetClassName()}")
            .WriteBlock(w =>
            {
                w
                .WriteToolPath()
                .WriteLogger()
                .WriteGenericTask();

                tool.Tasks.ForEach(x => new TaskWriter(x, toolWriter)
                                   .WriteToolSettingsTask()
                                   .WriteConfiguratorTask()
                                   .WriteCombinatorialConfiguratorTask());
            });
        }
Esempio n. 10
0
        private static ToolWriter WriteLogger(this ToolWriter writer)
        {
            var tool   = writer.Tool;
            var logger = tool.CustomLogger ? "CustomLogger" : "ProcessManager.DefaultLogger";

            return(writer
                   .WriteLine($"public static Action<OutputType, string> {tool.Name}Logger {{ get; set; }} = {logger};"));
        }
Esempio n. 11
0
        private static ToolWriter WriteDataClasses(this ToolWriter writer)
        {
            var dataClasses = writer.Tool.Tasks.Select(x => x.SettingsClass).Concat(writer.Tool.DataClasses).ToList();

            dataClasses.ForEach(x => DataClassGenerator.Run(x, writer));
            dataClasses.Where(x => x.ExtensionMethods).ForEach(x => DataClassExtensionGenerator.Run(x, writer));
            return(writer);
        }
        public static void Run(DataClass dataClass, ToolWriter toolWriter)
        {
            var writer = new DataClassWriter(dataClass, toolWriter);

            writer
            .WriteLine($"#region {dataClass.Name}Extensions")
            .WriteSummary(dataClass)
            .WriteLine("[PublicAPI]")
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine($"public static partial class {dataClass.Name}Extensions")
            .WriteBlock(w => w.ForEach(dataClass.Properties, WriteMethods))
            .WriteLine("#endregion");
        }
Esempio n. 13
0
        public static void Run(Tool tool, ToolWriter toolWriter)
        {
            if (tool.Tasks.Count == 0)
            {
                return;
            }

            toolWriter
            .WriteLine("[PublicAPI]")
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine($"public static partial class {tool.GetClassName()}")
            .WriteBlock(w => tool.Tasks.ForEach(x => new TaskWriter(x, toolWriter)
                                                .WritePreAndPostProcess()
                                                .WriteMainTask()
                                                .WriteTaskOverloads()));
        }
Esempio n. 14
0
        public static void Run(DataClass dataClass, ToolWriter toolWriter)
        {
            if (dataClass.ArgumentConstruction)
            {
                foreach (var property in dataClass.Properties)
                {
                    if (property.NoArgument)
                    {
                        continue;
                    }

                    if (new[] { "int", "bool" }.Contains(property.Type))
                    {
                        continue;
                    }

                    if (property.Format != null && property.Format.Contains("{value}"))
                    {
                        continue;
                    }

                    Console.WriteLine($"Format for '{dataClass.Name}.{property.Name}' doesn't contain '{{value}}'.");
                }
            }

            var writer   = new DataClassWriter(dataClass, toolWriter);
            var baseType = dataClass.BaseClass ?? (dataClass.Name.EndsWith("Settings") ? "ToolSettings" : "ISettingsEntity");

            writer
            .WriteLine($"#region {dataClass.Name}")
            .WriteSummary(dataClass)
            .WriteLine("[PublicAPI]")
            .WriteObsoleteAttributeWhenObsolete(dataClass)
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine("[Serializable]")
            .WriteLine($"public partial class {dataClass.Name} : {baseType}")
            .WriteBlock(w => w
                        .WriteToolPath()
                        .WriteLogLevelParser()
                        .ForEach(dataClass.Properties, WritePropertyDeclaration)
                        .WriteAssertValid()
                        .WriteConfigureArguments())
            .WriteLine("#endregion");
        }
Esempio n. 15
0
        public static void Run(Tool tool, ToolWriter toolWriter)
        {
            if (tool.Tasks.Count == 0 && !tool.CustomExecutable && tool.PathExecutable == null && tool.PackageId == null)
            {
                return;
            }

            toolWriter
            .WriteLine("[PublicAPI]")
            .WriteLine("[ExcludeFromCodeCoverage]")
            .WriteLine($"public static partial class {tool.GetClassName()}")
            .WriteBlock(w =>
            {
                w.WriteToolPath();
                w.WriteGenericTask();
                tool.Tasks.ForEach(x => new TaskWriter(x, toolWriter)
                                   .WriteMainTask()
                                   .WriteTaskOverloads());
            });
        }
Esempio n. 16
0
        public static void Run(Tool tool, StreamWriter streamWriter)
        {
            using var writer = new ToolWriter(tool, streamWriter);
            writer
            // TODO [3]: extract license from dotsettings file
            .WriteLineIfTrue(tool.SourceFile != null, $"// Generated from {tool.SourceFile}")
            .WriteLine(string.Empty)
            .ForEach(GetNamespaceImports(tool), x => writer.WriteLine($"using {x};"))
            .WriteLine(string.Empty)
            .WriteLineIfTrue(tool.Namespace != null, $"namespace {tool.Namespace}");

            if (!string.IsNullOrEmpty(tool.Namespace))
            {
                writer.WriteBlock(x => x.WriteAll());
            }
            else
            {
                writer.WriteAll();
            }
        }
Esempio n. 17
0
 public static void Run(Tool tool, StreamWriter streamWriter)
 {
     using (var writer = new ToolWriter(tool, streamWriter))
     {
         writer
         // TODO [3]: extract license from dotsettings file
         .WriteLine("// Copyright Matthias Koch 2017.")
         .WriteLine("// Distributed under the MIT License.")
         .WriteLine("// https://github.com/nuke-build/nuke/blob/master/LICENSE")
         .WriteLine(string.Empty)
         .WriteLine($"// Generated from {tool.RepositoryUrl} with Nuke.ToolGenerator.")
         .WriteLine(string.Empty)
         .ForEach(GetNamespaceImports(), x => writer.WriteLine($"using {x};"))
         .WriteLine(string.Empty)
         .WriteLine($"namespace {tool.GetNamespace()}")
         .WriteBlock(w => w
                     .WriteAlias()
                     .WriteDataClasses()
                     .WriteEnumerations());
     }
 }
Esempio n. 18
0
 private static ToolWriter WriteAlias(this ToolWriter writer)
 {
     TaskGenerator.Run(writer.Tool, writer);
     return(writer);
 }
Esempio n. 19
0
 private static ToolWriter WriteEnumerations(this ToolWriter writer)
 {
     writer.Tool.Enumerations.ForEach(x => EnumerationGenerator.Run(x, writer));
     return(writer);
 }