Exemplo n.º 1
0
        public static async Task <int> RunAndReturn(PushOptions options, PackageHelper packageHelper)
        {
            // --package=MyFile.zip
            // --package=./MyFile.zip
            // --package=../MyParentFolder.zip
            var filePath = options.Package;
            var apiKey   = options.ApiKey;

            var keyParts = packageHelper.SplitKey(apiKey);

            // Check we can find the file
            packageHelper.EnsurePackageExists(filePath);

            // Check File is a ZIP
            packageHelper.EnsureIsZip(filePath);

            // Check zip contains valid package.xml
            packageHelper.EnsureContainsPackageXml(filePath);

            // gets a package list from our.umbraco
            // if the api key is invalid we will also find out here.
            var packages = await packageHelper.GetPackageList(keyParts);

            var currentPackageId = await packageHelper.GetCurrentPackageFileId(keyParts);

            if (packages != null)
            {
                packageHelper.EnsurePackageDoesntAlreadyExists(packages, filePath);
            }

            // Archive packages
            var archivePatterns   = new List <string>();
            var packagesToArchive = new List <int>();

            if (options.Archive != null)
            {
                archivePatterns.AddRange(options.Archive);
            }

            if (archivePatterns.Count > 0)
            {
                foreach (var archivePattern in archivePatterns)
                {
                    if (archivePattern == "current")
                    {
                        // If the archive option is "current", then archive the current package
                        if (currentPackageId != "0")
                        {
                            packagesToArchive.Add(int.Parse(currentPackageId));
                        }
                    }
                    else
                    {
                        // Convert the archive option to a regex
                        var archiveRegex = new Regex("^" + archivePattern.Replace(".", "\\.").Replace("*", "(.*)") + "$", RegexOptions.IgnoreCase);

                        // Find packages that match the regex and extract their IDs
                        var archiveIds = packages.Where(x => archiveRegex.IsMatch(x.Value <string>("Name"))).Select(x => x.Value <int>("Id")).ToArray();

                        packagesToArchive.AddRange(archiveIds);
                    }
                }
            }

            if (packagesToArchive.Count > 0)
            {
                await packageHelper.ArchivePackages(keyParts, packagesToArchive.Distinct());

                Console.WriteLine($"Archived {packagesToArchive.Count} packages matching the archive pattern.");
            }

            // Parse package.xml before upload to print out info
            // and to use for comparison on what is already uploaded
            var packageInfo = Parse.PackageXml(filePath);

            // OK all checks passed - time to upload it
            await UploadPackage(options, packageHelper, packageInfo);

            return(0);
        }