-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectConfiguration.cs
More file actions
160 lines (125 loc) · 5.13 KB
/
ProjectConfiguration.cs
File metadata and controls
160 lines (125 loc) · 5.13 KB
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
// ----------------------------------------------------------------------
// <copyright file="ProjectConfiguration.cs" company="Xavier Solau">
// Copyright © 2021 Xavier Solau.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
// </copyright>
// ----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Construction;
using SoloX.CodeQuality.Test.Helpers.Solution.Exceptions;
namespace SoloX.CodeQuality.Test.Helpers.Solution.Impl
{
internal class ProjectConfiguration : IProjectConfiguration, IProjectFiles
{
private readonly SolutionBuilder solutionBuilder;
private readonly Action<IProjectConfiguration> configuration;
public ProjectConfiguration(
SolutionBuilder solutionBuilder,
string projectName,
string template,
string? framework,
Action<IProjectConfiguration> configuration)
{
this.solutionBuilder = solutionBuilder;
ProjectName = projectName;
Template = template;
Framework = framework;
ProjectFilePath = Path.Combine(projectName, $"{projectName}.csproj");
ProjectPath = projectName;
this.configuration = configuration;
}
/// <summary>
/// Project name.
/// </summary>
public string ProjectName { get; }
/// <summary>
/// Path to the project folder. Relative to the solution.
/// </summary>
public string ProjectPath { get; }
/// <summary>
/// Project file path. Relative to the solution.
/// </summary>
public string ProjectFilePath { get; }
/// <summary>
/// Project Template.
/// </summary>
public string Template { get; }
/// <summary>
/// Project Target Framework (default if null).
/// </summary>
public string? Framework { get; }
public void Build()
{
SolutionBuilder.DotnetCall<ProjectError>((out ProcessResult processResult) =>
DotnetHelper.New(this.solutionBuilder.SolutionPath, Template, Framework, ProjectName, out processResult)
);
this.configuration(this);
}
public IProjectConfiguration UseFiles(Action<IProjectFiles> files)
{
files(this);
return this;
}
public IProjectFiles Add(string source, string target, IEnumerable<(string key, string value)>? replaceItems = null)
{
CopyResourceFile(source, target, replaceItems);
return this;
}
public IProjectFiles AddContent(
string source,
string target,
string? copyToOutputDirectory = null,
IEnumerable<(string key, string value)>? replaceItems = null)
{
Add(source, target, replaceItems);
var projectRoot = ProjectRootElement.Open(Path.Combine(this.solutionBuilder.SolutionPath, ProjectFilePath));
var itemGroup = projectRoot.AddItemGroup();
var contentItem = itemGroup.AddItem("Content", target);
if (!string.IsNullOrEmpty(copyToOutputDirectory))
{
contentItem.AddMetadata("CopyToOutputDirectory", copyToOutputDirectory, expressAsAttribute: true);
}
projectRoot.Save();
return this;
}
public IProjectFiles Remove(string target)
{
var filePath = Path.Combine(this.solutionBuilder.SolutionPath, this.ProjectPath, target);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
return this;
}
public IProjectConfiguration UsePackageReference(string packageName)
{
SolutionBuilder.DotnetCall<ProjectError>((out ProcessResult processResult) =>
DotnetHelper.AddPackage(this.solutionBuilder.SolutionPath, ProjectFilePath, packageName, out processResult)
);
return this;
}
public IProjectConfiguration UseGlobalUsing(string usingNamespace)
{
var projectRoot = ProjectRootElement.Open(Path.Combine(this.solutionBuilder.SolutionPath, ProjectFilePath));
var itemGroup = projectRoot.AddItemGroup();
var contentItem = itemGroup.AddItem("Using", usingNamespace);
projectRoot.Save();
return this;
}
private void CopyResourceFile(string filePath, string targetPath, IEnumerable<(string key, string value)>? replaceItems = null)
{
var txt = File.ReadAllText(Path.Combine(this.solutionBuilder.Root, filePath));
if (replaceItems != null)
{
foreach (var item in replaceItems)
{
txt = txt.Replace(item.key, item.value, StringComparison.InvariantCulture);
}
}
File.WriteAllText(Path.Combine(this.solutionBuilder.SolutionPath, this.ProjectPath, targetPath), txt);
}
}
}