Last active
May 18, 2019 06:32
-
-
Save alanwei43/c1ef32b8d2ddaebca6d3a013552ee75c to your computer and use it in GitHub Desktop.
Pretty Hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dotnet-script | |
using System.Runtime.CompilerServices; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path); | |
static readonly var HOST_PATH = Environment.OSVersion.Platform == PlatformID.Unix ? "/etc/hosts" : @"C:\Windows\System32\drivers\etc\hosts"; | |
public static async Task<IDictionary<String, List<String>>> GetPrettyHosts() | |
{ | |
var lines = await File.ReadAllLinesAsync(HOST_PATH); | |
var result = lines.ToList() | |
.Select(line => line.Trim()) | |
.Where(line => !line.StartsWith("#") && !String.IsNullOrWhiteSpace(line)) | |
.Select(line => Regex.Split(line, @"\s+")) | |
.Select(blocks => new KeyValuePair<String, List<String>>(blocks[0], blocks.Skip(1).Where(block => block[0] != '#').ToList())) | |
.Where(kvs => kvs.Value.Count() > 0) | |
.Aggregate(new SortedDictionary<String, List<String>>(), (prev, next) => | |
{ | |
List<String> domains = prev.GetValueOrDefault(next.Key, new List<String>()); | |
domains.AddRange(next.Value); | |
domains.Sort(); | |
prev[next.Key] = domains; | |
return prev; | |
}); | |
return result; | |
} | |
public static async Task<String> PrettyHostsToFile(String targetHostFileName = "hosts") | |
{ | |
var hosts = await GetPrettyHosts(); | |
var hostsData = String.Join(Environment.NewLine, hosts.Select(kv => $"{kv.Key} {String.Join(" ", kv.Value)}")); | |
var filePath = $"{GetScriptFolder()}{Path.DirectorySeparatorChar}{targetHostFileName}"; | |
await File.WriteAllTextAsync(filePath, hostsData); | |
return filePath; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env dotnet-script | |
using System.Runtime.CompilerServices; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
static readonly var HOST_PATH = Environment.OSVersion.Platform == PlatformID.Unix ? "/etc/hosts" : @"C:\Windows\System32\drivers\etc\hosts"; | |
public static String DomainCommentSub(String domain) | |
{ | |
String[] splitted = domain.Split("#"); | |
return splitted[0]; | |
} | |
public static async Task<IDictionary<String, List<String>>> GetPrettyHosts() | |
{ | |
var lines = await File.ReadAllLinesAsync(HOST_PATH); | |
var result = lines.ToList() | |
.Select(line => line.Trim()) | |
.Where(line => !line.StartsWith("#") && !String.IsNullOrWhiteSpace(line)) | |
.Select(line => Regex.Split(line, @"\s+").Distinct().ToList()) | |
.Select(blocks => new KeyValuePair<String, List<String>>(blocks[0], blocks.Skip(1).Where(block => block[0] != '#').Select(block => block.Split("#")[0]).ToList())) | |
.Where(kvs => kvs.Value.Count() > 0) | |
.Aggregate(new SortedDictionary<String, List<String>>(), (prev, next) => | |
{ | |
List<String> domains = prev.GetValueOrDefault(next.Key, new List<String>()); | |
domains.AddRange(next.Value); | |
domains.Sort(); | |
prev[next.Key] = domains; | |
return prev; | |
}); | |
return result; | |
} | |
public static async Task<String> PrettyHostsToFile(String targetHostFileName = "hosts") | |
{ | |
var hosts = await GetPrettyHosts(); | |
var hostsData = String.Join(Environment.NewLine, hosts.Select(kv => $"{kv.Key} {String.Join(" ", kv.Value)}")); | |
var filePath = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}{Path.DirectorySeparatorChar}{targetHostFileName}"; | |
await File.WriteAllTextAsync(filePath, hostsData); | |
return filePath; | |
} | |
var filePath = await PrettyHostsToFile(); | |
Console.WriteLine($"格式化后的host文件保存在: {filePath}"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Execute follow command:
This will save prettied hosts to your desktop.