Giter Site home page Giter Site logo

Consider JSON output for CLI about minver HOT 22 CLOSED

adamralph avatar adamralph commented on August 15, 2024 1
Consider JSON output for CLI

from minver.

Comments (22)

augustoproiete avatar augustoproiete commented on August 15, 2024 10

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

from minver.

adamralph avatar adamralph commented on August 15, 2024 4

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

Why not just use SimpleExec?

from minver.

RehanSaeed avatar RehanSaeed commented on August 15, 2024 3

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.

from minver.

RehanSaeed avatar RehanSaeed commented on August 15, 2024 3

That looks awesome.

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

from minver.

damianh avatar damianh commented on August 15, 2024 3

@damianh Thanks for your support 🤷‍♂️

Thank me later ;)

from minver.

adamralph avatar adamralph commented on August 15, 2024 2

Thanks for chiming in @RehanSaeed.

BTW, this is quite a bit simpler with SimpleExec:

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

from minver.

adamralph avatar adamralph commented on August 15, 2024 2

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.

from minver.

adamralph avatar adamralph commented on August 15, 2024 2

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

from minver.

damianh avatar damianh commented on August 15, 2024 2

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

from minver.

augustoproiete avatar augustoproiete commented on August 15, 2024 2

@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

from minver.

RehanSaeed avatar RehanSaeed commented on August 15, 2024 2

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.

from minver.

adamralph avatar adamralph commented on August 15, 2024 1

@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

from minver.

adamralph avatar adamralph commented on August 15, 2024 1

@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.

from minver.

adamralph avatar adamralph commented on August 15, 2024 1

@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?

from minver.

adamralph avatar adamralph commented on August 15, 2024

@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.

from minver.

ben-foster-cko avatar ben-foster-cko commented on August 15, 2024

@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

from minver.

RehanSaeed avatar RehanSaeed commented on August 15, 2024

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.

from minver.

StefanOssendorf avatar StefanOssendorf commented on August 15, 2024

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

from minver.

adamralph avatar adamralph commented on August 15, 2024

@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.

from minver.

StefanOssendorf avatar StefanOssendorf commented on August 15, 2024

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

from minver.

augustoproiete avatar augustoproiete commented on August 15, 2024

@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.

from minver.

augustoproiete avatar augustoproiete commented on August 15, 2024

@damianh Thanks for your support 🤷‍♂️

from minver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.