Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider JSON output for CLI #214

Closed
ben-foster-cko opened this issue Mar 10, 2019 · 22 comments
Closed

Consider JSON output for CLI #214

ben-foster-cko opened this issue Mar 10, 2019 · 22 comments
Labels
question Further information is requested

Comments

@ben-foster-cko
Copy link

Hi,

Would you consider an option to output the version information as JSON? We need to parse the minver output in our build tool (Cake).

@adamralph adamralph added the question Further information is requested label Mar 10, 2019
@adamralph
Copy link
Owner

@ben-foster-cko do you just need the version string? Only the version is printed to stdout, as a plain string. All other messages go to stderr. If you just read stdout then the entire string is the version.

@ben-foster-cko
Copy link
Author

@adamralph so just to confirm, the last line will always be the version?

MinVer: No commit found with a valid SemVer 2.0 version. Using default version 0.0.0-alpha.0.
MinVer: Using { Commit: 798fedc, Tag: null, Version: 0.0.0-alpha.0, Height: 21 }.
MinVer: Calculated version 0.0.0-alpha.0.21.
0.0.0-alpha.0.21

@adamralph
Copy link
Owner

@ben-foster-cko the version is the only line. Bear in mind that there are two output streams: stdout and stderr. You should only read stdout:

(stderr) MinVer: No commit found with a valid SemVer 2.0 version. Using default version 0.0.0-alpha.0.
(stderr) MinVer: Using { Commit: 798fedc, Tag: null, Version: 0.0.0-alpha.0, Height: 21 }.
(stderr) MinVer: Calculated version 0.0.0-alpha.0.21.
(stdout) 0.0.0-alpha.0.21

@RehanSaeed
Copy link

Also came here looking to use the CLI tool in Cake build.

JSON output would be useful. An option to just output the ful version would be great too.

@adamralph
Copy link
Owner

@RehanSaeed as mentioned above, the full version, and only the full version, is written to stdout. You can read that, and then it is trivial to split it into its constituent parts, should you need to.

@StefanOssendorf
Copy link

Any link to an example how to use the cli within a cake script? Somehow I don't get this to work :-/

@adamralph
Copy link
Owner

@StefanOssendorf I'm not aware of any such example. The CLI is a regular .NET SDK tool. It is invoked with minver and it prints the version to stdout. You should be able to use any mechanism which is capable of executing a .NET SDK tool and reading its output.

@RehanSaeed
Copy link

I ended up with this code in my Cake script:

var directoryBuildPropsFilePath = GetFiles("Directory.Build.props").Single().ToString();
var directoryBuildPropsDocument = System.Xml.Linq.XDocument.Load(directoryBuildPropsFilePath);
var preReleasePhase = directoryBuildPropsDocument.Descendants("MinVerDefaultPreReleasePhase").Single().Value;

string version = null;
StartProcess(
    "dotnet",
    new ProcessSettings()
        .WithArguments(x => x
            .Append("minver")
            .AppendSwitch("--default-pre-release-phase", preReleasePhase))
        .SetRedirectStandardOutput(true)
        .SetRedirectedStandardOutputHandler( // Beware, this gets called twice, hence the null check.
            output =>
            {
                if (output != null)
                {
                    version = output;
                }
                return output;
            }));

I'm reading the pre-release phase from a Directory.Build.props file and plugging that into the dotnet minver CLI tool. Then you can use the version any way you like. In my case I'm using the version to tag a Docker image.

@StefanOssendorf
Copy link

That's exactly what I'm was looking for. Thank you very much :-)

@adamralph
Copy link
Owner

adamralph commented Jun 9, 2020

Thanks for chiming in @RehanSaeed.

BTW, this is quite a bit simpler with SimpleExec:

var version = Read("minver", $"--default-pre-release-phase {preReleasePhase}");

@augustoproiete
Copy link

Hi guys, I took a stab at writing a small Cake plugin to run MinVer and parse the version:
https://github.com/augustoproiete/Cake.MinVer

#addin "nuget:?package=Cake.MinVer&version=0.1.0"

var version = MinVer();

Task("Example")
    .Does(context =>
{
    context.Information($"Version: {version.Version}");
    context.Information($"Major: {version.Major}");
    context.Information($"Minor: {version.Minor}");
    context.Information($"Patch: {version.Patch}");
    context.Information($"PreRelease: {version.PreRelease}");
    context.Information($"BuildMetadata: {version.BuildMetadata}");
});

RunTarget("Example");

@RehanSaeed
Copy link

That looks awesome.

Would be nice if MinVer listed Cake.MinVer on it's ReadMe, for visibility.

@adamralph
Copy link
Owner

I'm not sure I understand the motivation. It seems to be, essentially, a .NET API for using MinVer. Why does it have a dependency on Cake?

BTW, I am considering releasing the underlying MinVer library as a package, so that would also provide a .NET API.

@adamralph
Copy link
Owner

BTW this can be acheived today with a single line of code, using the SimpleExec and semver packages:

var version = Semver.SemVersion.Parse(SimpleExec.Command.Read("minver").Trim());

@damianh
Copy link
Contributor

damianh commented Aug 11, 2020

A problem now is that Cake.Minver is a layer that needs maintenance both against Cake APIs and MinVer, will lag behind MinVer changes (which may be breaking) and may be abandoned (which has happened to other Cake plugins).

@adamralph has no obligations to that plugin so my advice is caveat emptor ("buyer beware")

@augustoproiete
Copy link

@adamralph It's the other way around. It's a Cake plugin, specifically built for Cake users who are familiar with how it works and its DSL, that has a dependency on MinVer to calculate the version.

@augustoproiete
Copy link

@damianh Thanks for your support 🤷‍♂️

@adamralph
Copy link
Owner

@augustoproiete how much value does it add over this single line?

var version = Semver.SemVersion.Parse(SimpleExec.Command.Read("minver").Trim());

Is the effort worth it?

@augustoproiete
Copy link

augustoproiete commented Aug 11, 2020

@adamralph Yes, I think so...

var version = MinVer();

Reads much better IMO, and has the added benefit of not having to take a dependency on Semver and on SimpleExec.

Also, version comes with a couple of handy properties such as AssemblyVersion and FileVersion ready to use.

Property Type Description
Version string The original, non-normalized version string
Major int The major version number
Minor int The minor version number
Patch int The patch version number
PreRelease string The pre-release extension
BuildMetadata string The build metadata extension
AssemblyVersion string {Major}.0.0.0
FileVersion string {Major}.{Minor}.{Patch}.0
InformationalVersion string same as Version above
PackageVersion string same as Version above

@damianh
Copy link
Contributor

damianh commented Aug 11, 2020

@damianh Thanks for your support 🤷‍♂️

Thank me later ;)

@RehanSaeed
Copy link

I've been bitten by both cake and NuGet dependencies before. I think the real solution is for Cake to have something akin to SimpleExec built into it. It has some methods which are very close. Until then, the solutions above are good enough. Pick your poison.

@adamralph
Copy link
Owner

I think the real solution is for Cake to have something akin to SimpleExec built into it.

Why not just use SimpleExec?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants