Ejemplo n.º 1
0
        private bool HasConfigurationChanged(IConfigurationGenerator generator)
        {
            generator.GeneratedFiles.ForEach(FileSystemTasks.EnsureExistingParentDirectory);
            var previousHashes = generator.GeneratedFiles
                                 .Where(File.Exists)
                                 .ToDictionary(x => x, FileSystemTasks.GetFileHash);

            var assembly = Assembly.GetEntryAssembly().NotNull("assembly != null");

            ProcessTasks.StartProcess(
                assembly.Location,
                $"--{ConfigurationParameterName} {generator.Id} --host {generator.HostName}",
                logInvocation: false,
                logOutput: true)
            .AssertZeroExitCode();

            var changedFiles = generator.GeneratedFiles
                               .Where(x => FileSystemTasks.GetFileHash(x) != previousHashes.GetValueOrDefault(x))
                               .Select(x => NukeBuild.RootDirectory.GetRelativePathTo(x)).ToList();

            if (changedFiles.Count == 0)
            {
                return(false);
            }

            Logger.Warn($"{generator.DisplayName} configuration files have changed.");
            changedFiles.ForEach(x => Logger.Trace($"Updated {x}"));
            return(true);
        }
        public void OnAfterLogo(
            NukeBuild build,
            IReadOnlyCollection <ExecutableTarget> executableTargets,
            IReadOnlyCollection <ExecutableTarget> executionPlan)
        {
            if (!AutoGenerate || NukeBuild.IsServerBuild)
            {
                return;
            }

            Logger.LogLevel = LogLevel.Trace;
            var previousHashes = GeneratedFiles
                                 .Where(File.Exists)
                                 .ToDictionary(x => x, FileSystemTasks.GetFileHash);

            var assembly = Assembly.GetEntryAssembly().NotNull("assembly != null");

            ProcessTasks.StartProcess(
                assembly.Location,
                $"--{ConfigurationParameterName} --host {HostType}",
                logInvocation: false,
                logOutput: true)
            .AssertZeroExitCode();

            var changedFiles = GeneratedFiles
                               .Where(x => FileSystemTasks.GetFileHash(x) != previousHashes.GetValueOrDefault(x))
                               .Select(x => GetRelativePath(NukeBuild.RootDirectory, x)).ToList();

            if (changedFiles.Count > 0)
            {
                Logger.Warn($"{HostType} configuration files have changed.");
                changedFiles.ForEach(x => Logger.Trace($"Updated {x}"));
            }
        }
        private bool HasConfigurationChanged(IConfigurationGenerator generator, NukeBuild build)
        {
            generator.GeneratedFiles.ForEach(FileSystemTasks.EnsureExistingParentDirectory);
            var previousHashes = generator.GeneratedFiles
                                 .Where(File.Exists)
                                 .ToDictionary(x => x, FileSystemTasks.GetFileHash);

            ProcessTasks.StartProcess(
                NukeBuild.BuildAssemblyFile,
                $"--{ConfigurationParameterName} {generator.Id} --host {generator.HostName}",
                logInvocation: false,
                logOutput: false)
            .AssertZeroExitCode();

            var changedFiles = generator.GeneratedFiles
                               .Where(x => FileSystemTasks.GetFileHash(x) != previousHashes.GetValueOrDefault(x))
                               .Select(x => NukeBuild.RootDirectory.GetRelativePathTo(x)).ToList();

            if (changedFiles.Count == 0)
            {
                return(false);
            }

            Telemetry.ConfigurationGenerated(generator.HostType, generator.Id, build);
            // TODO: multi-line logging
            Log.Warning("Configuration files for {Configuration} have changed.", generator.DisplayName);
            changedFiles.ForEach(x => Log.Verbose("Updated {File}", x));
            return(true);
        }
Ejemplo n.º 4
0
        public static async Task <bool> DownloadExternalFile(
            string externalFile,
            int timeout,
            Action <string, string> warningSink,
            Action <string, string> errorSink)
        {
            try
            {
                var lines         = File.ReadAllLines(externalFile).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
                var uriConversion = Uri.TryCreate(lines.FirstOrDefault(), UriKind.Absolute, out var uri);
                ControlFlow.Assert(uriConversion, $"Could not parse URI for external file from first line of '{externalFile}'.");

                var outputFile   = externalFile.Substring(0, externalFile.Length - 4);
                var previousHash = File.Exists(outputFile) ? FileSystemTasks.GetFileHash(outputFile) : null;

                var template = await HttpTasks.HttpDownloadStringAsync(uri.OriginalString);

                var replacements = lines.Skip(1)
                                   .Where(x => x.Contains('='))
                                   .Select(x => x.Split('='))
                                   .Where(x => x.Length == 2)
                                   .ToDictionary(
                    x => $"_{x.First().Trim('_').ToUpperInvariant()}_",
                    x => x.ElementAt(1));
                var definitions = lines.Skip(1)
                                  .Where(x => !x.Contains('=') && !string.IsNullOrWhiteSpace(x))
                                  .Select(x => x.ToUpperInvariant())
                                  .ToList();

                File.WriteAllText(outputFile, TemplateUtility.FillTemplate(template, definitions, replacements));
                var newHash = FileSystemTasks.GetFileHash(outputFile);

                if (newHash != previousHash)
                {
                    warningSink(externalFile, "External file has been updated.");
                }

                return(true);
            }
            catch (Exception exception)
            {
                errorSink(externalFile, exception.Message);
                return(false);
            }
        }
Ejemplo n.º 5
0
        private async Task <bool> DownloadExternalFile(ExternalFilesData data)
        {
            try
            {
                var previousHash = File.Exists(data.OutputPath) ? FileSystemTasks.GetFileHash(data.OutputPath) : null;

                var template = (await HttpTasks.HttpDownloadStringAsync(data.Uri.OriginalString)).SplitLineBreaks();
                TextTasks.WriteAllLines(data.OutputPath, TemplateUtility.FillTemplate(template, data.Tokens));

                var newHash = FileSystemTasks.GetFileHash(data.OutputPath);
                if (newHash != previousHash)
                {
                    LogWarning(message: $"External file '{data.OutputPath}' has been updated.", file: data.Identity);
                }

                return(true);
            }
            catch (Exception exception)
            {
                LogError(message: exception.Message, file: data.Identity);
                return(false);
            }
        }