-
Notifications
You must be signed in to change notification settings - Fork 214
/
MsBuildProjectSetupHelper.cs
149 lines (129 loc) · 6.8 KB
/
MsBuildProjectSetupHelper.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.DependencyModel;
using Xunit.Abstractions;
namespace Microsoft.VisualStudio.Web.CodeGeneration
{
internal class MsBuildProjectSetupHelper
{
#if RELEASE
public const string Configuration = "Release";
#else
public const string Configuration = "Debug";
#endif
private static object _syncObj = new object();
private static string artifactsDir;
private static string ArtifactsDirectory
{
get
{
if (string.IsNullOrEmpty(artifactsDir))
{
lock(_syncObj)
{
if (string.IsNullOrEmpty(artifactsDir))
{
var current = new DirectoryInfo(AppContext.BaseDirectory);
while (current != null)
{
if (File.Exists(Path.Combine(current.FullName, "Scaffolding.sln")))
{
artifactsDir = Path.Combine(current.FullName, "artifacts", "build");
break;
}
current = current.Parent;
}
}
}
}
return artifactsDir;
}
}
private string AspNetCoreVersion
{
get
{
var aspnetCoreLib = DependencyContext
.Default
.CompileLibraries
.FirstOrDefault(l => l.Name.StartsWith("Microsoft.Extensions.FileProviders.Abstractions", StringComparison.OrdinalIgnoreCase));
return aspnetCoreLib?.Version;
}
}
private string RuntimeFrameworkVersion =>
DependencyContext
.Default
.RuntimeLibraries
.FirstOrDefault(l => l.Name.StartsWith("Microsoft.NETCore.App", StringComparison.OrdinalIgnoreCase))
?.Version;
private string CodeGenerationVersion
{
get
{
var informationalVersionAttr = this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).First();
return ((AssemblyInformationalVersionAttribute)informationalVersionAttr).InformationalVersion;
}
}
public void SetupProjects(TemporaryFileProvider fileProvider, ITestOutputHelper output, bool fullFramework = false)
{
Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Root"));
Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Library1"));
var propsFileText = string.Format( MsBuildProjectStrings.PropsFileTxt, "");
fileProvider.Add("Shared.props", propsFileText);
var rootProjectTxt = fullFramework ? MsBuildProjectStrings.RootNet45ProjectTxt : MsBuildProjectStrings.RootProjectTxt;
fileProvider.Add($"Root/{MsBuildProjectStrings.RootProjectName}", string.Format(rootProjectTxt, AspNetCoreVersion, CodeGenerationVersion, RuntimeFrameworkVersion, "2.0.1-*"));
fileProvider.Add($"Root/Startup.cs", MsBuildProjectStrings.StartupTxt);
fileProvider.Add($"Root/{MsBuildProjectStrings.ProgramFileName}", MsBuildProjectStrings.ProgramFileText);
fileProvider.Add($"Library1/{MsBuildProjectStrings.LibraryProjectName}", MsBuildProjectStrings.LibraryProjectTxt);
fileProvider.Add($"Library1/ModelWithMatchingShortName.cs", "namespace Library1.Models { public class ModelWithMatchingShortName { } }");
fileProvider.Add($"Library1/Car.cs", MsBuildProjectStrings.CarTxt);
fileProvider.Add($"Library1/Product.cs", MsBuildProjectStrings.ProductTxt);
RestoreAndBuild(Path.Combine(fileProvider.Root, "Root"), output);
}
private void RestoreAndBuild(string path, ITestOutputHelper output)
{
var result = Command.CreateDotNet("restore",
new string[] {})
.WithEnvironmentVariable("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true")
.InWorkingDirectory(path)
.OnErrorLine(l => output.WriteLine(l))
.OnOutputLine(l => output.WriteLine(l))
.Execute();
if (result.ExitCode !=0)
{
throw new InvalidOperationException($"Restore failed with exit code: {result.ExitCode} :: Dotnet path: {DotNetMuxer.MuxerPathOrDefault()}");
}
result = Command.CreateDotNet("build", new string[] {"-c", Configuration})
.WithEnvironmentVariable("DOTNET_SKIP_FIRST_TIME_EXPERIENCE", "true")
.InWorkingDirectory(path)
.OnErrorLine(l => output.WriteLine(l))
.OnOutputLine(l => output.WriteLine(l))
.Execute();
if (result.ExitCode !=0)
{
throw new InvalidOperationException($"Build failed with exit code: {result.ExitCode}");
}
}
public void SetupProjectsWithoutEF(TemporaryFileProvider fileProvider, ITestOutputHelper output)
{
Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Root"));
Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Library1"));
var propsFileText = string.Format( MsBuildProjectStrings.PropsFileTxt, "");
fileProvider.Add("Shared.props", propsFileText);
var rootProjectTxt = MsBuildProjectStrings.RootProjectTxtWithoutEF;
fileProvider.Add($"Root/{MsBuildProjectStrings.RootProjectName}", string.Format(rootProjectTxt, AspNetCoreVersion, CodeGenerationVersion, RuntimeFrameworkVersion, "2.0.1-*"));
fileProvider.Add($"Root/Startup.cs", MsBuildProjectStrings.StartupTxtWithoutEf);
fileProvider.Add($"Root/{MsBuildProjectStrings.ProgramFileName}", MsBuildProjectStrings.ProgramFileText);
fileProvider.Add($"Library1/{MsBuildProjectStrings.LibraryProjectName}", MsBuildProjectStrings.LibraryProjectTxt);
fileProvider.Add($"Library1/ModelWithMatchingShortName.cs", "namespace Library1.Models { public class ModelWithMatchingShortName { } }");
fileProvider.Add($"Library1/Car.cs", MsBuildProjectStrings.CarTxt);
fileProvider.Add($"Library1/Product.cs", MsBuildProjectStrings.ProductTxt);
RestoreAndBuild(Path.Combine(fileProvider.Root, "Root"), output);
}
}
}