1
1
<?php
2
+
2
3
namespace Shivas \VersioningBundle \Command ;
3
4
4
- use Herrera \Version \Dumper ;
5
- use Herrera \Version \Parser as VersionParser ;
6
- use Herrera \Version \Version ;
7
- use Shivas \VersioningBundle \Handler \HandlerInterface ;
5
+ use RuntimeException ;
6
+ use Shivas \VersioningBundle \Provider \ProviderInterface ;
8
7
use Shivas \VersioningBundle \Service \VersionsManager ;
9
8
use Symfony \Bundle \FrameworkBundle \Command \ContainerAwareCommand ;
10
9
use Symfony \Component \Console \Helper \Table ;
14
13
use Symfony \Component \Console \Output \OutputInterface ;
15
14
use Symfony \Component \Yaml \Parser ;
16
15
use Symfony \Component \Yaml \Yaml ;
16
+ use Version \Version ;
17
17
18
18
class VersionBumpCommand extends ContainerAwareCommand
19
19
{
@@ -22,16 +22,16 @@ protected function configure()
22
22
$ this
23
23
->setName ('app:version:bump ' )
24
24
->setDescription (
25
- 'Bumping of application version using one of available handlers '
25
+ 'Bumping of application version using one of the available providers '
26
26
)
27
27
->addArgument ('version ' , InputArgument::OPTIONAL , 'Version to set, should be compatible with Semantic versioning 2.0.0 ' , null )
28
28
->addOption ('dry-run ' , 'd ' , InputOption::VALUE_NONE , 'Dry run, not update parameters file, just print it ' )
29
- ->addOption ('list-handlers ' , 'l ' , InputOption::VALUE_NONE , 'List registered version handlers ' )
29
+ ->addOption ('list-providers ' , 'l ' , InputOption::VALUE_NONE , 'List registered version providers ' )
30
30
->addOption ('major ' , null , InputOption::VALUE_OPTIONAL , 'Bump MAJOR version by given number ' , 0 )
31
31
->addOption ('minor ' , null , InputOption::VALUE_OPTIONAL , 'Bump MINOR version by given number ' , 0 )
32
32
->addOption ('patch ' , null , InputOption::VALUE_OPTIONAL , 'Bump PATCH version by given number ' , 0 )
33
- ->addOption ('prerelease ' , null , InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL , 'Add PRERELEASE to version ' , null )
34
- ->addOption ('build ' , null , InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL , 'Add BUILD to version ' , null );
33
+ ->addOption ('prerelease ' , null , InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL , 'Add PRERELEASE to version ' , array () )
34
+ ->addOption ('build ' , null , InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL , 'Add BUILD to version ' , array () );
35
35
}
36
36
37
37
protected function execute (InputInterface $ input , OutputInterface $ output )
@@ -44,68 +44,70 @@ protected function execute(InputInterface $input, OutputInterface $output)
44
44
/** @var VersionsManager $manager */
45
45
$ manager = $ this ->getContainer ()->get ('shivas_versioning.manager ' );
46
46
47
- if ($ input ->getOption ('list-handlers ' )) {
48
- $ this ->listHandlers ($ manager , $ output );
47
+ if ($ input ->getOption ('list-providers ' )) {
48
+ $ this ->listProviders ($ manager , $ output );
49
49
return ;
50
50
}
51
51
52
52
if ($ input ->getArgument ('version ' ) === null ) {
53
-
54
53
$ version = $ manager ->getVersion ();
55
54
56
55
if ($ output ->getVerbosity () >= OutputInterface::VERBOSITY_VERY_VERBOSE ) {
57
- $ output ->writeln (sprintf ('Handler : <comment>%s</comment> ' , $ manager ->getActiveHandler ()->getName ()));
56
+ $ output ->writeln (sprintf ('Provider : <comment>%s</comment> ' , $ manager ->getActiveProvider ()->getName ()));
58
57
}
59
58
60
- $ builder = VersionParser:: toBuilder (Dumper:: toString ( $ version ) );
61
-
62
-
63
- if ( $ input -> getOption ( ' major ' ) > 0 ) {
64
- $ builder -> incrementMajor ( intval ( $ input -> getOption ( ' major ' )));
59
+ $ incrementMajor = ( int ) $ input -> getOption ( ' major ' );
60
+ if ( $ incrementMajor > 0 ) {
61
+ for ( $ i = 0 ; $ i < $ incrementMajor ; $ i ++) {
62
+ $ version = $ version -> withMajorIncremented ();
63
+ }
65
64
}
66
65
67
- if ($ input ->getOption ('minor ' ) > 0 ) {
68
- $ builder ->incrementMinor (intval ($ input ->getOption ('minor ' )));
66
+ $ incrementMinor = (int ) $ input ->getOption ('minor ' );
67
+ if ($ incrementMinor > 0 ) {
68
+ for ($ i = 0 ; $ i < $ incrementMinor ; $ i ++) {
69
+ $ version = $ version ->withMinorIncremented ();
70
+ }
69
71
}
70
72
71
- if ($ input ->getOption ('patch ' ) > 0 ) {
72
- $ builder ->incrementPatch (intval ($ input ->getOption ('patch ' )));
73
+ $ incrementPatch = (int ) $ input ->getOption ('patch ' );
74
+ if ($ incrementPatch > 0 ) {
75
+ for ($ i = 0 ; $ i < $ incrementPatch ; $ i ++) {
76
+ $ version = $ version ->withPatchIncremented ();
77
+ }
73
78
}
74
79
75
- if ( $ input ->getOption ('prerelease ' )) {
76
- $ preRelease = $ input -> getOption ( ' prerelease ' );
80
+ $ preRelease = $ input ->getOption ('prerelease ' );
81
+ if (! empty ( $ preRelease)) {
77
82
if (in_array (null , $ preRelease )) {
78
83
$ preRelease = array ();
79
84
}
80
85
81
- $ builder -> setPreRelease ($ preRelease );
86
+ $ version = $ version -> withPreRelease ($ preRelease );
82
87
}
83
88
84
- if ( $ input ->getOption ('build ' )) {
85
- $ build = $ input -> getOption ( ' build ' );
89
+ $ build = $ input ->getOption ('build ' );
90
+ if (! empty ( $ build)) {
86
91
if (in_array (null , $ build )) {
87
92
$ build = array ();
88
93
}
89
94
90
- $ builder -> setBuild ($ build );
95
+ $ version = $ version -> withBuild ($ build );
91
96
}
92
-
93
- $ version = $ builder ->getVersion ();
94
-
95
97
} else {
96
- $ version = VersionParser:: toVersion ($ input ->getArgument ('version ' ));
98
+ $ version = Version:: fromString ($ input ->getArgument ('version ' ));
97
99
}
98
100
99
101
if (!$ input ->getOption ('dry-run ' )) {
100
102
if ($ output ->getVerbosity () >= OutputInterface::VERBOSITY_VERBOSE ) {
101
103
$ output ->writeln (
102
104
sprintf (
103
105
'Updating parameters file with version number: <info>%s</info> ' ,
104
- Dumper:: toString ( $ version )
106
+ $ version-> getVersionString ( )
105
107
)
106
108
);
107
109
} else {
108
- $ output ->writeln (Dumper:: toString ( $ version ));
110
+ $ output ->writeln ($ version-> getVersionString ( ));
109
111
}
110
112
111
113
if (!file_exists ($ paramFile )) {
@@ -115,9 +117,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
115
117
}
116
118
} else {
117
119
if ($ output ->getVerbosity () >= OutputInterface::VERBOSITY_VERBOSE ) {
118
- $ output ->writeln (sprintf ('Version: <comment>%s</comment> ' , Dumper:: toString ( $ version )));
120
+ $ output ->writeln (sprintf ('Version: <comment>%s</comment> ' , $ version-> getVersionString ( )));
119
121
} else {
120
- $ output ->writeln (Dumper:: toString ( $ version ));
122
+ $ output ->writeln ($ version-> getVersionString ( ));
121
123
}
122
124
}
123
125
}
@@ -126,52 +128,51 @@ protected function execute(InputInterface $input, OutputInterface $output)
126
128
* @param VersionsManager $manager
127
129
* @param OutputInterface $output
128
130
*/
129
- protected function listHandlers (VersionsManager $ manager , OutputInterface $ output )
131
+ protected function listProviders (VersionsManager $ manager , OutputInterface $ output )
130
132
{
131
- $ output ->writeln ('Registered Version handlers ' );
132
- $ handlers = $ manager ->getHandlers ();
133
+ $ output ->writeln ('Registered Version providers ' );
134
+ $ providers = $ manager ->getProviders ();
133
135
$ table = new Table ($ output );
134
136
$ table ->setHeaders (array ('Alias ' , 'Priority ' , 'Name ' , 'Supported ' ))
135
137
->setStyle ('borderless ' );
136
138
137
- foreach ($ handlers as $ key => $ handlerEntry ) {
138
- /** @var $handler HandlerInterface */
139
- $ handler = $ handlerEntry [ ' handler ' ];
140
- $ supported = $ handler ->isSupported () ? 'Yes ' : 'No ' ;
141
- $ table ->addRow (array ($ key , $ handlerEntry ['priority ' ], $ handler ->getName (), $ supported ));
139
+ foreach ($ providers as $ key => $ providerEntry ) {
140
+ /** @var $provider ProviderInterface */
141
+ $ provider = $ providerEntry [ ' provider ' ];
142
+ $ supported = $ provider ->isSupported () ? 'Yes ' : 'No ' ;
143
+ $ table ->addRow (array ($ key , $ providerEntry ['priority ' ], $ provider ->getName (), $ supported ));
142
144
}
143
145
144
146
$ table ->render ();
145
147
}
146
148
147
149
/**
148
- * @param Version $version
149
- * @param $file
150
- * @param $param
150
+ * @param Version $version
151
+ * @param string $file
152
+ * @param string $param
151
153
*/
152
154
protected function createParametersFile (Version $ version , $ file , $ param )
153
155
{
154
- $ params = array ('parameters ' => array ($ param => Dumper:: toString ( $ version )));
156
+ $ params = array ('parameters ' => array ($ param => $ version-> getVersionString ( )));
155
157
file_put_contents ($ file , Yaml::dump ($ params ));
156
158
}
157
159
158
160
/**
159
- * @param Version $version
160
- * @param $file
161
- * @param $param
162
- * @throws \ RuntimeException
161
+ * @param Version $version
162
+ * @param string $file
163
+ * @param string $param
164
+ * @throws RuntimeException
163
165
*/
164
166
protected function updateParametersFile (Version $ version , $ file , $ param )
165
167
{
166
168
$ yamlParser = new Parser ();
167
169
168
170
$ params = $ yamlParser ->parse (file_get_contents ($ file ));
169
171
if (!empty ($ params ['parameters ' ])) {
170
- $ params ['parameters ' ][$ param ] = Dumper:: toString ( $ version );
172
+ $ params ['parameters ' ][$ param ] = $ version-> getVersionString ( );
171
173
file_put_contents ($ file , Yaml::dump ($ params ));
172
174
} else {
173
- throw new \ RuntimeException ('Not valid parameters file? ' );
175
+ throw new RuntimeException ('Not valid parameters file? ' );
174
176
}
175
177
}
176
178
}
177
-
0 commit comments