Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
codegen.cs111 linesDownload Raw Back to lang
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Text;5 6using Dbg = System.Management.Automation.Diagnostics;7 8namespace System.Management.Automation.Language9{10    /// <summary>11    /// Contains utility methods for use in applications that generate PowerShell code.12    /// </summary>13    public static class CodeGeneration14    {15        /// <summary>16        /// Escapes content so that it is safe for inclusion in a single-quoted string.17        /// For example: "'" + EscapeSingleQuotedStringContent(userContent) + "'"18        /// </summary>19        /// <param name="value">The content to be included in a single-quoted string.</param>20        /// <returns>Content with all single-quotes escaped.</returns>21        public static string EscapeSingleQuotedStringContent(string value)22        {23            if (string.IsNullOrEmpty(value))24            {25                return string.Empty;26            }27 28            StringBuilder sb = new StringBuilder(value.Length);29            foreach (char c in value)30            {31                sb.Append(c);32                if (CharExtensions.IsSingleQuote(c))33                {34                    // double-up quotes to escape them35                    sb.Append(c);36                }37            }38 39            return sb.ToString();40        }41 42        /// <summary>43        /// Escapes content so that it is safe for inclusion in a block comment.44        /// For example: "&lt;#" + EscapeBlockCommentContent(userContent) + "#&gt;"45        /// </summary>46        /// <param name="value">The content to be included in a block comment.</param>47        /// <returns>Content with all block comment characters escaped.</returns>48        public static string EscapeBlockCommentContent(string value)49        {50            if (string.IsNullOrEmpty(value))51            {52                return string.Empty;53            }54 55            return value56                .Replace("<#", "<`#")57                .Replace("#>", "#`>");58        }59 60        /// <summary>61        /// Escapes content so that it is safe for inclusion in a string that will later be used as a62        /// format string. If this is to be embedded inside of a single-quoted string, be sure to also63        /// call EscapeSingleQuotedStringContent.64        /// For example: "'" + EscapeSingleQuotedStringContent(EscapeFormatStringContent(userContent)) + "'" -f $args.65        /// </summary>66        /// <param name="value">The content to be included in a format string.</param>67        /// <returns>Content with all curly braces escaped.</returns>68        public static string EscapeFormatStringContent(string value)69        {70            if (string.IsNullOrEmpty(value))71            {72                return string.Empty;73            }74 75            StringBuilder sb = new StringBuilder(value.Length);76            foreach (char c in value)77            {78                sb.Append(c);79                if (CharExtensions.IsCurlyBracket(c))80                {81                    // double-up curly brackets to escape them82                    sb.Append(c);83                }84            }85 86            return sb.ToString();87        }88 89        /// <summary>90        /// Escapes content so that it is safe for inclusion in a string that will later be used in a variable91        /// name reference. This is only valid when used within PowerShell's curly brace naming syntax.92        ///93        /// For example: '${' + EscapeVariableName('value') + '}'94        /// </summary>95        /// <param name="value">The content to be included as a variable name.</param>96        /// <returns>Content with all curly braces and back-ticks escaped.</returns>97        public static string EscapeVariableName(string value)98        {99            if (string.IsNullOrEmpty(value))100            {101                return string.Empty;102            }103 104            return value105                .Replace("`", "``")106                .Replace("}", "`}")107                .Replace("{", "`{");108        }109    }110}111