build.cake 8.02 KB
Newer Older
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#tool nuget:?package=NUnit.ConsoleRunner&version=3.9.0
#tool nuget:?package=JetBrains.ReSharper.CommandLineTools&version=2018.3.4

#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.Npx&version=1.3.0
#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.Issues&version=0.6.2
#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.Issues.InspectCode&version=0.6.1
#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.FileHelpers&version=3.1.0

//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");

//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////

// Define directories.
string projectName;
string projectPath;
DirectoryPath buildDir;
FilePath solutionFile;

// Error rules for resharper
// Example: {"InconsistentNaming", "RedundantUsingDirective"};
string [] resharperErrorRules = {};

Action<NpxSettings> requiredSemanticVersionPackages = settings => settings
    .AddPackage("semantic-release")
    .AddPackage("@semantic-release/commit-analyzer")
    .AddPackage("@semantic-release/release-notes-generator")
    .AddPackage("@semantic-release/gitlab")
    .AddPackage("@semantic-release/git")
    .AddPackage("@semantic-release/exec")
    .AddPackage("conventional-changelog-eslint");

//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////

Task("Get-Project-Name")
    .Does(() =>
{
    var solutions = GetFiles("./**/*.sln");
    projectName = solutions.First().GetFilenameWithoutExtension().ToString();
    Information("Project Name: {0}", projectName);
	
    solutionFile = solutions.First().ToString();
    Information("Solution File: {0}", solutionFile.ToString());
	
    projectPath = Context.Environment.WorkingDirectory.ToString().ToString() + "/src";
    Information("Project Directory: {0}", projectPath);
	
    buildDir = Directory(projectPath + "/" + projectName + "/bin") + Directory(configuration);
    Information("Build Directory: {0}", buildDir.ToString());
});

Task("Clean")
    .IsDependentOn("Get-Project-Name")
    .Does(() =>
{
    CleanDirectory(buildDir);
    CleanDirectory("./dist");
});

Task("Restore-NuGet-Packages")
    .IsDependentOn("Get-Project-Name")
    .Does(() =>
{
    NuGetRestore(solutionFile);
});

Task("Resharper")
    .IsDependentOn("Get-Project-Name")
    .Does(() =>
{
    FilePath dupLog = Context.Environment.WorkingDirectory + "/Resharper/dupfinder.xml";
    FilePath inspectLog = Context.Environment.WorkingDirectory + "/Resharper/inspectcode.xml";
    
    DupFinder(solutionFile, new DupFinderSettings() {
        OutputFile = dupLog.ToString()
    });
    
    Information("DupFinder Log:{0}{1}", Environment.NewLine, FileReadText(dupLog));
    
    InspectCode(solutionFile, new InspectCodeSettings() {
        OutputFile = inspectLog.ToString()
    });
    
    var issues = ReadIssues(
        InspectCodeIssuesFromFilePath(inspectLog.ToString()),
        Context.Environment.WorkingDirectory);

    Information("{0} issues are found.", issues.Count());
    
    Information("InspectCode Log:{0}{1}", Environment.NewLine, FileReadText(inspectLog));
    
    var errorIssues = issues.Where(issue => resharperErrorRules.Any(issue.Rule.Contains)).ToList();
 
    if(errorIssues.Any())
    {
        var errorMessage = errorIssues.Aggregate(new StringBuilder(), (stringBuilder, issue) => stringBuilder.AppendFormat("FileName: {0} Line: {1} Message: {2}{3}", issue.AffectedFileRelativePath, issue.Line, issue.Message, Environment.NewLine));
        throw new CakeException($"{errorIssues.Count} errors detected: {Environment.NewLine}{errorMessage}.");
    }
});
                
Task("Update-Assembly-Info")
    .IsDependentOn("Get-Project-Name")
    .Does(() =>
{
    
    Information("Running semantic-release in dry run mode to extract next semantic version number");

    string[] semanticReleaseOutput;
    Npx("semantic-release", "--dry-run", requiredSemanticVersionPackages, out semanticReleaseOutput);

    Information(string.Join(Environment.NewLine, semanticReleaseOutput));

    var nextSemanticVersionNumber = ExtractNextSemanticVersionNumber(semanticReleaseOutput);

    if (nextSemanticVersionNumber == null) {
        Warning("There are no relevant changes. AssemblyInfo won't be updated!");
    } else {
        Information("Next semantic version number is {0}", nextSemanticVersionNumber);
        
        var assemblyVersion = $"{nextSemanticVersionNumber}.0";
            
        CreateAssemblyInfo(projectPath + "/" + projectName + "/Properties/AssemblyInfo.cs", new AssemblyInfoSettings{
            Product = projectName,
            Title = projectName,
            Company = "RWTH Aachen University IT Center",
            Version = assemblyVersion,
            FileVersion = assemblyVersion,
            InformationalVersion  = assemblyVersion,
            Copyright = "RWTH Aachen University IT Center " + DateTime.Now.Year
        });
    }
});
                
Task("Build-Release")
    .IsDependentOn("Get-Project-Name")
    .Does(() =>
{        
    if(IsRunningOnWindows())
    {
        // Use MSBuild
        MSBuild(solutionFile, settings => 
        {
            settings.SetConfiguration(configuration);
            settings.WithProperty("DebugSymbols", "false");
            settings.WithProperty("DebugType", "None");
        });
    }
    else
    {
        // Use XBuild
        XBuild(solutionFile, settings =>
        {            
            settings.SetConfiguration(configuration);
            settings.WithProperty("DebugSymbols", "false");
            settings.WithProperty("DebugType", "None");
        });
    }
    CopyDirectory(buildDir, "./dist");
});

Task("Build")
    .IsDependentOn("Get-Project-Name")
    .Does(() =>
{        
    if(IsRunningOnWindows())
    {
        // Use MSBuild
        MSBuild(solutionFile, settings => 
        {
            settings.SetConfiguration(configuration);
            settings.WithProperty("RunCodeAnalysis", "true");
        });
    }
    else
    {
        // Use XBuild
        XBuild(solutionFile, settings =>
        {            
            settings.SetConfiguration(configuration);
            settings.WithProperty("RunCodeAnalysis", "true");
        });
    }
});

Task("Run-Unit-Tests")
    .Does(() =>
{
    NUnit3("./src/**/bin/" + configuration + "/*.Tests.dll", new NUnit3Settings {
        // generate the xml file
        NoResults = false,
        Results = new NUnit3Result[]{new NUnit3Result(){
            FileName = Context.Environment.WorkingDirectory + "/TestResult.xml",
            Transform = Context.Environment.WorkingDirectory + "/nunit3-junit.xslt"
        }}
    });
});

Task("Semantic-Release")
    .Does(() =>
{
    Npx("semantic-release", requiredSemanticVersionPackages);
});

//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////

Task("Default")
    .IsDependentOn("Clean")
    .IsDependentOn("Restore-NuGet-Packages")
    .IsDependentOn("Resharper")
    .IsDependentOn("Build")
    .IsDependentOn("Run-Unit-Tests");

//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////

RunTarget(target);

///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////

string ExtractNextSemanticVersionNumber(string[] semanticReleaseOutput)
{
    var extractRegEx = new System.Text.RegularExpressions.Regex("^.+next release version is (?<SemanticVersionNumber>.*)$");

    return semanticReleaseOutput
        .Select(line => extractRegEx.Match(line).Groups["SemanticVersionNumber"].Value)
        .Where(line => !string.IsNullOrWhiteSpace(line))
        .SingleOrDefault();
}