Example #1
0
        protected override void ExecuteCore()
        {
            var embeddedApphostPaths = new List <ITaskItem>();

            foreach (var runtimeIdentifier in ShimRuntimeIdentifiers.Select(r => r.ItemSpec))
            {
                var resolvedApphostAssetPath = GetApphostAsset(ApphostsForShimRuntimeIdentifiers, runtimeIdentifier);

                var packagedShimOutputDirectoryAndRid = Path.Combine(
                    PackagedShimOutputDirectory,
                    runtimeIdentifier);

                var appHostDestinationFilePath = Path.Combine(
                    packagedShimOutputDirectoryAndRid,
                    ToolCommandName + ExecutableExtension.ForRuntimeIdentifier(runtimeIdentifier));

                Directory.CreateDirectory(packagedShimOutputDirectoryAndRid);

                // per https://github.com/dotnet/cli/issues/9870 nuget layout (as in {packageid}/{packageversion}/tools/)is normalized version
                var normalizedPackageVersion = NuGetVersion.Parse(PackageVersion).ToNormalizedString();
                // This is the embedded string. We should normalize it on forward slash, so the file won't be different according to
                // build machine.
                var appBinaryFilePath = string.Join("/",
                                                    new[] {
                    ".store",
                    PackageId.ToLowerInvariant(),
                    normalizedPackageVersion,
                    PackageId.ToLowerInvariant(),
                    normalizedPackageVersion,
                    "tools",
                    NuGetUtils.ParseFrameworkName(TargetFrameworkMoniker).GetShortFolderName(),
                    "any",
                    ToolEntryPoint
                });

                AppHost.Create(
                    resolvedApphostAssetPath,
                    appHostDestinationFilePath,
                    appBinaryFilePath
                    );

                var item = new TaskItem(appHostDestinationFilePath);
                item.SetMetadata(MetadataKeys.ShimRuntimeIdentifier, runtimeIdentifier);
                embeddedApphostPaths.Add(item);
            }

            EmbeddedApphostPaths = embeddedApphostPaths.ToArray();
        }
Example #2
0
        protected override void ExecuteCore()
        {
            var embeddedApphostPaths = new List <ITaskItem>();

            foreach (var runtimeIdentifier in ShimRuntimeIdentifiers.Select(r => r.ItemSpec))
            {
                var packagedShimOutputDirectoryAndRid = Path.Combine(
                    PackagedShimOutputDirectory,
                    runtimeIdentifier);

                var appHostDestinationFilePath = Path.Combine(
                    packagedShimOutputDirectoryAndRid,
                    ToolCommandName + ExecutableExtension.ForRuntimeIdentifier(runtimeIdentifier));

                var item = new TaskItem(appHostDestinationFilePath);
                item.SetMetadata(MetadataKeys.ShimRuntimeIdentifier, runtimeIdentifier);
                embeddedApphostPaths.Add(item);
            }

            EmbeddedApphostPaths = embeddedApphostPaths.ToArray();
        }
