Skip to content

Commit c3354fa

Browse files
committed
Remove inline code to support xbuild
1 parent bba560f commit c3354fa

File tree

9 files changed

+302
-150
lines changed

9 files changed

+302
-150
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ x64/
3838
*.log
3939
*.vspscc
4040
*.vssscc
41+
*.dll
4142
.builds
4243

4344
# Visual C++ cache files
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{B9E540F6-B1DD-4CB6-B8BE-B2C7D7AB964C}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Baseclass.Contrib.Nuget.Output.Build</RootNamespace>
11+
<AssemblyName>Baseclass.Contrib.Nuget.Output.Build</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>..\build\net40\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>none</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>..\build\net40\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="Microsoft.Build.Framework" />
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Xml" />
40+
<Reference Include="WindowsBase" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="ITaskExtensions.cs" />
44+
<Compile Include="PackageFilterTask.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
48+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
49+
Other similar extension points exist, see Microsoft.Common.targets.
50+
<Target Name="BeforeBuild">
51+
</Target>
52+
<Target Name="AfterBuild">
53+
</Target>
54+
-->
55+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25123.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Baseclass.Contrib.Nuget.Output.Build", "Baseclass.Contrib.Nuget.Output.Build.csproj", "{B9E540F6-B1DD-4CB6-B8BE-B2C7D7AB964C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B9E540F6-B1DD-4CB6-B8BE-B2C7D7AB964C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B9E540F6-B1DD-4CB6-B8BE-B2C7D7AB964C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B9E540F6-B1DD-4CB6-B8BE-B2C7D7AB964C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B9E540F6-B1DD-4CB6-B8BE-B2C7D7AB964C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.Build.Framework;
2+
using System;
3+
4+
namespace Baseclass.Contrib.Nuget.Output.Build
5+
{
6+
public static class ITaskExtensions
7+
{
8+
public static void LogError(this ITask task, string message, params object[] messageArgs)
9+
{
10+
var buildEngine = task.BuildEngine;
11+
buildEngine.LogErrorEvent(new BuildErrorEventArgs(null, null, buildEngine.ProjectFileOfTaskNode, buildEngine.LineNumberOfTaskNode, buildEngine.ColumnNumberOfTaskNode, 0, 0, message, null, task.GetType().Name, DateTime.UtcNow, messageArgs));
12+
}
13+
14+
public static void LogMessage(this ITask task,string message, params object[] messageArgs)
15+
{
16+
var buildEngine = task.BuildEngine;
17+
BuildMessageEventArgs e = new BuildMessageEventArgs(message, (string)null, task.GetType().Name, MessageImportance.Normal, DateTime.UtcNow, messageArgs);
18+
buildEngine.LogMessageEvent(e);
19+
}
20+
}
21+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using System;
2+
using Microsoft.Build.Framework;
3+
using System.Collections.Generic;
4+
using System.Xml;
5+
using System.IO;
6+
using System.IO.Packaging;
7+
using System.Linq;
8+
using System.Threading;
9+
10+
namespace Baseclass.Contrib.Nuget.Output.Build
11+
{
12+
public class PackageFilterTask : ITask
13+
{
14+
[Required]
15+
public ITaskItem[] NugetPackages { get; set; }
16+
17+
[Required]
18+
public string ProjectDirectory { get; set; }
19+
20+
[Required]
21+
public string ProjectName { get; set; }
22+
23+
[Output]
24+
public ITaskItem[] Result { get; private set; }
25+
26+
public IBuildEngine BuildEngine { get; set; }
27+
28+
public ITaskHost HostObject { get; set; }
29+
30+
public bool Execute()
31+
{
32+
var usedPackages = new HashSet<string>(); // packaged used by the project
33+
try
34+
{
35+
var packageConfigPath = Path.Combine(this.ProjectDirectory, "packages.config");
36+
if(!File.Exists(packageConfigPath))
37+
{
38+
this.LogMessage("Config doesn't exist: {0}", packageConfigPath);
39+
packageConfigPath = Path.Combine(this.ProjectDirectory, "packages.{0}.config");
40+
}
41+
42+
if (!File.Exists(packageConfigPath))
43+
{
44+
this.LogError("Config doesn't exist: {0}", packageConfigPath);
45+
return false;
46+
}
47+
48+
this.LogMessage("Reading config: {0}", packageConfigPath);
49+
50+
var xml = new XmlDocument();
51+
xml.LoadXml(File.ReadAllText(packageConfigPath));
52+
var deps = xml.GetElementsByTagName("package");
53+
foreach (XmlNode dep in deps)
54+
{
55+
if (dep.Attributes == null)
56+
{
57+
continue;
58+
}
59+
var id = dep.Attributes.GetNamedItem("id").Value;
60+
if ("Baseclass.Contrib.Nuget.Output".Equals(id))
61+
{
62+
continue;
63+
}
64+
var version = dep.Attributes.GetNamedItem("version").Value;
65+
var s = string.Format("{0}.{1}", id, version);
66+
usedPackages.Add(s);
67+
}
68+
}
69+
catch (Exception e)
70+
{
71+
this.LogError("Failed to load package files: {0}", e.Message);
72+
return false;
73+
}
74+
75+
var usedNugetPackages = new List<ITaskItem>(); // list of nuget packages used by the project
76+
try
77+
{
78+
foreach (var nugetPackage in this.NugetPackages)
79+
{
80+
var path = nugetPackage.GetMetadata("Fullpath");
81+
var parts = path.Split(Path.DirectorySeparatorChar);
82+
usedNugetPackages.AddRange(from part in parts where usedPackages.Contains(part) select nugetPackage);
83+
}
84+
}
85+
catch (Exception e)
86+
{
87+
this.LogError("Failed to filter nuget specs: {0}", e.Message);
88+
return false;
89+
}
90+
91+
var result = new List<ITaskItem>(); // list of nuget packages used by the project that depends on Baseclass.Contrib.Nuget.Output
92+
foreach (var nugetPackage in usedNugetPackages)
93+
{
94+
var path = nugetPackage.GetMetadata("Fullpath");
95+
var nupkgpath = Path.GetDirectoryName(path);
96+
97+
using (var archive = Package.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
98+
{
99+
var nuspec = archive.GetParts().Single(part => part.Uri.ToString().EndsWith(".nuspec"));
100+
var nugetSpec = Path.Combine(nupkgpath, Path.GetFileName(nuspec.Uri.ToString()));
101+
102+
// use a mutex to ensure that only one process unzip the nuspec
103+
// and that one process do not start reading it due to its existence while another one is still writing it.
104+
if (!File.Exists(nugetSpec))
105+
{
106+
var mut = new Mutex(false, "UnzipNuSpec");
107+
try
108+
{
109+
mut.WaitOne();
110+
111+
if (!File.Exists(nugetSpec))
112+
{
113+
// unpack the nuget spec file from the package
114+
try
115+
{
116+
using (var outputstream = new FileStream(nugetSpec, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
117+
{
118+
using (var nspecstream = nuspec.GetStream())
119+
{
120+
nspecstream.CopyTo(outputstream);
121+
}
122+
}
123+
}
124+
catch (IOException)
125+
{
126+
if (!File.Exists(nugetSpec))
127+
{
128+
throw;
129+
}
130+
}
131+
}
132+
}
133+
finally
134+
{
135+
mut.ReleaseMutex();
136+
}
137+
}
138+
139+
var xml = new XmlDocument();
140+
xml.LoadXml(File.ReadAllText(nugetSpec));
141+
var deps = xml.GetElementsByTagName("dependency");
142+
foreach (XmlNode dep in deps)
143+
{
144+
if (dep.Attributes == null)
145+
{
146+
continue;
147+
}
148+
var id = dep.Attributes.GetNamedItem("id").Value;
149+
if ("Baseclass.Contrib.Nuget.Output".Equals(id))
150+
{
151+
result.Add(nugetPackage);
152+
break;
153+
}
154+
}
155+
}
156+
}
157+
158+
this.Result = result.ToArray();
159+
return true;
160+
}
161+
}
162+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Baseclass.Contrib.Nuget.Output.Build")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Baseclass.Contrib.Nuget.Output.Build")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("b9e540f6-b1dd-4cb6-b8be-b2c7d7ab964c")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Baseclass.Contrib.Nuget.Output/Baseclass.Contrib.Nuget.Output.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
</metadata>
1616
<files>
1717
<file src="build\net40\Baseclass.Contrib.Nuget.Output.targets" target="build\net40\Baseclass.Contrib.Nuget.Output.targets" />
18+
<file src="build\net40\Baseclass.Contrib.Nuget.Output.Build.dll" target="build\net40\Baseclass.Contrib.Nuget.Output.Build.dll" />
1819
</files>
1920
</package>

0 commit comments

Comments
 (0)