Failed Conditions
Push — develop ( 09d4b0...05c421 )
by Reüel
13:44 queued 12s
created

src/repositories.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Repositories.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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-money'    => 'Money',
19
	),
20
	'wp-pay'            => array(
21
		'core' => 'core',
22
	),
23
	'wp-pay-gateways'   => array(
24
		'adyen'              => 'Adyen',
25
		'buckaroo'           => 'Buckaroo',
26
		'ems-e-commerce'     => 'EMS e-Commerce;',
27
		'icepay'             => 'ICEPAY',
28
		'ideal'              => 'iDEAL',
29
		'ideal-advanced-v3'  => 'iDEAL Advanced v3',
30
		'ideal-basic'        => 'iDEAL Basic',
31
		'ing-kassa-compleet' => 'ING Kassa Compleet',
32
		'mollie'             => 'Mollie',
33
		'mollie-ideal'       => 'Mollie iDEAL',
34
		'multisafepay'       => 'MultiSafepay',
35
		'nocks'              => 'Nocks',
36
		'ogone'              => 'Ingenico',
37
		'omnikassa'          => 'OmniKassa',
38
		'omnikassa-2'        => 'OmniKassa 2.0',
39
		'pay-nl'             => 'Pay.nl',
40
		'sisow'              => 'Sisow',
41
		'targetpay'          => 'TargetPay',
42
	),
43
	'wp-pay-extensions' => array(
44
		'charitable'             => 'Charitable',
45
		'contact-form-7'         => 'Contact Form 7',
46
		'easy-digital-downloads' => 'Easy Digital Downloads',
47
		'event-espresso'         => 'Event Espresso',
48
		'event-espresso-legacy'  => 'Event Espresso (legacy)',
49
		'formidable-forms'       => 'Formidable Forms',
50
		'give'                   => 'Give',
51
		'gravityforms'           => 'Gravity Forms',
52
		'memberpress'            => 'MemberPress',
53
		'ninjaforms'             => 'Ninja Forms',
54
		'restrict-content-pro'   => 'Restrict Content Pro',
55
		's2member'               => 's2Member',
56
		'woocommerce'            => 'WooCommerce',
57
		'wp-e-commerce'          => 'WP eCommerce',
58
	),
59
);
60
61
/**
62
 * Version update `awk` actions.
63
 *
64
 * @return string
65
 */
66
function version_update_awk_actions() {
67
	global $argv;
68
69
	$version_update = isset( $argv[2] ) ? $argv[2] : 'patch';
70
71
	switch ( $version_update ) {
72
		case 'major':
73
			return 'NR==1{printf "%s.0.0", ++$NR};';
74
75
		case 'minor':
76
			return 'NF==2{print ++$NF}; NF>0{$(NF-1)++; $NF=0; print};';
77
78
		case 'patch':
79
		default:
80
			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};';
81
	}
82
}
83
84
if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
85
	fwrite( fopen( __DIR__ . '/changelog-release.json', 'w+' ), '[null' );
0 ignored issues
show
It seems like fopen(__DIR__ . '/changelog-release.json', 'w+') can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
	fwrite( /** @scrutinizer ignore-type */ fopen( __DIR__ . '/changelog-release.json', 'w+' ), '[null' );
