コード例 #1
0
        public static async Task <PowerShellScriptInfo> TryLoadAsync(LooselyQualifiedName scriptName)
        {
            using (var raft = RaftRepository.OpenRaft(scriptName.Namespace ?? RaftRepository.DefaultName))
            {
                if (raft == null)
                {
                    return(null);
                }

                using (var item = await raft.OpenRaftItemAsync(RaftItemType.Script, scriptName.Name + ".ps1", FileMode.Open, FileAccess.Read).ConfigureAwait(false))
                {
                    if (item == null)
                    {
                        return(null);
                    }

                    using (var reader = new StreamReader(item, InedoLib.UTF8Encoding))
                    {
                        if (!TryParse(reader, out var info))
                        {
                            return(null);
                        }

                        return(info);
                    }
                }
            }
        }
コード例 #2
0
        public async Task <IEnumerable <string> > GetSuggestionsAsync(IComponentConfiguration config)
        {
            using (var raft = RaftRepository.OpenRaft(RaftRepository.DefaultName))
            {
                if (raft == null)
                {
                    return(Enumerable.Empty <string>());
                }

                var items = await raft.GetRaftItemsAsync(RaftItemType.TextTemplate).ConfigureAwait(false);

                return(items
                       .Select(i => i.ItemName)
                       .ToList());
            }
        }
コード例 #3
0
        private static async Task <string> GetScriptTextAsync(ILogSink logger, string fullScriptName, IOperationExecutionContext context)
        {
            string scriptName;
            string raftName;
            var    scriptNameParts = fullScriptName.Split(new[] { "::" }, 2, StringSplitOptions.None);

            if (scriptNameParts.Length == 2)
            {
                raftName   = scriptNameParts[0];
                scriptName = scriptNameParts[1];
            }
            else
            {
                raftName   = RaftRepository.DefaultName;
                scriptName = scriptNameParts[0];
            }

            using (var raft = RaftRepository.OpenRaft(raftName))
            {
                if (raft == null)
                {
                    logger.LogError($"Raft {raftName} not found.");
                    return(null);
                }

                using (var scriptItem = await raft.OpenRaftItemAsync(RaftItemType.Script, scriptName + ".ps1", FileMode.Open, FileAccess.Read))
                {
                    if (scriptItem == null)
                    {
                        logger.LogError($"Script {scriptName}.ps1 not found in {raftName} raft.");
                        return(null);
                    }

                    using (var reader = new StreamReader(scriptItem, InedoLib.UTF8Encoding))
                    {
                        var scriptText = reader.ReadToEnd();
                        logger.LogDebug($"Found script {scriptName}.ps1 in {raftName} raft.");
                        return(scriptText);
                    }
                }
            }
        }
コード例 #4
0
        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);
            }
        }