Beispiel #1
0
        public void HttpRepositoryManagerTest()
        {
            var manager = new HttpPackageRepository("http://packages.opentap.io/");

            RepositoryManagerReceivePackageList(manager);
            TestDownload(manager);
        }
        public async Task TestDownload()
        {
            var Repo   = new HttpPackageRepository("http://Get.FasterLaw.com/AlphaDrive/Windows/Stable");
            var Update = await Repo.CheckForUpdate();

            var Entries = await Repo.AvailablePackages();

            var ToDownload = Entries.FirstOrDefault();

            var Package = await Repo.AcquirePackage(ToDownload);

            Package.Execute();
        }
Beispiel #3
0
        public void TestUserId()
        {
            var    idPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create), "OpenTAP", "OpenTapGeneratedId");
            string orgId  = null;

            if (File.Exists(idPath))
            {
                orgId = File.ReadAllText(idPath);
            }

            try
            {
                // Check id are the same
                var id  = HttpPackageRepository.GetUserId();
                var id2 = HttpPackageRepository.GetUserId();
                Assert.AreEqual(id, id2, "User id are different between runs.");

                // Remove id file
                File.Delete(idPath);
                if (File.Exists(idPath))
                {
                    Assert.Fail("Id still exists.");
                }
                Assert.AreNotEqual(HttpPackageRepository.GetUserId(), default(Guid), "Failed to create new user id after deleting file.");

                // Remove directory
                Directory.Delete(Path.GetDirectoryName(idPath), true);
                Assert.AreNotEqual(HttpPackageRepository.GetUserId(), default(Guid), "Failed to create new user id after deleting directory.");
            }
            finally
            {
                // Revert changes
                if (orgId != null)
                {
                    File.WriteAllText(idPath, orgId);
                }
            }
        }
Beispiel #4
0
        public int Execute(CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(PackageName))
            {
                log.Error("Please specify a valid package name.");
                return(1);
            }

            {   // check if the package exists in the repo.
                var repo = new HttpPackageRepository(PackageRepository);
                var spec = new PackageSpecifier(PackageName, Version != null ? VersionSpecifier.Parse(Version) : null);
                var pkg  = repo.GetPackages(spec, TapThread.Current.AbortToken).FirstOrDefault();
                if (pkg == null)
                {
                    log.Error("No such package");
                    return(2);
                }
                if (Version == null)
                {
                    Version = pkg.Version.ToString();
                }
                PackageName = pkg.Name;
            }

            string ifRefStr = "";

            if (NoReference)
            {
                ifRefStr = " Reference=\"false\"";
            }

            string insert =
                $"<OpenTapPackageReference Include=\"{PackageName}\" Version=\"{Version}\" Repository=\"{PackageRepository}\"{ifRefStr}/>";

            var csproj = Project ?? get_csproj();

            if (csproj == null)
            {  // error was already printed.
                return(1);
            }

            if (File.Exists(csproj) == false)
            {
                log.Error("C# project files does not exist {0}", csproj);
                return(2);
            }

            var      document     = XDocument.Load(csproj, LoadOptions.PreserveWhitespace);
            var      projectXml   = document.Element("Project");
            XElement itemGroupXml = null;
            var      condition    = $"'$(Configuration)' == '{Configuration}'";

            var itemGroups = projectXml.Elements("ItemGroup");

            // if condition is select take the group matching the conditions.
            if (string.IsNullOrWhiteSpace(Configuration) == false)
            {
                itemGroups = itemGroups.Where(grp => grp.Attribute("Condition")?.Value == condition);
            }

            bool needsAddNewElem = true;

            foreach (var grp in itemGroups)
            {
                var existingElem = grp.Elements("OpenTapPackageReference")
                                   .Where(elem => string.Equals(elem.Attribute("Reference")?.Value ?? "True", (!NoReference).ToString(), StringComparison.InvariantCultureIgnoreCase))
                                   .FirstOrDefault(elem => elem.Attribute("Include")?.Value == PackageName);

                if (existingElem != null)
                {  // the package reference already exists. In this case, lets try to just update set the version.
                    var version = existingElem.Attribute("Version");
                    if (version != null)
                    {
                        if (version.Value == Version)
                        {
                            log.Info("Package {0} version {1} already in the csproj.", PackageName, Version);
                            return(0);
                        }

                        log.Info("Package {0} version {1} changed to version {2}.", PackageName, version.Value, Version);

                        version.Value   = Version;
                        needsAddNewElem = false;
                        break;
                    }
                }
            }

            if (needsAddNewElem)
            {
                // add a new OpenTapPackageReference or AdditionalOpenTapPackage element.
                // to make the whitespace look right, there is some adding additional whitespace

                // Try to find the existing item group used for package references.
                foreach (var grp in itemGroups)
                {
                    if (grp.Elements("OpenTapPackageReference").Any())
                    {
                        itemGroupXml = grp;
                        break;
                    }
                }

                if (itemGroupXml == null)
                {
                    itemGroupXml = new XElement("ItemGroup");
                    if (string.IsNullOrWhiteSpace(Configuration) == false)
                    {
                        // e.g Condition="'{Configuration}' == 'Debug'"
                        itemGroupXml.Add(new XAttribute("Condition", condition));
                    }
                    itemGroupXml.Add("\n");
                    itemGroupXml.Add("   ");
                    projectXml.Add("\n");
                    projectXml.Add("   ");
                    projectXml.Add(itemGroupXml);
                    projectXml.Add("\n");
                }

                itemGroupXml.Add("   ");
                itemGroupXml.Add(XElement.Parse(insert));
                itemGroupXml.Add("\n");
                itemGroupXml.Add("   ");
                log.Info("Package {0} version {1} reference added to the project.", PackageName, Version);
            }

            document.Save(csproj, SaveOptions.DisableFormatting);
            return(0);
        }