Skip to content

Commit

Permalink
Merge pull request #84 from jviau/support-dotnetcore
Browse files Browse the repository at this point in the history
Support dotnet build (on Windows)
  • Loading branch information
Jacob Viau authored Sep 27, 2017
2 parents 483cba1 + 1be91e3 commit 4a3c09d
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)stylecop.json" Visible="false" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Microsoft.VisualStudio.SlowCheetah.Tests/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="xunit.shadowCopy" value="false"/>
<add key="xunit.shadowCopy" value="false" />
</appSettings>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ namespace Microsoft.VisualStudio.SlowCheetah.Tests.BuildTests
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using Xunit;

/// <summary>
Expand Down Expand Up @@ -47,6 +44,18 @@ public string TestProjectsDir
get { return Path.Combine(this.SolutionDir, @"Microsoft.VisualStudio.SlowCheetah.Tests\BuildTests\TestProjects"); }
}

/// <summary>
/// Gets the msbuild exe path that was cached during build
/// </summary>
private static string MSBuildExePath
{
get
{
string msbuildPathCache = Path.Combine(Environment.CurrentDirectory, "msbuildPath.txt");
return Path.Combine(File.ReadAllLines(msbuildPathCache).First(), "msbuild.exe");
}
}

/// <summary>
/// Builds the project of the given name from the <see cref="TestProjectsDir"/>
/// </summary>
Expand All @@ -58,13 +67,29 @@ public void BuildProject(string projectName)
{
{ "Configuration", "Debug" },
{ "OutputPath", this.OutputPath },
{ "VSToolsPath", string.Empty }
};

var project = new Project(Path.Combine(this.TestProjectsDir, projectName, projectName + ".csproj"), globalProperties, "4.0");
var logger = new ConsoleLogger(LoggerVerbosity.Quiet);
bool buildSuccess = project.Build(logger);
Assert.True(buildSuccess);
// We use an external process to run msbuild, because XUnit test discovery breaks
// when using <Reference Include="$(MSBuildToolsPath)\Microsoft.Build.dll" />.
// MSBuild NuGet packages proved to be difficult in getting in-proc test builds to run.
string projectPath = Path.Combine(this.TestProjectsDir, projectName, projectName + ".csproj");
string msbuildPath = MSBuildExePath;
string properties = "/p:" + string.Join(",", globalProperties.Select(x => $"{x.Key}={x.Value}"));

var startInfo = new System.Diagnostics.ProcessStartInfo()
{
FileName = msbuildPath,
Arguments = $"{projectPath} {properties}",
CreateNoWindow = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
};

using (var process = System.Diagnostics.Process.Start(startInfo))
{
process.WaitForExit();
Assert.Equal(0, process.ExitCode);
process.Close();
}
}