Loading history...
86
}
87
88
foreach ( $organisations as $organisation => $repositories ) {
89
	echo '# ', $organisation, PHP_EOL;
90
91
	foreach ( $repositories as $repository => $name ) {
92
		echo '- ', $repository, PHP_EOL;
93
94
		$git_url = sprintf(
95
			'https://github.com/%s/%s.git',
96
			$organisation,
97
			$repository
98
		);
99
100
		$git_dir = $repositories_dir . '/' . $organisation . '/' . $repository;
101
102
		if ( ! is_dir( $git_dir ) ) {
103
			echo shell_exec( 'git clone ' . $git_url . ' ' . $git_dir );
104
		}
105
106
		// Git flow.
107
		chdir( $git_dir );
108
109
		$command = null;
110
111
		if ( isset( $argv[1] ) && 'develop' === $argv[1] ) {
112
			$command = 'git checkout develop';
113
		}
114
115
		if ( isset( $argv[1] ) && 'master' === $argv[1] ) {
116
			$command = 'git checkout master';
117
		}
118
119
		if ( isset( $argv[1] ) && 'pull' === $argv[1] ) {
120
			$command = 'git pull';
121
		}
122
123
		if ( isset( $argv[1] ) && 'log' === $argv[1] ) {
124
			$command = 'git --no-pager log $(git describe --tags --abbrev=0)..HEAD --oneline';
125
		}
126
127
		if ( isset( $argv[1] ) && 'changelog' === $argv[1] ) {
128
			$command = '
129
				CURRENT_TAG=$(git describe --tags --abbrev=0);
130
				LOG=$(git --no-pager log ${CURRENT_TAG}..HEAD --oneline);
131
132
				# Exit if there are no changes in Git repository.
133
				if [ $(echo "$LOG" | wc -l) -eq 1 ]; then
134
					echo "Version: ${CURRENT_TAG}";
135
					exit;
136
				fi;
137
138
				# Set version numbers.
139
				NEW_VERSION=$(echo "$CURRENT_TAG" | awk -F. -v OFS=. \'' . version_update_awk_actions() . '\');
140
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
141
142
				# Add title and log.
143
				TITLE_LINENR=$(grep -n "## \[" CHANGELOG.md | head -2 | tail -1 | cut -d: -f1);
144
				LOG=$(echo "$LOG" | sed \'s/^[a-z0-9]\{7\}/-/\' | sed \'s/^- Merge tag.*//\'; echo "";);
145
				TITLE="## [${NEW_VERSION}] - $(date \'+%Y-%m-%d\')' . \PHP_EOL . '";
146
				ex -s -c "${TITLE_LINENR}i|${TITLE}${LOG}' . str_repeat( \PHP_EOL, 2 ) . '" -c x CHANGELOG.md;
147
148
				# Update footer links.
149
				LINK_LINENR=$(grep -n "\[unreleased\]" CHANGELOG.md | tail -1 | cut -d: -f1);
150
				LINK="[${NEW_VERSION}]: https://github.com/' . $organisation . '/' . $repository . '/compare/${CURRENT_TAG}...${NEW_VERSION}";
151
				CHANGELOG=$(cat CHANGELOG.MD | sed "${LINK_LINENR}s/${CURRENT_TAG}...HEAD/${NEW_VERSION}...HEAD/" | tee CHANGELOG.md);
152
				ex -s -c "$(( ${LINK_LINENR} + 1 ))i|${LINK}" -c x CHANGELOG.md';
153
		}
154
155
		if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
156
			$command = '
157
				CURRENT_TAG=$(git describe --tags --abbrev=0);
158
				NEW_VERSION=$(cat package.json | jq --raw-output \'.version\' );
159
160
				# Exit if there are no changes in Git repository.
161
				if [[ "$CURRENT_TAG" == "$NEW_VERSION" ]]; then
162
					echo "Version: ${CURRENT_TAG}";
163
					exit;
164
				fi;
165
166
				# Echo new version number.
167
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
168
169
				# Write temporary changelog JSON.
170
				FROM=$(( $(grep -n "## \[" CHANGELOG.md | head -2 | tail -1 | cut -d: -f1) + 1 ));
171
				TO=$(( $(grep -n "## \[" CHANGELOG.md | head -3 | tail -1 | cut -d: -f1) - 2 ));
172
				LOG=$(cat CHANGELOG.md | head -n $TO | tail -n +$FROM );
173
				echo "${LOG}"
174
				echo ",{\"description\":\"Updated WordPress ' . ( 'pronamic' === $organisation ? null : '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';
175
		}
176
177
		if ( isset( $argv[1] ) && 'update-package-version' === $argv[1] ) {
178
			$command = '
179
				CURRENT_TAG=$(git describe --tags --abbrev=0);
180
				LOG=$(git --no-pager log ${CURRENT_TAG}..HEAD --oneline);
181
182
				# Exit if there are no changes in Git repository.
183
				if [ $(echo "$LOG" | wc -l) -eq 1 ]; then
184
					echo "Version: ${CURRENT_TAG}";
185
					exit;
186
				fi;
187
188
				# Set version numbers.
189
				NEW_VERSION=$(echo "$CURRENT_TAG" | awk -F. -v OFS=. \'' . version_update_awk_actions() . '\');
190
				echo "Version: ${CURRENT_TAG} --> ${NEW_VERSION}";
191
192
				# Update version number.
193
				VERSION_LINENR=$(grep -n "\"version\":" package.json | tail -1 | cut -d: -f1);
194
				PACKAGE_JSON=$(cat package.json | sed "${VERSION_LINENR}s/\"${CURRENT_TAG}\"/\"${NEW_VERSION}\"/" | tee package.json);';
195
		}
196
197
		if ( isset( $argv[1] ) && in_array( $argv[1], array( 'git', 'grunt', 'composer', 'npm', 'ncu' ), true ) ) {
198
			if ( isset( $argv[2] ) ) {
199
				$command = sprintf( '%s %s', $argv[1], $argv[2] );
200
			} else {
201
				$command = sprintf( '%s', $argv[1] );
202
			}
203
		}
204
205
		if ( null !== $command ) {
206
			if ( ! isset( $argv[1] ) || ( isset( $argv[1] ) && ! in_array( $argv[1], array( 'changelog', 'changelog-plugin', 'update-package-version' ), true ) ) ) {
207
				echo $command, PHP_EOL;
208
			}
209
210
			echo shell_exec( $command ), PHP_EOL;
211
		}
212
	}
213
}
214
215
if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
216
	// Get release changelog items from temporary file.
217
	$changelog_plugin = file_get_contents( __DIR__ . '/changelog-release.json' ) . ']';
218
	$changelog_plugin = str_replace( '\\', '\\\\', $changelog_plugin );
219
	$changelog_plugin = str_replace( '\\\\"', '\\"', $changelog_plugin );
220
221
	unlink( __DIR__ . '/changelog-release.json' );
222
223
	$updates = json_decode( $changelog_plugin );
224
225
	array_shift( $updates );
226
227
	// Release item.
228
	$package = json_decode( file_get_contents( __DIR__ . '/../package.json' ) );
229
230
	$release = array(
231
		array(
232
			'version' => $package->version,
233
			'date'    => date( 'Y-m-d' ),
234
			'changes' => array(
235
				'name'    => 'Changed',
236
				'changes' => $updates,
237
			),
238
		),
239
	);
240
241
	// Insert in changelog after 'Unreleased' item.
242
	$changelog = json_decode( file_get_contents( __DIR__ . '/changelog.json' ) );
243
244
	array_splice( $changelog, 1, 0, $release );
245
246
	// Use tabs for indentation.
247
	$json = preg_replace_callback(
248
		'/^ +/m',
249
		function( $indentation ) {
250
			return str_repeat( '	', strlen( $indentation[0] ) / 4 );
251
		},
252
		json_encode( $changelog, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE )
253
	);
254
255
	// Write updated changelog.
256
	fwrite( fopen( __DIR__ . '/changelog.json', 'w+' ), $json . PHP_EOL );
257
}
258