Skip to content

Commit

Permalink
Merge pull request #28 from mrjpeterj/issue_27
Browse files Browse the repository at this point in the history
Handle output filename when targetting .Net Framework
  • Loading branch information
ChrisMaddock authored Jul 24, 2018
2 parents 0f60d06 + a6eff3f commit 089a514
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 28 deletions.
64 changes: 45 additions & 19 deletions src/extension/VSProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public class VSProject : IProject
/// <summary>
/// VS Project extentions
/// </summary>
private static readonly string[] PROJECT_EXTENSIONS = { ".csproj", ".vbproj", ".vjsproj", ".vcproj", ".fsproj" };

private static readonly string[] PROJECT_EXTENSIONS = { ".csproj", ".vbproj", ".vjsproj", ".vcproj", ".fsproj" };

/// <summary>
/// VS Solution extension
/// </summary>
private const string SOLUTION_EXTENSION = ".sln";

private static readonly Regex netFramework = new Regex("^net[1-9]");

/// <summary>
/// The XML representation of the project
/// </summary>
Expand Down Expand Up @@ -125,10 +127,10 @@ public TestPackage GetTestPackage(string configName)
{
var config = _configs[name];
package.AddSubPackage(new TestPackage(config.AssemblyPath));
appbase = config.OutputDirectory;

appbase = config.OutputDirectory;

break;
}
}
}

if (appbase != null)
Expand Down Expand Up @@ -159,8 +161,8 @@ public static bool IsProjectFile(string path)
return false;

if (path.ToLower().IndexOf("http:") >= 0)
return false;

return false;

string extension = Path.GetExtension(path);

