1 | <?php |
---|
2 | |
---|
3 | use Symfony\Component\Console\Command\Command; |
---|
4 | use Symfony\Component\Console\Input\InputArgument; |
---|
5 | use Symfony\Component\Console\Input\InputInterface; |
---|
6 | use Symfony\Component\Console\Input\InputOption; |
---|
7 | use Symfony\Component\Console\Output\OutputInterface; |
---|
8 | |
---|
9 | class GithubRev extends Command { |
---|
10 | protected function configure() { |
---|
11 | $this |
---|
12 | ->setName('github:revision') |
---|
13 | ->setDescription("\tCommit de fichiers de contenu sur Github (svn status, up, add, commit etc…)\n\t\t\t\tInitialiser avec un dépot Github avec la commande : spip rev -g https://github.com/xx/xx.git\n") |
---|
14 | ->setAliases(array( |
---|
15 | 'rev' // abbréviation commune pour "synchro" |
---|
16 | )) |
---|
17 | ->addOption( |
---|
18 | 'dest', |
---|
19 | 'd', |
---|
20 | InputOption::VALUE_OPTIONAL, |
---|
21 | 'Répertoire de destination', |
---|
22 | 'conversion_spip' |
---|
23 | ) |
---|
24 | ->addOption( |
---|
25 | 'depot', |
---|
26 | 'g', |
---|
27 | InputOption::VALUE_OPTIONAL, |
---|
28 | 'Dépot GIT ou synchroniser les conversions', |
---|
29 | '' |
---|
30 | ) |
---|
31 | ->addOption( |
---|
32 | 'action', |
---|
33 | 'a', |
---|
34 | InputOption::VALUE_OPTIONAL, |
---|
35 | 'Actions possibles : commit, diff, ...', |
---|
36 | '' |
---|
37 | ) |
---|
38 | |
---|
39 | ; |
---|
40 | } |
---|
41 | |
---|
42 | protected function execute(InputInterface $input, OutputInterface $output) { |
---|
43 | global $spip_racine; |
---|
44 | global $spip_loaded; |
---|
45 | |
---|
46 | include_spip("iterateur/data"); |
---|
47 | |
---|
48 | $depot = $input->getOption('depot') ; |
---|
49 | |
---|
50 | // Répertoire de destination ou l'on effectue le svn checkout (/convertion_spip par défaut). |
---|
51 | $dest = $input->getOption('dest') ; |
---|
52 | if(!is_dir($dest)) mkdir($dest); |
---|
53 | |
---|
54 | if ($spip_loaded) { |
---|
55 | |
---|
56 | chdir($spip_racine); |
---|
57 | |
---|
58 | exec('svn info ' . $dest , $r); |
---|
59 | |
---|
60 | if($r[0] == "Path: $dest"){ |
---|
61 | $c = inc_ls_to_array_dist($dest . "/*/collections") ; |
---|
62 | $collections = $c[0]['dirname'] . "/" . $c[0]['basename'] ; |
---|
63 | $dest = "$collections" ; |
---|
64 | $output->writeln("<info>Dépot Git OK : $dest</info>"); |
---|
65 | }else{ // pas de dépot GIT |
---|
66 | if($depot){ |
---|
67 | $output->writeln("<error>Checkout du dépot $depot dans $dest</error>"); |
---|
68 | passthru("svn co $depot $dest"); |
---|
69 | $output->writeln("<info>Relancez la commande.</info>"); |
---|
70 | die() ; |
---|
71 | }else{ |
---|
72 | $output->writeln("<info>Relancez la commande avec l'option : spip rev -g https://github.com/xx/xx.git</info>"); |
---|
73 | die() ; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | if (!function_exists('passthru')){ |
---|
78 | $output->writeln("<error>Votre installation de PHP doit pouvoir exécuter des commandes externes avec la fonction passthru().</error>"); |
---|
79 | } |
---|
80 | /* Si c'est bon on continue */ |
---|
81 | else{ |
---|
82 | |
---|
83 | chdir($dest); |
---|
84 | |
---|
85 | $output->writeln( |
---|
86 | array( |
---|
87 | "<info>C'est parti pour une vérif de commit.</info>" |
---|
88 | )); |
---|
89 | |
---|
90 | // Quelques vérifs en svn status. |
---|
91 | exec('svn status .', $results, $err); |
---|
92 | |
---|
93 | if ($err) { |
---|
94 | $output->writeln(array("<error>Erreur SVN.</error>")); |
---|
95 | } else { |
---|
96 | |
---|
97 | // Pas de modification de fichier notable => RAS, on quitte. |
---|
98 | if(sizeof($results) == 0){ |
---|
99 | $output->writeln(array( |
---|
100 | '<info>Status : RAS</info>' |
---|
101 | )); |
---|
102 | die(); |
---|
103 | } |
---|
104 | |
---|
105 | // Sinon détaillons le status |
---|
106 | $output->writeln(array( |
---|
107 | '<info>Status</info>', |
---|
108 | join("\n", $results) |
---|
109 | )); |
---|
110 | |
---|
111 | // Ajouts ? |
---|
112 | $results_n = array_filter($results, function ($line) { |
---|
113 | return preg_match(',^\?,', $line); |
---|
114 | }); |
---|
115 | |
---|
116 | if (count($results_n) > 0) { |
---|
117 | |
---|
118 | /* nettoyage */ |
---|
119 | foreach($results_n as $a){ |
---|
120 | $a = preg_replace("/^\?\s*/", "", $a) ; |
---|
121 | |
---|
122 | if(is_dir($a)) |
---|
123 | $dossiers_ajoutes[] = $a ; |
---|
124 | else |
---|
125 | $fichiers_ajoutes[] = $a ; |
---|
126 | } |
---|
127 | |
---|
128 | if(sizeof($dossiers_ajoutes) > 0){ |
---|
129 | $output->writeln(array( |
---|
130 | "<info>Svn add des dossiers</info>", |
---|
131 | join("\n", $dossiers_ajoutes) |
---|
132 | )); |
---|
133 | |
---|
134 | foreach($dossiers_ajoutes as $dos){ |
---|
135 | exec("svn add " . $dos, $results, $err); |
---|
136 | if ($err) { |
---|
137 | $output->writeln(array("<error>Erreur SVN add $dos</error>")); |
---|
138 | } else { |
---|
139 | $output->writeln(array( |
---|
140 | "<info>Svn add $dos</info>" |
---|
141 | )); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | if(sizeof($fichiers_ajoutes) > 0){ |
---|
146 | |
---|
147 | $output->writeln(array( |
---|
148 | "<info>Svn add des fichiers</info>", |
---|
149 | join("\n", $fichiers_ajoutes) |
---|
150 | )); |
---|
151 | exec("svn add " . join(" , ", $fichiers_ajoutes), $results, $err); |
---|
152 | if ($err) { |
---|
153 | $output->writeln(array("<error>Erreur SVN add fichiers.</error>")); |
---|
154 | } else { |
---|
155 | $output->writeln(array( |
---|
156 | "<info>Svn add </info>", |
---|
157 | join(",", $fichiers_ajoutes) |
---|
158 | )); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | $output->writeln(array( |
---|
163 | "<info>Relancez la commande.</info>" |
---|
164 | )); |
---|
165 | die(); |
---|
166 | } |
---|
167 | |
---|
168 | // Modif ? |
---|
169 | $results_m = array_filter($results, function ($line) { |
---|
170 | return preg_match(',^M,', $line); |
---|
171 | }); |
---|
172 | |
---|
173 | // Ajout ? |
---|
174 | $results_a = array_filter($results, function ($line) { |
---|
175 | return preg_match(',^A,', $line); |
---|
176 | }); |
---|
177 | |
---|
178 | // Suppression ? |
---|
179 | $results_d = array_filter($results, function ($line) { |
---|
180 | return preg_match(',^D,', $line); |
---|
181 | }); |
---|
182 | |
---|
183 | |
---|
184 | if (count($results_m) > 0 OR count($results_a) > 0 OR count($results_d) > 0) { |
---|
185 | |
---|
186 | if($input->getOption('action') != "commit"){ |
---|
187 | $output->writeln(array( |
---|
188 | "<error>Des fichiers ont été modifié localement.</error>\n<info> Pour commiter : spip rev -a commit</info>" |
---|
189 | )); |
---|
190 | die(); |
---|
191 | }else{ |
---|
192 | $output->writeln(array( |
---|
193 | "<info>Commit…</info>" |
---|
194 | )); |
---|
195 | |
---|
196 | exec('svn commit -m "Ajouts de fichiers"', $results, $err); |
---|
197 | if ($err) |
---|
198 | $output->writeln(array("<error>Erreur SVN.</error>")); |
---|
199 | |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | } |
---|
206 | else{ |
---|
207 | $output->writeln('<error>Vous n’êtes pas dans une installation de SPIP. Impossible de convertir le texte.</error>'); |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|