version_update_awk_actions()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 15
ccs 0
cts 11
cp 0
crap 30
rs 9.6111
1
<?php
2
/**
3
 * Repositories.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
$working_dir      = getcwd();
12
$project_dir      = dirname( __DIR__ );
13
$repositories_dir = $project_dir . '/repositories';
14
15
$organisations = array(
16
	'pronamic'          => array(
17
		'wp-datetime' => 'DateTime',
18
		'wp-html'     => 'HTML',
19
		'wp-http'     => 'HTTP',
20
		'wp-money'    => 'Money',
21
	),
22
	'wp-pay'            => array(
23
		'core'        => 'core',
24
		'fundraising' => 'Fundraising',
25
	),
26
	'wp-pay-gateways'   => array(
27
		'adyen'              => 'Adyen',
28
		'buckaroo'           => 'Buckaroo',
29
		'digiwallet'         => 'DigiWallet',
30
		'ems-e-commerce'     => 'EMS e-Commerce;',
31
		'icepay'             => 'ICEPAY',
32
		'ideal'              => 'iDEAL',
33
		'ideal-advanced-v3'  => 'iDEAL Advanced v3',
34
		'ideal-basic'        => 'iDEAL Basic',
35
		'ing-kassa-compleet' => 'ING Kassa Compleet',
36
		'mollie'             => 'Mollie',
37
		'mollie-ideal'       => 'Mollie iDEAL',
38
		'multisafepay'       => 'MultiSafepay',
39
		'ogone'              => 'Ingenico',
40
		'omnikassa-2'        => 'OmniKassa 2.0',
41
		'pay-nl'             => 'Pay.nl',
42
		'paypal'             => 'PayPal',
43
		'payvision'          => 'Payvision',
44
		'sisow'              => 'Sisow',
45
		'targetpay'          => 'TargetPay',
46
	),
47
	'wp-pay-extensions' => array(
48
		'charitable'             => 'Charitable',
49
		'contact-form-7'         => 'Contact Form 7',
50
		'easy-digital-downloads' => 'Easy Digital Downloads',
51
		'event-espresso'         => 'Event Espresso',
52
		'event-espresso-legacy'  => 'Event Espresso (legacy)',
53
		'formidable-forms'       => 'Formidable Forms',
54
		'give'                   => 'Give',
55
		'gravityforms'           => 'Gravity Forms',
56
		'memberpress'            => 'MemberPress',
57
		'ninjaforms'             => 'Ninja Forms',
58
		'restrict-content-pro'   => 'Restrict Content Pro',
59
		's2member'               => 's2Member',
60
		'woocommerce'            => 'WooCommerce',
61
		'wp-e-commerce'          => 'WP eCommerce',
62
	),
63
);
64
65
/**
66
 * Version update `awk` actions.
67
 *
68
 * @return string
69
 */
70
function version_update_awk_actions() {
71
	global $argv;
72
73
	$version_update = isset( $argv[2] ) ? $argv[2] : 'patch';
74
75
	switch ( $version_update ) {
76
		case 'major':
77
			return 'NR==1{printf "%s.0.0", ++$NR};';
78
79
		case 'minor':
80
			return 'NF==2{print ++$NF}; NF>0{$(NF-1)++; $NF=0; print};';
81
82
		case 'patch':
83
		default:
84
			return 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print};';
85
	}
86
}
87
88
if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
89
	$changelog_release = fopen( __DIR__ . '/changelog-release.json', 'w+' );
90
91
	if ( false !== $changelog_release ) {
92
		fwrite( $changelog_release, '[null' );
93
	}
