Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Program.cs190 linesDownload Raw Back to ResGen
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.IO;7using System.Text;8using System.Xml.Linq;9 10namespace ConsoleApplication11{12    public class Program13    {14        public static void Main(string[] args)15        {16            IEnumerable<string> dirs;17            string fileFilter;18 19            if (args.Length == 1)20            {21                // We are assuming resgen is run with 'dotnet run pathToResxFile.resx'.22                fileFilter = Path.GetFileName(args[0]);23                string moduleDirectory = Path.GetDirectoryName(Path.GetDirectoryName(args[0]));24                dirs = new List<string>() { moduleDirectory };25            }26            else27            {28                // We are assuming resgen is run with 'dotnet run'29                // so we can use relative path to get a parent directory30                // to process all *.resx files in all project subdirectories.31                fileFilter = "*.resx";32                dirs = Directory.EnumerateDirectories("..");33            }34 35            foreach (string folder in dirs)36            {37                string moduleName = Path.GetFileName(folder);38                string resourcePath = Path.Combine(folder, "resources");39 40                if (Directory.Exists(resourcePath))41                {42                    string genFolder = Path.Combine(folder, "gen");43                    if (!Directory.Exists(genFolder))44                    {45                        Directory.CreateDirectory(genFolder);46                    }47 48                    foreach (string resxPath in Directory.EnumerateFiles(resourcePath, fileFilter))49                    {50                        string accessModifier = "internal";51                        string className = Path.GetFileNameWithoutExtension(resxPath);52                        if (className.StartsWith("public.", StringComparison.InvariantCultureIgnoreCase))53                        {54                            accessModifier = "public";55                            className = className.Substring(className.IndexOf('.') + 1);56                        }57 58                        string sourceCode = GetStronglyTypeCsFileForResx(resxPath, moduleName, className, accessModifier);59                        string outPath = Path.Combine(genFolder, className + ".cs");60                        Console.WriteLine("ResGen for " + outPath);61                        File.WriteAllText(outPath, sourceCode);62                    }63                }64            }65        }66 67        private static string GetStronglyTypeCsFileForResx(string xmlPath, string moduleName, string className, string accessModifier)68        {69            // Example70            //71            // className = Full.Name.Of.The.ClassFoo72            // shortClassName = ClassFoo73            // namespaceName = Full.Name.Of.The74            string shortClassName = className;75            string namespaceName = null;76            int lastIndexOfDot = className.LastIndexOf('.');77            if (lastIndexOfDot != -1)78            {79                namespaceName = className.Substring(0, lastIndexOfDot);80                shortClassName = className.Substring(lastIndexOfDot + 1);81            }82 83            var entries = new StringBuilder();84            XElement root = XElement.Parse(File.ReadAllText(xmlPath));85            foreach (var data in root.Elements("data"))86            {87                string value = data.Value.Replace("\n", "\n    ///");88                string name = data.Attribute("name").Value.Replace(' ', '_');89                entries.AppendFormat(ENTRY, name, value, accessModifier);90            }91 92            string bodyCode = string.Format(BODY, shortClassName, moduleName, entries.ToString(), className, accessModifier, accessModifier.Equals("public", StringComparison.InvariantCultureIgnoreCase) ? "public." : string.Empty);93            if (namespaceName != null)94            {95                bodyCode = string.Format(NAMESPACE, namespaceName, bodyCode);96            }97 98            string resultCode = string.Format(BANNER, bodyCode).Replace("\r\n?|\n", "\r\n");99            return resultCode;100        }101 102        private static readonly string BANNER = @"103//------------------------------------------------------------------------------104// <auto-generated>105//     This code was generated by a dotnet run from src\ResGen folder.106//     To add or remove a member, edit your .resx file then rerun src\ResGen.107//108//     Changes to this file may cause incorrect behavior and will be lost if109//     the code is regenerated.110// </auto-generated>111//------------------------------------------------------------------------------112 113{0}114";115 116        private static readonly string NAMESPACE = @"117namespace {0} {{118{1}119}}120";121 122        private static readonly string BODY = @"123using System;124using System.Reflection;125 126/// <summary>127///   A strongly-typed resource class, for looking up localized strings, etc.128/// </summary>129[global::System.CodeDom.Compiler.GeneratedCodeAttribute(""System.Resources.Tools.StronglyTypedResourceBuilder"", ""4.0.0.0"")]130[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]131[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]132 133{4} class {0} {{134 135    private static global::System.Resources.ResourceManager resourceMan;136 137    private static global::System.Globalization.CultureInfo resourceCulture;138 139    /// <summary>constructor</summary>140    [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute(""Microsoft.Performance"", ""CA1811:AvoidUncalledPrivateCode"")]141    {4} {0}() {{142    }}143 144    /// <summary>145    ///   Returns the cached ResourceManager instance used by this class.146    /// </summary>147    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]148    {4} static global::System.Resources.ResourceManager ResourceManager {{149        get {{150            if (resourceMan is null) {{151                global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager(""{1}.resources.{5}{3}"", typeof({0}).Assembly);152                resourceMan = temp;153            }}154 155            return resourceMan;156        }}157    }}158 159    /// <summary>160    ///   Overrides the current threads CurrentUICulture property for all161    ///   resource lookups using this strongly typed resource class.162    /// </summary>163    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]164    {4} static global::System.Globalization.CultureInfo Culture {{165        get {{166            return resourceCulture;167        }}168 169        set {{170            resourceCulture = value;171        }}172    }}173    {2}174}}175";176 177    private static readonly string ENTRY = @"178 179    /// <summary>180    ///   Looks up a localized string similar to {1}181    /// </summary>182    {2} static string {0} {{183        get {{184            return ResourceManager.GetString(""{0}"", resourceCulture);185        }}186    }}187";188    }189}190