コード例 #1
0
        /// <summary>
        /// Substitutes the property tokens in the supplied string.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="required">A value indicating whether the parameter is required.</param>
        /// <returns>The substituted string</returns>
        internal static string SubstituteTokensInString(string input, IDictionary <string, string> parameters, bool required)
        {
            string          output        = input;
            MatchCollection paramsMatches = ParameterRegex.Matches(output);

            while (paramsMatches.Count > 0)
            {
                Match  paramsMatch = paramsMatches[paramsMatches.Count - 1];
                string parameterValue;

                if (!parameters.TryGetValue(paramsMatch.Groups[1].Value, out parameterValue))
                {
                    if (required)
                    {
                        throw new TemplateParserException($"Value for parameter '{paramsMatch.Groups[1].Value}' has not been provided.");
                    }

                    parameterValue = string.Empty;
                }

                if (MacroUtilities.IsNested(output, paramsMatch))
                {
                    parameterValue = "'" + parameterValue + "'";
                }

                output        = output.Replace(paramsMatch.Groups[0].Value, parameterValue);
                paramsMatches = ParameterRegex.Matches(output);
            }

            return(output);
        }
コード例 #2
0
ファイル: ResourcesMacro.cs プロジェクト: vovoma/CaaSDeploy
        /// <summary>
        /// Substitutes the property tokens in the supplied string.
        /// </summary>
        /// <param name="runtimeContext">The runtime context.</param>
        /// <param name="taskContext">The task execution context.</param>
        /// <param name="input">The input string.</param>
        /// <returns>The substituted string</returns>
        public async Task <string> SubstituteTokensInString(RuntimeContext runtimeContext, TaskContext taskContext, string input)
        {
            return(await Task.Run(() =>
            {
                string output = input;

                if (taskContext.ResourcesProperties != null)
                {
                    MatchCollection resourceMatches = ResourcePropertyRegex.Matches(output);

                    while (resourceMatches.Count > 0)
                    {
                        Match resourceMatch = resourceMatches[resourceMatches.Count - 1];
                        string resourceId = resourceMatch.Groups[1].Value;
                        string property = resourceMatch.Groups[2].Value;

                        JObject resource;

                        if (!taskContext.ResourcesProperties.TryGetValue(resourceId, out resource))
                        {
                            throw new TemplateParserException($"Referenced resource '{resourceId}' not found.");
                        }

                        var newValue = resource.SelectToken(property).Value <string>();

                        if (MacroUtilities.IsNested(output, resourceMatch))
                        {
                            newValue = "'" + newValue + "'";
                        }

                        output = output.Replace(resourceMatch.Groups[0].Value, newValue);
                        resourceMatches = ResourcePropertyRegex.Matches(output);
                    }
                }

                return output;
            }));
        }