CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
Utils.cs
Go to the documentation of this file.
1 using System.IO;
2 using System.Linq;
3 using System.Net;
4 
5 namespace CodeGen.utils
6 {
10  public static class Utils
11  {
18  public static string Read(string name)
19  {
20  if (File.Exists(name))
21  {
22  return File.ReadAllText(name);
23  }
24 
25  throw new FileNotFoundException("file does not exit");
26  }
27 
33  public static void Write(string path, string fileContext)
34  {
35  File.WriteAllText(path, fileContext);
36  }
37 
43  public static string Download(string url)
44  {
45  var webClient = new WebClient();
46  return webClient.DownloadString(url);
47  }
48 
55  public static void ValidateArgs(string lang, string file)
56  {
57  if (lang == "")
58  {
59  throw new InvalidDataException("specify language (-l) flag");
60  }
61 
62  if (file == "")
63  {
64  throw new InvalidDataException("specify file path (-f) flag");
65  }
66  }
67 
74  public static string GetFileFormat(string name)
75  {
76  var arr = name.Split('.');
77  if (!(arr?.Length > 1)) throw new InvalidDataException("invalid input file");
78  if (arr.Last().Any(char.IsLetter))
79  {
80  return arr.Last();
81  }
82 
83  throw new InvalidDataException("invalid input file");
84  }
85 
91  public static string Title(string @string)
92  {
93  return string.IsNullOrEmpty(@string)
94  ? @string
95  : (
96  @string.Length > 1
97  ? char.ToUpper(@string[0]) + @string.Substring(1)
98  : @string.ToUpper()
99  );
100  }
101  }
102 }