Esempio n. 1
0
        private string ExpandTemplate(string regexTemplate, string replaceTemplate, string whichVersion,
                                      string contents, VersionParts incomingVersion, VersionFormat format = VersionFormat.File)
        {
            try
            {
                var regex  = new Regex(string.Format(regexTemplate, whichVersion));
                var result = regex.Match(contents);
                if (result == Match.Empty)
                {
                    return(contents);
                }
                var versionTemplateInFile = ParseVersionString(result.Groups[1].Value, format);
                var newVersion            = MergeTemplates(incomingVersion, versionTemplateInFile);

                SafeLog("StampAssemblies: Merging existing {0} with incoming {1} to produce {2}.",
                        versionTemplateInFile.ToString(), incomingVersion.ToString(), newVersion);
                return(regex.Replace(contents, string.Format(replaceTemplate, whichVersion, newVersion)));
            }
            catch (Exception e)
            {
                Log.LogError("Could not parse the {0} attribute, which should be something like 0.7.*.* or 1.0.0.0",
                             whichVersion);
                Log.LogErrorFromException(e);
                throw;
            }
        }
Esempio n. 2
0
        public DownloadPackageCommand(CombinedScriptEngine scriptEngine)
        {
            this.scriptEngine = scriptEngine;
            Options.Add("packageId=", "Package ID to download", v => packageId = v);
            Options.Add("packageVersion=", "Package version to download", v => packageVersion = v);
            Options.Add("packageVersionFormat=", $"[Optional] Format of version. Options {string.Join(", ", Enum.GetNames(typeof(VersionFormat)))}. Defaults to `{VersionFormat.Semver}`.",
                        v =>
            {
                if (!Enum.TryParse(v, out VersionFormat format))
                {
                    throw new CommandException($"The provided version format `{format}` is not recognised.");
                }
                versionFormat = format;
            });
            Options.Add("feedId=", "Id of the NuGet feed", v => feedId = v);
            Options.Add("feedUri=", "URL to NuGet feed", v => feedUri  = v);
            Options.Add("feedUsername="******"[Optional] Username to use for an authenticated NuGet feed", v => feedUsername = v);
            Options.Add("feedPassword="******"[Optional] Password to use for an authenticated NuGet feed", v => feedPassword = v);
            Options.Add("feedType=", $"[Optional] Type of feed. Options {string.Join(", ", Enum.GetNames(typeof(FeedType)))}. Defaults to `{FeedType.NuGet}`.",
                        v =>
            {
                if (!Enum.TryParse(v, out FeedType type))
                {
                    throw new CommandException($"The provided feed type `{type}` is not recognised.");
                }

                feedType = type;
            });
            Options.Add("attempts=", $"[Optional] The number of times to attempt downloading the package. Default: {maxDownloadAttempts}", v => maxDownloadAttempts = v);
            Options.Add("attemptBackoffSeconds=", $"[Optional] The number of seconds to apply as a linear backoff between each download attempt. Default: {attemptBackoffSeconds}", v => attemptBackoffSeconds = v);
            Options.Add("forcePackageDownload", "[Optional, Flag] if specified, the package will be downloaded even if it is already in the package cache", v => forcePackageDownload = true);
        }
Esempio n. 3
0
        static async Task <string> GetVersionSummary(IList <Product> products, VersionFormat format)
        {
            if (products.Count == 0)
            {
                return("No products enabled.");
            }
            var sb = new StringBuilder();

            foreach (var product in products)
            {
                var version = await product.PrintVersion(VersionFormat.Summary).ConfigureAwait(false);

                if (format == VersionFormat.Summary)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append("<br>");
                    }
                    sb.Append($"{product.ShortName}: {version}");
                }
                else
                {
                    sb.AppendLine($"{product.Name}: {version}");
                }
            }
            return(sb.ToString());
        }
