1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| public class RenameProgram { protected string InPath { get; set; } protected string OutPath { get; set; } protected string CompanyName { get; set; } protected string ProjectName { get; set; } protected string FullName { get; set; }
private static readonly string _includeFiles = ".original,.yml,.yaml,.cs,.cshtml,.js,.ts,.csproj,.sln,.xml,.config,.dotsettings,.json,.txt,.html,.gitignore,.ps1,.xaml,.plist,.local,.tpl,dockerfile";
public RenameProgram(string inPath, string outPath, string projectName, string companyName = null) { InPath = inPath; OutPath = outPath; ProjectName = projectName; CompanyName = companyName; FullName = string.Format("{1}{0}", projectName, !string.IsNullOrWhiteSpace(companyName) ? companyName + "." : ""); }
private bool isSkip(string file) { if (string.IsNullOrWhiteSpace(file)) return true; var skipFiles = _includeFiles.Split(','); return !skipFiles.Contains(file.ToLower()); }
private string replaceData(string data, string extension = null) { if (string.IsNullOrWhiteSpace(data)) return data; data = data.Replace(InPath, OutPath); data = data.Replace("EShopOnAbp.", FullName + ".", StringComparison.Ordinal); data = data.Replace("namespace EShopOnAbp", "namespace " + FullName, StringComparison.Ordinal); data = data.Replace("EShopOnAbp", ProjectName, StringComparison.Ordinal);
if (!string.IsNullOrWhiteSpace(extension) && extension == ".js") { var tempName = ProjectName.Substring(0, 1).ToLower() + ProjectName.Substring(1); data = data.Replace("eShopOnAbp", tempName, StringComparison.Ordinal); } else { data = data.Replace("eShopOnAbp", ProjectName, StringComparison.Ordinal); }
data = data.Replace("eshoponabp.", FullName.ToLower() + ".", StringComparison.Ordinal); data = data.Replace("eshoponabp", ProjectName.ToLower(), StringComparison.Ordinal); return data; }
private void Rename(string path) { var outputPath = replaceData(path); if (!Directory.Exists(outputPath)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("find: " + outputPath); Directory.CreateDirectory(outputPath); }
var files = Directory.GetFiles(path); foreach (var file in files) { var fileInfo = new FileInfo(file); var outputFile = replaceData(file); var data = File.ReadAllText(file); var extension = string.IsNullOrWhiteSpace(fileInfo.Extension) ? fileInfo.Name : fileInfo.Extension; if (string.IsNullOrWhiteSpace(data) || !data.Contains("EShopOnAbp", StringComparison.OrdinalIgnoreCase) || isSkip(extension)) { File.Copy(file, outputFile); } else { var outputData = replaceData(data, extension); File.WriteAllText(outputFile, outputData); } }
var directories = Directory.GetDirectories(path); foreach (var directory in directories) { if (directory.EndsWith(".git") || directory.EndsWith(".vs") || directory.EndsWith("bin") || directory.EndsWith("obj") || directory.EndsWith("node_modules")) { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("skip: " + directory); continue; } Rename(directory); } }
public void Rename() { Rename(InPath); } }
|