From 365b10112865e8cf175ab8058b56b30bcee19b31 Mon Sep 17 00:00:00 2001 From: Peter Ng Date: Thu, 6 Sep 2018 15:21:19 +0800 Subject: [PATCH 1/3] Add support of PackageReferences Change-Id: I9a311744f44d5d86605b5f4cb593ac0c4ca061c9 --- src/main/groovy/com/ullink/NuGetSpec.groovy | 2 ++ .../PackageReferenceParser.groovy | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy diff --git a/src/main/groovy/com/ullink/NuGetSpec.groovy b/src/main/groovy/com/ullink/NuGetSpec.groovy index e9487f9..fa38d28 100644 --- a/src/main/groovy/com/ullink/NuGetSpec.groovy +++ b/src/main/groovy/com/ullink/NuGetSpec.groovy @@ -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 @@ -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) diff --git a/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy b/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy new file mode 100644 index 0000000..2c7d4e7 --- /dev/null +++ b/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy @@ -0,0 +1,27 @@ +package com.ullink.packagesparser + + +class PackageReferenceParser implements NugetParser { + @Override + Collection getDependencies(File file) { + def defaultDependencies = [] + + def project = new XmlParser().parse(file) + + project.ItemGroup.each { node -> + node.PackageReference.findAll { it.PrivateAssets.text().trim() != 'all' }.each { reference -> + if (reference.IncludeAssets) { + defaultDependencies.add({ + dependency(id: reference.@Include, version: reference.Version.text(), include: reference.IncludeAssets.text()) + }) + } else { + defaultDependencies.add({ + dependency(id: reference.@Include, version: reference.Version.text()) + }) + } + reference.attributes().include + } + } + return defaultDependencies + } +} From cbab505a592aea0f1044ea64480e22d0f3218af0 Mon Sep 17 00:00:00 2001 From: Peter Ng Date: Fri, 7 Sep 2018 16:30:53 +0800 Subject: [PATCH 2/3] Fix case version is in attribute instead of node add unit test Change-Id: I2187628173fc3132bbc8c4a85d6ae00cc2847ec2 --- gradle.properties | 3 +- .../PackageReferenceParser.groovy | 48 +++++++++----- .../PackageReferenceParserSpec.groovy | 36 +++++++++++ .../ProjectJsonParserSpec.groovy | 2 +- .../packageparser/packagereference.csproj | 63 +++++++++++++++++++ 5 files changed, 135 insertions(+), 17 deletions(-) create mode 100644 src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy rename src/test/groovy/com/ullink/{ => packageparser}/ProjectJsonParserSpec.groovy (98%) create mode 100644 src/test/resources/com/ullink/packageparser/packagereference.csproj diff --git a/gradle.properties b/gradle.properties index 9213a62..75e51b8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1 @@ -version = 2.16 - +version = 2.17 \ No newline at end of file diff --git a/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy b/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy index 2c7d4e7..f9abb11 100644 --- a/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy +++ b/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy @@ -1,27 +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 XmlParser().parse(file) - - project.ItemGroup.each { node -> - node.PackageReference.findAll { it.PrivateAssets.text().trim() != 'all' }.each { reference -> - if (reference.IncludeAssets) { - defaultDependencies.add({ - dependency(id: reference.@Include, version: reference.Version.text(), include: reference.IncludeAssets.text()) - }) - } else { - defaultDependencies.add({ - dependency(id: reference.@Include, version: reference.Version.text()) - }) + 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 } - reference.attributes().include - } + + 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}"] + } + } diff --git a/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy b/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy new file mode 100644 index 0000000..1af6162 --- /dev/null +++ b/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy @@ -0,0 +1,36 @@ +/************************************************************************* + * 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 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: + result == [ + [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']]] + } +} \ No newline at end of file diff --git a/src/test/groovy/com/ullink/ProjectJsonParserSpec.groovy b/src/test/groovy/com/ullink/packageparser/ProjectJsonParserSpec.groovy similarity index 98% rename from src/test/groovy/com/ullink/ProjectJsonParserSpec.groovy rename to src/test/groovy/com/ullink/packageparser/ProjectJsonParserSpec.groovy index 7a84262..686f0ee 100644 --- a/src/test/groovy/com/ullink/ProjectJsonParserSpec.groovy +++ b/src/test/groovy/com/ullink/packageparser/ProjectJsonParserSpec.groovy @@ -1,4 +1,4 @@ -package com.ullink +package com.ullink.packageparser import com.ullink.packagesparser.ProjectJsonParser import groovy.json.JsonBuilder diff --git a/src/test/resources/com/ullink/packageparser/packagereference.csproj b/src/test/resources/com/ullink/packageparser/packagereference.csproj new file mode 100644 index 0000000..b02d726 --- /dev/null +++ b/src/test/resources/com/ullink/packageparser/packagereference.csproj @@ -0,0 +1,63 @@ + + + + ... + ASP.NET Core MVC 6 Boilerplate Framework + 2.2.2 + Muhammad Rehan Saeed (RehanSaeed.com) + netstandard1.6;net461 + true + true + Boilerplate.AspNetCore + ../../../Key.snk + true + true + Boilerplate.AspNetCore + ASP.NET;ASP.NET Core;MVC;MVC 6;Boilerplate;Muhammad Rehan Saeed;Framework + Updated to ASP.NET Core 1.1.2. + https://raw.githubusercontent.com/RehanSaeed/ASP.NET-MVC-Boilerplate/master/Images/Icon.png + https://github.com/ASP-NET-MVC-Boilerplate/Framework + https://github.com/ASP-NET-MVC-Boilerplate/Framework/blob/master/LICENSE + true + git + https://github.com/ASP-NET-MVC-Boilerplate/Framework.git + false + false + false + + + + + + + + + 1.1.2 + + + + + + + All + + + build + none + contentfiles;analyzers;build + + + + + + + + + + + + + + + + \ No newline at end of file From 4ec637347693cbb9611e3d96022388e06d52649a Mon Sep 17 00:00:00 2001 From: Peter Ng Date: Sat, 8 Sep 2018 16:30:53 +0800 Subject: [PATCH 3/3] Add and fix unit test --- .../com/ullink/MSBuildTaskBuilder.groovy | 5 +++ .../groovy/com/ullink/NuGetSpecTest.groovy | 44 +++++++++++++++++++ .../PackageReferenceParserSpec.groovy | 29 ++++++++---- 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/test/groovy/com/ullink/MSBuildTaskBuilder.groovy b/src/test/groovy/com/ullink/MSBuildTaskBuilder.groovy index 9bef663..2301968 100644 --- a/src/test/groovy/com/ullink/MSBuildTaskBuilder.groovy +++ b/src/test/groovy/com/ullink/MSBuildTaskBuilder.groovy @@ -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 + } } diff --git a/src/test/groovy/com/ullink/NuGetSpecTest.groovy b/src/test/groovy/com/ullink/NuGetSpecTest.groovy index e003355..53f547d 100644 --- a/src/test/groovy/com/ullink/NuGetSpecTest.groovy +++ b/src/test/groovy/com/ullink/NuGetSpecTest.groovy @@ -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 = + ''' + + + foo + 2.1 + fooDescription + + + + + + + + + + + + ''' + def nuspecGenerated = project.tasks.nugetSpec.generateNuspec() + assertXMLEqual(expected, nuspecGenerated) + } + } } diff --git a/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy b/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy index 1af6162..1bb0b36 100644 --- a/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy +++ b/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy @@ -12,6 +12,7 @@ package com.ullink.packageparser import com.ullink.packagesparser.PackageReferenceParser +import groovy.xml.MarkupBuilder import spock.lang.Specification class PackageReferenceParserSpec extends Specification { @@ -23,14 +24,24 @@ class PackageReferenceParserSpec extends Specification { def result = new PackageReferenceParser().getDependencies(csproj) then: - result == [ - [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']]] + def writer = new StringWriter() + def xml = new MarkupBuilder(writer) + xml.dependencies() { + result.each { + it.resolveStrategy = DELEGATE_FIRST + it.delegate = delegate + it.call() + } + } + writer.toString() == ''' + + + + + + + + +''' } } \ No newline at end of file