Esempio n. 4
0
        public static string GetVersion(VersionFormat format = VersionFormat.Normal)
        {
            var version = ((AssemblyInformationalVersionAttribute)
                Assembly
                    .GetExecutingAssembly()
                    .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)[0]
            ).InformationalVersion;

            var dashIndex = version.IndexOf('-');
            var plusIndex = version.IndexOf('+');

            switch (format)
            {
                case VersionFormat.Short:
                    if (dashIndex >= 0)
                        version = version.Substring(0, dashIndex);
                    else if (plusIndex >= 0)
                        version = version.Substring(0, plusIndex);

                    break;
                case VersionFormat.Normal:
                    if (plusIndex >= 0)
                        version = version.Substring(0, plusIndex);

                    break;
                case VersionFormat.Full:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(format), format, null);
            }

            return "v" + version;
        }
 CalamariResult FindPackages(string id, string version, string hash, VersionFormat versionFormat = VersionFormat.Semver)
 {
     return(Invoke(Calamari()
                   .Action("find-package")
                   .Argument("packageId", id)
                   .Argument("packageVersion", version)
                   .Argument("packageVersionFormat", versionFormat)
                   .Argument("packageHash", hash)));
 }
Esempio n. 6
0
 public FindPackageCommand()
 {
     Options.Add("packageId=", "Package ID to find", v => packageId = v);
     Options.Add("packageVersion=", "Package version to find", v => rawPackageVersion = v);
     Options.Add("packageHash=", "Package hash to compare against", v => packageHash  = v);
     Options.Add("packageVersionFormat=", $"[Optional] Format of version. Options {string.Join(", ", Enum.GetNames(typeof(VersionFormat)))}. Defaults to `{VersionFormat.Semver}`.",
                 v =>
     {
         if (!Enum.TryParse(v, out VersionFormat format))
         {
             throw new CommandException($"The provided version format `{format}` is not recognised.");
         }
         versionFormat = format;
     });
     Options.Add("exactMatch=", "Only return exact matches", v => exactMatchOnly = bool.Parse(v));
 }
 public RegisterPackageUseCommand(ILog log, IManagePackageCache journal, ICalamariFileSystem fileSystem)
 {
     this.log        = log;
     this.journal    = journal;
     this.fileSystem = fileSystem;
     Options.Add("packageId=", "Package ID of the used package", v => packageId = new PackageId(v));
     Options.Add("packageVersionFormat=", $"[Optional] Format of version. Options {string.Join(", ", Enum.GetNames(typeof(VersionFormat)))}. Defaults to `{VersionFormat.Semver}`.",
                 v =>
     {
         if (!Enum.TryParse(v, out VersionFormat format))
         {
             throw new CommandException($"The provided version format `{format}` is not recognised.");
         }
         versionFormat = format;
     });
     Options.Add("packageVersion=", "Package version of the used package", v => packageVersion = VersionFactory.TryCreateVersion(v, versionFormat));
     Options.Add("packagePath=", "Path to the package", v => packagePath            = new PackagePath(v));
     Options.Add("taskId=", "Id of the task that is using the package", v => taskId = new ServerTaskId(v));
 }
Esempio n. 8
0
        public static string GetVersion(VersionFormat format = VersionFormat.Normal)
        {
            var version = ((AssemblyInformationalVersionAttribute)
                           Assembly
                           .GetExecutingAssembly()
                           .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false)[0]
                           ).InformationalVersion;

            var dashIndex = version.IndexOf('-');
            var plusIndex = version.IndexOf('+');

            switch (format)
            {
            case VersionFormat.Short:
                if (dashIndex >= 0)
                {
                    version = version.Substring(0, dashIndex);
                }
                else if (plusIndex >= 0)
                {
                    version = version.Substring(0, plusIndex);
                }

                break;

            case VersionFormat.Normal:
                if (plusIndex >= 0)
                {
                    version = version.Substring(0, plusIndex);
                }

                break;

            case VersionFormat.Full:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(format), format, null);
            }

            return("v" + version);
        }
        CalamariResult DownloadPackage(string packageId,
                                       string packageVersion,
                                       string feedId,
                                       string feedUri,
                                       string feedUsername         = "",
                                       string feedPassword         = "",
                                       FeedType feedType           = FeedType.NuGet,
                                       VersionFormat versionFormat = VersionFormat.Semver,
                                       bool forcePackageDownload   = false,
                                       int attempts = 5,
                                       int attemptBackoffSeconds = 0)
        {
            var calamari = Calamari()
                           .Action("download-package")
                           .Argument("packageId", packageId)
                           .Argument("packageVersion", packageVersion)
                           .Argument("packageVersionFormat", versionFormat)
                           .Argument("feedId", feedId)
                           .Argument("feedUri", feedUri)
                           .Argument("feedType", feedType)
                           .Argument("attempts", attempts.ToString())
                           .Argument("attemptBackoffSeconds", attemptBackoffSeconds.ToString());

            if (!String.IsNullOrWhiteSpace(feedUsername))
            {
                calamari.Argument("feedUsername", feedUsername);
            }

            if (!String.IsNullOrWhiteSpace(feedPassword))
            {
                calamari.Argument("feedPassword", feedPassword);
            }

            if (forcePackageDownload)
            {
                calamari.Flag("forcePackageDownload");
            }

            return(Invoke(calamari));
        }
