-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlib-sourcelink.cake
106 lines (86 loc) · 3.85 KB
/
lib-sourcelink.cake
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
public static bool IsSourceLinkSupported(BuildContext buildContext, string projectName, string projectFileName)
{
if (buildContext.General.SourceLink.IsDisabled)
{
return false;
}
if (buildContext.General.IsLocalBuild)
{
return false;
}
if (string.IsNullOrWhiteSpace(buildContext.General.Repository.Url))
{
return false;
}
// Only support C# projects
if (!projectFileName.EndsWith(".csproj"))
{
return false;
}
// Is this a test project?
if (buildContext.Tests.Items.Contains(projectName))
{
return false;
}
// Only support when running a real build, e.g. ot for 'Package' only
if (!buildContext.General.Target.ToLower().Contains("build"))
{
return false;
}
return true;
}
//-------------------------------------------------------------
public static void InjectSourceLinkInProjectFile(BuildContext buildContext, string projectName, string projectFileName)
{
try
{
// Only support C# projects
if (!IsSourceLinkSupported(buildContext, projectName, projectFileName))
{
return;
}
// For SourceLink to work, the .csproj should contain something like this:
// <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-63127-02" PrivateAssets="all" />
var projectFileContents = System.IO.File.ReadAllText(projectFileName);
if (projectFileContents.Contains("Microsoft.SourceLink.GitHub"))
{
return;
}
buildContext.CakeContext.Warning("No SourceLink reference found, automatically injecting SourceLink package reference now");
//const string MSBuildNS = (XNamespace) "http://schemas.microsoft.com/developer/msbuild/2003";
var xmlDocument = XDocument.Parse(projectFileContents);
var projectElement = xmlDocument.Root;
// Item group with package reference
var referencesItemGroup = new XElement("ItemGroup");
var sourceLinkPackageReference = new XElement("PackageReference");
sourceLinkPackageReference.Add(new XAttribute("Include", "Microsoft.SourceLink.GitHub"));
sourceLinkPackageReference.Add(new XAttribute("Version", "1.1.1"));
sourceLinkPackageReference.Add(new XAttribute("PrivateAssets", "all"));
referencesItemGroup.Add(sourceLinkPackageReference);
projectElement.Add(referencesItemGroup);
// Item group with source root
// <SourceRoot Include="{repository root}" RepositoryUrl="{repository url}"/>
var sourceRootItemGroup = new XElement("ItemGroup");
var sourceRoot = new XElement("SourceRoot");
// Required to end with a \
var sourceRootValue = buildContext.General.RootDirectory;
var directorySeparator = System.IO.Path.DirectorySeparatorChar.ToString();
if (!sourceRootValue.EndsWith(directorySeparator))
{
sourceRootValue += directorySeparator;
};
sourceRoot.Add(new XAttribute("Include", sourceRootValue));
sourceRoot.Add(new XAttribute("RepositoryUrl", buildContext.General.Repository.Url));
// Note: since we are not allowing source control manager queries (we don't want to require a .git directory),
// we must specify the additional information below
sourceRoot.Add(new XAttribute("SourceControl", "git"));
sourceRoot.Add(new XAttribute("RevisionId", buildContext.General.Repository.CommitId));
sourceRootItemGroup.Add(sourceRoot);
projectElement.Add(sourceRootItemGroup);
xmlDocument.Save(projectFileName);
}
catch (Exception ex)
{
buildContext.CakeContext.Error($"Failed to process source link for project '{projectFileName}': {ex.Message}");
}
}