Hoa central
Requiresnapshot.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Devtools\Bin;
38 
39 use Hoa\Console;
40 
50 {
56  protected $options = [
57  ['no-verbose', Console\GetOption::NO_ARGUMENT, 'V'],
58  ['snapshot', Console\GetOption::NO_ARGUMENT, 's'],
59  ['days', Console\GetOption::NO_ARGUMENT, 'd'],
60  ['commits', Console\GetOption::NO_ARGUMENT, 'c'],
61  ['help', Console\GetOption::NO_ARGUMENT, 'h'],
62  ['help', Console\GetOption::NO_ARGUMENT, '?']
63  ];
64 
65 
66 
72  public function main()
73  {
74  $verbose = Console::isDirect(STDOUT);
75  $printSnapshot = false;
76  $printDays = false;
77  $printCommits = false;
78 
79  while (false !== $c = $this->getOption($v)) {
80  switch ($c) {
81  case '__ambiguous':
82  $this->resolveOptionAmbiguity($v);
83 
84  break;
85 
86  case 'V':
87  $verbose = false;
88 
89  break;
90 
91  case 's':
92  $printSnapshot = true;
93 
94  break;
95 
96  case 'd':
97  $printDays = true;
98 
99  break;
100 
101  case 'c':
102  $printCommits = true;
103 
104  break;
105 
106  case 'h':
107  case '?':
108  default:
109  return $this->usage();
110  }
111  }
112 
113  $this->parser->listInputs($repositoryRoot);
114 
115  if (empty($repositoryRoot)) {
116  return $this->usage();
117  }
118 
119  if (false === file_exists($repositoryRoot . DS . '.git')) {
120  throw new Console\Exception(
121  '%s is not a valid Git repository.',
122  0,
123  $repositoryRoot
124  );
125  }
126 
128  'git --git-dir=' . $repositoryRoot . '/.git ' .
129  'describe --abbrev=0 --tags origin/master'
130  );
131 
132  if (empty($tag)) {
133  throw new Console\Exception('No tag.', 1);
134  }
135 
136  $timeZone = new \DateTimeZone('UTC');
137  $snapshotDT = \DateTime::createFromFormat(
138  '*.y.m.d',
139  $tag,
140  $timeZone
141  );
142  $sixWeeks = new \DateInterval('P6W');
143  $nextSnapshotDT = clone $snapshotDT;
144  $nextSnapshotDT->add($sixWeeks);
145  $today = new \DateTime('now', $timeZone);
146 
147  $needNewSnapshot = '+' === $nextSnapshotDT->diff($today)->format('%R');
148  $numberOfDays = 0;
149  $numberOfCommits = 0;
150  $output = 'No snapshot is required.';
151 
152  if (true === $needNewSnapshot) {
153  $numberOfDays = (int) $nextSnapshotDT->diff($today)->format('%a');
154  $numberOfCommits = (int) Console\Processus::execute(
155  'git --git-dir=' . $repositoryRoot . '/.git ' .
156  'rev-list ' . $tag . '..origin/master --count'
157  );
158  $needNewSnapshot = 0 < $numberOfCommits;
159 
160  if (true === $needNewSnapshot) {
161  $output =
162  'A snapshot is required, since ' . $numberOfDays .
163  ' day' . (1 < $numberOfDays ? 's' : '') .
164  ' (tag ' . $tag . ', ' . $numberOfCommits .
165  ' commit' . (1 < $numberOfCommits ? 's' : '') .
166  ' to publish)!';
167  }
168  }
169 
170  if (true === $printSnapshot ||
171  true === $printDays ||
172  true === $printCommits) {
173  $columns = [];
174 
175  if (true === $printSnapshot) {
176  $columns[] = $tag;
177  }
178 
179  if (true === $printDays) {
180  $columns[] =
181  $numberOfDays . ' day' .
182  (1 < $numberOfDays ? 's' : '');
183  }
184 
185  if (true === $printCommits) {
186  $columns[] =
187  $numberOfCommits . ' commit' .
188  (1 < $numberOfCommits ? 's' : '');
189  }
190 
191  echo implode("\t", $columns), "\n";
192  } elseif (true === $verbose) {
193  echo $output, "\n";
194  }
195 
196  return !$needNewSnapshot;
197  }
198 
204  public function usage()
205  {
206  echo
207  'Usage : devtools:requiresnapshot <options> repository-root', "\n",
208  'Options :', "\n",
209  $this->makeUsageOptionsList([
210  'V' => 'No-verbose, i.e. be as quiet as possible, just ' .
211  'print essential informations.',
212  's' => 'Print the latest snapshot name in a column.',
213  'd' => 'Print the number of days since the latest ' .
214  'snapshot in a column.',
215  'c' => 'Print the number of commits since the latest ' .
216  'snapshot in a column.',
217  'help' => 'This help.'
218  ]), "\n";
219 
220  return;
221  }
222 }
223 
224 __halt_compiler();
225 Check if a library requires a new snapshot or not.
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
static execute($commandLine, $escape=true)
Definition: Processus.php:1165
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190
static isDirect($pipe)
Definition: Console.php:261