Failed Conditions
Pull Request — develop (#104)
by
unknown
11:48
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
		'easy-digital-downloads' => 'Easy Digital Downloads',
46
		'event-espresso'         => 'Event Espresso',
47
		'event-espresso-legacy'  => 'Event Espresso (legacy)',
48
		'formidable-forms'       => 'Formidable Forms',
49
		'give'                   => 'Give',
50
		'gravityforms'           => 'Gravity Forms',
51
		'memberpress'            => 'MemberPress',
52
		'ninjaforms'             => 'Ninja Forms',
53
		'restrict-content-pro'   => 'Restrict Content Pro',
54
		's2member'               => 's2Member',
55
		'woocommerce'            => 'WooCommerce',
56
		'wp-e-commerce'          => 'WP eCommerce',
57
	),
58
);
59
60
/**
61
 * Version update `awk` actions.
62
 *
63
 * @return string
64
 */
65
function version_update_awk_actions() {
66
	global $argv;
67
68
	$version_update = isset( $argv[2] ) ? $argv[2] : 'patch';
69
70
	switch ( $version_update ) {
71
		case 'major':
72
			return 'NR==1{printf "%s.0.0", ++$NR};';
73
74
		case 'minor':
75
			return 'NF==2{print ++$NF}; NF>0{$(NF-1)++; $NF=0; print};';
76
77
		case 'patch':
78
		default:
79
			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};';
80
	}
81
}
82
83
if ( isset( $argv[1] ) && 'changelog-plugin' === $argv[1] ) {
84
	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

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