public IProjectFile <NuGetPackageReference> Load(string path) { var content = File.ReadAllText(path); var scriptFileContent = new ScriptFileContent() { SourceCode = content }; var matches = Regex.Matches(content, ReferenceDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast <Match>() .Union(Regex.Matches(content, LoadDirectivePattern, RegexOptions.IgnoreCase | RegexOptions.Multiline).Cast <Match>()).ToArray(); var packageReferences = new List <ScriptPackageReference>(); foreach (var match in matches) { var packageName = match.Groups[2].Value; var packageVersion = match.Groups[4].Value; if (FloatRange.TryParse(packageVersion, out var floatRange)) { var nugetVersion = floatRange.MinVersion; var nugetPackageReference = new ScriptPackageReference(packageName, packageVersion, floatRange, scriptFileContent); packageReferences.Add(nugetPackageReference); } else { console.WriteError($"Warning: The package '{packageName}' has an invalid version number '{packageVersion}'"); } } return(new ScriptProjectFile(scriptFileContent, path) { PackageReferences = packageReferences.ToArray() }); }
public IProjectFile <NuGetPackageReference> Load(string path) { var projectFile = XDocument.Load(path); var nameSpace = projectFile.Root.Name.Namespace; var msBuildProjectFile = new MsBuildProjectFile(projectFile, path); var packageReferenceElements = projectFile.Descendants(nameSpace + "PackageReference"); var packageReferences = new List <MsBuildPackageReference>(); foreach (var packageReferenceElement in packageReferenceElements) { var packageName = packageReferenceElement.Attribute("Include")?.Value; if (packageName == null) { packageName = packageReferenceElement.Attribute("Update")?.Value; } if (string.IsNullOrWhiteSpace(packageName)) { continue; } var packageVersion = packageReferenceElement.Attribute("Version")?.Value; if (packageVersion == null || packageVersion.StartsWith("$")) { continue; } //packageReferences.Add(new PackageReference(packageName, packageVersion)); if (FloatRange.TryParse(packageVersion, out var floatRange)) { var nugetVersion = floatRange.MinVersion; var nugetPackageReference = new MsBuildPackageReference(packageName, packageVersion, floatRange, packageReferenceElement); packageReferences.Add(nugetPackageReference); } else { console.WriteError($"Warning: The package '{packageName}' has an invalid version number '{packageVersion}'"); } } msBuildProjectFile.PackageReferences = packageReferences.ToArray(); return(msBuildProjectFile); }
public IProjectFile <NuGetPackageReference> Load(string path) { var projectFile = XDocument.Load(path); var nameSpace = projectFile.Root.Name.Namespace; var nugetSpecProjectFile = new NuspecProjectFile(projectFile, path); var packageReferenceElements = projectFile.Descendants(nameSpace + "dependency"); var packageReferences = new List <NuspecPackageReference>(); foreach (var packageReferenceElement in packageReferenceElements) { var packageName = packageReferenceElement.Attribute("id")?.Value; if (string.IsNullOrWhiteSpace(packageName)) { continue; } var packageVersion = packageReferenceElement.Attribute("version")?.Value; if (packageVersion == null) { continue; } if (FloatRange.TryParse(packageVersion, out var floatRange)) { var nugetVersion = floatRange.MinVersion; var nugetPackageReference = new NuspecPackageReference(packageName, packageVersion, floatRange, packageReferenceElement); packageReferences.Add(nugetPackageReference); } else { console.WriteError($"Warning: The package '{packageName}' has an invalid version number '{packageVersion}'"); } } nugetSpecProjectFile.PackageReferences = packageReferences.ToArray(); return(nugetSpecProjectFile); }