private NugetPackageInfo CreateNugetPackageInfoFromIPackage(IPackage matchingPackage) { var packageInfo = new NugetPackageInfo() { Info = new VersionInfo() { Name = matchingPackage.Id, VersionNumber = matchingPackage.Version.Version } }; // sholmesby: May need to download packages to filesystem here. Then we can get the DLL version number. // sholmesby: how can we find out the version number of this assembly? //foreach(var assembly in matchingPackage.AssemblyReferences) //{ // Assembly.LoadFrom( assembly.GetStream() //} int uniqueDLLCount = matchingPackage.AssemblyReferences.Select(x => x.EffectivePath).Distinct().Count(); if (uniqueDLLCount > 1) { Console.WriteLine("---- '" + matchingPackage.Id + "' may contain more than 1 DLL."); } // sholmesby: find which package would be installed? could be any number of packages throughout a range of the VersionSpec. //matchingPackage.DependencySets.Select(package => package.Dependencies.Where(pack => pack.VersionSpec) return(packageInfo); }
protected virtual Task AddUsingDotnetCli(NugetPackageInfo package, string version = null) { var versionOption = version == null ? "" : $" -v {version}"; CmdHelper.Run("dotnet", $"add package {package.Name}{versionOption}"); return(Task.CompletedTask); }
protected virtual async Task DownloadSourceCode(string targetFolder, NugetPackageInfo package, string version = null) { await SourceCodeDownloadService.DownloadNugetPackageAsync( package.Name, targetFolder, version ); }
public void SavePackage(NugetPackageInfo package) { lock (Packages) { Packages.Add(package); File.WriteAllText(packagesIndex, Packages.ToJson()); } }
protected virtual async Task AddAsPackageReference(string projectFile, NugetPackageInfo package, string version, bool useDotnetCliToInstall) { var projectFileContent = File.ReadAllText(projectFile); if (projectFileContent.Contains($"\"{package.Name}\"")) { return; } using (DirectoryHelper.ChangeCurrentDirectory(Path.GetDirectoryName(projectFile))) { Logger.LogInformation( $"Installing '{package.Name}' package to the project '{Path.GetFileNameWithoutExtension(projectFile)}'..."); if (useDotnetCliToInstall) { AddUsingDotnetCli(package, version); } else { AddToCsprojManuallyAsync(projectFile, package, version); } var moduleFiles = ModuleClassFinder.Find(projectFile, "AbpModule"); if (moduleFiles.Count == 0) { throw new CliUsageException( $"Could not find a class derived from AbpModule in the project {projectFile}"); } if (moduleFiles.Count > 1) { throw new CliUsageException( $"There are multiple classes derived from AbpModule in the project {projectFile}: " + moduleFiles.JoinAsString(", ")); } ModuleClassDependcyAdder.Add(moduleFiles.First(), package.ModuleClass); } if (useDotnetCliToInstall && (package.Target == NuGetPackageTarget.Blazor || package.Target == NuGetPackageTarget.BlazorServer || package.Target == NuGetPackageTarget.BlazorWebAssembly)) { try { await RunBundleForBlazorAsync(projectFile); } catch (Exception e) { Logger.LogWarning("Couldn't run bundle for blazor."); } } Logger.LogInformation("Successfully installed."); }
private void WriteLineToFile(NugetPackageInfo nugetPackageInfo) { // Version information is prefixed with a tab character in order to prevent Excel from automatically converting them to date values. string version = AppInfo.Options.ExcelCompatibility ? $"\t{nugetPackageInfo.Version}" : nugetPackageInfo.Version; List <string> columns = new List <string> { nugetPackageInfo.SolutionPath, nugetPackageInfo.ProjectName, nugetPackageInfo.Id, version, nugetPackageInfo.TargetFramework }; string joined = this.JoinColumns(columns); this.writer.WriteLine(joined); }
private string GetProjectFile(string solutionFile, NugetPackageInfo package) { var projectFiles = Directory.GetFiles(Path.GetDirectoryName(solutionFile), "*.csproj", SearchOption.AllDirectories); var isSolutionTiered = IsSolutionTiered(projectFiles); var projectFile = ProjectFinder.FindNuGetTargetProjectFile( projectFiles, isSolutionTiered && package.TieredTarget != NuGetPackageTarget.Undefined ? package.TieredTarget : package.Target); return(projectFile); }
private async Task DownloadSources() { const string listUrl = "https://api.nuget.org/v3/registration1/liteapi/index.json"; HttpClient client = new HttpClient(); string json = await client.GetStringAsync(listUrl); NugetPackageInfo info = JsonConvert.DeserializeObject <NugetPackageInfo>(json); var packages = info.items[0].items; foreach (var p in packages) { await DownloadPackage(p.packageContent, client); } }
public async Task AddAsync( string solutionFile, string projectFile, NugetPackageInfo package, string version = null, bool useDotnetCliToInstall = true, bool withSourceCode = false, bool addSourceCodeToSolutionFile = false) { if (projectFile == null) { if (solutionFile == null) { throw new CliUsageException("Couldn't find any project/solution."); } projectFile = GetProjectFile(solutionFile, package); if (projectFile == null) { throw new CliUsageException("Couldn't find any project/solution."); } } solutionFile ??= FindSolutionFile(projectFile); if (version == null) { version = GetAbpVersionOrNull(projectFile); } await AddAsPackageReference(projectFile, package, version, useDotnetCliToInstall); if (withSourceCode) { await AddSourceCode(projectFile, solutionFile, package, version); var projectFilesInSolution = Directory.GetFiles(Path.GetDirectoryName(solutionFile), "*.csproj", SearchOption.AllDirectories); foreach (var projectFileInSolution in projectFilesInSolution) { await ConvertPackageReferenceToProjectReference(projectFileInSolution, solutionFile, package); } if (addSourceCodeToSolutionFile) { await SolutionFileModifier.AddPackageToSolutionFileAsync(package, solutionFile); } } }
public SpiderXNewCmdProcessWrapper(string projectName, string projectNamespace, IConfiguration config, ICmdProcessCaller caller = null) { if (config is null) { throw new ArgumentNullException(); } if (string.IsNullOrEmpty(projectName)) { throw new ArgumentNullException(); } Caller = caller ?? new DotnetCmdProcessCaller(); ProjectName = projectName; ProjectNamespace = projectNamespace; NugetPackageInfo = new NugetPackageInfo(config.GetSection("NugetPackageId").Value); CmdKey = config.GetSection(nameof(CmdKey)).Value; }
} //TODO: Fill the symbols, like "UI-Angular", "CMS-KIT"! public ProjectBuildContext( TemplateInfo template, ModuleInfo module, NugetPackageInfo package, [NotNull] TemplateFile templateFile, [NotNull] ProjectBuildArgs buildArgs) { Template = template; Module = module; Package = package; TemplateFile = Check.NotNull(templateFile, nameof(templateFile)); BuildArgs = Check.NotNull(buildArgs, nameof(buildArgs)); Symbols = new List <string>(); Result = new ProjectResult(); }
protected virtual Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo package, string version = null) { var projectFileContent = File.ReadAllText(projectFile); var doc = new XmlDocument() { PreserveWhitespace = true }; using (var stream = StreamHelper.GenerateStreamFromString(projectFileContent)) { doc.Load(stream); var itemGroupNodes = doc.SelectNodes("/Project/ItemGroup"); XmlNode itemGroupNode = null; if (itemGroupNodes == null || itemGroupNodes.Count < 1) { var projectNodes = doc.SelectNodes("/Project"); var projectNode = projectNodes[0]; itemGroupNode = doc.CreateElement("ItemGroup"); projectNode.AppendChild(itemGroupNode); } else { itemGroupNode = itemGroupNodes[0]; } var packageReferenceNode = doc.CreateElement("PackageReference"); var includeAttr = doc.CreateAttribute("Include"); includeAttr.Value = package.Name; packageReferenceNode.Attributes.Append(includeAttr); if (version != null) { var versionAttr = doc.CreateAttribute("Version"); versionAttr.Value = version; packageReferenceNode.Attributes.Append(versionAttr); } itemGroupNode.AppendChild(packageReferenceNode); File.WriteAllText(projectFile, doc.OuterXml); return(Task.CompletedTask); } }
public static NugetPackageInfo GetPackageInfo(this IPackage package) { var info = new NugetPackageInfo { Id = package.Id, Version = package.Version.ToNormalizedString() }; info.Assemblies = package.AssemblyReferences .Select(a => new AssemblyReference { Name = a.Name, Path = Path.Combine(info.Id + "." + package.Version.ToNormalizedString(), a.Path) }) .ToList(); return(info); }
static async Task ClonePackageIfExistsInCacheAsync( ILogger logger, ISet <string> packages, NugetPackageInfo nugetPackageInfo, string nugetCacheRootPath, string destinationPath, Func <string, Task> processPackageAsync) { var fullName = nugetPackageInfo.ToString(); lock (packages) { if (packages.Contains(fullName)) { logger.LogInformation("Package {PackageName} is already processed", fullName); return; } packages.Add(fullName); } var packageFileName = $"{fullName}.nupkg"; var destinationFilePath = Path.Combine(destinationPath, packageFileName); if (File.Exists(destinationFilePath)) { logger.LogInformation("Package {PackageName} already exists in destination directory {DestinationDirectoryPath}", fullName, destinationPath); return; } var packageNugetCacheFilePath = Path.Combine(nugetCacheRootPath, nugetPackageInfo.Name, nugetPackageInfo.Version.ToString(), packageFileName); if (File.Exists(packageNugetCacheFilePath)) { File.Copy(packageNugetCacheFilePath, destinationFilePath); logger.LogInformation("Copied {PackageName} from cache to {DestinationDirectoryPath}", fullName, destinationPath); await processPackageAsync(packageNugetCacheFilePath).ConfigureAwait(false); } else { logger.LogWarning("Package {PackageName} does not exist in the cache {NugetCacheDirectoryPath}", fullName, nugetCacheRootPath); } }
public static bool InstallTemplate(ICmdProcessCaller processCaller, NugetPackageInfo packageInfo) { string command = "new -i " + packageInfo.Id + (string.IsNullOrEmpty(packageInfo.Version) ? null : ("::" + packageInfo.Version)); bool instOK = true; bool exeOK = Execute(processCaller, command, (sr) => { while (!sr.EndOfStream) { string str = sr.ReadLine(); if (!string.IsNullOrEmpty(str) && str.Contains(": error ")) { Console.WriteLine($"{nameof(InstallTemplate)} Error: {str}"); instOK = false; break; } } }); return(instOK && exeOK); }
private async Task AddPackageAsync(NugetPackageInfo package, string solutionFile) { var srcFolderId = await AddNewFolderAndGetIdOrGetExistingIdAsync(solutionFile, "src"); var file = File.ReadAllText(solutionFile); var lines = file.Split(Environment.NewLine).ToList(); if (lines.Any(l => l.Contains($"\"{package.Name}\""))) { return; } var projectGuid = Guid.NewGuid().ToString(); var newProjectLine = "Project(\"{9A19103F-16F7-4668-BE54-9A1E7A4F7556}\") = \"" + package.Name + "\"," + " \"packages\\" + package.Name + "\\" + "\\" + package.Name + ".csproj\", \"{" + projectGuid + "}\"" + Environment.NewLine + "EndProject"; lines.InsertAfter(l => l.Trim().Equals("EndProject"), newProjectLine); var newPostSolutionLine = " {"+ projectGuid + "}.Debug|Any CPU.ActiveCfg = Debug|Any CPU" + Environment.NewLine + " {"+ projectGuid + "}.Debug|Any CPU.Build.0 = Debug|Any CPU" + Environment.NewLine + " {"+ projectGuid + "}.Release|Any CPU.ActiveCfg = Release|Any CPU" + Environment.NewLine + " {"+ projectGuid + "}.Release|Any CPU.Build.0 = Release|Any CPU"; lines.InsertAfter(l => l.Contains("GlobalSection") && l.Contains("ProjectConfigurationPlatforms"), newPostSolutionLine); var newPreSolutionLine = " {"+ projectGuid + "} = {" + srcFolderId + "}"; lines.InsertAfter(l => l.Contains("GlobalSection") && l.Contains("NestedProjects"), newPreSolutionLine); File.WriteAllText(solutionFile, string.Join(Environment.NewLine, lines)); }
static void DeleteNonExistingPackages( ILogger logger, ISet <string> packages, string destinationPath) { var nupkgRegex = new Regex("^(.*?)\\.((?:\\.?[0-9]+){3,}(?:[-a-z]+)?)\\.nupkg$", RegexOptions.Compiled | RegexOptions.IgnoreCase); var destinationNugetFiles = Directory.EnumerateFiles(destinationPath, "*.nupkg", SearchOption.AllDirectories); foreach (var nugetFilePath in destinationNugetFiles) { var fileName = Path.GetFileName(nugetFilePath); var matches = nupkgRegex.Matches(fileName); if (matches.Count != 1) { continue; } var match = matches.Single(); if (match.Groups.Count != 3) { continue; } var name = match.Groups[1].Value; var version = match.Groups[2].Value; var fullName = new NugetPackageInfo(name, new Version(version)).ToString(); lock (packages) { if (!packages.Contains(fullName)) { File.Delete(nugetFilePath); logger.LogInformation("Deleted {PackagePath} from {DestinationDirectoryPath}", fullName, destinationPath); } } } }
public WritePackageMessage(NugetPackageInfo nugetPackageInfo, IActorRef processResultActor) { this.NugetPackageInfo = nugetPackageInfo; this.ProcessResultActor = processResultActor; }
private static int Process(string binDirectoryPath, string configSaveFilename, string filter) { List <NugetPackageInfo> discoveredNugetPackages = new List <NugetPackageInfo>(); List <DLLIssueInfo> issues = new List <DLLIssueInfo>(); // get versions of all DLLs // i.e get DLLs not starting with Sitecore* IEnumerable <VersionInfo> dllsInWebRoot = _versionIdentifier.GetDLLVersions(binDirectoryPath, filter); List <VersionInfo> remainingDLLsToCheck = dllsInWebRoot.ToList(); // extract DLLs into known groups from packages, and resolve them out of the list // i.e Lucene.Contrib contains Lucene.Net.Contrib* List <PackageSearchResult> resolvedPackagesFromGroups = _packageDiscoverer.ResolveKnownPackageGroups(ref remainingDLLsToCheck); discoveredNugetPackages.AddRange(resolvedPackagesFromGroups.Where(x => x.NugetPackage != null).Select(x => x.NugetPackage)); issues.AddRange(resolvedPackagesFromGroups.Where(x => x.Issue != null).Select(x => x.Issue)); // convert known DLL to Nuget package conversions // i.e System.* to Microsoft.AspNet.* List <PackageSearchResult> resolvedPackagesFromNames = _packageDiscoverer.ResolveKnownPackageNames(ref remainingDLLsToCheck); discoveredNugetPackages.AddRange(resolvedPackagesFromNames.Where(x => x.NugetPackage != null).Select(x => x.NugetPackage)); issues.AddRange(resolvedPackagesFromNames.Where(x => x.Issue != null).Select(x => x.Issue)); //TODO: beta package versions DotNetOpenAuth. Also check DLL against package DLL. // TODO: System.Net.Http - says need newer version of Nuget // TODO: version mismatches? duplicates? // i.e WebApi 4.0.20710 or 4.0.30506 or 4.0.40810? // TODO: group all three above into one method, dependent elsewhere while (remainingDLLsToCheck.Any()) { VersionInfo dllVersionInfo = remainingDLLsToCheck.First(); // find corresponding package per DLL // search nuget.org for package with the same name as DLL // see if matching version is available on nuget.org PackageSearchResult searchResult = _packageDiscoverer.SearchForPackage(dllVersionInfo); NugetPackageInfo nugetPackage = searchResult.NugetPackage; if (nugetPackage != null) { // check for other DLLs that exist in this package List <DLLIssueInfo> extraDLLIssues = _dllAuditor.AuditDLLsAgainstWebroot(nugetPackage.DLLsInPackage, dllsInWebRoot); issues.AddRange(extraDLLIssues); foreach (var extraDLL in nugetPackage.DLLsInPackage) { remainingDLLsToCheck.Remove(extraDLL); } discoveredNugetPackages.Add(nugetPackage); } else { issues.Add(searchResult.Issue); } remainingDLLsToCheck.Remove(dllVersionInfo); // so we can continue the loop } // TODO: check for dependency packages for extra validation //foreach(var nugetPackage in discoveredNugetPackages) //{ // // flatten out dependency packages (so we have a straight list) // List<NugetPackageInfo> dependantPackages = _packageDiscoverer.GetFlattenedDependencyPackages(nugetPackage); // foreach(var dependency in dependantPackages) // { // // extra auditing. make sure the package we've already discovered is compatible with the dependency package // if (discoveredNugetPackages.Exists(package => package.Info.Name.Equals(dependency.Info.Name))) // { // } // else // { // //TODO: issues.Add(error: dependency package doesn't exist in webroot); // } // } //} // run report XmlDocument packagesXml = _resultsFormatter.CreatePackagesDotConfigFile(discoveredNugetPackages.OrderBy(x => x.Info.Name)); string comment = _resultsFormatter.CreateIssuesComment(issues.OrderBy(x => x.DLLInSitecore.Name)); XmlComment commentXml = packagesXml.CreateComment(comment); packagesXml.DocumentElement.AppendChild(commentXml); packagesXml.Save(configSaveFilename); return(0); }
public static bool UninstallTemplate(ICmdProcessCaller processCaller, NugetPackageInfo packageInfo) => Execute(processCaller, "new -u " + packageInfo.Id);
public async Task AddPackageToSolutionFileAsync(NugetPackageInfo package, string solutionFile) { await AddPackageAsync(package, solutionFile); }
public List <NugetPackageInfo> GetFlattenedDependencyPackages(NugetPackageInfo nugetPackage) { throw new NotImplementedException(); //return new List<NugetPackageInfo>(); }
protected virtual async Task ConvertPackageReferenceToProjectReference(string projectFile, string solutionFile, NugetPackageInfo package) { var content = File.ReadAllText(projectFile); var doc = new XmlDocument() { PreserveWhitespace = true }; using (var stream = StreamHelper.GenerateStreamFromString(content)) { doc.Load(stream); var nodes = doc.SelectNodes($"/Project/ItemGroup/PackageReference[starts-with(@Include, '{package.Name}')]"); if (nodes == null || nodes.Count < 1) { return; } var downloadedProjectPath = FindRelativeFolderToDownloadPackage(projectFile, solutionFile, package); var oldNodeIncludeValue = nodes[0]?.Attributes?["Include"]?.Value; if (package.Name == oldNodeIncludeValue) { var referenceProjectPath = $"{downloadedProjectPath}\\{package.Name}.csproj"; var newNode = doc.CreateElement("ProjectReference"); var includeAttr = doc.CreateAttribute("Include"); includeAttr.Value = referenceProjectPath; newNode.Attributes.Append(includeAttr); nodes[0]?.ParentNode?.ReplaceChild(newNode, nodes[0]); } File.WriteAllText(projectFile, doc.OuterXml); await Task.CompletedTask; } }
protected virtual async Task AddSourceCode(string projectFile, string solutionFile, NugetPackageInfo package, string version = null) { var targetFolder = FindFolderToDownloadPackage(solutionFile, package); if (Directory.Exists(targetFolder)) { Directory.Delete(targetFolder, true); } await DownloadSourceCode(targetFolder, package, version); }
protected virtual string FindFolderToDownloadPackage(string solutionFile, NugetPackageInfo package) { return(Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name)); }
public WriteResultMessage(NugetPackageInfo nugetPackageInfo) { this.NugetPackageInfo = nugetPackageInfo; }
/// <summary> /// 加载解决方案中各个工程项目的包信息 /// </summary> /// <param name="slnInfo"></param> /// <returns></returns> public SolutionInfo LoadProjectInfo(SolutionInfo slnInfo) { try { if (slnInfo == null) { throw new Exception("解决方案信息为空!"); } if (slnInfo.ProjectInfos == null || slnInfo.ProjectInfos.Count == 0) { throw new Exception("没有需要加载的项目工程!"); } string projFullFilePath = ""; string packageFullFilePath = ""; string dirPath = slnInfo.DirPath.TrimEnd(new char[] { '\\' }); ProjectCollection projInfos = new ProjectCollection(); Project projInfo = null; foreach (var proj in slnInfo.ProjectInfos) { #region 加载包文件下的包信息 packageFullFilePath = proj.ProjDirPath + "\\" + "packages.config"; if (!File.Exists(packageFullFilePath)) { proj.OptMessage += "没有找到package.config文件,解读为此项目未引用nuget包."; continue; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(packageFullFilePath); XmlNode root = xmlDoc.SelectSingleNode("/packages"); //获取节点下所有直接子节点 if (!root.HasChildNodes) { proj.OptMessage += "package.config文件下的packages节点没有子节点,解读为此项目未引用nuget包."; continue; } proj.NugetPackageList = new List <NugetPackageInfo>(); XmlNodeList childlist = root.ChildNodes; foreach (XmlNode node in childlist) { if (node.Name != "package") { continue; } var nugetPackage = new NugetPackageInfo() { Name = node.Attributes["id"].Value.ToString(), Version = node.Attributes["version"].Value.ToString(), TargetFramework = node.Attributes["targetFramework"].Value.ToString(), }; proj.NugetPackageList.Add(nugetPackage); } #endregion #region 加载项目文件下的包信息 projFullFilePath = dirPath + "\\" + proj.ProjFilePath.TrimStart(new char[] { '\\' }); projInfo = projInfos.LoadProject(projFullFilePath);//加载项目文件 if (projInfo == null) { proj.OptMessage += "加载.csproj文件为空!"; continue; } proj.ProjTargetFrameworkVersion = projInfo.AllEvaluatedProperties.Where(x => x.Name == "TargetFrameworkVersion").FirstOrDefault().EvaluatedValue.TrimStart('v'); //筛选包含HintPath子节点的Reference节点 var projReferenceItems = projInfo.AllEvaluatedItems.Where(e => e.ItemType == "Reference" && e.Metadata.Any(x => x.Name == "HintPath" && x.EvaluatedValue.Contains(@"..\packages\"))).ToList(); if (projReferenceItems == null || projReferenceItems.Count == 0) { proj.OptMessage += "加载.csproj文件时未找到包含HintPath子节点的Reference节点!"; continue; } proj.ProjPackageList = new List <ProjFilePackageInfo>(); foreach (var projReference in projReferenceItems) { var pack = ConvetProjectPackage(projReference.Xml); proj.ProjPackageList.Add(pack); } #endregion } return(slnInfo); } catch (Exception ex) { throw new Exception("加载工程项目包信息时异常!", ex); } }
protected virtual string FindRelativeFolderToDownloadPackage(string projectFile, string solutionFile, NugetPackageInfo package) { var folder = Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name); return(new Uri(projectFile).MakeRelativeUri(new Uri(folder)).ToString().Replace("/", "\\")); }