Skip to content

Commit d447cb2

Browse files
authored
Merge pull request #2460 from Shenniey/copyOfMain
Adding logic to migrate to new javascript project experience
2 parents 954f6ea + 50b82d6 commit d447cb2

32 files changed

+1440
-18
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Xml;
6+
using System.Xml.Serialization;
7+
8+
namespace MigrateToJsps
9+
{
10+
[XmlRoot(ElementName = "Project")]
11+
public class EsprojFile
12+
{
13+
[XmlAttribute(AttributeName = "Sdk")]
14+
public string Sdk = @"Microsoft.VisualStudio.JavaScript.Sdk/0.5.85-alpha";
15+
16+
[XmlElement(ElementName = "PropertyGroup")]
17+
public EsprojPropertyGroup PropertyGroup { get; set; }
18+
}
19+
20+
[XmlRoot(ElementName = "PropertyGroup")]
21+
public class EsprojPropertyGroup
22+
{
23+
[XmlAnyElement("BuildCommandComment")]
24+
public XmlComment BuildCommandComment = new XmlDocument().CreateComment("Command to run on project build");
25+
26+
[XmlElement(ElementName = "BuildCommand", IsNullable = false)]
27+
public string BuildCommand;
28+
29+
[XmlAnyElement("CleanCommandComment")]
30+
public XmlComment CleanCommandComment = new XmlDocument().CreateComment("Command to run on project clean");
31+
32+
[XmlElement(ElementName = "CleanCommand", IsNullable = false)]
33+
public string CleanCommand;
34+
35+
[XmlElement(ElementName = "StartupCommand", IsNullable = false)]
36+
public string StartupCommand;
37+
}
38+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.IO;
2+
3+
namespace MigrateToJsps
4+
{
5+
internal class JspsProjectCreator
6+
{
7+
private string projectDir;
8+
private NjsprojFileModel njsprojFileModel;
9+
private Logger logger;
10+
11+
public JspsProjectCreator(string projectDir, NjsprojFileModel njsprojFileModel, Logger logger)
12+
{
13+
this.projectDir = projectDir;
14+
this.njsprojFileModel = njsprojFileModel;
15+
16+
this.logger = logger;
17+
}
18+
19+
public string CreateJspsProject() // to be called by outsiders
20+
{
21+
LaunchJsonWriter.CreateLaunchJson(projectDir, njsprojFileModel, this.logger);
22+
23+
NugetConfigWriter.GenerateNugetConfig(projectDir, this.logger);
24+
25+
var port = njsprojFileModel.NodejsPort;
26+
27+
return EsprojFileWriter.WriteEsproj(projectDir, njsprojFileModel.ProjectName, njsprojFileModel.StartupFile, port, this.logger);
28+
}
29+
}
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
9+
namespace MigrateToJsps
10+
{
11+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
12+
internal class Compound
13+
{
14+
/// <summary>
15+
/// The friendly name of the compound configuration
16+
/// </summary>
17+
[JsonProperty("name")]
18+
public string Name { get; set; }
19+
20+
/// <summary>
21+
/// A string array containing the friendly names of the launch configs that this compound will start
22+
/// </summary>
23+
[JsonProperty("configurations")]
24+
public string[] Configurations { get; set; }
25+
26+
/// <summary>
27+
/// Indicates whether terminating one session will terminate all debugging sessions
28+
/// </summary>
29+
[JsonProperty("stopAll")]
30+
public bool? StopAll { get; set; }
31+
32+
/// <summary>
33+
/// Task to run before any of the compound configurations start
34+
/// </summary>
35+
[JsonProperty("preLaunchTask")]
36+
public string PreLaunchTask { get; set; }
37+
38+
public string ToJsonString() => JsonConvert.SerializeObject(this, Formatting.Indented);
39+
40+
public bool IsEqualToJson(JToken jsonToCompare)
41+
{
42+
JToken config = JToken.FromObject(this);
43+
return JToken.DeepEquals(config, jsonToCompare);
44+
}
45+
}
46+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json.Linq;
7+
using Newtonsoft.Json;
8+
9+
namespace MigrateToJsps
10+
{
11+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
12+
internal class Configuration
13+
{
14+
/// <summary>
15+
/// The friendly name of the launch configuration
16+
/// </summary>
17+
[JsonProperty("name")]
18+
public string Name { get; set; }
19+
20+
/// <summary>
21+
/// The type of configuration (chrome, edge, or node)
22+
/// </summary>
23+
[JsonProperty("type")]
24+
public string Type { get; set; }
25+
26+
/// <summary>
27+
/// The debug request type ("launch" or "attach")
28+
/// </summary>
29+
[JsonProperty("request")]
30+
public string Request { get; set; }
31+
32+
/// <summary>
33+
/// The url to open a browser to
34+
/// </summary>
35+
[JsonProperty("url")]
36+
public string Url { get; set; }
37+
38+
/// <summary>
39+
/// User data directory to use when launching browser
40+
/// </summary>
41+
[JsonProperty("userDataDir")]
42+
public object UserDataDir { get; set; }
43+
44+
/// <summary>
45+
/// This specifies the workspace absolute path to the webserver root. Used to resolve paths like `/app.js` to files on disk. Shorthand for a pathMapping for "/"
46+
/// </summary>
47+
[JsonProperty("webRoot")]
48+
public string WebRoot { get; set; }
49+
50+
/// <summary>
51+
/// Optional current working directory for the runtime executable.
52+
/// </summary>
53+
[JsonProperty("cwd")]
54+
public string Cwd { get; set; }
55+
56+
/// <summary>
57+
/// This specifies the workspace absolute path to the program being debugged (currently only applies to Node launch configurations)
58+
/// </summary>
59+
[JsonProperty("program")]
60+
public string Program { get; set; }
61+
62+
/// <summary>
63+
/// Tells the debugger which files to skip over
64+
/// </summary>
65+
[JsonProperty("skipFiles")]
66+
public string[] Skipfiles { get; set; }
67+
68+
/// <summary>
69+
/// Automatically stops the progam after launch (only applies to Node launch configurations)
70+
/// </summary>
71+
[JsonProperty("stopOnEntry")]
72+
public bool? StopOnEntry { get; set; }
73+
74+
/// <summary>
75+
/// Specifies which console -- only used for Node
76+
/// </summary>
77+
[JsonProperty("console")]
78+
public string Console { get; set; }
79+
80+
/// <summary>
81+
/// Environment variables passed to the program
82+
/// </summary>
83+
[JsonProperty("env")]
84+
public JObject Env { get; set; }
85+
86+
/// <summary>
87+
/// Other configuration properties which aren't explicitly listed in this class
88+
/// </summary>
89+
[JsonExtensionData]
90+
public Dictionary<string, JToken> ConfigurationProperties { get; private set; }
91+
92+
public string ToJsonString() => JsonConvert.SerializeObject(this, Formatting.Indented);
93+
94+
public bool IsEqualToJson(JToken jsonToCompare)
95+
{
96+
JToken config = JToken.FromObject(this);
97+
return JToken.DeepEquals(config, jsonToCompare);
98+
}
99+
}
100+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System.Collections.Generic;
4+
using System.Configuration;
5+
6+
namespace MigrateToJsps
7+
{
8+
[JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)]
9+
internal class LaunchJson
10+
{
11+
/// <summary>
12+
/// The friendly name of the launch configuration
13+
/// </summary>
14+
[JsonProperty("version")]
15+
public string Version = "0.2.0";
16+
17+
/// <summary>
18+
/// The type of configuration (chrome, edge, or node)
19+
/// </summary>
20+
[JsonProperty("configurations")]
21+
public Configuration[] Configurations { get; set; }
22+
23+
/// <summary>
24+
/// The debug request type ("launch" or "attach")
25+
/// </summary>
26+
[JsonProperty("compounds")]
27+
public Compound[] Compounds { get; set; }
28+
29+
/// <summary>
30+
/// Other launch.json properties which aren't explicitly listed in this class
31+
/// </summary>
32+
[JsonExtensionData]
33+
public Dictionary<string, JToken> LaunchJsonProperties { get; private set; }
34+
35+
public string ToJsonString()
36+
{
37+
return JsonConvert.SerializeObject(this, Formatting.Indented);
38+
}
39+
}
40+
41+
}

0 commit comments

Comments
 (0)