Skip to content

Commit

Permalink
Merge pull request #67 from ngyukman/master
Browse files Browse the repository at this point in the history
Support dependencies from PackageReference
  • Loading branch information
ngyukman authored Sep 12, 2018
2 parents 7d00f4f + 4ec6373 commit 451efe2
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 3 deletions.
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
version = 2.16

version = 2.17
2 changes: 2 additions & 0 deletions src/main/groovy/com/ullink/NuGetSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ullink

import com.ullink.packagesparser.NugetParser
import com.ullink.packagesparser.PackageReferenceParser
import com.ullink.packagesparser.PackagesConfigParser
import com.ullink.packagesparser.ProjectJsonParser
import groovy.util.slurpersupport.GPathResult
Expand Down Expand Up @@ -111,6 +112,7 @@ class NuGetSpec extends Exec {
def dependencies = []
dependencies.addAll getDependencies(mainProject, packageConfigFileName, new PackagesConfigParser())
dependencies.addAll getDependencies(mainProject, projectJsonFileName, new ProjectJsonParser())
dependencies.addAll getDependencies(mainProject, mainProject.properties.MSBuildProjectFile.toString(), new PackageReferenceParser())

if (!dependencies.isEmpty())
setDefaultMetadata('dependencies', dependencies)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.ullink.packagesparser

import groovy.util.slurpersupport.GPathResult


class PackageReferenceParser implements NugetParser {

// Limitation: does not resolve the conditional package references into groups

@Override
Collection getDependencies(File file) {
def defaultDependencies = []

def project = new XmlSlurper().parse(file)

project.'**'
.findAll { it.name() == 'PackageReference' && getAttributeOrNodeText(it, 'PrivateAssets') != 'all' }
.each {
def reference = it as GPathResult
def includeAssets = getAttributeOrNodeText(reference, 'IncludeAssets')
def excludeAssets = getAttributeOrNodeText(reference, 'ExcludeAssets')
def args = [
id: getAttributeOrNodeText(reference, 'Include'), version: getAttributeOrNodeText(reference, 'Version')
]
if (includeAssets) {
args.include = includeAssets
}
if (excludeAssets) {
args.exclude = excludeAssets
}

defaultDependencies.add({
dependency(args)
})
}
return defaultDependencies
}

private static String getAttributeOrNodeText(GPathResult parentNode, String key) {
def node = parentNode."${key}".toString().trim()
if (node) {
return node
}
return parentNode["@${key}"]
}

}
5 changes: 5 additions & 0 deletions src/test/groovy/com/ullink/MSBuildTaskBuilder.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ class MSBuildTaskBuilder {
public MSBuildTaskBuilder withProjectFile(File file) {
withProjectFile(file.path)
}

public MSBuildTaskBuilder withMainProjectProperty(String name, String value) {
mainProjectProperties[name] = value
this
}
}
44 changes: 44 additions & 0 deletions src/test/groovy/com/ullink/NuGetSpecTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,48 @@ class NuGetSpecTest {
assertXMLEqual(expected, nuspecGenerated)
}
}

