1 | <?php |
||
2 | /** |
||
3 | * This file is part of the SVN-Buddy library. |
||
4 | * For the full copyright and license information, please view |
||
5 | * the LICENSE file that was distributed with this source code. |
||
6 | * |
||
7 | * @copyright Alexander Obuhovich <[email protected]> |
||
8 | * @link https://github.com/console-helpers/svn-buddy |
||
9 | */ |
||
10 | |||
11 | namespace ConsoleHelpers\SVNBuddy\Command; |
||
12 | |||
13 | |||
14 | use ConsoleHelpers\SVNBuddy\Config\AbstractConfigSetting; |
||
15 | use ConsoleHelpers\SVNBuddy\Config\ChoiceConfigSetting; |
||
16 | use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyConflictTracker; |
||
17 | use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
||
18 | use Symfony\Component\Console\Input\InputArgument; |
||
19 | use Symfony\Component\Console\Input\InputInterface; |
||
20 | use Symfony\Component\Console\Input\InputOption; |
||
21 | use Symfony\Component\Console\Output\OutputInterface; |
||
22 | |||
23 | class UpdateCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand |
||
24 | { |
||
25 | |||
26 | const SETTING_UPDATE_AUTO_DEPLOY = 'update.auto-deploy'; |
||
27 | |||
28 | /** |
||
29 | * Working copy conflict tracker. |
||
30 | * |
||
31 | * @var WorkingCopyConflictTracker |
||
32 | */ |
||
33 | private $_workingCopyConflictTracker; |
||
34 | |||
35 | /** |
||
36 | * Prepare dependencies. |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | protected function prepareDependencies() |
||
41 | { |
||
42 | parent::prepareDependencies(); |
||
43 | |||
44 | $container = $this->getContainer(); |
||
45 | |||
46 | $this->_workingCopyConflictTracker = $container['working_copy_conflict_tracker']; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | protected function configure() |
||
53 | { |
||
54 | $this |
||
55 | ->setName('update') |
||
56 | ->setDescription('Bring changes from the repository into the working copy') |
||
57 | ->setAliases(array('up')) |
||
58 | ->addArgument( |
||
59 | 'path', |
||
60 | InputArgument::OPTIONAL, |
||
61 | 'Working copy path', |
||
62 | '.' |
||
63 | ) |
||
64 | ->addOption( |
||
65 | 'revision', |
||
66 | 'r', |
||
67 | InputOption::VALUE_REQUIRED, |
||
68 | 'Update working copy to specified revision, e.g. <comment>NUMBER</comment>, <comment>{DATE}</comment>, <comment>HEAD</comment>, <comment>BASE</comment>, <comment>COMMITTED</comment>, <comment>PREV</comment>' |
||
69 | ) |
||
70 | ->addOption( |
||
71 | 'ignore-externals', |
||
72 | null, |
||
73 | InputOption::VALUE_NONE, |
||
74 | 'Ignore externals definitions' |
||
75 | ) |
||
76 | ->addOption( |
||
77 | 'auto-deploy', |
||
78 | null, |
||
79 | InputOption::VALUE_REQUIRED, |
||
80 | 'Automatically perform local deployment on successful update, e.g. <comment>yes</comment> or <comment>no</comment>' |
||
81 | ); |
||
82 | |||
83 | parent::configure(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Return possible values for the named option |
||
88 | * |
||
89 | * @param string $optionName Option name. |
||
90 | * @param CompletionContext $context Completion context. |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function completeOptionValues($optionName, CompletionContext $context) |
||
95 | { |
||
96 | $ret = parent::completeOptionValues($optionName, $context); |
||
97 | |||
98 | if ( $optionName === 'auto-deploy' ) { |
||
99 | return array('yes', 'no'); |
||
100 | } |
||
101 | |||
102 | return $ret; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | protected function execute(InputInterface $input, OutputInterface $output) |
||
109 | { |
||
110 | $wc_path = $this->getWorkingCopyPath(); |
||
111 | $revision = $this->io->getOption('revision'); |
||
112 | $ignore_externals = $this->io->getOption('ignore-externals'); |
||
113 | |||
114 | $show_revision = $revision ? $revision : 'HEAD'; |
||
115 | $show_externals = $ignore_externals ? '(excluding externals)' : '(including externals)'; |
||
116 | $this->io->writeln( |
||
117 | 'Updating working copy to <info>' . $show_revision . '</info> revision ' . $show_externals . ' ... ' |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
118 | ); |
||
119 | |||
120 | $arguments = array($wc_path); |
||
121 | |||
122 | if ( $revision ) { |
||
123 | $arguments[] = '--revision'; |
||
124 | $arguments[] = $revision; |
||
125 | } |
||
126 | |||
127 | if ( $ignore_externals ) { |
||
128 | $arguments[] = '--ignore-externals'; |
||
129 | } |
||
130 | |||
131 | $command = $this->repositoryConnector->getCommand('update', $arguments); |
||
132 | $command->runLive(array( |
||
133 | $wc_path => '.', |
||
134 | )); |
||
135 | |||
136 | if ( $this->_workingCopyConflictTracker->getNewConflicts($wc_path) ) { |
||
137 | $this->_workingCopyConflictTracker->add($wc_path); |
||
138 | } |
||
139 | |||
140 | $this->io->writeln('<info>Done</info>'); |
||
141 | |||
142 | $this->deploy(); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Performs a deploy. |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | protected function deploy() |
||
151 | { |
||
152 | $auto_deploy = $this->io->getOption('auto-deploy'); |
||
153 | |||
154 | if ( $auto_deploy !== null ) { |
||
155 | $auto_deploy = $auto_deploy === 'yes'; |
||
156 | } |
||
157 | else { |
||
158 | $auto_deploy = (boolean)$this->getSetting(self::SETTING_UPDATE_AUTO_DEPLOY); |
||
159 | } |
||
160 | |||
161 | if ( !$auto_deploy ) { |
||
162 | return; |
||
163 | } |
||
164 | |||
165 | $this->runOtherCommand( |
||
166 | 'deploy', |
||
167 | array( |
||
168 | 'path' => $this->io->getArgument('path'), |
||
169 | '--local' => true, |
||
170 | ) |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Returns option names, that makes sense to use in aggregation mode. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function getAggregatedOptions() |
||
180 | { |
||
181 | return array('ignore-externals'); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Returns list of config settings. |
||
186 | * |
||
187 | * @return AbstractConfigSetting[] |
||
188 | */ |
||
189 | public function getConfigSettings() |
||
190 | { |
||
191 | return array( |
||
192 | new ChoiceConfigSetting( |
||
193 | self::SETTING_UPDATE_AUTO_DEPLOY, |
||
194 | array(1 => 'Yes', 0 => 'No'), |
||
195 | 1 |
||
196 | ), |
||
197 | ); |
||
198 | } |
||
199 | |||
200 | } |
||
201 |