/// <summary> /// The imported environment variables do NOT override eventually already present values. /// Projects or other settings cannot be imported ! /// </summary> private void ReadImports(Dictionary <string, string> env, Dictionary <string, List <PackageType> > bundle, List <NuGetPackageInfo> packages) { //Evaluate eventually defined import(s) if (_slnx.import != null) { foreach (var import in _slnx.import) { Assert(string.IsNullOrEmpty(import.path) ^ string.IsNullOrEmpty(import.bundle), "path and bundle are exclusive attributes in an import element"); if (!string.IsNullOrEmpty(import.path)) //File import { var slnxImportFile = Path.GetFullPath(SafeExpandAndTrimEnvironmentVariables(import.path)); if (!File.Exists(slnxImportFile)) { throw new Exception(string.Format("SLNX import not found, file path: {0}", slnxImportFile)); } var slnx = SlnxHandler.ReadSlnx(slnxImportFile); var imported = new SlnxHandler(slnxImportFile, _fileWriter, _logger, _offlineMode); _imports.Add(imported); ExtendDictionary(env, slnx.env, false); ExtendDictionary(bundle, slnx.bundle, false); } else //if (!string.IsNullOrEmpty(import.bundle)) { Assert(_packageBundles.ContainsKey(import.bundle), "Missing bundle with key '{0}'", import.bundle); ExtendList(packages, bundle[import.bundle].ToArray(), _offlineCache); } } } }
void AppendDebugElement(XmlDocument nugetDebugXml, CsProject referencingProject, NuGetClientHelper.NuGetPackage debugPackage, SlnxHandler debugHandler) { var propertyGroup = nugetDebugXml.CreateNode(XmlNodeType.Element, "PropertyGroup", null); nugetDebugXml.DocumentElement.AppendChild(propertyGroup); propertyGroup.InnerXml = string.Format("<{0}>1</{0}>", NuGetClientHelper.NuGetPackage.GetDebugEnvironmentVariableKey(debugPackage.Identity.Id)); var itemGroup = nugetDebugXml.CreateNode(XmlNodeType.Element, "ItemGroup", null); nugetDebugXml.DocumentElement.AppendChild(itemGroup); itemGroup.InnerXml = ""; HashSet <string> projectReferences = new HashSet <string>(); var projectCandidates = debugHandler.Projects.Where(x => !x.IsTestProject); foreach (var referencedProject in projectCandidates) { if (referencingProject.AssemblyReferences.Any(r => referencedProject.Name == Path.GetFileNameWithoutExtension(r.HintPath))) { projectReferences.Add(referencedProject.FullPath); } } var matchingPackage = referencingProject.PackageReferencesFromSlnX.Where(x => x.Identity.Id.Equals(debugPackage.Identity.Id, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (matchingPackage != null) //The debug project is referenced as NuGet package, add all Projects in the solution { projectCandidates.ToList().ForEach(x => projectReferences.Add(x.FullPath)); } foreach (var p in projectReferences) { itemGroup.InnerXml = string.Format("{0}<ProjectReference Include=\"{1}\"/>", itemGroup.InnerXml, p); } }
public SlnxHandler(string fName, SlnXType userSettings, IFileWriter writer, ILogger logger, string debugPackageId, bool offlineMode) { _logger = logger; _fileWriter = writer; _offlineMode = offlineMode; if (!string.IsNullOrEmpty(debugPackageId)) { userSettings = null; //Ignore use settings in case of an import via <debug package.../> } _slnxPath = Path.GetFullPath(fName); Assert(File.Exists(_slnxPath), "The provided SlnX file '{0}' is not a file or it doesn't exists", _slnxPath); _slnxDirectory = Path.GetDirectoryName(_slnxPath); _slnxFile = Path.GetFileName(fName); _slnxName = Path.GetFileNameWithoutExtension(fName); _specialSlnxKeys["slnx"] = _slnxDirectory; _slnx = ReadSlnx(fName); AppendSpecialKeys(false); ExtendDictionary(_environmentVariables, _slnx.env, true); ExtendDictionary(_packageBundles, _slnx.bundle, true); TryApplyUserEnvironmentValues(userSettings); //Eventually apply user settings SetAll(_environmentVariables); // to allow import having env variable in the path ExpandAll(_environmentVariables); // to allow import having env variable with special keys in the path ReadImports(_environmentVariables, _packageBundles, _packagesInfo); TryApplyUserEnvironmentValues(userSettings); //re-Apply, to override value from the imports ExpandAll(_environmentVariables); // to apply imports & user env vars string enforcedContainer = null; if (!string.IsNullOrEmpty(debugPackageId)) { enforcedContainer = $"_DEBUG/{debugPackageId}"; } if (offlineMode) { _offlineCache = new Uri(PackagesPath); _logger.Info($"Offline mode for {SlnxName}, using {PackagesPath} as package source."); } ExtendList(_packagesInfo, _slnx.package, _offlineCache); _projects = FindProjects(_slnx.project, ProjectsSearchPath, _slnx.skip, enforcedContainer); if (string.IsNullOrEmpty(debugPackageId)) //Main SlnX { EvalueteDebugPackages(_slnx.debug); EvalueteDebugPackages(userSettings?.debug); //Read debug SlnX foreach (var item in _packagesToDebug) { var debugSourcePakckage = PackagesInfo.Where(x => x.Identity.Id.Equals(item.Key, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); Assert(debugSourcePakckage != default(NuGetPackageInfo), $"The package {item.Key} is marked for debug, but it is not present as nuget package in the main SlnX file."); var slnxItem = new SlnxHandler(item.Value, _fileWriter, _logger, offlineMode, item.Key); _debugSlnxItems[debugSourcePakckage] = slnxItem; _packagesInfo.Remove(debugSourcePakckage); foreach (var candidate in slnxItem.Packages) { if (_packagesToDebug.ContainsKey(candidate.Identity.Id)) { //This package is under debug, no version check needed. continue; } var known = Packages.Where(x => x.Identity.Id.Equals(candidate.Identity.Id, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (known == default(NuGetPackage)) { _logger?.Warn($"The package {candidate} required by the SlnX {item.Key} selected for debug, is not present in the current SlnX file {_slnxName}{SlnxExtension}"); } else { Assert(known.Identity.MinVersion == candidate.Identity.MinVersion && known.TargetFramework == candidate.TargetFramework && known.PackageType == candidate.PackageType, $"The package {candidate} required by the SlnX {item.Key} selected for debug, does not match the already known one {known}"); } } } } }