Exemplo n.º 1
0
        public Task AddAsync(string projectFile, NugetPackageInfo package)
        {
            if (File.ReadAllText(projectFile).Contains($"\"{package.Name}\""))
            {
                return(Task.CompletedTask);
            }

            using (DirectoryHelper.ChangeCurrentDirectory(Path.GetDirectoryName(projectFile)))
            {
                Logger.LogInformation($"Installing '{package.Name}' package to the project '{Path.GetFileNameWithoutExtension(projectFile)}'...");

                CmdHelper.Run("dotnet", "add package " + package.Name);

                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);

                Logger.LogInformation("Successfully installed.");
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        public async Task AddAsync(
            string projectFile,
            NugetPackageInfo package,
            string version                   = null,
            bool useDotnetCliToInstall       = true,
            bool withSourceCode              = false,
            bool addSourceCodeToSolutionFile = false)
        {
            if (version == null)
            {
                version = GetAbpVersionOrNull(projectFile);
            }

            await AddAsPackageReference(projectFile, package, version, useDotnetCliToInstall);

            if (withSourceCode)
            {
                await AddSourceCode(projectFile, package, version);
                await ConvertPackageReferenceToProjectReference(projectFile, package);

                if (addSourceCodeToSolutionFile)
                {
                    await SolutionFileModifier.AddPackageToSolutionFileAsync(package, FindSolutionFile(projectFile));
                }
            }
        }
Exemplo n.º 3
0
        private bool IsPackageInCompatible(NugetPackageInfo package, string version)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(package.MinVersion))
                {
                    if (SemanticVersion.Parse(package.MinVersion) > SemanticVersion.Parse(version))
                    {
                        return(true);
                    }
                }
                if (!string.IsNullOrWhiteSpace(package.MaxVersion))
                {
                    if (SemanticVersion.Parse(package.MaxVersion) < SemanticVersion.Parse(version))
                    {
                        return(true);
                    }
                }

                return(false);
            }
            catch (ArgumentException)
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public async Task AddAsync(string projectFile, NugetPackageInfo package)
        {
            using (DirectoryHelper.ChangeCurrentDirectory(Path.GetDirectoryName(projectFile)))
            {
                Logger.LogInformation($"Installing '{package.Name}' package to the project '{Path.GetFileNameWithoutExtension(projectFile)}'...");

                var procStartInfo = new ProcessStartInfo("dotnet", "add package " + package.Name);
                Process.Start(procStartInfo).WaitForExit();

                var moduleFiles = ModuleClassFinder.Find(projectFile);
                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 (package.DependedNpmPackage != null)
                {
                    await NpmPackageAdder.AddAsync(Path.GetDirectoryName(projectFile), package.DependedNpmPackage);
                }

                Logger.LogInformation("Successfully installed.");
            }
        }
Exemplo n.º 5
0
 protected virtual async Task DownloadSourceCode(string targetFolder, NugetPackageInfo package, string version = null)
 {
     await SourceCodeDownloadService.DownloadNugetPackageAsync(
         package.Name,
         targetFolder,
         version
         );
 }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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.");
        }
Exemplo n.º 8
0
        private async Task AddSourceCode(string projectFile, NugetPackageInfo package, string version = null)
        {
            var targetFolder = FindFolderToDownloadPackage(projectFile, package);

            if (Directory.Exists(targetFolder))
            {
                Directory.Delete(targetFolder, true);
            }

            await DownloadSourceCode(targetFolder, package, version);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        public Task AddAsync(string projectFile, NugetPackageInfo package, string version = null,
                             bool useDotnetCliToInstall = true)
        {
            var projectFileContent = File.ReadAllText(projectFile);

            if (projectFileContent.Contains($"\"{package.Name}\""))
            {
                return(Task.CompletedTask);
            }

            if (version == null)
            {
                version = GetAbpVersionOrNull(projectFileContent);
            }

            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);

                Logger.LogInformation("Successfully installed.");
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 11
0
        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);
                }
            }
        }
Exemplo n.º 12
0
        protected virtual Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo package, string version = null)
        {
            var projectFileContent = File.ReadAllText(projectFile);
            var doc = new XmlDocument()
            {
                PreserveWhitespace = true
            };

            doc.Load(StreamHelper.GenerateStreamFromString(projectFileContent));

            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);
        }
Exemplo n.º 13
0
        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);
                await ConvertPackageReferenceToProjectReference(projectFile, solutionFile, package);

                if (addSourceCodeToSolutionFile)
                {
                    await SolutionFileModifier.AddPackageToSolutionFileAsync(package, solutionFile);
                }
            }
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
 private string FindFolderToDownloadPackage(string projectFile, NugetPackageInfo package)
 {
     return(Path.Combine(FindSolutionFolder(projectFile), "packages", package.Name));
 }
Exemplo n.º 16
0
        private string FindRelativeFolderToDownloadPackage(string projectFile, NugetPackageInfo package)
        {
            var folder = Path.Combine(FindSolutionFolder(projectFile), "packages", package.Name);

            return(new Uri(projectFile).MakeRelativeUri(new Uri(folder)).ToString().Replace("/", "\\"));
        }
Exemplo n.º 17
0
        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("/", "\\"));
        }
Exemplo n.º 18
0
 protected virtual string FindFolderToDownloadPackage(string solutionFile, NugetPackageInfo package)
 {
     return(Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name));
 }
Exemplo n.º 19
0
        protected virtual async Task ConvertPackageReferenceToProjectReference(string projectFile, string solutionFile, NugetPackageInfo package)
        {
            var content = File.ReadAllText(projectFile);
            var doc     = new XmlDocument()
            {
                PreserveWhitespace = true
            };

            doc.Load(StreamHelper.GenerateStreamFromString(content));

            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);
        }
Exemplo n.º 20
0
 public async Task AddPackageToSolutionFileAsync(NugetPackageInfo package, string solutionFile)
 {
     await AddPackageAsync(package, solutionFile);
 }