Example #3
0
        private ITaskItem GetHostItem(string runtimeIdentifier,
                                      List <ITaskItem> knownAppHostPacksForTargetFramework,
                                      List <ITaskItem> packagesToDownload,
                                      string hostNameWithoutExtension,
                                      string itemName,
                                      bool isExecutable,
                                      bool errorIfNotFound)
        {
            var selectedAppHostPack = knownAppHostPacksForTargetFramework.Single();

            string appHostRuntimeIdentifiers = selectedAppHostPack.GetMetadata("AppHostRuntimeIdentifiers");
            string appHostPackPattern        = selectedAppHostPack.GetMetadata("AppHostPackNamePattern");
            string appHostPackVersion        = selectedAppHostPack.GetMetadata("AppHostPackVersion");

            if (!string.IsNullOrEmpty(RuntimeFrameworkVersion))
            {
                appHostPackVersion = RuntimeFrameworkVersion;
            }

            string bestAppHostRuntimeIdentifier = NuGetUtils.GetBestMatchingRid(
                new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath),
                runtimeIdentifier,
                appHostRuntimeIdentifiers.Split(';'),
                out bool wasInGraph);

            if (bestAppHostRuntimeIdentifier == null)
            {
                if (wasInGraph)
                {
                    //  NETSDK1084: There was no app host for available for the specified RuntimeIdentifier '{0}'.
                    if (errorIfNotFound)
                    {
                        Log.LogError(Strings.NoAppHostAvailable, runtimeIdentifier);
                    }
                    else
                    {
                        Log.LogMessage(Strings.NoAppHostAvailable, runtimeIdentifier);
                    }
                }
                else
                {
                    //  NETSDK1083: The specified RuntimeIdentifier '{0}' is not recognized.
                    if (errorIfNotFound)
                    {
                        Log.LogError(Strings.RuntimeIdentifierNotRecognized, runtimeIdentifier);
                    }
                    else
                    {
                        Log.LogMessage(Strings.RuntimeIdentifierNotRecognized, runtimeIdentifier);
                    }
                }
                return(null);
            }
            else
            {
                string hostPackName = appHostPackPattern.Replace("**RID**", bestAppHostRuntimeIdentifier);

                string hostRelativePathInPackage = Path.Combine("runtimes", bestAppHostRuntimeIdentifier, "native",
                                                                hostNameWithoutExtension + (isExecutable ? ExecutableExtension.ForRuntimeIdentifier(bestAppHostRuntimeIdentifier) : ".dll"));


                TaskItem appHostItem     = new TaskItem(itemName);
                string   appHostPackPath = null;
                if (!string.IsNullOrEmpty(TargetingPackRoot))
                {
                    appHostPackPath = Path.Combine(TargetingPackRoot, hostPackName, appHostPackVersion);
                }
                if (appHostPackPath != null && Directory.Exists(appHostPackPath))
                {
                    //  Use AppHost from packs folder
                    appHostItem.SetMetadata(MetadataKeys.PackageDirectory, appHostPackPath);
                    appHostItem.SetMetadata(MetadataKeys.Path, Path.Combine(appHostPackPath, hostRelativePathInPackage));
                }
                else
                {
                    // C++/CLI does not support package download && dedup error
                    if (!NuGetRestoreSupported && !packagesToDownload.Any(p => p.ItemSpec == hostPackName))
                    {
                        Log.LogError(
                            Strings.TargetingApphostPackMissingCannotRestore,
                            "Apphost",
                            $"{NetCoreTargetingPackRoot}\\{hostPackName}",
                            selectedAppHostPack.GetMetadata("TargetFramework") ?? "",
                            hostPackName,
                            appHostPackVersion
                            );
                    }

                    //  Download apphost pack
                    TaskItem packageToDownload = new TaskItem(hostPackName);
                    packageToDownload.SetMetadata(MetadataKeys.Version, appHostPackVersion);

                    packagesToDownload.Add(packageToDownload);

                    appHostItem.SetMetadata(MetadataKeys.NuGetPackageId, hostPackName);
                    appHostItem.SetMetadata(MetadataKeys.NuGetPackageVersion, appHostPackVersion);
                }

                appHostItem.SetMetadata(MetadataKeys.PathInPackage, hostRelativePathInPackage);
                appHostItem.SetMetadata(MetadataKeys.RuntimeIdentifier, runtimeIdentifier);

                return(appHostItem);
            }
        }
