22
33/**
44 * @file
5- * Contains \DennisDigital\Drupal\Console\Command\Checkout\AbstractCheckoutCommand .
5+ * Contains \DennisDigital\Drupal\Console\Command\Site\CheckoutCommand .
66 *
77 * Does repo checkouts.
88 */
99
1010namespace DennisDigital \Drupal \Console \Command \Site \Checkout ;
1111
1212use Symfony \Component \Console \Input \InputOption ;
13- use Symfony \Component \Console \Input \InputInterface ;
14- use Symfony \Component \Console \Output \OutputInterface ;
15- use DennisDigital \Drupal \Console \Command \Exception \CommandException ;
1613use DennisDigital \Drupal \Console \Command \Site \AbstractCommand ;
1714
1815/**
19- * Class AbstractCheckoutCommand
16+ * Class SiteCheckoutCommand
2017 *
21- * @package DennisDigital\Drupal\Console\Command\Site\Checkout
18+ * @package DennisDigital\Drupal\Console\Command
2219 */
2320abstract class AbstractCheckoutCommand extends AbstractCommand {
24-
25- /**
26- * Stores repo information.
27- *
28- * @var array Repo.
29- */
30- protected $ repo ;
31-
3221 /**
33- * The branch/tag to checkout .
22+ * Types of refs that can be checked out .
3423 *
35- * @var string
24+ * @var array
3625 */
37- protected $ ref ;
38-
39- /**
40- * Current branch/tag
41- *
42- * @var string
43- */
44- protected $ currentRef ;
26+ protected $ refTypes = ['tag ' , 'branch ' ];
4527
4628 /**
4729 * {@inheritdoc}
@@ -56,211 +38,15 @@ protected function configure() {
5638 InputOption::VALUE_NONE ,
5739 'Will force the checkout and replace all local changes '
5840 );
59- }
60-
61- /**
62- * {@inheritdoc}
63- */
64- protected function interact (InputInterface $ input , OutputInterface $ output ) {
65- parent ::interact ($ input , $ output );
66-
67- // Validate repo.
68- $ this ->validateRepo ();
69-
70- $ this ->currentRef = $ this ->getCurrentRef ();
71- }
72-
73- /**
74- * {@inheritdoc}
75- */
76- protected function execute (InputInterface $ input , OutputInterface $ output ) {
77- parent ::execute ($ input , $ output );
78-
79- // Validate repo.
80- $ this ->validateRepo ();
81-
82- // Validate ref.
83- $ this ->ref = $ this ->getRef ($ input );
84-
85- if ($ this ->ref == $ this ->currentRef ) {
86- $ this ->io ->commentBlock ('Current branch/tag selected, skipping checkout command. ' );
87- return ;
88- }
89-
90- $ this ->io ->comment (sprintf ('Checking out %s (%s) on %s ' ,
91- $ this ->siteName ,
92- $ this ->ref ,
93- $ this ->destination
94- ));
95-
96- switch ($ this ->repo ['type ' ]) {
97- case 'git ' :
98- // Check if repo exists and has any changes.
99- if ($ this ->fileExists ($ this ->destination ) &&
100- $ this ->fileExists ($ this ->destination . '. ' . $ this ->repo ['type ' ])
101- ) {
102- if ($ input ->hasOption ('force ' ) &&
103- !$ input ->getOption ('force ' )
104- ) {
105- // Check for uncommitted changes.
106- $ this ->gitDiff ();
107- }
108- // Check out ref on existing repo.
109- $ this ->gitCheckout ();
110- }
111- else {
112- // Clone repo.
113- $ this ->gitClone ();
114- }
115- break ;
116-
117- default :
118- $ message = sprintf ('%s is not supported. ' ,
119- $ this ->repo ['type ' ]
120- );
121- throw new CommandException ($ message );
122- }
123- }
124-
125- /**
126- * Helper to validate repo.
127- *
128- * @throws CommandException
129- *
130- * @return string Repo url
131- */
132- protected function validateRepo () {
133- if (isset ($ this ->config ['repo ' ])) {
134- $ this ->repo = $ this ->config ['repo ' ];
135- }
136- else {
137- throw new CommandException ('Repo not found in sites.yml ' );
138- }
139-
140- return $ this ->repo ;
141- }
142-
143- /**
144- * Helper to detect local modifications.
145- *
146- * @return TRUE If everything is ok.
147- *
148- * @throws CommandException
149- */
150- protected function gitDiff () {
151- $ command = sprintf (
152- 'cd %s && git diff-files --name-status -r --ignore-submodules ' ,
153- $ this ->shellPath ($ this ->destination )
154- );
155-
156- $ shellProcess = $ this ->getShellProcess ();
157-
158- if ($ shellProcess ->exec ($ command , TRUE )) {
159- if (!empty ($ shellProcess ->getOutput ())) {
160- $ message = sprintf ('You have uncommitted changes on %s ' . PHP_EOL .
161- 'Please commit or revert your changes before checking out the site. ' . PHP_EOL .
162- 'If you want to wipe your local changes use --force. ' ,
163- $ this ->destination
164- );
165- throw new CommandException ($ message );
166- }
167- }
168- else {
169- throw new CommandException ($ shellProcess ->getOutput ());
170- }
171-
172- return TRUE ;
173- }
174-
175- /**
176- * Helper to do the actual clone command.
177- *
178- * @return bool TRUE If successful.
179- *
180- * @throws CommandException
181- */
182- protected function gitClone () {
183- $ command = sprintf ('git clone %s %s ' ,
184- $ this ->repo ['url ' ],
185- $ this ->shellPath ($ this ->destination )
186- );
187- $ this ->io ->commentBlock ($ command );
18841
189- $ shellProcess = $ this ->getShellProcess ();
190-
191- if ($ shellProcess ->exec ($ command , TRUE )) {
192- $ this ->io ->success (sprintf ('Repo cloned on %s ' , $ this ->destination ));
193- }
194- else {
195- throw new CommandException ($ shellProcess ->getOutput ());
196- }
197-
198- // Checkout the tag.
199- $ this ->gitCheckout ();
200-
201- return TRUE ;
202- }
203-
204- /**
205- * Helper to check out a tag.
206- *
207- * @return bool TRUE If successful.
208- *
209- * @throws CommandException
210- */
211- protected function gitCheckout () {
212- $ command = sprintf (
213- 'cd %s && ' .
214- 'git fetch --all && ' .
215- 'chmod 777 web/sites/default && ' .
216- 'chmod 777 web/sites/default/settings.php && ' .
217- 'git checkout %s --force ' ,
218- $ this ->shellPath ($ this ->destination ),
219- $ this ->ref
220- );
221-
222- $ shellProcess = $ this ->getShellProcess ();
223-
224- if ($ shellProcess ->exec ($ command , TRUE )) {
225- $ this ->io ->success (sprintf ('Checked out %s ' , $ this ->ref ));
226- }
227- else {
228- throw new CommandException ($ shellProcess ->getOutput ());
229-
230- }
231-
232- return TRUE ;
233- }
234-
235- /**
236- * Helper to retrieve the current working branch on the site's directory.
237- *
238- * @return mixed
239- */
240- protected function getCurrentRef () {
241- if ($ this ->fileExists ($ this ->destination )) {
242- // Get branch from site directory.
243- $ command = sprintf ('cd %s && git branch ' ,
244- $ this ->shellPath ($ this ->destination )
42+ // Allow different ref types to be checked out.
43+ foreach ($ this ->refTypes as $ refType ) {
44+ $ this ->addOption (
45+ $ refType ,
46+ '- ' . strtoupper (substr ($ refType , 0 , 1 )),
47+ InputOption::VALUE_OPTIONAL ,
48+ sprintf ('Specify which %s to checkout. ' , $ refType )
24549 );
246-
247- $ shellProcess = $ this ->getShellProcess ()->printOutput (FALSE );
248-
249- if ($ shellProcess ->exec ($ command , TRUE )) {
250- preg_match_all ("|\*\s(.*)| " , $ shellProcess ->getOutput (), $ matches );
251- if (!empty ($ matches [1 ] && is_array ($ matches [1 ]))) {
252- $ match = explode (' ' , trim (reset ($ matches [1 ]), '() ' ));
253- return array_pop ($ match );
254- }
255- }
25650 }
25751 }
258-
259- /**
260- * Get the requested ref (tag/branch).
261- *
262- * @param InputInterface $input
263- * @return string
264- */
265- abstract protected function getRef (InputInterface $ input );
26652}
0 commit comments