@Test
public void generateNuspec_defaultDependenciesFromProjectPackageReferences() {
def project = newNugetProject()

project.nugetSpec {
nuspec {}
}

File.createTempDir().with { projectFolder ->
deleteOnExit()

def csproj = new File(getClass().getResource('packageparser/packagereference.csproj').toURI())
def msbuildTask = new MSBuildTaskBuilder()
.withAssemblyName('bar')
.withProjectFile(csproj)
.withMainProjectProperty('MSBuildProjectFile', 'packagereference.csproj')
.build()

project.tasks.add(msbuildTask)

def expected =
'''
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>foo</id>
<version>2.1</version>
<description>fooDescription</description>
<dependencies>
<dependency id="Microsoft.AspNetCore.Mvc.Abstractions" version="1.1.2"/>
<dependency id="Microsoft.AspNetCore.Mvc.Core" version="1.1.2"/>
<dependency id="Microsoft.Extensions.Caching.Abstractions" version="1.1.1"/>
<dependency id="Microsoft.Extensions.Configuration.Binder" version="1.1.1"/>
<dependency id="Newtonsoft.Json" version="9.0.1"/>
<dependency id="StyleCop.Analyzers" version="1.0.0"/>
<dependency id="NuGet.Versioning" version="3.6.0" include="build" exclude="none"/>
<dependency id="System.Xml.XDocument" version="4.3.0"/>
</dependencies>
</metadata>
</package>'''
def nuspecGenerated = project.tasks.nugetSpec.generateNuspec()
assertXMLEqual(expected, nuspecGenerated)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*************************************************************************
* ULLINK CONFIDENTIAL INFORMATION
* _______________________________
*
* All Rights Reserved.
*
* NOTICE: This file and its content are the property of Ullink. The
* information included has been classified as Confidential and may
* not be copied, modified, distributed, or otherwise disseminated, in
* whole or part, without the express written permission of Ullink.
************************************************************************/
package com.ullink.packageparser

import com.ullink.packagesparser.PackageReferenceParser
import groovy.xml.MarkupBuilder
import spock.lang.Specification

class PackageReferenceParserSpec extends Specification {
def 'Extract dependencies from PackageReference from csproj'() {
given:
def csproj = new File(getClass().getResource('packagereference.csproj').toURI())

when:
def result = new PackageReferenceParser().getDependencies(csproj)

then:
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.dependencies() {
result.each {
it.resolveStrategy = DELEGATE_FIRST
it.delegate = delegate
it.call()
}
}
writer.toString() == '''<dependencies>
<dependency id='Microsoft.AspNetCore.Mvc.Abstractions' version='1.1.2' />
<dependency id='Microsoft.AspNetCore.Mvc.Core' version='1.1.2' />
<dependency id='Microsoft.Extensions.Caching.Abstractions' version='1.1.1' />
<dependency id='Microsoft.Extensions.Configuration.Binder' version='1.1.1' />
<dependency id='Newtonsoft.Json' version='9.0.1' />
<dependency id='StyleCop.Analyzers' version='1.0.0' />
<dependency id='NuGet.Versioning' version='3.6.0' include='build' exclude='none' />
<dependency id='System.Xml.XDocument' version='4.3.0' />
</dependencies>'''
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ullink
package com.ullink.packageparser

import com.ullink.packagesparser.ProjectJsonParser
import groovy.json.JsonBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>...</Description>
<AssemblyTitle>ASP.NET Core MVC 6 Boilerplate Framework</AssemblyTitle>
<VersionPrefix>2.2.2</VersionPrefix>
<Authors>Muhammad Rehan Saeed (RehanSaeed.com)</Authors>
<TargetFrameworks>netstandard1.6;net461</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>Boilerplate.AspNetCore</AssemblyName>
<AssemblyOriginatorKeyFile>../../../Key.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageId>Boilerplate.AspNetCore</PackageId>
<PackageTags>ASP.NET;ASP.NET Core;MVC;MVC 6;Boilerplate;Muhammad Rehan Saeed;Framework</PackageTags>
<PackageReleaseNotes>Updated to ASP.NET Core 1.1.2.</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/RehanSaeed/ASP.NET-MVC-Boilerplate/master/Images/Icon.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/ASP-NET-MVC-Boilerplate/Framework</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/ASP-NET-MVC-Boilerplate/Framework/blob/master/LICENSE</PackageLicenseUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/ASP-NET-MVC-Boilerplate/Framework.git</RepositoryUrl>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Boilerplate\Boilerplate.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions">
<Version>1.1.2</Version>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="1.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.0.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="NuGet.Versioning" Version="3.6.0">
<IncludeAssets>build</IncludeAssets>
<ExcludeAssets>none</ExcludeAssets>
<PrivateAssets>contentfiles;analyzers;build</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
<PackageReference Include="System.Xml.XDocument" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

</Project>

0 comments on commit 451efe2

Please sign in to comment.