|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Xml; |
| 8 | +using System.Xml.Serialization; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | + |
| 12 | +namespace MigrateToJsps |
| 13 | +{ |
| 14 | + internal static class EsprojFileWriter |
| 15 | + { |
| 16 | + public static string WriteEsproj(string projectDir, string projectName, string startupFile, string port, Logger logger) |
| 17 | + { |
| 18 | + var fileName = projectName + ".esproj"; |
| 19 | + var filePath = Path.Combine(projectDir, fileName); |
| 20 | + |
| 21 | + EsprojPropertyGroup propGroup = new EsprojPropertyGroup(); |
| 22 | + |
| 23 | + var pkgJson = ReadPackageJson(projectDir); |
| 24 | + if (pkgJson != null) |
| 25 | + { |
| 26 | + if (StartCmdExistsInPkgJson(pkgJson)) { |
| 27 | + var startupCmd = "npm run start"; |
| 28 | + |
| 29 | + if (!string.IsNullOrEmpty(port)) |
| 30 | + { |
| 31 | + startupCmd = $"set PORT={port} & " + startupCmd; |
| 32 | + } |
| 33 | + propGroup.StartupCommand = startupCmd; |
| 34 | + } |
| 35 | + |
| 36 | + propGroup.BuildCommand = BuildCmdExistsInPkgJson(pkgJson) ? "npm run build" : " "; |
| 37 | + |
| 38 | + propGroup.CleanCommand = CleanCmdExistsInPkgJson(pkgJson) ? "npm run clean" : " "; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + propGroup.StartupCommand = "node " + startupFile; |
| 43 | + } |
| 44 | + |
| 45 | + EsprojFile esprojFile = new EsprojFile() { PropertyGroup = propGroup }; |
| 46 | + |
| 47 | + XmlSerializer serializer = new XmlSerializer(typeof(EsprojFile)); |
| 48 | + |
| 49 | + XmlWriterSettings settings = new XmlWriterSettings(); |
| 50 | + settings.Indent = true; |
| 51 | + settings.OmitXmlDeclaration = true; |
| 52 | + |
| 53 | + XmlSerializerNamespaces emptyNamespaces = new XmlSerializerNamespaces(); |
| 54 | + emptyNamespaces.Add("", ""); |
| 55 | + |
| 56 | + using (XmlWriter writer = XmlWriter.Create(filePath, settings)) |
| 57 | + { |
| 58 | + serializer.Serialize(writer, esprojFile, emptyNamespaces); |
| 59 | + } |
| 60 | + |
| 61 | + logger.AddFile(filePath); |
| 62 | + |
| 63 | + return filePath; |
| 64 | + } |
| 65 | + |
| 66 | + private static bool StartCmdExistsInPkgJson(JObject packageJson) |
| 67 | + { |
| 68 | + if (packageJson.ContainsKey("scripts")) |
| 69 | + { |
| 70 | + return packageJson["scripts"]["start"] != null; |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + private static bool BuildCmdExistsInPkgJson(JObject packageJson) |
| 77 | + { |
| 78 | + if (packageJson.ContainsKey("scripts")) |
| 79 | + { |
| 80 | + return packageJson["scripts"]["build"] != null; |
| 81 | + } |
| 82 | + |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + private static bool CleanCmdExistsInPkgJson(JObject packageJson) |
| 87 | + { |
| 88 | + if (packageJson.ContainsKey("scripts")) |
| 89 | + { |
| 90 | + return packageJson["scripts"]["clean"] != null; |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + private static JObject ReadPackageJson(string projectDir) |
| 97 | + { |
| 98 | + string filePath = Path.Combine(projectDir, "package.json"); |
| 99 | + if (File.Exists(filePath)) |
| 100 | + { |
| 101 | + string packageJsonContent = File.ReadAllText(filePath); |
| 102 | + |
| 103 | + JObject packageJson = JsonConvert.DeserializeObject<JObject>(packageJsonContent); |
| 104 | + |
| 105 | + return packageJson; |
| 106 | + } |
| 107 | + |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + private static string GetSdkVersion() |
| 112 | + { |
| 113 | + // TODO: figure out if sdk is installed on machine? |
| 114 | + // do i need to do that or will VS automatically install given version |
| 115 | + throw new NotImplementedException(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments