private List <Generated.AssemblyReference> GetAndFixAssemblyReferences(IEnumerable <NuGetPackage> packages) { var xmlSer = new XmlSerializer(typeof(Generated.AssemblyReference)); var ret = new List <Generated.AssemblyReference>(); foreach (XmlNode r in _xml.GetElementsByTagName(AssemblyReferenceElementTag)) { var assemblyRef = (Generated.AssemblyReference)xmlSer.Deserialize(new StringReader(StripOuterXmlNamespace(r))); if (!string.IsNullOrEmpty(assemblyRef.HintPath)) // && !assemblyRef.HintPath.StartsWith("$")) { var candidatePackageName = assemblyRef.Include.Split(',').First(); NuGetPackage candidatePackage = packages.Where((x) => x.Identity.Id.Equals(candidatePackageName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (candidatePackage == null) //The assembly name might not match the package name { var candidateAssmblyName = Path.GetFileName(assemblyRef.HintPath); candidatePackage = packages.Where(x => x.Libraries.Where(y => y.EndsWith(candidateAssmblyName)).FirstOrDefault() != null).FirstOrDefault(); } if (candidatePackage != null) //The current project references candidatePackage { AddPackageReferenceFromAssembly(candidatePackage); var candidatePackageKey = NuGetPackage.EscapeStringAsEnvironmentVariableAsKey(candidatePackage.Identity.Id); var candidatePackageMsBuilVar = string.Format(KeyAsMsBuildProjectVariableTemplate, candidatePackageKey); var assemblyRoot = Path.GetDirectoryName(assemblyRef.HintPath); if (string.IsNullOrEmpty(assemblyRoot)) { assemblyRef.HintPath = Path.Combine(candidatePackageMsBuilVar, assemblyRef.HintPath); } else { assemblyRef.HintPath = assemblyRef.HintPath.Replace(assemblyRoot, candidatePackageMsBuilVar); } assemblyRef.Condition = string.Format(AssemblyReferenceConditionTemplate, candidatePackageKey); r[HintPathAttributeTag].InnerText = assemblyRef.HintPath; var conditionAttr = _xml.CreateAttribute(ConditionAttributeTag); if (r.Attributes.GetNamedItem(conditionAttr.Name) == null) { r.Attributes.Append(conditionAttr); } r.Attributes[conditionAttr.Name].Value = assemblyRef.Condition; ret.Add(assemblyRef); } } } return(ret); }
private List <Generated.ProjectReference> GetAndFixProjectReferences() { var xmlSer = new XmlSerializer(typeof(Generated.ProjectReference)); var ret = new List <Generated.ProjectReference>(); foreach (XmlNode r in _xml.GetElementsByTagName(ProjectReferenceElementTag)) { var projectRef = (Generated.ProjectReference)xmlSer.Deserialize(new StringReader(StripOuterXmlNamespace(r))); if (!string.IsNullOrEmpty(projectRef.Include)) { var candidateProjectName = Path.GetFileNameWithoutExtension(projectRef.Include); var candidatePackageKey = NuGetPackage.EscapeStringAsEnvironmentVariableAsKey(candidateProjectName); projectRef.Include = string.Format(ProjectReferenceIncludeTemplate, candidatePackageKey, candidateProjectName, FileExtension); r.Attributes[IncludeAttributeTag].InnerText = projectRef.Include; ret.Add(projectRef); } } return(ret); }
public CsProject(string fullpath, string container, IFileWriter writer, ILogger logger = null) { _logger = logger; _logger?.Debug($"Processing project: {fullpath}"); _fileWriter = writer; _typeGuid = CsProjectTypeGuid.ToUpper(); _fullPath = Path.GetFullPath(fullpath); if (!File.Exists(FullPath)) { throw new Exception(string.Format("The project '{0}' does not exist!", FullPath)); } _name = Path.GetFileNameWithoutExtension(FullPath); _isTestProject = Name.EndsWith($".{TestContainerName}"); _container = FormatContainer(container); if (_container == null && IsTestProject) //Test project, add the Test container under the default container { _container = FormatContainer(TestContainerName); } _projectOriginalContent = File.ReadAllText(FullPath); _xml = new XmlDocument(); _xml.LoadXml(_projectOriginalContent); var projectSdk = _xml.DocumentElement.GetAttribute("Sdk"); if (projectSdk.StartsWith("Microsoft.NET.Sdk")) { _projectGuid = Guid.NewGuid().ToString(); PlatformType platformTargetTmp; PlatformTarget = PlatformType.AnyCPU; if (TryGetPlatformTarget(out platformTargetTmp)) { PlatformTarget = platformTargetTmp; } string platformsTmp; Platforms = PlatformType.AnyCPU.ToPrettyString(); if (TryGetPlatforms(out platformsTmp)) { Platforms = platformsTmp; } Framework = GetFramework(); IsPackable = GetIsPackable(); AddInFilePackageReferences(); } else { _logger?.Error($"The current solution contains a legacy project: {Name}, which are not supported anymore!"); throw new Exception("Legacy project are not supported anymore by the slnlauncher (>=v3.0.0). Upgrade you project (.NET Framework/Core) to the new SDK style."); } var environmentVariableKey = NuGetPackage.EscapeStringAsEnvironmentVariableAsKey(Name); var environmentVariableFrameworkKey = NuGetClientHelper.NuGetPackage.GetFrameworkEnvironmentVariableKey(environmentVariableKey); EnvironmentVariableKeys = new List <string>() { environmentVariableKey, environmentVariableFrameworkKey }; Environment.SetEnvironmentVariable(environmentVariableKey, Path.GetDirectoryName(FullPath)); Environment.SetEnvironmentVariable(environmentVariableFrameworkKey, Framework); }