Skip to content

Commit

Permalink
Set SourceLink path in target (#272)
Browse files Browse the repository at this point in the history
* Set SourceLink path in target

* Allow disabling Source Link file generation in a target
  • Loading branch information
tmat committed May 2, 2019
1 parent e5e4f3a commit 8508270
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 12 deletions.
26 changes: 16 additions & 10 deletions src/SourceLink.Common/build/Microsoft.SourceLink.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<Import Project="InitializeSourceControlInformation.targets"/>

<UsingTask TaskName="Microsoft.SourceLink.Common.GenerateSourceLinkFile" AssemblyFile="$(_MicrosoftSourceLinkCommonAssemblyFile)"/>

<PropertyGroup Condition="'$(EnableSourceLink)' == 'true' and '$(DebugType)' != 'none'">
<SourceLink>$(IntermediateOutputPath)$(MSBuildProjectName).sourcelink.json</SourceLink>
</PropertyGroup>

<Target Name="_SetSourceLinkFilePath">
<PropertyGroup>
<SourceLink>$(IntermediateOutputPath)$(MSBuildProjectName).sourcelink.json</SourceLink>
</PropertyGroup>
</Target>

<!--
Triggers SetEmbeddedFilesFromSourceControlManagerUntrackedFiles target defined by a source control package Microsoft.Build.Tasks.{Git|Tfvc|...}.
Expand Down Expand Up @@ -40,12 +42,11 @@
Each source control provider package adds its SourceLinkUrl initialization target to SourceLinkUrlInitializerTargets.
This target shall initialize SourceLinkUrl of all items that don't have it initialized yet and belong to the source control provider.
-->
<Target Name="GenerateSourceLinkFile"
DependsOnTargets="InitializeSourceControlInformation;$(_GenerateSourceLinkFileDependsOnTargets);_InitializeSourceRootMappedPathsOpt;$(SourceLinkUrlInitializerTargets)"
BeforeTargets="$(_GenerateSourceLinkFileBeforeTargets)"
Outputs="$(SourceLink)"
Condition="'$(SourceLink)' != '' and '$(SourceControlInformationFeatureSupported)' == 'true'">

<Target Name="_GenerateSourceLinkFile"
DependsOnTargets="_SetSourceLinkFilePath;$(_GenerateSourceLinkFileDependsOnTargets);_InitializeSourceRootMappedPathsOpt;$(SourceLinkUrlInitializerTargets)"
Condition="'$(EnableSourceLink)' == 'true' and '$(DebugType)' != 'none'"
Outputs="$(SourceLink)">

<Microsoft.SourceLink.Common.GenerateSourceLinkFile SourceRoots="@(SourceRoot)" OutputFile="$(SourceLink)" />

<ItemGroup>
Expand All @@ -59,5 +60,10 @@
</Link>
</ItemGroup>
</Target>

<Target Name="GenerateSourceLinkFile"
DependsOnTargets="InitializeSourceControlInformation;_GenerateSourceLinkFile"
Condition="'$(SourceControlInformationFeatureSupported)' == 'true'"
BeforeTargets="$(_GenerateSourceLinkFileBeforeTargets)"/>

</Project>
132 changes: 132 additions & 0 deletions src/SourceLink.Git.IntegrationTests/TargetTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Microsoft. 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.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using LibGit2Sharp;
using TestUtilities;
using Xunit;

namespace Microsoft.SourceLink.IntegrationTests
{
public class TargetTests : DotNetSdkTestBase
{
public TargetTests()
: base("Microsoft.SourceLink.GitHub")
{
}

[ConditionalFact(typeof(DotNetSdkAvailable))]
public void GenerateSourceLinkFileTarget_EnableSourceLinkCondition()
{
GitUtilities.CreateGitRepositoryWithSingleCommit(ProjectDir.Path, new[] { ProjectFileName }, "http://github.com/test-org/test-repo");

VerifyValues(
customProps: @"
<PropertyGroup>
<Test_DefaultEnableSourceControlManagerQueries>$(EnableSourceControlManagerQueries)</Test_DefaultEnableSourceControlManagerQueries>
<Test_DefaultEnableSourceLink>$(EnableSourceLink)</Test_DefaultEnableSourceLink>
</PropertyGroup>
",
customTargets: @"
<Target Name=""Test_SetEnableSourceLink"" BeforeTargets=""InitializeSourceControlInformation"">
<PropertyGroup>
<EnableSourceLink>false</EnableSourceLink>
</PropertyGroup>
</Target>
",
targets: new[]
{
"Build"
},
expressions: new[]
{
"@(SourceRoot)",
"$(Test_DefaultEnableSourceControlManagerQueries)",
"$(Test_DefaultEnableSourceLink)",
"$(SourceLink)"
},
expectedResults: new[]
{
ProjectSourceRoot,
"true",
"true",
""
});
}

[ConditionalFact(typeof(DotNetSdkAvailable))]
public void DefaultValuesForEnableProperties_DesignTimeBuild()
{
GitUtilities.CreateGitRepositoryWithSingleCommit(ProjectDir.Path, new[] { ProjectFileName }, "http://github.com/test-org/test-repo");

VerifyValues(
customProps: @"
<PropertyGroup>
<Test_DefaultEnableSourceControlManagerQueries>$(EnableSourceControlManagerQueries)</Test_DefaultEnableSourceControlManagerQueries>
<Test_DefaultEnableSourceLink>$(EnableSourceLink)</Test_DefaultEnableSourceLink>
</PropertyGroup>
",
customTargets: "",
targets: new[]
{
"Build"
},
expressions: new[]
{
"@(SourceRoot)",
"$(Test_DefaultEnableSourceControlManagerQueries)",
"$(Test_DefaultEnableSourceLink)",
"$(SourceLink)"
},
expectedResults: new[]
{
"",
"",
"",
""
},
additionalCommandLineArgs: "/p:DesignTimeBuild=true");
}

[ConditionalFact(typeof(DotNetSdkAvailable))]
public void DefaultValuesForEnableProperties_BuildingForLiveUnitTesting()
{
GitUtilities.CreateGitRepositoryWithSingleCommit(ProjectDir.Path, new[] { ProjectFileName }, "http://github.com/test-org/test-repo");

VerifyValues(
customProps: @"
<PropertyGroup>
<Test_DefaultEnableSourceControlManagerQueries>$(EnableSourceControlManagerQueries)</Test_DefaultEnableSourceControlManagerQueries>
<Test_DefaultEnableSourceLink>$(EnableSourceLink)</Test_DefaultEnableSourceLink>
</PropertyGroup>
",
customTargets: "",
targets: new[]
{
"Build"
},
expressions: new[]
{
"@(SourceRoot)",
"$(Test_DefaultEnableSourceControlManagerQueries)",
"$(Test_DefaultEnableSourceLink)",
"$(SourceLink)"
},
expectedResults: new[]
{
"",
"",
"",
""
},
additionalCommandLineArgs: "/p:BuildingForLiveUnitTesting=true");
}
}
}
12 changes: 10 additions & 2 deletions src/TestUtilities/DotNetSdk/DotNetSdkTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,15 @@ public DotNetSdkTestBase(params string[] packages)
Assert.True(File.Exists(Path.Combine(ObjDir.Path, ProjectFileName + ".nuget.g.targets")));
}

protected void VerifyValues(string customProps, string customTargets, string[] targets, string[] expressions, string[] expectedResults = null, string[] expectedErrors = null, string[] expectedWarnings = null)
protected void VerifyValues(
string customProps,
string customTargets,
string[] targets,
string[] expressions,
string[] expectedResults = null,
string[] expectedErrors = null,
string[] expectedWarnings = null,
string additionalCommandLineArgs = null)
{
Debug.Assert(targets != null);
Debug.Assert(expressions != null);
Expand All @@ -217,7 +225,7 @@ protected void VerifyValues(string customProps, string customTargets, string[] t
bool success = false;
try
{
var buildResult = ProcessUtilities.Run(DotNetPath, $@"msbuild ""{Project.Path}"" /t:{targetsArg} /p:Configuration={Configuration} /bl:""{buildLog}""",
var buildResult = ProcessUtilities.Run(DotNetPath, $@"msbuild ""{Project.Path}"" /t:{targetsArg} /p:Configuration={Configuration} /bl:""{buildLog}"" {additionalCommandLineArgs}",
additionalEnvironmentVars: EnvironmentVariables);

string[] getDiagnostics(string[] lines, bool error)
Expand Down

0 comments on commit 8508270

Please sign in to comment.