public bool IsDependencyInstalled(CodeGenerationContext context) { if (context == null) { throw new ArgumentNullException("context"); } bool isODataAssemblyReferenced = ProjectReferences.IsAssemblyReferenced(context.ActiveProject, AssemblyVersions.ODataAssemblyName); // It is possible that this function could be called multiple times to check the status of dependency installation. // Hence, updating the value of any previously stored state with the most recent status of the referenced assembly. context.Items[ContextKeys.IsODataAssemblyReferencedKey] = isODataAssemblyReferenced; if (context == null) { throw new ArgumentNullException("context"); } bool isWebApiAssemblyReferenced = ProjectReferences.IsAssemblyReferenced(context.ActiveProject, AssemblyVersions.WebApiAssemblyName); // It is possible that this function could be called multiple times to check the status of dependency installation. // Hence, updating the value of any previously stored state with the most recent status of the referenced assembly. context.Items[ContextKeys.IsWebApiAssemblyReferencedKey] = isWebApiAssemblyReferenced; return(isWebApiAssemblyReferenced && isODataAssemblyReferenced); }
// The parameter 'latestKnownAssemblyVersion' can be null and if yes will be // infered from 'assemblyReferenceName' parameter. // I added that as a parameter to make unit testing easier. internal static void GetPackageFileNameForPackage(CodeGenerationContext context, IEnumerable <IVsPackageMetadata> installedPackages, string packageId, string assemblyReferenceName, Version minSupportedAssemblyReferenceVersion, ref string packageFileName) { if (ProjectReferences.IsAssemblyReferenced(context.ActiveProject, assemblyReferenceName)) { // If the project has reference to MVC version 5.0 or // above the corresponding package versions file is loaded from the file system. IVsPackageMetadata installedPackage = installedPackages .Where(package => String.Equals(packageId, package.Id, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); Version versionForPackageFile; // First try to get the package version and if not fallback to assembly reference version. if (installedPackage == null || !SemanticVersionParser.TryParse(installedPackage.VersionString, out versionForPackageFile)) { // Note: In this scenario where user does not have a package // reference but only has assembly reference, we will end up // using 5.1.0 instead of 5.1.1 version of package file (even // if the assembly reference is the same as the one shipped in 5.1.1) // because the assembly version did not change for 5.1.1 versionForPackageFile = ProjectReferences.GetAssemblyVersion(context.ActiveProject, assemblyReferenceName); } // Version should have been either available from package or atleast the assembly reference. Contract.Assert(versionForPackageFile != null); // For example : If 5.1 is the latest tooling version and if we release just the NuGet packages for a higher version of runtime (may be 5.2), // we will still continue to scaffold the latest tooling version (in this case 5.1). if (versionForPackageFile >= minSupportedAssemblyReferenceVersion && versionForPackageFile <= _latestKnownPackageVersion) { packageFileName = GetPackageVersionsFileName(versionForPackageFile); } } }