/// <summary>
Expand Down Expand Up @@ -99,10 +124,9 @@ public string GetConfigNodeValue(string configFilePath, string nodeName)
/// </summary>
public void Dispose()
{
ProjectCollection.GlobalProjectCollection.UnloadAllProjects();
if (Directory.Exists(this.OutputPath))
{
Directory.Delete(this.OutputPath, true);
Directory.Delete(this.OutputPath, recursive: true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Project />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<Project />
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<Reference Include="System.EnterpriseServices" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />
<Content Include="Other.config">
<TransformOnBuild>true</TransformOnBuild>
</Content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<TargetFramework>net46</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -13,9 +13,9 @@

<ItemGroup>
<PackageReference Include="MicroBuild.NonShipping" Version="2.0.40" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.3.0-beta1-build3642" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta1-build1309" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Nerdbank.GitVersioning" Version="1.6.30" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.0" />
</ItemGroup>
Expand All @@ -24,11 +24,6 @@
<ProjectReference Include="..\Microsoft.VisualStudio.SlowCheetah\Microsoft.VisualStudio.SlowCheetah.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
Expand All @@ -40,11 +35,19 @@
</MSBuild>
<ItemGroup>
<_CopyTools Include="@(_DependentAssemblies);%(_DependentAssemblies.RelativeDir)Microsoft.Web.XmlTransform.dll" />
<_CopyTools Include="@(_DependentAssemblies);%(_DependentAssemblies.RelativeDir)Microsoft.VisualStudio.Jdt.dll" />
<_CopyBuild Include="%(_DependentAssemblies.RelativeDir)Build\Microsoft.VisualStudio.SlowCheetah*.targets" />
</ItemGroup>
<RemoveDir Directories="$(MSBuildThisFileDirectory)BuildTests\TestProjects\packages\slowcheetah\" />
<Copy SourceFiles="@(_CopyTools)" DestinationFolder="$(MSBuildThisFileDirectory)BuildTests\TestProjects\packages\slowcheetah\tools" />
<Copy SourceFiles="@(_CopyBuild)" DestinationFolder="$(MSBuildThisFileDirectory)BuildTests\TestProjects\packages\slowcheetah\build" />
</Target>

<Target Name="CacheMSBuildPath" AfterTargets="Build">
<ItemGroup>
<_MSBuildPathLines Include="$(MSBuildToolsPath)" />
</ItemGroup>
<WriteLinesToFile File="$(OutputPath)msbuildPath.txt" Lines="@(_MSBuildPathLines)" Overwrite="true" />
</Target>

</Project>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<TargetFramework>net46</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MicroBuild.NonShipping" Version="2.0.40" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.3.0-beta1-build3642" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta1-build1309" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Nerdbank.GitVersioning" Version="1.6.30" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ protected override void OnInvoke(object sender, EventArgs e)
foreach (string config in transformsToCreate)
{
string itemName = string.Format(CultureInfo.CurrentCulture, Resources.Resources.String_FormatTransformFilename, itemFilename, config, itemExtension);
this.AddTransformFile(selectedProjectItem, itemName, itemFolder, optionsPage.AddDependentUpon);
hierarchy.ParseCanonicalName(Path.Combine(itemFolder, itemName), out uint addedFileId);
buildPropertyStorage.SetItemAttribute(addedFileId, SlowCheetahPackage.IsTransformFile, "True");
this.AddTransformFile(hierarchy, selectedProjectItem, itemName, itemFolder, optionsPage.AddDependentUpon);
}
}
}
Expand All @@ -181,11 +179,17 @@ protected override void OnInvoke(object sender, EventArgs e)
/// <summary>
/// Creates a new transformation file and adds it to the project.
/// </summary>
/// <param name="hierarchy">The project hierarchy</param>
/// <param name="selectedProjectItem">The selected item to be transformed</param>
/// <param name="itemName">Full name of the transformation file</param>
/// <param name="projectPath">Full path to the current project</param>
/// <param name="addDependentUpon">Wheter to add the new file dependent upon the source file</param>
private void AddTransformFile(ProjectItem selectedProjectItem, string itemName, string projectPath, bool addDependentUpon)
private void AddTransformFile(
IVsHierarchy hierarchy,
ProjectItem selectedProjectItem,
string itemName,
string projectPath,
bool addDependentUpon)
{
try
{
Expand All @@ -205,13 +209,27 @@ private void AddTransformFile(ProjectItem selectedProjectItem, string itemName,
// We need to set the Build Action to None to ensure that it doesn't get published for web projects
addedItem.Properties.Item("ItemType").Value = "None";

IVsHierarchy hierarchy = null;
IVsProject vsProject = (IVsProject)hierarchy;
IVsBuildPropertyStorage buildPropertyStorage = vsProject as IVsBuildPropertyStorage;
IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;

if (buildPropertyStorage == null)
{
this.logger.LogMessage("Error obtaining IVsBuildPropertyStorage from hierarcy.");
}
else if (ErrorHandler.Succeeded(hierarchy.ParseCanonicalName(addedItem.FileNames[0], out uint addedItemId)))
{
buildPropertyStorage.SetItemAttribute(addedItemId, SlowCheetahPackage.IsTransformFile, "true");

if (addDependentUpon)
{
// Not all projects (like CPS) set the dependent upon metadata when using the automation object
buildPropertyStorage.GetItemAttribute(addedItemId, SlowCheetahPackage.DependentUpon, out string dependentUponValue);
if (string.IsNullOrEmpty(dependentUponValue))
{
// It didm not set it
buildPropertyStorage.SetItemAttribute(addedItemId, SlowCheetahPackage.DependentUpon, selectedProjectItem.Name);
}
}
}
}
catch (Exception ex)
{
Expand All @@ -237,7 +255,7 @@ private bool ItemSupportsTransforms(IVsProject project, uint itemid)
return false;
}

if (!this.package.IsItemTransformItem(project, itemid))
if (this.package.IsItemTransformItem(project, itemid))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class PackageSolutionEvents : IVsUpdateSolutionEvents, IDisposable
/// Initializes a new instance of the <see cref="PackageSolutionEvents"/> class.
/// </summary>
/// <param name="serviceProvider">The VSPackage</param>
public PackageSolutionEvents(IServiceProvider serviceProvider)
/// <param name="errorListProvider">The error list provider</param>
public PackageSolutionEvents(IServiceProvider serviceProvider, ErrorListProvider errorListProvider)
{
this.ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
this.ErrorListProvider = errorListProvider ?? throw new ArgumentNullException(nameof(errorListProvider));

if (this.ServiceProvider.GetService(typeof(SVsSolutionBuildManager)) is IVsSolutionBuildManager solutionBuildManager)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,14 @@ private bool TryGetFileToTransform(IVsHierarchy hierarchy, uint parentId, string
// Get the project configurations to use in comparing the name
IEnumerable<string> configs = ProjectUtilities.GetProjectConfigurations(hierarchy);

docId = 0;
if (ErrorHandler.Failed(project.GetMkDocument(parentId, out documentPath)))
{
docId = 0;
return false;
}

if (!PackageUtilities.IsPathValid(documentPath))
{
return false;
}

Expand All @@ -311,7 +316,8 @@ private bool TryGetFileToTransform(IVsHierarchy hierarchy, uint parentId, string
return false;
}

if (PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
if (PackageUtilities.IsPathValid(documentPath)
&& PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
{
return true;
}
Expand All @@ -322,12 +328,11 @@ private bool TryGetFileToTransform(IVsHierarchy hierarchy, uint parentId, string
{
hierarchy.GetProperty(docId, (int)__VSHPROPID.VSHPROPID_NextVisibleSibling, out childIdObj);
docId = (uint)(int)childIdObj;
if (ErrorHandler.Succeeded(project.GetMkDocument(docId, out documentPath)))
if (ErrorHandler.Succeeded(project.GetMkDocument(docId, out documentPath))
&& PackageUtilities.IsPathValid(documentPath)
&& PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
{
if (PackageUtilities.IsFileTransform(Path.GetFileName(documentPath), transformName, configs))
{
return true;
}
return true;
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Microsoft.VisualStudio.SlowCheetah.VS/SlowCheetahPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ namespace Microsoft.VisualStudio.SlowCheetah.VS
public sealed partial class SlowCheetahPackage : Package
{
/// <summary>
/// The TransformOnBuild metadata
/// The TransformOnBuild metadata name
/// </summary>
public static readonly string TransformOnBuild = "TransformOnBuild";

/// <summary>
/// The IsTransformFile metadata
/// The IsTransformFile metadata name
/// </summary>
public static readonly string IsTransformFile = "IsTransformFile";

/// <summary>
/// The DependentUpon metadata name
/// </summary>
public static readonly string DependentUpon = "DependentUpon";

/// <summary>
/// Initializes a new instance of the <see cref="SlowCheetahPackage"/> class.
/// </summary>
Expand Down Expand Up @@ -140,7 +145,7 @@ protected override void Initialize()
this.ErrorListProvider = new ErrorListProvider(this);
this.AddCommand = new AddTransformCommand(this, this.NuGetManager, this.PackageLogger);
this.PreviewCommand = new PreviewTransformCommand(this, this.NuGetManager, this.PackageLogger, this.ErrorListProvider);
this.SolutionEvents = new PackageSolutionEvents(this);
this.SolutionEvents = new PackageSolutionEvents(this, this.ErrorListProvider);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ public static bool IsFileTransform(string documentName, string transformName, IE
}
}

/// <summary>
/// Gets if a path is valid or not.
/// </summary>
/// <param name="path">The path to check if it is valid</param>
/// <returns>True if path is valid, false otherwise.</returns>
public static bool IsPathValid(string path)
{
// empty path is valid
if (string.IsNullOrEmpty(path))
{
return true;
}

return !path.Any(c => Path.GetInvalidPathChars().Contains(c));
}

/// <summary>
/// Gets an item from the project hierarchy
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</NuGetPackageImportStamp>
<UseCodebase>true</UseCodebase>
<StartAction>Program</StartAction>
<StartProgram>devenv.exe</StartProgram>
<StartProgram>$(DevenvDir)devenv.exe</StartProgram>
<StartArguments>/rootsuffix Exp</StartArguments>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ public class JsonShimLogger : IJsonTransformationLogger
/// <param name="logger">Our own logger</param>
public JsonShimLogger(ITransformationLogger logger)
{
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

this.logger = logger;
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

/// <inheritdoc/>
Expand Down
Loading

0 comments on commit 4a3c09d

Please sign in to comment.