This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
generate-markdown-readme
executable file
·158 lines (148 loc) · 6 KB
/
generate-markdown-readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env php
<?php
/**
* Look for WordPress readme in current directory or above and convert into markdown readme in same directory
* @version 1.0.1
* @author Weston Ruter <[email protected]> (@westonruter)
* @copyright Copyright (c) 2017, XWP <https://xwp.co/>
* @license MIT
*/
try {
if ( php_sapi_name() !== 'cli' ) {
throw new Exception( 'Only allowed in CLI mode.' );
}
$readme_txt_path = null;
while ( true ) {
foreach ( array( 'readme.txt', 'README.txt' ) as $readme_filename ) {
if ( file_exists( $readme_filename ) ) {
$readme_txt_path = realpath( $readme_filename );
break;
}
}
$old_cwd = getcwd();
if ( ! empty( $readme_txt_path ) || ! chdir( '..' ) || getcwd() === $old_cwd ) {
break;
}
}
if ( empty( $readme_txt_path ) ) {
throw new Exception( 'Failed to find a readme.txt or README.txt above the current working directory.' );
}
$readme_root = dirname( $readme_txt_path );
$readme_md_path = preg_replace( '/txt$/', 'md', $readme_txt_path );
require_once __DIR__ . '/class-wordpress-readme-parser.php';
$readme = new WordPress_Readme_Parser( array( 'path' => $readme_txt_path ) );
$md_args = array(
'assets_dir' => 'assets',
);
$github_account_repo = null;
$github_url_regex = '#.+github\.com[:/](?P<account_repo>[^/]+/[^/]+?)(?:\.git)?$#';
$remote_urls = array();
foreach ( explode( "\n", `git remote -v | grep fetch` ) as $remote_line ) {
$remote_line = trim( $remote_line );
if ( $remote_line ) {
list( $name, $url ) = preg_split( '/\s+/', $remote_line );
$remote_urls[ $name ] = $url;
}
}
if ( ! empty( $remote_urls['origin'] ) && preg_match( $github_url_regex, $remote_urls['origin'], $matches ) ) {
$github_account_repo = $matches['account_repo'];
} else {
foreach ( $remote_urls as $remote_name => $remote_url ) {
if ( preg_match( $github_url_regex, $remote_url, $matches ) ) {
$github_account_repo = $matches['account_repo'];
break;
}
}
}
if ( $github_account_repo ) {
$env_file = $readme_root . '/.dev-lib';
if ( ! file_exists( $env_file ) ) {
$env_file = $readme_root . '/.ci-env.sh';
}
if ( file_exists( $env_file ) ) {
$env_vars = array();
if ( preg_match_all( '/^\s*(?!#)(?P<name>.+)=([\'"]?)(?P<value>.+)\2/m', file_get_contents( $env_file ), $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $match ) {
$env_vars[ $match['name'] ] = $match['value'];
}
}
if ( isset( $env_vars['README_MD_TITLE'] ) ) {
$md_args['title'] = $env_vars['README_MD_TITLE'];
}
if ( isset( $env_vars['BADGE_FURY_URL'] ) ) {
$md_args['badge_fury_url'] = $env_vars['BADGE_FURY_URL'];
}
if ( isset( $env_vars['COVERALLS_BADGE'] ) ) {
$coveralls_badge = $env_vars['COVERALLS_BADGE'];
}
if ( isset( $env_vars['GEMNASIUM_BADGE'] ) ) {
$gemnasium_badge = $env_vars['GEMNASIUM_BADGE'];
}
if ( isset( $env_vars['GEMNASIUM_DEV_BADGE'] ) ) {
$gemnasium_dev_badge = $env_vars['GEMNASIUM_DEV_BADGE'];
}
if ( isset( $env_vars['TRAVIS_CI_PRO_BADGE'] ) ) {
$travis_ci_pro_badge = $env_vars['TRAVIS_CI_PRO_BADGE'];
}
if ( isset( $env_vars['ASSETS_DIR'] ) ) {
$md_args['assets_dir'] = $env_vars['ASSETS_DIR'];
}
}
if ( file_exists( $readme_root . '/.travis.yml' ) ) {
$branch = isset( $env_vars['DEFAULT_BASE_BRANCH'] ) ? $env_vars['DEFAULT_BASE_BRANCH'] : 'master';
if ( isset( $travis_ci_pro_badge ) ) {
$md_args['travis_ci_pro_url'] = "https://travis-ci.com/$github_account_repo";
$md_args['travis_ci_pro_badge_src'] = $md_args['travis_ci_pro_url'] . ".svg?token=$travis_ci_pro_badge&branch=$branch";
}
if ( ! isset( $md_args['travis_ci_pro_url'] ) ) {
$tld = isset( $env_vars['TRAVIS_CI_COM_URL'] ) ? 'com' : 'org';
$md_args['travis_ci_url'] = "https://travis-ci.$tld/$github_account_repo";
$md_args['travis_ci_badge_src'] = $md_args['travis_ci_url'] . ".svg?branch=$branch";
}
}
if ( file_exists( $readme_root . '/.coveralls.yml' ) ) {
$branch = isset( $env_vars['DEFAULT_BASE_BRANCH'] ) ? $env_vars['DEFAULT_BASE_BRANCH'] : 'master';
$md_args['coveralls_url'] = "https://coveralls.io/github/$github_account_repo";
$md_args['coveralls_badge_src'] = "https://coveralls.io/repos/$github_account_repo/badge.svg?branch=$branch";
if ( isset( $coveralls_badge ) ) {
$md_args['coveralls_badge_src'] .= "&service=github&t=$coveralls_badge";
}
}
if ( file_exists( $readme_root . '/codecov.yml' ) || file_exists( $readme_root . '/.codecov.yml' ) ) {
$branch = isset( $env_vars['DEFAULT_BASE_BRANCH'] ) ? $env_vars['DEFAULT_BASE_BRANCH'] : 'master';
$md_args['codecov_url'] = "https://codecov.io/gh/$github_account_repo";
$md_args['codecov_badge_src'] = "https://img.shields.io/codecov/c/github/$github_account_repo/$branch.svg";
}
if ( file_exists( $readme_root . '/Gruntfile.js' ) ) {
$md_args['grunt_url'] = 'gruntjs.com';
}
if ( file_exists( $readme_root . '/.david' ) ) {
$md_args['david_url'] = "https://david-dm.org/$github_account_repo";
}
if ( file_exists( $readme_root . '/.david-dev' ) ) {
$md_args['david_dev_url'] = "https://david-dm.org/$github_account_repo";
}
if ( isset( $gemnasium_badge ) ) {
$md_args['gemnasium_url'] = "https://gemnasium.com/$github_account_repo";
$md_args['gemnasium_badge_src'] = "https://gemnasium.com/$gemnasium_badge.svg";
}
if ( isset( $gemnasium_dev_badge ) ) {
$md_args['gemnasium_dev_url'] = "https://gemnasium.com/$github_account_repo";
$md_args['gemnasium_dev_badge_src'] = "https://gemnasium.com/$gemnasium_dev_badge.svg";
}
if ( file_exists( $readme_root . '/.gitter' ) ) {
$md_args['gitter_url'] = "https://gitter.im/$github_account_repo";
}
}
$markdown = $readme->to_markdown( $md_args );
$is_written = file_put_contents( $readme_md_path, $markdown );
if ( ! $is_written ) {
throw new Exception( sprintf( 'Failed to write to %s', $readme_md_path ) );
}
fwrite( STDERR, 'Successfully converted WordPress README to Markdown' . PHP_EOL );
fwrite( STDOUT, $readme_md_path . PHP_EOL );
}
catch( Exception $e ) {
fwrite( STDERR, $e->getMessage() . PHP_EOL );
exit( 1 );
}