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/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..f9abb11 --- /dev/null +++ b/src/main/groovy/com/ullink/packagesparser/PackageReferenceParser.groovy @@ -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}"] + } + +} 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 new file mode 100644 index 0000000..1bb0b36 --- /dev/null +++ b/src/test/groovy/com/ullink/packageparser/PackageReferenceParserSpec.groovy @@ -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() == ''' + + + + + + + + +''' + } +} \ 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