mikefourie-zz / MSBuildExtensionPack

MIT License
366 stars 104 forks source link

Update Framework.NuGet.Packager to Enhance PopulateFolder() Method to Support Architecture & Platform #84

Open indomitableMan opened 6 years ago

indomitableMan commented 6 years ago

The PopulateFolder() method in packager.cs currently only supports the use of the <Framework> attribute, however NuGet packages can also support multiple processor architectures (e.g. 'x86' and 'x64') and platforms (e.g.'win81').

So if a user were to specify:

<LibraryFiles Include="..\dir\myLibrary.dll"> <Architecture>x86</Architecture> <Framework>net40</Framework> <Platform>win</Platform> </LibraryFiles>

The PopulateFolder() method would test for the presence of the <Architecture> and <Platform> tags before testing for the presence of the <Framework> tag, and if found, would populate the folders like this: .\runtimes\{platform}-{architecture}\lib\{framework}

For example: .\runtimes \win-x86 ____\lib ____\net40 ____\myLibrary.dll ____\net45 ____\myLibrary.dll

\win-x64 ____\lib ____\net40 ____\myLibrary.dll ____\net45 ____\myLibrary.dll

More information on this syntax is here: Supporting multiple .NET framework versions

indomitableMan commented 6 years ago

A modified foreach loop similar to this would work:

        foreach (var item in items)
        {
            var architecture = item.GetMetadata("architecture");
            var framework = item.GetMetadata("framework");
            var platform = item.GetMetadata("platform");
            if (!string.IsNullOrWhiteSpace(platform))
            {
                if (!string.IsNullOrWhiteSpace(architecture))
                {
                    runtime = Path.Combine(libDirectory.FullName, "runtime");
                    if (!Directory.Exists(runtime))
                    {
                        Directory.CreateDirectory(runtime);
                    }
                    platformWithArch = string.Concat(platform, "-", architecture);
                    fullPlatformAndArch = path.Combine(runtime, fullPlatformAndArch);
                    if (!Directory.Exists(fullPlatformAndArch))
                    {
                        Directory.CreateDirectory(fullPlatformAndArch);
                    }
                    if (!string.IsNullOrWhiteSpace(framework))
                    {
                        framework = Path.Combine(fullPlatformAndArch, framework);
                        if (!Directory.Exists(framework))
                        {
                            Directory.CreateDirectory(framework);
                        }

                        File.Copy(item.ItemSpec, Path.Combine(framework, Path.GetFileName(item.ItemSpec)));
                    }
                    else
                    {
                        File.Copy(item.ItemSpec, Path.Combine(fullPlatformAndArch, Path.GetFileName(item.ItemSpec)));
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(framework))
            {
                framework = Path.Combine(libDirectory.FullName, framework);
                if (!Directory.Exists(framework))
                {
                    Directory.CreateDirectory(framework);
                }

                File.Copy(item.ItemSpec, Path.Combine(framework, Path.GetFileName(item.ItemSpec)));
            }
            else
            {
                File.Copy(item.ItemSpec, Path.Combine(libDirectory.FullName, Path.GetFileName(item.ItemSpec)));
            }
        }
mikefourie-zz commented 5 years ago

Hi, please submit a pull request.

blorger commented 5 years ago

Is it really a good idea to package development dependency for every possible target platform? I've repackaged NuGet package as neutral (removed net35 folders and moved content of net40 folders one level up). Such package can now be used on any project, native and .NET. At least in Visual Studio 2015.

Why does NuGet package contain binaries for .NET 3.5? Is it to support some older version of Visual Studio? In such case it may be better to provide separate NuGet packages.