Example #4
0
        protected override void ExecuteCore()
        {
            var knownFrameworkReferences = KnownFrameworkReferences.Select(item => new KnownFrameworkReference(item))
                                           .Where(kfr => kfr.TargetFramework.Framework.Equals(TargetFrameworkIdentifier, StringComparison.OrdinalIgnoreCase) &&
                                                  NormalizeVersion(kfr.TargetFramework.Version) == NormalizeVersion(new Version(TargetFrameworkVersion)))
                                           .ToDictionary(kfr => kfr.Name);

            List <ITaskItem> packagesToDownload            = new List <ITaskItem>();
            List <ITaskItem> runtimeFrameworks             = new List <ITaskItem>();
            List <ITaskItem> targetingPacks                = new List <ITaskItem>();
            List <ITaskItem> runtimePacks                  = new List <ITaskItem>();
            List <string>    unresolvedFrameworkReferences = new List <string>();

            string appHostPackPattern        = null;
            string appHostPackVersion        = null;
            string appHostRuntimeIdentifiers = null;

            foreach (var frameworkReference in FrameworkReferences)
            {
                KnownFrameworkReference knownFrameworkReference;
                if (knownFrameworkReferences.TryGetValue(frameworkReference.ItemSpec, out knownFrameworkReference))
                {
                    if (!string.IsNullOrEmpty(knownFrameworkReference.AppHostPackNamePattern))
                    {
                        if (appHostPackPattern == null)
                        {
                            appHostPackPattern        = knownFrameworkReference.AppHostPackNamePattern;
                            appHostPackVersion        = knownFrameworkReference.LatestRuntimeFrameworkVersion;
                            appHostRuntimeIdentifiers = knownFrameworkReference.AppHostRuntimeIdentifiers;
                        }
                        else
                        {
                            throw new InvalidOperationException("Multiple FrameworkReferences defined an AppHostPack, which is not supposed to happen");
                        }
                    }

                    //  Get the path of the targeting pack in the targeting pack root (e.g. dotnet/ref)
                    TaskItem targetingPack = new TaskItem(knownFrameworkReference.Name);
                    targetingPack.SetMetadata(MetadataKeys.PackageName, knownFrameworkReference.TargetingPackName);
                    targetingPack.SetMetadata(MetadataKeys.PackageVersion, knownFrameworkReference.TargetingPackVersion);
                    targetingPack.SetMetadata(MetadataKeys.RelativePath, "");

                    string targetingPackPath = null;
                    if (!string.IsNullOrEmpty(TargetingPackRoot))
                    {
                        targetingPackPath = GetPackPath(knownFrameworkReference.TargetingPackName, knownFrameworkReference.TargetingPackVersion);
                    }
                    if (targetingPackPath != null && Directory.Exists(targetingPackPath))
                    {
                        targetingPack.SetMetadata(MetadataKeys.Path, targetingPackPath);
                        targetingPack.SetMetadata(MetadataKeys.PackageDirectory, targetingPackPath);
                    }
                    else
                    {
                        //  Download targeting pack
                        TaskItem packageToDownload = new TaskItem(knownFrameworkReference.TargetingPackName);
                        packageToDownload.SetMetadata(MetadataKeys.Version, knownFrameworkReference.TargetingPackVersion);

                        packagesToDownload.Add(packageToDownload);
                    }

                    targetingPacks.Add(targetingPack);

                    if (SelfContained &&
                        !string.IsNullOrEmpty(RuntimeIdentifier) &&
                        !string.IsNullOrEmpty(knownFrameworkReference.RuntimePackNamePatterns))
                    {
                        foreach (var runtimePackNamePattern in knownFrameworkReference.RuntimePackNamePatterns.Split(';'))
                        {
                            string runtimePackRuntimeIdentifier = GetBestRuntimeIdentifier(RuntimeIdentifier, knownFrameworkReference.RuntimePackRuntimeIdentifiers,
                                                                                           out bool wasInGraph);
                            if (runtimePackRuntimeIdentifier == null)
                            {
                                if (wasInGraph)
                                {
                                    //  NETSDK1082: There was no runtime pack for {0} available for the specified RuntimeIdentifier '{1}'.
                                    Log.LogError(Strings.NoRuntimePackAvailable, knownFrameworkReference.Name, RuntimeIdentifier);
                                }
                                else
                                {
                                    //  NETSDK1083: The specified RuntimeIdentifier '{0}' is not recognized.
                                    Log.LogError(Strings.UnsupportedRuntimeIdentifier, RuntimeIdentifier);
                                }
                            }
                            else
                            {
                                string runtimePackName = runtimePackNamePattern.Replace("**RID**", runtimePackRuntimeIdentifier);

                                TaskItem runtimePackItem = new TaskItem(runtimePackName);
                                runtimePackItem.SetMetadata(MetadataKeys.PackageName, runtimePackName);
                                runtimePackItem.SetMetadata(MetadataKeys.PackageVersion, knownFrameworkReference.LatestRuntimeFrameworkVersion);
                                runtimePackItem.SetMetadata("FrameworkReference", knownFrameworkReference.Name);
                                runtimePackItem.SetMetadata(MetadataKeys.RuntimeIdentifier, runtimePackRuntimeIdentifier);

                                runtimePacks.Add(runtimePackItem);

                                TaskItem packageToDownload = new TaskItem(runtimePackName);
                                packageToDownload.SetMetadata(MetadataKeys.Version, knownFrameworkReference.LatestRuntimeFrameworkVersion);

                                packagesToDownload.Add(packageToDownload);
                            }
                        }
                    }

                    TaskItem runtimeFramework = new TaskItem(knownFrameworkReference.RuntimeFrameworkName);

                    //  Use default (non roll-forward) version for now.  Eventually we'll need to add support for rolling
                    //  forward, and for publishing assets from a runtime pack for self-contained apps
                    runtimeFramework.SetMetadata(MetadataKeys.Version, knownFrameworkReference.DefaultRuntimeFrameworkVersion);

                    runtimeFrameworks.Add(runtimeFramework);
                }
                else
                {
                    unresolvedFrameworkReferences.Add(frameworkReference.ItemSpec);
                }
            }

            if (!string.IsNullOrEmpty(AppHostRuntimeIdentifier) && !string.IsNullOrEmpty(appHostPackPattern))
            {
                //  Choose AppHost RID as best match of the specified RID
                string bestAppHostRuntimeIdentifier = GetBestRuntimeIdentifier(AppHostRuntimeIdentifier, appHostRuntimeIdentifiers, out bool wasInGraph);
                if (bestAppHostRuntimeIdentifier == null)
                {
                    if (wasInGraph)
                    {
                        //  NETSDK1084: There was no app host for available for the specified RuntimeIdentifier '{0}'.
                        Log.LogError(Strings.NoAppHostAvailable, AppHostRuntimeIdentifier);
                    }
                    else
                    {
                        //  NETSDK1083: The specified RuntimeIdentifier '{0}' is not recognized.
                        Log.LogError(Strings.UnsupportedRuntimeIdentifier, AppHostRuntimeIdentifier);
                    }
                }
                else
                {
                    string appHostPackName = appHostPackPattern.Replace("**RID**", bestAppHostRuntimeIdentifier);

                    string appHostRelativePathInPackage = Path.Combine("runtimes", bestAppHostRuntimeIdentifier, "native",
                                                                       DotNetAppHostExecutableNameWithoutExtension + ExecutableExtension.ForRuntimeIdentifier(bestAppHostRuntimeIdentifier));


                    TaskItem appHostItem     = new TaskItem("AppHost");
                    string   appHostPackPath = null;
                    if (!string.IsNullOrEmpty(TargetingPackRoot))
                    {
                        appHostPackPath = GetPackPath(appHostPackName, appHostPackVersion);
                    }
                    if (appHostPackPath != null && Directory.Exists(appHostPackPath))
                    {
                        //  Use AppHost from packs folder
                        appHostItem.SetMetadata(MetadataKeys.Path, Path.Combine(appHostPackPath, appHostRelativePathInPackage));
                    }
                    else
                    {
                        //  Download apphost pack
                        TaskItem packageToDownload = new TaskItem(appHostPackName);
                        packageToDownload.SetMetadata(MetadataKeys.Version, appHostPackVersion);
                        packagesToDownload.Add(packageToDownload);

                        appHostItem.SetMetadata(MetadataKeys.PackageName, appHostPackName);
                        appHostItem.SetMetadata(MetadataKeys.PackageVersion, appHostPackVersion);
                        appHostItem.SetMetadata(MetadataKeys.RelativePath, appHostRelativePathInPackage);
                    }

                    AppHost = new ITaskItem[] { appHostItem };
                }
            }

            if (packagesToDownload.Any())
            {
                PackagesToDownload = packagesToDownload.ToArray();
            }

            if (runtimeFrameworks.Any())
            {
                RuntimeFrameworks = runtimeFrameworks.ToArray();
            }

            if (targetingPacks.Any())
            {
                TargetingPacks = targetingPacks.ToArray();
            }

            if (runtimePacks.Any())
            {
                RuntimePacks = runtimePacks.ToArray();
            }

            if (unresolvedFrameworkReferences.Any())
            {
                UnresolvedFrameworkReferences = unresolvedFrameworkReferences.ToArray();
            }
        }