foreach (string validExtension in PROJECT_EXTENSIONS)
Expand All @@ -184,7 +186,7 @@ public static bool IsSolutionFile(string path)
/// </summary>
private void Load()
{
if (!IsProjectFile(ProjectPath))
if (!IsProjectFile(ProjectPath))
ThrowInvalidFileType(ProjectPath);

StreamReader rdr = new StreamReader(ProjectPath, System.Text.Encoding.UTF8);
Expand Down Expand Up @@ -240,17 +242,42 @@ private void Load()
/// is only called for file extensions .csproj.
/// </summary>
/// <returns>True if the project was successfully loaded, false otherwise.</returns>
private bool TryLoadDotNetCoreProject()
private bool TryLoadDotNetCoreProject()
{
XmlNode root = _doc.SelectSingleNode("Project");

if (root != null && SafeAttributeValue(root, "Sdk") != null)
if (root != null && SafeAttributeValue(root, "Sdk") != null)
{
string targetFramework = _doc.SelectSingleNode("Project/PropertyGroup/TargetFramework").InnerText;

XmlNode assemblyNameNode = _doc.SelectSingleNode("Project/PropertyGroup/AssemblyName");
// Even console apps are dll's even if <OutputType> has value 'EXE'
string assemblyName = assemblyNameNode == null ? $"{Name}.dll" : $"{assemblyNameNode.InnerText}.dll";

// Even console apps are dll's even if <OutputType> has value 'EXE',
// if TargetFramework is netcore
string outputType = "dll";

if (netFramework.IsMatch(targetFramework))
{
// When targetting standard .Net framework, the default is still dll,
// however, OutputType is now honoured.
// Also if Sdk = 'Microsoft.NET.Sdk.Web' then OutputType default is exe
string sdk = root.Attributes["Sdk"].Value;

if (sdk == "Microsoft.NET.Sdk.Web")
{
outputType = "exe";
}
else
{
XmlNode outputTypeNode = _doc.SelectSingleNode("Project/PropertyGroup/OutputType");
if (outputTypeNode != null && outputTypeNode.InnerText != "Library")
{
outputType = "exe";
}
}
}

string assemblyName = assemblyNameNode == null ? $"{Name}.{outputType}" : $"{assemblyNameNode.InnerText}.{outputType}";

XmlNodeList nodes = _doc.SelectNodes("/Project/PropertyGroup");

Expand Down Expand Up @@ -360,7 +387,7 @@ private void LoadMSBuildProject()
else
assemblyName = assemblyName + ".dll";

string commonOutputPath = null;
string commonOutputPath = null;
var explicitOutputPaths = new Dictionary<string, string>();

foreach (XmlElement configNode in nodes)
Expand All @@ -374,20 +401,19 @@ private void LoadMSBuildProject()

if (name == null)
{
if (outputPathElement != null)
if (outputPathElement != null)
commonOutputPath = outputPath;
continue;
}

if (outputPathElement != null)
explicitOutputPaths[name] = outputPath;

if (outputPathElement != null)
explicitOutputPaths[name] = outputPath;

if (outputPath == null)
outputPath = explicitOutputPaths.ContainsKey(name) ? explicitOutputPaths[name] : commonOutputPath;

if (outputPath != null)
_configs[name] = new ProjectConfig(this, name, outputPath.Replace("$(Configuration)", name), assemblyName);

_configs[name] = new ProjectConfig(this, name, outputPath.Replace("$(Configuration)", name), assemblyName);
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/tests/VisualStudioProjectLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,18 @@ public void PicksUpCorrectOutputPath(string resourceName, string configuration,
}
}

[TestCase("netcoreapp1.1-minimal.csproj", "netcoreapp1.1-minimal")]
[TestCase("netcoreapp1.1-with-assembly-name.csproj", "the-assembly-name")]
[TestCase("netcoreapp1.1-minimal.csproj", "netcoreapp1.1-minimal.dll")]
[TestCase("netcoreapp1.1-with-assembly-name.csproj", "the-assembly-name.dll")]
[TestCase("netcoreapp2.0-minimal-dll.csproj", "netcoreapp2.0-minimal-dll.dll")]
[TestCase("netcoreapp2.0-minimal-exe.csproj", "netcoreapp2.0-minimal-exe.dll")]
[TestCase("netcoreapp2.0-minimal-web.csproj", "netcoreapp2.0-minimal-web.dll")]
[TestCase("netcoreapp2.0-with-assembly-name-exe.csproj", "the-assembly-name.dll")]
[TestCase("netcorefmwk-minimal-exe.csproj", "netcorefmwk-minimal-exe.exe")]
[TestCase("netcorefmwk-minimal-web.csproj", "netcorefmwk-minimal-web.exe")]
[TestCase("netcorefmwk-with-assembly-name-dll.csproj", "the-assembly-name.dll")]
[TestCase("netcorefmwk-with-assembly-name-exe.csproj", "the-assembly-name.exe")]
[TestCase("netstd2.0-minimal-dll.csproj", "netstd2.0-minimal-dll.dll")]
[TestCase("netstd2.0-minimal-exe.csproj", "netstd2.0-minimal-exe.dll")]
public void PicksUpCorrectAssemblyName(string resourceName, string expectedAssemblyName)
{
using (TestResource file = new TestResource(resourceName))
Expand All @@ -173,7 +183,7 @@ public void PicksUpCorrectAssemblyName(string resourceName, string expectedAssem
{
TestPackage package = project.GetTestPackage(config);

Assert.That(Path.GetFileNameWithoutExtension(package.SubPackages[0].FullName) == expectedAssemblyName);
Assert.That(Path.GetFileName(package.SubPackages[0].FullName) == expectedAssemblyName);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/tests/resources/netcoreapp2.0-minimal-dll.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions src/tests/resources/netcoreapp2.0-minimal-exe.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions src/tests/resources/netcoreapp2.0-minimal-web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.9" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/tests/resources/netcoreapp2.0-with-assembly-name-exe.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<ApplicationIcon />
<StartupObject />
<AssemblyName>the-assembly-name</AssemblyName>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions src/tests/resources/netcorefmwk-minimal-exe.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions src/tests/resources/netcorefmwk-minimal-web.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.4" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/tests/resources/netcorefmwk-with-assembly-name-dll.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<ApplicationIcon />
<OutputType>Library</OutputType>
<StartupObject />
<AssemblyName>the-assembly-name</AssemblyName>
</PropertyGroup>

</Project>
11 changes: 11 additions & 0 deletions src/tests/resources/netcorefmwk-with-assembly-name-exe.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net461</TargetFramework>
<AssemblyName>the-assembly-name</AssemblyName>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>

</Project>
7 changes: 7 additions & 0 deletions src/tests/resources/netstd2.0-minimal-dll.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
10 changes: 10 additions & 0 deletions src/tests/resources/netstd2.0-minimal-exe.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

</Project>
30 changes: 24 additions & 6 deletions src/tests/vs-project-loader.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,33 @@
<ItemGroup>
<EmbeddedResource Include="resources\TestTemplatedPaths.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="resources\netcoreapp2.0-minimal-dll.csproj" />
<EmbeddedResource Include="resources\netcoreapp2.0-minimal-exe.csproj" />
<EmbeddedResource Include="resources\netcoreapp2.0-with-assembly-name-exe.csproj" />
<EmbeddedResource Include="resources\netcorefmwk-minimal-exe.csproj" />
<EmbeddedResource Include="resources\netcorefmwk-with-assembly-name-dll.csproj" />
<EmbeddedResource Include="resources\netcorefmwk-with-assembly-name-exe.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="resources\netcoreapp2.0-minimal-web.csproj" />
<EmbeddedResource Include="resources\netcorefmwk-minimal-web.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="resources\netstd2.0-minimal-dll.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="resources\netstd2.0-minimal-exe.csproj" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="resources\test-duplicated-key-project.csproj" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

0 comments on commit 089a514

Please sign in to comment.