Exemple #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);
                    }
                }
            }
        }
Exemple #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());
            }
        }
        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);
                    }
                }
            }
        }
Exemple #4
0
        protected async Task <bool> RaftItemsEqualAsync(RaftRepository raft1, RaftRepository raft2, RaftItemType itemType, string itemName)
        {
            // This relies on raft items (usually) being pretty small. If that's no longer the case, we need to read the files in chunks instead.
            var contents = await Task.WhenAll(
                getItemContentsAsync(raft1),
                getItemContentsAsync(raft2)
                );

            return(contents[0].SequenceEqual(contents[1]));

            async Task <byte[]> getItemContentsAsync(RaftRepository raft)
            {
                using (var stream = await raft.OpenRaftItemAsync(itemType, itemName, FileMode.Open, FileAccess.Read))
                    using (var memory = new MemoryStream())
                    {
                        await stream.CopyToAsync(memory);

                        return(memory.ToArray());
                    }
            }
        }
        protected override async Task ExecuteRaftAsync(IOperationExecutionContext context, RaftRepository actualRaft, RaftRepository raftShim)
        {
            var actualItems = await actualRaft.GetRaftItemsAsync();

            var shimItems = await raftShim.GetRaftItemsAsync();

            var  actualLookup = actualItems.ToLookup(i => (i.ItemType, i.ItemName));
            var  shimLookup   = shimItems.ToLookup(i => (i.ItemType, i.ItemName));
            bool any          = false;

            if (this.DeleteMissing)
            {
                foreach (var item in actualItems)
                {
                    if (shimLookup.Contains((item.ItemType, item.ItemName)))
                    {
                        continue;
                    }

                    any = true;
                    this.LogInformation($"Deleting {item.ItemType} {item.ItemName}, which is present in the raft but not locally.");
                    await actualRaft.DeleteRaftItemAsync(item.ItemType, item.ItemName);
                }
            }

            foreach (var item in shimItems)
            {
                var actualItem = actualLookup[(item.ItemType, item.ItemName)].FirstOrDefault();
 protected abstract Task ExecuteRaftAsync(IOperationExecutionContext context, RaftRepository actualRaft, RaftRepository raftShim);
        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);
            }
        }