forked from microsoft/TemplateStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
201 lines (169 loc) · 7.89 KB
/
App.xaml.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using CommandLine;
using CommandLine.Text;
using Microsoft.Templates.Core;
using Microsoft.Templates.Core.Diagnostics;
using Microsoft.Templates.Core.Gen;
using Microsoft.Templates.Core.Locations;
using Microsoft.Templates.Fakes;
using Microsoft.Templates.Fakes.GenShell;
using Microsoft.Templates.UI.Launcher;
using Microsoft.Templates.UI.Threading;
using Microsoft.Templates.VsEmulator.Services;
using Microsoft.VisualStudio.TemplateWizard;
namespace Microsoft.Templates.VsEmulator
{
public partial class App : Application
{
public App()
{
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (e.Args.Any())
{
var parserResult = Parser.Default.ParseArguments<CommandLineOptions>(e.Args);
var exitCode = parserResult
.MapResult(
(CommandLineOptions options) => { return LaunchWizardFromCommandLineForAutomatedTesting(options); },
errors => { return ShowErrorMessage(parserResult); });
Application.Current.Shutdown(exitCode);
}
}
private int ShowErrorMessage(ParserResult<CommandLineOptions> parserResult)
{
try
{
// Ensure there's a console window available to display output
if (!NativeMethods.AttachConsole(-1))
{
NativeMethods.AllocConsole();
}
var helpText = HelpText.AutoBuild(parserResult);
Console.Write(helpText);
}
finally
{
NativeMethods.FreeConsole();
}
return -1;
}
private int LaunchWizardFromCommandLineForAutomatedTesting(CommandLineOptions options)
{
var exitCode = 0;
SafeThreading.JoinableTaskFactory.Run(async () =>
{
await SafeThreading.JoinableTaskFactory.SwitchToMainThreadAsync();
try
{
var cultureArg = options.Culture;
if (!string.IsNullOrWhiteSpace(cultureArg))
{
var culture = new CultureInfo(cultureArg);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
var progLanguage = options.ProgLang;
var newProjectName = string.IsNullOrWhiteSpace(options.ProjectName)
? Path.GetFileName(Path.GetTempFileName().Replace(".", string.Empty))
: options.ProjectName;
GenContext.Bootstrap(
new LocalTemplatesSource(string.Empty, "0.0.0.0", string.Empty),
new FakeGenShell(Platforms.Uwp, progLanguage),
"0.0.0.0",
Platforms.Uwp,
progLanguage,
"0.1.8.9");
await GenContext.ToolBox.Repo.RefreshAsync();
GenContext.SetCurrentLanguage(progLanguage);
var fakeShell = GenContext.ToolBox.Shell as FakeGenShell;
fakeShell?.SetCurrentLanguage(progLanguage);
var newProjectLocation = Path.Combine(Path.GetTempPath(), "UiTest");
var projectPath = Path.Combine(newProjectLocation, newProjectName, newProjectName);
GenContext.Current = new FakeContextProvider
{
ProjectName = newProjectName,
DestinationPath = projectPath,
GenerationOutputPath = Path.Combine(Path.GetTempPath(), newProjectName, newProjectName),
};
// Set resources to be used for the UI
FakeStyleValuesProvider.Instance.LoadResources("Light");
switch (options.UI.ToUpperInvariant())
{
case "PAGE":
EnableRightClickSupportForProject(projectPath, progLanguage);
var userPageSelection = WizardLauncher.Instance.StartAddTemplate(GenContext.CurrentLanguage, FakeStyleValuesProvider.Instance, TemplateType.Page, WizardTypeEnum.AddPage);
break;
case "FEATURE":
EnableRightClickSupportForProject(projectPath, progLanguage);
var userFeatureSelection = WizardLauncher.Instance.StartAddTemplate(GenContext.CurrentLanguage, FakeStyleValuesProvider.Instance, TemplateType.Feature, WizardTypeEnum.AddFeature);
break;
case "PROJECT":
default:
var context = new UserSelectionContext(progLanguage, Platforms.Uwp);
var userSelectionIsNotUsed = WizardLauncher.Instance.StartNewProject(context, string.Empty, string.Empty, FakeStyleValuesProvider.Instance);
break;
}
}
catch (WizardBackoutException)
{
// Get this if cancel out of the wizard
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error");
exitCode = exception.HResult;
}
});
return exitCode;
}
private void EnableRightClickSupportForProject(string projectPath, string progLanguage = null)
{
Directory.CreateDirectory(projectPath);
// Add a manifest file with enough info for the wizard to function
var fakeAppxManifest = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Package
xmlns=""http://schemas.microsoft.com/appx/manifest/foundation/windows10""
xmlns:mp=""http://schemas.microsoft.com/appx/2014/phone/manifest""
xmlns:uap=""http://schemas.microsoft.com/appx/manifest/uap/windows10""
xmlns:genTemplate=""http://schemas.microsoft.com/appx/developer/templatestudio""
IgnorableNamespaces=""uap mp genTemplate"">
<genTemplate:Metadata>
<genTemplate:Item Name=""generator"" Value=""Template Studio""/>
<genTemplate:Item Name=""wizardVersion"" Version=""v0.0.0.0"" />
<genTemplate:Item Name=""templatesVersion"" Version=""v0.0.0.0"" />
<genTemplate:Item Name=""platform"" Value=""Uwp"" />
<genTemplate:Item Name=""projectType"" Value=""Blank"" />
<genTemplate:Item Name=""framework"" Value=""CodeBehind"" />
</genTemplate:Metadata>
</Package>
";
File.WriteAllText(Path.Combine(projectPath, "package.appxmanifest"), fakeAppxManifest, Encoding.UTF8);
if (!string.IsNullOrWhiteSpace(progLanguage))
{
switch (progLanguage)
{
case ProgrammingLanguages.CSharp:
File.WriteAllText(Path.Combine(projectPath, ".csproj"), "Placeholder for C# project file.");
break;
case ProgrammingLanguages.VisualBasic:
File.WriteAllText(Path.Combine(projectPath, ".vbproj"), "Placeholder for VB.Net project file.");
break;
case ProgrammingLanguages.Cpp:
File.WriteAllText(Path.Combine(projectPath, ".vcxproj"), "Placeholder for C++ project file.");
break;
}
}
}
}
}