forked from rpavlik/generate-cpp-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.php
140 lines (116 loc) · 3.07 KB
/
generate.php
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
<?php
/**
* generate.php
*
* @package default
* @see download.php
*/
require_once 'common.php';
require 'external/guid.php';
require 'support/attachment.php';
$indentation = $defaults['indentation'];
$defaultAuthor = $defaults['author'];
$defaultLicense = $defaults['license'];
date_default_timezone_set('America/New_York');
/**
*
*
* @param string $authorinfo
* @return string author info indented to match the doxygen header
*/
function indentAuthorInfo($authorinfo) {
global $indentation;
return implode("\n", array_map(
function ($line) {
global $indentation;
return rtrim($indentation . trim($line));
},
explode("\n", $authorinfo)
)
);
}
/**
*
*
* @param string $licenseraw
* @return string license with appropriate comment syntax
*/
function commentLicense($licenseraw) {
return implode("\n", array_map(
function ($line) {
return rtrim('// ' . rtrim($line));
},
explode("\n", $licenseraw)
)
);
}
/** @brief for each value of KEY, replace @KEY@ in
* the input with $vars["KEY"]
*
* @param string $input
* @param array $vars
* @return string result of replacement
*/
function doSubstitutions($input, $vars) {
$ret = $input;
foreach ($vars as $key=>$val) {
$ret = str_replace("@$key@", $val, $ret);
}
return $ret;
}
/** @brief checks to see if $arr[$k] is a nonempty string
*
*
* @param string $k key
* @param array $arr array to search
* @return true or false
*/
function array_has_valid_string_for_key($k, $arr) {
return array_key_exists($k, $arr) && strlen($arr[$k]) > 0;
}
/**
*
*
* @param array $params with at least filebase and ext as required keys,
* and licenselines and authorlines as optional keys.
*/
function generateBoilerplate($params) {
$ext = $params['ext'];
$type = getTemplateType($ext);
if (strlen($type) == 0) {
die("Warning: Bad value for 'ext' - could not look up template type for: $ext\n");
}
$output_ext = getExtensionForType($ext);
$mimetype = getMimeForExtension($output_ext);
$filebase = $params['filebase'];
$filename = $filebase . '.' . $output_ext;
// TODO hardcoded hack for prettier templates
$headerext = '.h';
$year = date('Y');
$substitutions = array(
'YEAR' => $year
);
if (array_has_valid_string_for_key('licenselines', $params)) {
$licenseraw = '[LICENSE]' . $params['licenselines'] . '[LICENSE]';
} else {
global $defaultLicense;
$licenseraw = $defaultLicense;
}
if (array_has_valid_string_for_key('authorlines', $params)) {
$authorinfo = $params['authorlines'];
} else {
global $defaultAuthor;
$authorinfo = $defaultAuthor;
}
generateAttachment($filename, $mimetype);
$mysubstitutions = array(
'YEAR' => $year,
'AUTHORLINES' => doSubstitutions(indentAuthorInfo($authorinfo), $substitutions),
'LICENSELINES' => doSubstitutions(commentLicense($licenseraw), $substitutions),
'DEF' => makeCIdentifier('INCLUDED_' . $filebase . '_' . $output_ext . '_GUID_' . strtoupper(generateGUID())) ,
'FILEBASE' => $filebase,
'HEADEREXT' => $headerext
);
print(doSubstitutions(file_get_contents('templates/' . $type . '.tpl', true), $mysubstitutions));
}
?>