summaryrefslogtreecommitdiff
path: root/lib/Version/Semantic.pm6
blob: cc2945ff631c9daf6e927fa42adc52ad67368ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /usr/bin/env false

use v6.c;

class Version::Semantic
{
	has Int $.major;
	has Int $.minor;
	has Int $.patch;

	multi method new (
		Version:D $v,
    ) {
		self.new(|$v.parts);
    }

	multi method new (
		Str:D $v,
    ) {
		self.new(|$v.split(".")>>.Int);
    }

	multi method new (
		Int:D $major where -1 < * = 0,
		Int:D $minor where -1 < * = 0,
		Int:D $patch where -1 < * = 0,
    ) {
		self.bless(
			:$major,
			:$minor,
			:$patch,
        );
    }

	multi method new
	{
		die "Semantic::Version should be created with .new(Int, Int, Int), where each Int is 0 or higher";
	}

	method bump-major
	{
		$!major++;

		$!minor = 0;
		$!patch = 0;

		return self;
	}

	method bump-minor
	{
		$!minor++;

		$!patch = 0;

		return self;
	}

	method bump-patch
	{
		$!patch++;

		return self;
	}

	method parts
	{
		return ($!major, $!minor, $!patch);
	}

	method perl
	{
		return "Semantic::Version.new(\"{self.Str}\")";
	}

	method gist
	{
		return "Semantic::Version.new({self.Str})";
	}

	method Str
	{
		return "{$!major}.{$!minor}.{$!patch}";
	}
}

=begin pod

=NAME    Version::Semantic
=AUTHOR  Patrick Spek <p.spek@tyil.work>
=VERSION 0.1.0

=head1 SYNOPSIS

=item1 C<Version::Semantic.new>
=item1 C<Version::Semantic.new(v0)>

=head1 DESCRIPTION

A variant on C<Version> which enforces
L<Semantic Versioning|https://semver.org>.

=head1 EXAMPLES

=head2 Creating, bumping and saying

=begin input
use Version::Semantic;

my Version::Semantic $v .= new;

$v.bump-major;
$v.bump-patch;

say $v;
=end input

=begin output
1.0.1
=end output

=head2 Create from existing Version

=begin input
use Version::Semantic;

say Version::Semantic.new(v2.6.7);
=end input

=begin output
2.6.7
=end output

=head1 SEE ALSO

=item1 C<Version>

=end pod

# vim: ft=perl6 noet