Example #5
0
        private ITaskItem GetAppHostItem(string runtimeIdentifier, List <ITaskItem> knownAppHostPacksForTargetFramework,
                                         List <ITaskItem> packagesToDownload)
        {
            var selectedAppHostPack = knownAppHostPacksForTargetFramework.Single();

            string appHostRuntimeIdentifiers = selectedAppHostPack.GetMetadata("AppHostRuntimeIdentifiers");
            string appHostPackPattern        = selectedAppHostPack.GetMetadata("AppHostPackNamePattern");
            string appHostPackVersion        = selectedAppHostPack.GetMetadata("AppHostPackVersion");

            if (!string.IsNullOrEmpty(RuntimeFrameworkVersion))
            {
                appHostPackVersion = RuntimeFrameworkVersion;
            }

            string bestAppHostRuntimeIdentifier = NuGetUtils.GetBestMatchingRid(
                new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath),
                runtimeIdentifier,
                appHostRuntimeIdentifiers.Split(';'),
                out bool wasInGraph);

            if (bestAppHostRuntimeIdentifier == null)
            {
                if (wasInGraph)
                {
                    //  NETSDK1084: There was no app host for available for the specified RuntimeIdentifier '{0}'.
                    Log.LogError(Strings.NoAppHostAvailable, runtimeIdentifier);
                }
                else
                {
                    //  NETSDK1083: The specified RuntimeIdentifier '{0}' is not recognized.
                    Log.LogError(Strings.RuntimeIdentifierNotRecognized, runtimeIdentifier);
                }
                return(null);
            }
            else
            {
                string appHostPackName = appHostPackPattern.Replace("**RID**", bestAppHostRuntimeIdentifier);

                string appHostRelativePathInPackage = Path.Combine("runtimes", bestAppHostRuntimeIdentifier, "native",
                                                                   DotNetAppHostExecutableNameWithoutExtension + ExecutableExtension.ForRuntimeIdentifier(bestAppHostRuntimeIdentifier));


                TaskItem appHostItem     = new TaskItem("AppHost");
                string   appHostPackPath = null;
                if (!string.IsNullOrEmpty(TargetingPackRoot))
                {
                    appHostPackPath = Path.Combine(TargetingPackRoot, appHostPackName, appHostPackVersion);
                }
                if (appHostPackPath != null && Directory.Exists(appHostPackPath))
                {
                    //  Use AppHost from packs folder
                    appHostItem.SetMetadata(MetadataKeys.PackageDirectory, appHostPackPath);
                    appHostItem.SetMetadata(MetadataKeys.Path, Path.Combine(appHostPackPath, appHostRelativePathInPackage));
                }
                else
                {
                    //  Download apphost pack
                    TaskItem packageToDownload = new TaskItem(appHostPackName);
                    packageToDownload.SetMetadata(MetadataKeys.Version, appHostPackVersion);

                    packagesToDownload.Add(packageToDownload);

                    appHostItem.SetMetadata(MetadataKeys.RuntimeIdentifier, runtimeIdentifier);
                    appHostItem.SetMetadata(MetadataKeys.PackageName, appHostPackName);
                    appHostItem.SetMetadata(MetadataKeys.PackageVersion, appHostPackVersion);
                    appHostItem.SetMetadata(MetadataKeys.RelativePath, appHostRelativePathInPackage);
                }

                return(appHostItem);
            }
        }
