Skip to content

Commit b67eb54

Browse files
author
Nelio Agostinho
committed
DEVOPS-303 Update generated .gitignore file contents
1 parent 91db2f8 commit b67eb54

1 file changed

Lines changed: 134 additions & 79 deletions

File tree

src/Processor/Generate.php

Lines changed: 134 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -204,88 +204,143 @@ private function get_themes() {
204204
return $themes;
205205
}
206206

207-
private function create_gitignore_wp_build_block( $custom_items = [] ) {
208-
$gitignore = [];
209-
210-
// Start block and add common stuff.
211-
$gitignore[] = "# START WP-CLI BUILD BLOCK\n";
212-
$gitignore[] = "# ------------------------------------------------------------\n";
213-
$gitignore[] = "# This block is auto generated every time you run 'wp build-generate'\n";
214-
$gitignore[] = "# Rules: Exclude everything from Git except for your custom plugins and themes (that is: those that are not on wordpress.org)\n";
215-
$gitignore[] = "# ------------------------------------------------------------\n";
216-
$gitignore[] = "/*\n";
217-
$gitignore[] = "!.gitignore\n";
218-
$gitignore[] = "!{$this->build_filename}\n";
219-
$gitignore[] = "!wp-content\n";
220-
$gitignore[] = "wp-content/*\n";
221-
$gitignore[] = "!wp-content/plugins\n";
222-
$gitignore[] = "wp-content/plugins/*\n";
223-
$gitignore[] = "!wp-content/themes\n";
224-
$gitignore[] = "wp-content/themes/*\n";
225-
226-
// Add custom items.
227-
if ( ! empty( $custom_items ) ) {
228-
$gitignore[] = "# ------------------------------------------------------------\n";
229-
$gitignore[] = "# Your custom themes/plugins\n";
230-
$gitignore[] = "# Added automagically by WP-CLI Build (wp build-generate)\n";
231-
$gitignore[] = "# ------------------------------------------------------------\n";
232-
foreach ( $custom_items as $type => $items ) {
233-
foreach ( $items as $slug => $version ) {
234-
if ( ! empty( $slug ) ) {
235-
$gitignore[] = "!wp-content/$type/$slug/\n";
236-
}
237-
}
238-
}
239-
}
240-
241-
// Close gitignore block.
242-
$gitignore[] = "# ------------------------------------------------------------\n";
243-
$gitignore[] = "# END WP-CLI BUILD BLOCK\n";
244-
245-
return $gitignore;
207+
private function get_custom_gitignore( $current_gitignore = [] ) {
208+
if ( empty( $current_gitignore ) ) {
209+
return [];
210+
}
211+
212+
// Remove existing block from .gitignore.
213+
$start = array_search( "# START WP-CLI BUILD BLOCK\n", $current_gitignore );
214+
$end = array_search( "# END WP-CLI BUILD BLOCK\n", $current_gitignore );
215+
$custom_gitignore = $current_gitignore;
216+
217+
if ( ( is_int( $start ) ) && ( is_int( $end ) ) ) {
218+
for ( $i = $start; $i <= $end; $i ++ ) {
219+
unset( $custom_gitignore[ $i ] );
220+
}
221+
}
222+
223+
foreach( $custom_gitignore as $key => $line ) {
224+
if ( ( strpos( $line, '# ------' ) !== false ) || ( strpos( $line, '# START CUSTOM' ) !== false ) || ( strpos( $line, '# Place any' ) !== false ) || ( strpos( $line, '# END CUSTOM' ) !== false ) ) {
225+
unset( $custom_gitignore[$key] );
226+
}
227+
}
228+
229+
return $custom_gitignore;
246230
}
247231

248-
private function save_gitignore( $custom_items = [] ) {
249-
// .gitignore path.
250-
$gitignore_path = ABSPATH . '.gitignore';
251-
if ( $gitignore_path == '/.gitignore' ) {
252-
$gitignore_path = realpath( '.' ) . '/.gitignore';
253-
}
254-
255-
// Check if the file exists and load.
256-
$gitignore_wp_build_block = $this->create_gitignore_wp_build_block( $custom_items );
257-
258-
if ( file_exists( $gitignore_path ) ) {
259-
$gitignore = @file( $gitignore_path );
260-
261-
// Check if the path is already in ignore file.
262-
if ( ! empty( $gitignore ) ) {
263-
// search for existing block from gitignore.
264-
$start = array_search( "# START WP-CLI BUILD BLOCK\n", $gitignore );
265-
$end = array_search( "# END WP-CLI BUILD BLOCK\n", $gitignore );
266-
267-
// if existing block is found it is replaced, if not block is appended
268-
if ( ( is_int( $start ) ) && ( is_int( $end ) ) ) {
269-
$before_wp_build_block = array_slice($gitignore, 0, $start - 1);
270-
$after_wp_build_block = array_slice($gitignore, $end + 1);
271-
$gitignore = array_merge($before_wp_build_block, $gitignore_wp_build_block, $after_wp_build_block);
272-
} else {
273-
$gitignore = array_merge($gitignore, $gitignore_wp_build_block);
274-
}
275-
}
276-
} else {
277-
$gitignore = $gitignore_wp_build_block;
278-
}
279-
280-
// Put content in .gitignore.
281-
if ( ! empty( $gitignore ) ) {
282-
@file_put_contents( $gitignore_path, $gitignore );
283-
284-
return TRUE;
285-
}
286-
287-
return FALSE;
232+
private function generate_gitignore( $custom_gitignore = [], $custom_items = [], $composer_exists = FALSE ) {
233+
$gitignore = [];
234+
235+
// Start WP-CLI Build block.
236+
$gitignore[] = "# ------------------------------------------------------------\n";
237+
$gitignore[] = "# START WP-CLI BUILD BLOCK\n";
238+
$gitignore[] = "# ------------------------------------------------------------\n";
239+
$gitignore[] = "# This block is auto generated every time you run 'wp build-generate'.\n";
240+
$gitignore[] = "# Rules: Exclude everything from Git except for your custom plugins and themes\n";
241+
$gitignore[] = "# (that is: those that are not on wordpress.org).\n";
242+
$gitignore[] = "# ------------------------------------------------------------\n";
243+
244+
// Add main items.
245+
$gitignore[] = "/*\n";
246+
$gitignore[] = "!.gitignore\n";
247+
$gitignore[] = "!{$this->build_filename}\n";
248+
249+
// Add composer.json.
250+
if ( ! empty( $composer_exists ) ) {
251+
$gitignore[] = "!composer.json\n";
252+
}
253+
254+
// Add common items.
255+
$gitignore[] = "!README.md\n";
256+
$gitignore[] = "!wp-content\n";
257+
$gitignore[] = "wp-content/*\n";
258+
$gitignore[] = "!wp-content/plugins\n";
259+
$gitignore[] = "wp-content/plugins/*\n";
260+
$gitignore[] = "!wp-content/themes\n";
261+
$gitignore[] = "wp-content/themes/*\n";
262+
263+
// Add custom themes and plugins.
264+
if ( ! empty( $custom_items ) ) {
265+
$gitignore[] = "# ------------------------------------------------------------\n";
266+
$gitignore[] = "# Your custom themes and plugins.\n";
267+
$gitignore[] = "# Added automagically by WP-CLI Build ('wp build-generate')\n";
268+
$gitignore[] = "# ------------------------------------------------------------\n";
269+
270+
foreach ( $custom_items as $type => $items ) {
271+
foreach ( $items as $slug => $version ) {
272+
if ( ! empty( $slug ) ) {
273+
$gitignore[] = "!wp-content/$type/$slug/\n";
274+
}
275+
}
276+
}
277+
}
278+
279+
// End WP-CLI Build block.
280+
$gitignore[] = "# ------------------------------------------------------------\n";
281+
$gitignore[] = "# END WP-CLI BUILD BLOCK\n";
282+
$gitignore[] = "# ------------------------------------------------------------\n\n";
283+
284+
// Start custom block.
285+
$gitignore[] = "# ------------------------------------------------------------\n";
286+
$gitignore[] = "# START CUSTOM BLOCK\n";
287+
$gitignore[] = "# ------------------------------------------------------------\n";
288+
$gitignore[] = "# Place any additional items here.\n";
289+
$gitignore[] = "# ------------------------------------------------------------\n\n";
290+
291+
// Add custom items.
292+
$gitignore = array_merge( $gitignore, $custom_gitignore );
293+
294+
// End custom block.
295+
$gitignore[] = "# ------------------------------------------------------------\n";
296+
$gitignore[] = "# END CUSTOM BLOCK\n";
297+
$gitignore[] = "# ------------------------------------------------------------\n\n";
298+
299+
return $gitignore;
288300
}
289301

302+
private function save_gitignore( $custom_items = [] ) {
303+
// composer.json path.
304+
$composer_path = ABSPATH . 'composer.json';
305+
306+
if ( $composer_path == '/composer.json' ) {
307+
$composer_path = realpath( '.' ) . '/composer.json';
308+
}
309+
310+
// Check if the composer.json file exists.
311+
if ( file_exists( $composer_path ) ) {
312+
$composer_exists = true;
313+
}
314+
315+
// .gitignore path.
316+
$gitignore_path = ABSPATH . '.gitignore';
317+
318+
if ( $gitignore_path == '/.gitignore' ) {
319+
$gitignore_path = realpath( '.' ) . '/.gitignore';
320+
}
321+
322+
$custom_gitignore = [];
323+
324+
// Check if the .gitignore file exists and load it.
325+
if ( file_exists( $gitignore_path ) ) {
326+
$current_gitignore = @file( $gitignore_path );
327+
328+
// Check if the .gitignore file is not empty and get custom items.
329+
if ( ! empty( $current_gitignore ) ) {
330+
$custom_gitignore = $this->get_custom_gitignore( $current_gitignore );
331+
}
332+
}
333+
334+
$gitignore = $this->generate_gitignore( $custom_gitignore, $custom_items, $composer_exists );
335+
336+
// Put content in .gitignore.
337+
if ( ! empty( $gitignore ) ) {
338+
@file_put_contents( $gitignore_path, $gitignore );
339+
340+
return TRUE;
341+
}
342+
343+
return FALSE;
344+
}
290345

291346
}

0 commit comments

Comments
 (0)