Esempio n. 1
0
 private static RuntimeValue?TryGetFunctionValue(RuntimeVariableName functionName, IOperationExecutionContext context)
 {
     try
     {
         return(context.TryGetFunctionValue(functionName.ToString()));
     }
     catch
     {
         return(null);
     }
 }
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            var template = await getTemplateAsync().ConfigureAwait(false);

            this.LogDebug("Detecting newlines in source template...");
            var sourceNewLines = template.Contains("\r\n") ? TemplateNewLineMode.Windows
                : template.Contains("\n") ? TemplateNewLineMode.Linux
                : TemplateNewLineMode.Auto;

            this.LogDebug("Applying template...");
            var result = await context.ApplyTextTemplateAsync(template, this.AdditionalVariables).ConfigureAwait(false);

            this.LogInformation("Template applied.");

            if (this.NewLineMode == TemplateNewLineMode.Windows)
            {
                result = result.Replace("\n", "\r\n");
            }

            if (!string.IsNullOrWhiteSpace(this.OutputFile))
            {
                var path = context.ResolvePath(this.OutputFile);
                this.LogDebug($"Writing output to {path}...");
                var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>().ConfigureAwait(false);

                if (this.NewLineMode == TemplateNewLineMode.Auto)
                {
                    result = result.Replace("\n", fileOps.NewLine);
                }

                await fileOps.CreateDirectoryAsync(PathEx.GetDirectoryName(path));

                await fileOps.WriteAllTextAsync(path, result).ConfigureAwait(false);
            }

            this.LogDebug("Setting output variable...");
            this.OutputVariable = result;

            async Task <string> getTemplateAsync()
            {
                if (!string.IsNullOrEmpty(this.Literal))
                {
                    this.LogDebug("Applying literal template: " + this.Literal);
                    return(this.Literal);
                }

                if (!string.IsNullOrEmpty(this.InputFile))
                {
                    var path = context.ResolvePath(this.InputFile);
                    this.LogDebug($"Using file {path} as template...");
                    var fileOps = await context.Agent.GetServiceAsync <IFileOperationsExecuter>().ConfigureAwait(false);

                    if (!await fileOps.FileExistsAsync(path).ConfigureAwait(false))
                    {
                        throw new ExecutionFailureException("Template file not found.");
                    }

                    return(await fileOps.ReadAllTextAsync(path).ConfigureAwait(false));
                }

                if (!string.IsNullOrEmpty(this.Asset))
                {
                    string templateName;
                    string raftName;

                    var templateNameParts = this.Asset.Split(new[] { "::" }, 2, StringSplitOptions.None);
                    if (templateNameParts.Length == 2)
                    {
                        raftName     = templateNameParts[0];
                        templateName = templateNameParts[1];
                    }
                    else
                    {
                        if (SDK.ProductName == "BuildMaster")
                        {
                            raftName = context.TryGetFunctionValue("$ApplicationName")?.AsString() ?? "";
                        }
                        else
                        {
                            raftName = RaftRepository.DefaultName;
                        }

                        templateName = templateNameParts[0];
                    }

                    using (var raft = RaftRepository.OpenRaft(raftName))
                    {
                        if (raft == null)
                        {
                            throw new ExecutionFailureException("Raft not found.");
                        }

                        using (var stream = await raft.OpenRaftItemAsync(RaftItemType.TextTemplate, templateName, FileMode.Open, FileAccess.Read))
                        {
                            if (stream == null)
                            {
                                throw new ExecutionFailureException($"Template \"{templateName}\" not found in raft.");
                            }

                            this.LogDebug("Loading template from raft...");
                            using (var reader = new StreamReader(stream, InedoLib.UTF8Encoding))
                            {
                                return(await reader.ReadToEndAsync().ConfigureAwait(false));
                            }
                        }
                    }
                }

                this.LogWarning("No template specified. Setting output to empty string.");
                return(string.Empty);
            }
        }