public static Dictionary <string, HashSet <string> > LoadCustomViewMapFile(IBuildEngine4 engine, string mapFile) { var cachedMap = (Dictionary <string, HashSet <string> >)engine?.GetRegisteredTaskObject(mapFile, RegisteredTaskObjectLifetime.Build); if (cachedMap != null) { return(cachedMap); } var map = new Dictionary <string, HashSet <string> > (); if (!File.Exists(mapFile)) { return(map); } foreach (var s in File.ReadLines(mapFile)) { var items = s.Split(new char [] { ';' }, count: 2); var key = items [0]; var value = items [1]; HashSet <string> set; if (!map.TryGetValue(key, out set)) { map.Add(key, set = new HashSet <string> ()); } set.Add(value); } return(map); }
public AssemblyFoldersExResolver(string searchPathElement, GetAssemblyName getAssemblyName, FileExists fileExists, GetRegistrySubKeyNames getRegistrySubKeyNames, GetRegistrySubKeyDefaultValue getRegistrySubKeyDefaultValue, GetAssemblyRuntimeVersion getRuntimeVersion, OpenBaseKey openBaseKey, Version targetedRuntimeVesion, ProcessorArchitecture targetProcessorArchitecture, bool compareProcessorArchitecture, IBuildEngine buildEngine) : base(searchPathElement, getAssemblyName, fileExists, getRuntimeVersion, targetedRuntimeVesion, targetProcessorArchitecture, compareProcessorArchitecture) { _buildEngine = buildEngine as IBuildEngine4; _getRegistrySubKeyNames = getRegistrySubKeyNames; _getRegistrySubKeyDefaultValue = getRegistrySubKeyDefaultValue; _openBaseKey = openBaseKey; }
/// <summary> /// Construct. /// </summary> public AssemblyFoldersFromConfigResolver(string searchPathElement, GetAssemblyName getAssemblyName, FileExists fileExists, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVesion, ProcessorArchitecture targetProcessorArchitecture, bool compareProcessorArchitecture, IBuildEngine buildEngine, TaskLoggingHelper log) : base( searchPathElement, getAssemblyName, fileExists, getRuntimeVersion, targetedRuntimeVesion, targetProcessorArchitecture, compareProcessorArchitecture) { _buildEngine = buildEngine as IBuildEngine4; _taskLogger = log; }
public static Aapt2Daemon GetInstance(IBuildEngine4 engine, string aapt2, int numberOfInstances, int initalNumberOfDaemons, bool registerInDomain = false) { var area = registerInDomain ? RegisteredTaskObjectLifetime.AppDomain : RegisteredTaskObjectLifetime.Build; var daemon = engine.GetRegisteredTaskObjectAssemblyLocal <Aapt2Daemon> (RegisterTaskObjectKey, area); if (daemon == null) { daemon = new Aapt2Daemon(aapt2, numberOfInstances, initalNumberOfDaemons); engine.RegisterTaskObjectAssemblyLocal(RegisterTaskObjectKey, daemon, area, allowEarlyCollection: false); } return(daemon); }
public static Aapt2Daemon GetInstance(IBuildEngine4 engine, string aapt2, int numberOfInstances, int initalNumberOfDaemons, bool registerInDomain = false) { var area = registerInDomain ? RegisteredTaskObjectLifetime.AppDomain : RegisteredTaskObjectLifetime.Build; Aapt2Daemon daemon = (Aapt2Daemon)engine.GetRegisteredTaskObject(typeof(Aapt2Daemon).FullName, area); if (daemon == null) { daemon = new Aapt2Daemon(aapt2, numberOfInstances, initalNumberOfDaemons); engine.RegisterTaskObject(typeof(Aapt2Daemon).FullName, daemon, area, allowEarlyCollection: false); } return(daemon); }
public static bool SaveCustomViewMapFile(IBuildEngine4 engine, string mapFile, Dictionary <string, HashSet <string> > map) { engine?.RegisterTaskObject(mapFile, map, RegisteredTaskObjectLifetime.Build, allowEarlyCollection: false); using (var writer = MemoryStreamPool.Shared.CreateStreamWriter()) { foreach (var i in map.OrderBy(x => x.Key)) { foreach (var v in i.Value.OrderBy(x => x)) { writer.WriteLine($"{i.Key};{v}"); } } writer.Flush(); return(CopyIfStreamChanged(writer.BaseStream, mapFile)); } }
/// <summary> /// Gets the registered task object. /// </summary> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="buildEngine">The build engine.</param> /// <param name="key">The key.</param> /// <param name="ctor">The ctor.</param> /// <param name="lifetime">The lifetime.</param> /// <returns></returns> public static TValue GetRegisteredTaskObject <TValue>(this IBuildEngine4 buildEngine, string key, Func <TValue> ctor, RegisteredTaskObjectLifetime lifetime = RegisteredTaskObjectLifetime.Build) { // when run as app, we don't have a filled Task if (buildEngine == null) { return(ctor()); } // otherwise, this works var o = buildEngine.GetRegisteredTaskObject(key, lifetime); if (o == null) { o = ctor(); buildEngine.RegisterTaskObject(key, o, lifetime, false); } return((TValue)o); }
public static void SaveCustomViewMapFile(IBuildEngine4 engine, string mapFile, Dictionary <string, HashSet <string> > map) { engine?.RegisterTaskObject(mapFile, map, RegisteredTaskObjectLifetime.Build, allowEarlyCollection: false); var temp = Path.GetTempFileName(); try { using (var m = new StreamWriter(temp)) { foreach (var i in map.OrderBy(x => x.Key)) { foreach (var v in i.Value.OrderBy(x => x)) { m.WriteLine($"{i.Key};{v}"); } } } CopyIfChanged(temp, mapFile); } finally { File.Delete(temp); } }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <param name="getRuntimeVersion">Delegate to get the runtime version from a file path</param> /// <param name="targetedRuntimeVersion">What version of the runtime are we targeting</param> /// <param name="fullFusionName">Are we guranteed to have a full fusion name. This really can only happen if we have already resolved the assembly</param> /// <returns>The path to the assembly. Empty if none exists.</returns> internal static string GetLocation ( IBuildEngine4 buildEngine, AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion ) { ConcurrentDictionary <AssemblyNameExtension, string> fusionNameToResolvedPath = null; bool useGacRarCache = Environment.GetEnvironmentVariable("MSBUILDDISABLEGACRARCACHE") == null; if (buildEngine != null && useGacRarCache) { string key = "44d78b60-3bbe-48fe-9493-04119ebf515f" + "|" + targetProcessorArchitecture.ToString() + "|" + targetedRuntimeVersion.ToString() + "|" + fullFusionName.ToString() + "|" + specificVersion.ToString(); fusionNameToResolvedPath = buildEngine.GetRegisteredTaskObject(key, RegisteredTaskObjectLifetime.Build) as ConcurrentDictionary <AssemblyNameExtension, string>; if (fusionNameToResolvedPath == null) { fusionNameToResolvedPath = new ConcurrentDictionary <AssemblyNameExtension, string>(AssemblyNameComparer.GenericComparer); buildEngine.RegisterTaskObject(key, fusionNameToResolvedPath, RegisteredTaskObjectLifetime.Build, true /* dispose early ok*/); } else { if (fusionNameToResolvedPath.ContainsKey(strongName)) { string fusionName = null; fusionNameToResolvedPath.TryGetValue(strongName, out fusionName); return(fusionName); } } } // Optimize out the case where the public key token is null, if it is null it is not a strongly named assembly and CANNOT be in the gac. // also passing it would cause the gac enumeration method to throw an exception indicating the assembly is not a strongnamed assembly. string location = null; // If the publickeyToken is null and the publickeytoken is in the fusion name then this means we are passing in a null or empty PublicKeyToken and then this cannot possibly be in the gac. if ((strongName.GetPublicKeyToken() == null || strongName.GetPublicKeyToken().Length == 0) && strongName.FullName.IndexOf("PublicKeyToken", StringComparison.OrdinalIgnoreCase) != -1) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } // A delegate was not passed in to use the default one getPathFromFusionName = getPathFromFusionName ?? pathFromFusionName; // A delegate was not passed in to use the default one getGacEnumerator = getGacEnumerator ?? gacEnumerator; // If we have no processor architecture set then we can tryout a number of processor architectures. if (!strongName.HasProcessorArchitectureInFusionName) { if (targetProcessorArchitecture != ProcessorArchitecture.MSIL && targetProcessorArchitecture != ProcessorArchitecture.None) { string processorArchitecture = ResolveAssemblyReference.ProcessorArchitectureToString(targetProcessorArchitecture); // Try processor specific first. if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, processorArchitecture, getPathFromFusionName); } else { location = GetLocationImpl(strongName, processorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } } // Next, try MSIL if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, "MSIL", getPathFromFusionName); } else { location = GetLocationImpl(strongName, "MSIL", getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } } // Next, try no processor architecure if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, null, getPathFromFusionName); } else { location = GetLocationImpl(strongName, null, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return(location); } if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, null); } return(null); }
public RuntimeGraphCache(TaskBase task) { _buildEngine = task.BuildEngine4; _log = task.Log; }
public LockFileCache(IBuildEngine4 buildEngine) { _buildEngine = buildEngine; }
public static void SaveResourceCaseMap(IBuildEngine4 engine, Dictionary <string, string> map) => engine.RegisterTaskObjectAssemblyLocal(ResourceCaseMapKey, map, RegisteredTaskObjectLifetime.Build);
/// <summary> /// IBuildEngine4.RegisterTaskObject, but adds the current assembly path into the key /// </summary> public static void RegisterTaskObjectAssemblyLocal(this IBuildEngine4 engine, object key, object value, RegisteredTaskObjectLifetime lifetime, bool allowEarlyCollection = false) => engine.RegisterTaskObject((AssemblyLocation, key), value, lifetime, allowEarlyCollection);
/// <summary> /// Construct. /// </summary> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64, the processor architecture being targetted.</param> public GacResolver(System.Reflection.ProcessorArchitecture targetProcessorArchitecture, string searchPathElement, GetAssemblyName getAssemblyName, FileExists fileExists, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVesion, IBuildEngine buildEngine) : base(searchPathElement, getAssemblyName, fileExists, getRuntimeVersion, targetedRuntimeVesion, targetProcessorArchitecture, true) { _buildEngine = buildEngine as IBuildEngine4; }
public FrameworkListReader(IBuildEngine4 buildEngine) { _buildEngine = buildEngine; }
public static Dictionary <string, string> LoadResourceCaseMap(IBuildEngine4 engine) => engine.GetRegisteredTaskObject(ResourceCaseMapKey, RegisteredTaskObjectLifetime.Build) as Dictionary <string, string> ?? new Dictionary <string, string> (0);
/// <summary> /// Get the SDK. /// </summary> /// <returns>true</returns> public override bool Execute() { // TargetPlatformVersion and TargetPlatformIdentifier are requried to correctly look for SDKs. if (String.IsNullOrEmpty(TargetPlatformVersion) || String.IsNullOrEmpty(TargetPlatformIdentifier)) { Log.LogErrorWithCodeFromResources("GetInstalledSDKs.TargetPlatformInformationMissing"); return(false); } // Dictionary of ESDKs. Each entry is a (location, platform version) tuple IDictionary <string, Tuple <string, string> > installedSDKs = null; try { Log.LogMessageFromResources("GetInstalledSDKs.SearchingForSDKs", _targetPlatformIdentifier, _targetPlatformVersion); Version platformVersion = Version.Parse(TargetPlatformVersion); installedSDKs = ToolLocationHelper.GetPlatformExtensionSDKLocationsAndVersions(SDKDirectoryRoots, SDKExtensionDirectoryRoots, SDKRegistryRoot, TargetPlatformIdentifier, platformVersion); } catch (Exception e) { if (ExceptionHandling.IsCriticalException(e)) { throw; } Log.LogErrorWithCodeFromResources("GetInstalledSDKs.CouldNotGetSDKList", e.Message); } List <ITaskItem> outputItems = new List <ITaskItem>(); if (installedSDKs != null && installedSDKs.Count > 0) { Log.LogMessageFromResources(MessageImportance.Low, "GetInstalledSDKs.FoundSDKs", installedSDKs.Count); Log.LogMessageFromResources(MessageImportance.Low, "GetInstalledSDKs.ListInstalledSDKs"); foreach (KeyValuePair <string, Tuple <string, string> > sdk in installedSDKs) { string sdkInfo = ResourceUtilities.FormatResourceString("GetInstalledSDKs.SDKNameAndLocation", sdk.Key, sdk.Value.Item1); Log.LogMessageFromResources(MessageImportance.Low, "ResolveAssemblyReference.FourSpaceIndent", sdkInfo); TaskItem item = new TaskItem(sdk.Value.Item1); item.SetMetadata("SDKName", sdk.Key); item.SetMetadata("PlatformVersion", sdk.Value.Item2); // Need to stash these so we can unroll the platform via GetMatchingPlatformSDK when we get the reference files for the sdks item.SetMetadata(DirectoryRootsMetadataName, String.Join(";", SDKDirectoryRoots ?? new string[0])); item.SetMetadata(ExtensionDirectoryRootsMetadataName, String.Join(";", SDKExtensionDirectoryRoots ?? new string[0])); item.SetMetadata(RegistryRootMetadataName, SDKRegistryRoot); outputItems.Add(item); } } else { Log.LogWarningWithCodeFromResources("GetInstalledSDKs.NoSDksFound"); } InstalledSDKs = outputItems.ToArray(); // We need to register an object so that at the end of the build we will clear the static toolLocationhelper caches. // this is important because if someone adds an SDK between builds we would not know about it and not be able to use it. // This code is mainly used to deal with the case where msbuild nodes hang around between builds. IBuildEngine4 buildEngine4 = BuildEngine as IBuildEngine4; if (buildEngine4 != null) { object staticCacheDisposer = buildEngine4.GetRegisteredTaskObject(StaticSDKCacheKey, RegisteredTaskObjectLifetime.Build); if (staticCacheDisposer == null) { BuildCacheDisposeWrapper staticDisposer = new BuildCacheDisposeWrapper(new BuildCacheDisposeWrapper.CallDuringDispose(ToolLocationHelper.ClearSDKStaticCache)); buildEngine4.RegisterTaskObject(StaticSDKCacheKey, staticDisposer, RegisteredTaskObjectLifetime.Build, allowEarlyCollection: false); } } return(!Log.HasLoggedErrors); }
/// <summary> /// Given a strong name, find its path in the GAC. /// </summary> /// <param name="strongName">The strong name.</param> /// <param name="targetProcessorArchitecture">Like x86 or IA64\AMD64.</param> /// <param name="getRuntimeVersion">Delegate to get the runtime version from a file path</param> /// <param name="targetedRuntimeVersion">What version of the runtime are we targeting</param> /// <param name="fullFusionName">Are we guranteed to have a full fusion name. This really can only happen if we have already resolved the assembly</param> /// <returns>The path to the assembly. Empty if none exists.</returns> internal static string GetLocation ( IBuildEngine4 buildEngine, AssemblyNameExtension strongName, ProcessorArchitecture targetProcessorArchitecture, GetAssemblyRuntimeVersion getRuntimeVersion, Version targetedRuntimeVersion, bool fullFusionName, FileExists fileExists, GetPathFromFusionName getPathFromFusionName, GetGacEnumerator getGacEnumerator, bool specificVersion ) { ConcurrentDictionary<AssemblyNameExtension, string> fusionNameToResolvedPath = null; bool useGacRarCache = Environment.GetEnvironmentVariable("MSBUILDDISABLEGACRARCACHE") == null; if (buildEngine != null && useGacRarCache) { string key = "44d78b60-3bbe-48fe-9493-04119ebf515f" + "|" + targetProcessorArchitecture.ToString() + "|" + targetedRuntimeVersion.ToString() + "|" + fullFusionName.ToString() + "|" + specificVersion.ToString(); fusionNameToResolvedPath = buildEngine.GetRegisteredTaskObject(key, RegisteredTaskObjectLifetime.Build) as ConcurrentDictionary<AssemblyNameExtension, string>; if (fusionNameToResolvedPath == null) { fusionNameToResolvedPath = new ConcurrentDictionary<AssemblyNameExtension, string>(AssemblyNameComparer.GenericComparer); buildEngine.RegisterTaskObject(key, fusionNameToResolvedPath, RegisteredTaskObjectLifetime.Build, true /* dispose early ok*/); } else { if (fusionNameToResolvedPath.ContainsKey(strongName)) { string fusionName = null; fusionNameToResolvedPath.TryGetValue(strongName, out fusionName); return fusionName; } } } // Optimize out the case where the public key token is null, if it is null it is not a strongly named assembly and CANNOT be in the gac. // also passing it would cause the gac enumeration method to throw an exception indicating the assembly is not a strongnamed assembly. string location = null; // If the publickeyToken is null and the publickeytoken is in the fusion name then this means we are passing in a null or empty PublicKeyToken and then this cannot possibly be in the gac. if ((strongName.GetPublicKeyToken() == null || strongName.GetPublicKeyToken().Length == 0) && strongName.FullName.IndexOf("PublicKeyToken", StringComparison.OrdinalIgnoreCase) != -1) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } // A delegate was not passed in to use the default one getPathFromFusionName = getPathFromFusionName ?? pathFromFusionName; // A delegate was not passed in to use the default one getGacEnumerator = getGacEnumerator ?? gacEnumerator; // If we have no processor architecture set then we can tryout a number of processor architectures. if (!strongName.HasProcessorArchitectureInFusionName) { if (targetProcessorArchitecture != ProcessorArchitecture.MSIL && targetProcessorArchitecture != ProcessorArchitecture.None) { string processorArchitecture = ResolveAssemblyReference.ProcessorArchitectureToString(targetProcessorArchitecture); // Try processor specific first. if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, processorArchitecture, getPathFromFusionName); } else { location = GetLocationImpl(strongName, processorArchitecture, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } } // Next, try MSIL if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, "MSIL", getPathFromFusionName); } else { location = GetLocationImpl(strongName, "MSIL", getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } } // Next, try no processor architecure if (fullFusionName) { location = CheckForFullFusionNameInGac(strongName, null, getPathFromFusionName); } else { location = GetLocationImpl(strongName, null, getRuntimeVersion, targetedRuntimeVersion, fileExists, getPathFromFusionName, getGacEnumerator, specificVersion); } if (location != null && location.Length > 0) { if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, location); } return location; } if (fusionNameToResolvedPath != null) { fusionNameToResolvedPath.TryAdd(strongName, null); } return null; }
public SolutionInfoFactory(TaskLoggingHelper logger, IBuildEngine4 buildEngine) { _logger = logger; _buildEngine = buildEngine; }
/// <summary> /// Construct. /// </summary> public AssemblyFoldersExResolver(string searchPathElement, GetAssemblyName getAssemblyName, FileExists fileExists, GetRegistrySubKeyNames getRegistrySubKeyNames, GetRegistrySubKeyDefaultValue getRegistrySubKeyDefaultValue, GetAssemblyRuntimeVersion getRuntimeVersion, OpenBaseKey openBaseKey, Version targetedRuntimeVesion, ProcessorArchitecture targetProcessorArchitecture, bool compareProcessorArchitecture, IBuildEngine buildEngine) : base(searchPathElement, getAssemblyName, fileExists, getRuntimeVersion, targetedRuntimeVesion, targetProcessorArchitecture, compareProcessorArchitecture) { _buildEngine = buildEngine as IBuildEngine4; _getRegistrySubKeyNames = getRegistrySubKeyNames; _getRegistrySubKeyDefaultValue = getRegistrySubKeyDefaultValue; _openBaseKey = openBaseKey; }
public LockFileCache(Task task) { _buildEngine = task.BuildEngine4; _log = task.Log; }
public static void SaveResourceCaseMap(IBuildEngine4 engine, Dictionary <string, string> map) => engine.RegisterTaskObject(ResourceCaseMapKey, map, RegisteredTaskObjectLifetime.Build, allowEarlyCollection: false);