11
11
using Microsoft . Build . Execution ;
12
12
using Microsoft . Build . Framework ;
13
13
using Microsoft . Build . Logging . StructuredLogger ;
14
+ using NuGet . ProjectManagement ;
14
15
using Xunit ;
15
16
using Xunit . Abstractions ;
16
17
using Xunit . Sdk ;
21
22
static partial class Builder
22
23
{
23
24
public static TargetResult BuildProject (
24
- string projectContent = null ,
25
+ string projectContent ,
26
+ string target = "GetPackageContents" ,
27
+ ITestOutputHelper output = null ,
28
+ LoggerVerbosity ? verbosity = null ,
29
+ params ( string name , string contents ) [ ] files )
30
+ {
31
+ return BuildProjects (
32
+ target : target ,
33
+ output : output ,
34
+ verbosity : verbosity ,
35
+ files : new [ ] { ( "scenario.csproj" , projectContent ) } . Concat ( files ) . ToArray ( ) ) ;
36
+ }
37
+
38
+ public static TargetResult BuildProjects (
25
39
string target = "GetPackageContents" ,
26
40
ITestOutputHelper output = null ,
27
41
LoggerVerbosity ? verbosity = null ,
@@ -31,22 +45,17 @@ public static TargetResult BuildProject(
31
45
32
46
// Combination of last write time for the test assembly + contents of the projects.
33
47
var hash = Base62 . Encode ( Math . Abs ( BitConverter . ToInt64 (
34
- sha . ComputeHash ( Encoding . UTF8 . GetBytes ( projectContent + string . Join ( "|" , files . Select ( f => f . contents ) ) ) ) , 0 ) ) ) ;
48
+ sha . ComputeHash ( Encoding . UTF8 . GetBytes ( string . Join ( "|" , files . Select ( f => f . contents ) ) ) ) , 0 ) ) ) ;
35
49
36
50
var scenarioName = hash ;
37
51
var scenarioDir = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "Scenarios" , scenarioName ) ;
38
52
Directory . CreateDirectory ( scenarioDir ) ;
39
- var doc = XDocument . Parse ( projectContent ) ;
40
- doc . Root . AddFirst ( XElement
41
- . Parse ( "<Import Project='$([MSBuild]::GetPathOfFileAbove(Scenario.props, $(MSBuildThisFileDirectory)))' />" ) ) ;
42
-
43
- doc . Save ( Path . Combine ( scenarioDir , "scenario.csproj" ) ) ;
44
53
45
54
foreach ( var file in files )
46
55
{
47
56
try
48
57
{
49
- doc = XDocument . Parse ( file . contents ) ;
58
+ var doc = XDocument . Parse ( file . contents ) ;
50
59
doc . Root . AddFirst ( XElement
51
60
. Parse ( "<Import Project='$([MSBuild]::GetPathOfFileAbove(Scenario.props, $(MSBuildThisFileDirectory)))' />" ) ) ;
52
61
@@ -56,14 +65,15 @@ public static TargetResult BuildProject(
56
65
{
57
66
File . WriteAllText ( Path . Combine ( scenarioDir , file . name ) , file . contents ) ;
58
67
}
59
-
60
68
}
61
69
70
+ var main = files . First ( ) . name ;
71
+
62
72
using ( var disable = OpenBuildLogAttribute . Disable ( ) )
63
- BuildScenario ( scenarioName , projectName : "scenario" , target : "Restore" , output : output , verbosity : verbosity )
73
+ BuildScenario ( scenarioName , projectName : main , target : "Restore" , output : output , verbosity : verbosity )
64
74
. AssertSuccess ( output ) ;
65
75
66
- return BuildScenario ( scenarioName , projectName : "scenario" , target : target , output : output , verbosity : verbosity ) ;
76
+ return BuildScenario ( scenarioName , projectName : main , target : target , output : output , verbosity : verbosity ) ;
67
77
}
68
78
69
79
public static TargetResult BuildScenario (
@@ -75,22 +85,24 @@ public static TargetResult BuildScenario(
75
85
LoggerVerbosity ? verbosity = null )
76
86
{
77
87
var scenarioDir = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "Scenarios" , scenarioName ) ;
88
+ if ( projectName != null && ! Path . HasExtension ( projectName ) )
89
+ projectName = projectName + ".csproj" ;
78
90
79
91
if ( projectName == null )
80
92
{
81
93
projectName = scenarioName ;
82
94
if ( scenarioName . StartsWith ( "given" , StringComparison . OrdinalIgnoreCase ) )
83
95
projectName = string . Join ( "_" , scenarioName . Split ( '_' ) . Skip ( 2 ) ) ;
84
96
}
85
- else if ( ! File . Exists ( Path . Combine ( scenarioDir , $ " { projectName } .csproj" ) ) )
97
+ else if ( ! File . Exists ( Path . Combine ( scenarioDir , projectName ) ) )
86
98
{
87
99
throw new FileNotFoundException ( $ "Project '{ projectName } ' was not found under scenario path '{ scenarioDir } '.", projectName ) ;
88
100
}
89
101
90
102
string projectOrSolution ;
91
103
92
- if ( File . Exists ( Path . Combine ( scenarioDir , $ " { projectName } .csproj" ) ) )
93
- projectOrSolution = Path . Combine ( scenarioDir , $ " { projectName } .csproj" ) ;
104
+ if ( File . Exists ( Path . Combine ( scenarioDir , projectName ) ) )
105
+ projectOrSolution = Path . Combine ( scenarioDir , projectName ) ;
94
106
else
95
107
projectOrSolution = Directory . EnumerateFiles ( scenarioDir , "*.*proj" ) . FirstOrDefault ( ) ;
96
108
0 commit comments