Example #6
0
        protected override void ExecuteCore()
        {
            var embeddedApphostPaths = new List <ITaskItem>();

            foreach (var runtimeIdentifier in ShimRuntimeIdentifiers.Select(r => r.ItemSpec))
            {
                var resolvedApphostAssetPath = GetApphostAsset(ApphostsForShimRuntimeIdentifiers, runtimeIdentifier);

                var packagedShimOutputDirectoryAndRid = Path.Combine(
                    PackagedShimOutputDirectory,
                    runtimeIdentifier);

                var appHostDestinationFilePath = Path.Combine(
                    packagedShimOutputDirectoryAndRid,
                    ToolCommandName + ExecutableExtension.ForRuntimeIdentifier(runtimeIdentifier));

                Directory.CreateDirectory(packagedShimOutputDirectoryAndRid);

                // per https://github.com/dotnet/cli/issues/9870 nuget layout (as in {packageid}/{packageversion}/tools/)is normalized version
                var normalizedPackageVersion = NuGetVersion.Parse(PackageVersion).ToNormalizedString();
                // This is the embedded string. We should normalize it on forward slash, so the file won't be different according to
                // build machine.
                var appBinaryFilePath = string.Join("/",
                                                    new[] {
                    ".store",
                    PackageId.ToLowerInvariant(),
                    normalizedPackageVersion,
                    PackageId.ToLowerInvariant(),
                    normalizedPackageVersion,
                    "tools",
                    NuGetUtils.ParseFrameworkName(TargetFrameworkMoniker).GetShortFolderName(),
                    "any",
                    ToolEntryPoint
                });

                try
                {
                    var windowsGraphicalUserInterface = runtimeIdentifier.StartsWith("win") && "WinExe".Equals(OutputType, StringComparison.OrdinalIgnoreCase);
                    if (ResourceUpdater.IsSupportedOS() && runtimeIdentifier.StartsWith("win"))
                    {
                        HostWriter.CreateAppHost(appHostSourceFilePath: resolvedApphostAssetPath,
                                                 appHostDestinationFilePath: appHostDestinationFilePath,
                                                 appBinaryFilePath: appBinaryFilePath,
                                                 windowsGraphicalUserInterface: windowsGraphicalUserInterface,
                                                 assemblyToCopyResorcesFrom: IntermediateAssembly);
                    }
                    else
                    {
                        // by passing null to assemblyToCopyResorcesFrom, it will skip copying resources,
                        // which is only supported on Windows
                        if (windowsGraphicalUserInterface)
                        {
                            Log.LogWarning(Strings.AppHostCustomizationRequiresWindowsHostWarning);
                        }

                        HostWriter.CreateAppHost(appHostSourceFilePath: resolvedApphostAssetPath,
                                                 appHostDestinationFilePath: appHostDestinationFilePath,
                                                 appBinaryFilePath: appBinaryFilePath,
                                                 windowsGraphicalUserInterface: false,
                                                 assemblyToCopyResorcesFrom: null);
                    }
                }
                catch (AppNameTooLongException ex)
                {
                    throw new BuildErrorException(Strings.FileNameIsTooLong, ex.LongName);
                }
                catch (PlaceHolderNotFoundInAppHostException ex)
                {
                    throw new BuildErrorException(Strings.AppHostHasBeenModified, resolvedApphostAssetPath, BitConverter.ToString(ex.MissingPattern));
                }

                var item = new TaskItem(appHostDestinationFilePath);
                item.SetMetadata(MetadataKeys.ShimRuntimeIdentifier, runtimeIdentifier);
                embeddedApphostPaths.Add(item);
            }

            EmbeddedApphostPaths = embeddedApphostPaths.ToArray();
        }