Esempio n. 10
0
        public async Task <string> PrintVersion(VersionFormat format)
        {
            if (!IsFrameworkInstalled)
            {
                return("not installed");
            }
            var frameworkVersion = GetFrameworkVersion();

            if (string.IsNullOrEmpty(frameworkVersion))
            {
                return("not installed");
            }

            string version;

            try {
                version = await GetVersion(CancellationToken.None).ConfigureAwait(false);

                if (string.IsNullOrEmpty(version))
                {
                    throw new NotSupportedException();
                }
            } catch {
                if (format == VersionFormat.Summary)
                {
                    return("error");
                }
                return($"{frameworkVersion} (<unable to get detailed version info>)");
            }

            string revision, shortWithRevision;

            try {
                string branch, commit;
                var    shortVersion = GetShortVersion(version, out branch, out commit);
                if (string.IsNullOrWhiteSpace(shortVersion))
                {
                    throw new NotSupportedException();
                }
                if (string.IsNullOrWhiteSpace(branch) || string.IsNullOrWhiteSpace(commit))
                {
                    revision          = "<unknown>";
                    shortWithRevision = shortVersion;
                }
                else
                {
                    revision          = $"{branch}/{commit}";
                    shortWithRevision = $"{shortVersion} ({revision})";
                }
            } catch {
                revision          = "<unknown>";
                shortWithRevision = "<unknown>";
            }

            switch (format)
            {
            case VersionFormat.Verbose:
                return($"{frameworkVersion}: {shortWithRevision}");

            case VersionFormat.Normal:
                return(shortWithRevision);

            case VersionFormat.Summary:
                return(revision);

            default:
                return($"{frameworkVersion}: {shortWithRevision}\n{version}");
            }
        }
Esempio n. 11
0
        private static VersionParts ParseVersionString(string contents, VersionFormat format)
        {
            VersionParts v;

            if (format == VersionFormat.Semantic)
            {
                Match result = Regex.Match(contents, @"([\d\*]+)\.([\d\*]+)\.([\d\*]+)(?:\-(.*))?");

                v = new VersionParts
                {
                    parts = new[]
                    {
                        result.Groups[1].Value,
                        result.Groups[2].Value,
                        result.Groups[3].Value
                    },
                    Prerelease = result.Groups[4].Value
                };
            }
            else
            {
                Match result = Regex.Match(contents, @"(.+)\.(.+)\.(.+)\.(.+)");
                if (!result.Success)
                {
                    //handle 1.0.*  (I'm not good enough with regex to
                    //overcome greediness and get a single pattern to work for both situations).
                    result = Regex.Match(contents, @"(.+)\.(.+)\.(\*)");
                }
                if (!result.Success)
                {
                    //handle 0.0.12
                    result = Regex.Match(contents, @"(.+)\.(.+)\.(.+)");
                }

                v = new VersionParts
                {
                    parts = new[]
                    {
                        result.Groups[1].Value,
                        result.Groups[2].Value,
                        result.Groups[3].Value,
                        result.Groups[4].Value
                    }
                };

                if (format == VersionFormat.File && v.parts.Length == 4 &&
                    v.parts[3].IndexOfAny(new[] { 'a', 'b', 'c', 'd', 'e', 'f' }) != -1)
                {
                    // zero out hash code which we can't have in numeric version numbers
                    v.parts[3] = "0";
                }
            }

            for (int i = 0; i < v.parts.Length; i++)
            {
                if (string.IsNullOrEmpty(v.parts[i]))
                {
                    v.parts[i] = "*";
                }
            }

            return(v);
        }