94
}
95
96
foreach ( $organisations as $organisation => $repositories ) {
97
	echo '# ', $organisation, PHP_EOL;
98
99
	foreach ( $repositories as $repository => $name ) {
100
		echo '- ', $repository, PHP_EOL;
101
102
		$git_url = sprintf(
103
			'https://github.com/%s/%s.git',
104
			$organisation,
105
			$repository
106
		);
107
108
		$git_dir = $repositories_dir . '/' . $organisation . '/' . $repository;
109
110
		if ( ! is_dir( $git_dir ) ) {
111
			echo shell_exec( 'git clone ' . $git_url . ' ' . $git_dir );
112
		}
113
114
		// Git flow.
115
		chdir( $git_dir );
116
117
		$command = null;
118
119
		if ( isset( $argv[1] ) && 'develop' === $argv[1] ) {
120
			$command = 'git checkout develop';
121
		}
122
123
		if ( isset( $argv[1] ) && 'master' === $argv[1] ) {
124
			$command = 'git checkout master';
125
		}
126
127
		if ( isset( $argv[1] ) && 'pull' === $argv[1] ) {
128
			$command = 'git pull';
129
		}
130
131
		if ( isset( $argv[1] ) && 'log' === $argv[1] ) {
132
			$command = 'git --no-pager log $(git describe --tags --abbrev=0)..HEAD --oneline';
133
		}
134
135
		if ( isset( $argv[1] ) && 'changelog' === $argv[1] ) {
136
			$command = '
137
				CURRENT_TAG=$(git describe --tags --abbrev=0);
138
				LOG=$(git --no-pager log ${CURRENT_TAG}..HEAD --oneline);
139
140
				# Exit if there are no changes in Git repository.
141
				if [ $(echo "$LOG" | wc -l) -eq 1 ]; then
142
					echo "Version: ${CURRENT_TAG}";
143
					exit;
144
				fi;
145
146
				# Set version numbers.
147
				NEW_VERSION=$(echo "$CURRENT_TAG" | awk -F. -v OFS=. \'' . version_update_awk_actions() . '\');
148
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
149
150
				# Add title and log.
151
				TITLE_LINENR=$(grep -n "## \[" CHANGELOG.md | head -2 | tail -1 | cut -d: -f1);
152
				LOG=$(echo "$LOG" | sed \'s/^[a-z0-9]\{7\}/-/\' | sed \'s/^- Merge tag.*//\'; echo "";);
153
				TITLE="## [${NEW_VERSION}] - $(date \'+%Y-%m-%d\')' . \PHP_EOL . '";
154
				ex -s -c "${TITLE_LINENR}i|${TITLE}${LOG}' . str_repeat( \PHP_EOL, 2 ) . '" -c x CHANGELOG.md;
155
156
				# Update footer links.
157
				LINK_LINENR=$(grep -n "\[unreleased\]" CHANGELOG.md | tail -1 | cut -d: -f1);
158
				LINK="[${NEW_VERSION}]: https://github.com/' . $organisation . '/' . $repository . '/compare/${CURRENT_TAG}...${NEW_VERSION}";
159
				CHANGELOG=$(cat CHANGELOG.MD | sed "${LINK_LINENR}s/${CURRENT_TAG}...HEAD/${NEW_VERSION}...HEAD/" | tee CHANGELOG.md);
160
				ex -s -c "$(( ${LINK_LINENR} + 1 ))i|${LINK}" -c x CHANGELOG.md';
161
		}
162
163
		if ( isset( $argv[1] ) && 'update-package-version' === $argv[1] ) {
164
			$command = '
165
				CURRENT_TAG=$(git describe --tags --abbrev=0);
166
				LOG=$(git --no-pager log ${CURRENT_TAG}..HEAD --oneline);
167
168
				# Exit if there are no changes in Git repository.
169
				if [ $(echo "$LOG" | wc -l) -eq 1 ]; then
170
					echo "Version: ${CURRENT_TAG}";
171
					exit;
172
				fi;
173
174
				# Set version numbers.
175
				NEW_VERSION=$(echo "$CURRENT_TAG" | awk -F. -v OFS=. \'' . version_update_awk_actions() . '\');
176
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
177
178
				# Update version number.
179
				VERSION_LINENR=$(grep -n "\"version\":" package.json | tail -1 | cut -d: -f1);
180
				PACKAGE_JSON=$(cat package.json | sed "${VERSION_LINENR}s/\"${CURRENT_TAG}\"/\"${NEW_VERSION}\"/" | tee package.json);';
181
		}
182
183
		if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
184
			$command = '
185
				CURRENT_TAG=$(git describe --tags --abbrev=0);
186
				NEW_VERSION=$(cat package.json | jq --raw-output \'.version\' );
187
188
				# Exit if there are no changes in Git repository.
189
				if [[ "$CURRENT_TAG" == "$NEW_VERSION" ]]; then
190
					echo "Version: ${CURRENT_TAG}";
191
					exit;
192
				fi;
193
194
				# Echo new version number.
195
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
196
197
				# Write temporary changelog JSON.
198
				FROM=$(( $(grep -n "## \[" CHANGELOG.md | head -2 | tail -1 | cut -d: -f1) + 1 ));
199
				TO=$(( $(grep -n "## \[" CHANGELOG.md | head -3 | tail -1 | cut -d: -f1) - 2 ));
200
				LOG=$(cat CHANGELOG.md | head -n $TO | tail -n +$FROM );
201
				echo "${LOG}"
202
				echo ",{\"description\":\"Updated WordPress ' . ( 'pronamic' === $organisation ? '' : 'pay ' ) . $name . ' library to version ${NEW_VERSION}.\",\"changes\":$(echo "${LOG}" | sed \'s/^- //\' | jq --raw-input --raw-output --slurp \'split("\\n") | .[0:-1]\')}" >> ../../../src/changelog-release.json';
203
		}
204
205
		if ( isset( $argv[1] ) && in_array( $argv[1], array( 'git', 'grunt', 'composer', 'npm', 'ncu' ), true ) ) {
206
			if ( isset( $argv[2] ) ) {
207
				$command = sprintf( '%s %s', $argv[1], $argv[2] );
208
			} else {
209
				$command = sprintf( '%s', $argv[1] );
210
			}
211
		}
212
213
		if ( null !== $command ) {
214
			if ( ! isset( $argv[1] ) || ( isset( $argv[1] ) && ! in_array( $argv[1], array( 'changelog', 'changelog-plugin', 'update-package-version' ), true ) ) ) {
215
				echo $command, PHP_EOL;
216
			}
217
218
			echo shell_exec( $command ), PHP_EOL;
219
		}
220
	}
221
}
222
223
if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
224
	// Get release changelog items from temporary file.
225
	$changelog_plugin = file_get_contents( __DIR__ . '/changelog-release.json' ) . ']';
226
	$changelog_plugin = str_replace( '\\', '\\\\', $changelog_plugin );
227
	$changelog_plugin = str_replace( '\\\\"', '\\"', $changelog_plugin );
228
229
	unlink( __DIR__ . '/changelog-release.json' );
230
231
	$updates = json_decode( $changelog_plugin );
232
233
	array_shift( $updates );
234
235
	// Release item.
236
	$package = file_get_contents( __DIR__ . '/../package.json' );
237
238
	// Check if package file could be read.
239
	if ( false === $package ) {
240
		return;
241
	}
242
243
	$package = json_decode( $package );
244
245
	$release = array(
246
		array(
247
			'version' => $package->version,
248
			'date'    => gmdate( 'Y-m-d' ),
249
			'changes' => array(
250
				'name'    => 'Changed',
251
				'changes' => $updates,
252
			),
253
		),
254
	);
255
256
	// Insert in changelog after 'Unreleased' item.
257
	$changelog = file_get_contents( __DIR__ . '/changelog.json' );
258
259
	// Check if changelog file could be read.
260
	if ( false === $changelog ) {
261
		return;
262
	}
263
264
	$changelog = json_decode( $changelog );
265
266
	array_splice( $changelog, 1, 0, $release );
267
268
	// Use tabs for indentation.
269
	// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
270
	$changelog_json = json_encode( $changelog, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
271
272
	// Check if changelog could be JSON encoded.
273
	if ( false === $changelog_json ) {
274
		return;
275
	}
276
277
	$json = preg_replace_callback(
278
		'/^ +/m',
279
		/**
280
		 * Repeated tabs based on indentation length.
281
		 *
282
		 * @param array<int, string> $indentation Indentation spaces.
283
		 * @return string
284
		 */
285
		function ( $indentation ) {
286
			return str_repeat( '	', (int) ceil( strlen( $indentation[0] ) / 4 ) );
287
		},
288
		$changelog_json
289
	);
290
291
	// Write updated changelog.
292
	$handle = fopen( __DIR__ . '/changelog.json', 'w+' );
293
294
	if ( false !== $handle ) {
295
		fwrite( $handle, $json . PHP_EOL );
296
	}
297
}
298