Merge pull request #22850 from ixiam/dev_Issue#3080
[civicrm-core.git] / distmaker / dists / common.sh
1 #!/bin/bash
2
3 ## Delete/create a dir
4 ## usage: dm_reset_dirs <path1> <path2> ...
5 function dm_reset_dirs() {
6 for d in "$@" ; do
7 [ -d "$d" ] && rm -rf "$d"
8 done
9
10 mkdir -p "$@"
11 }
12
13 ## Assert that a folder contains no symlinks
14 ##
15 ## ex: dev/core#1393, dev/core#1990
16 ## usage: dm_assert_no_symlinks <basedir>
17 function dm_assert_no_symlinks() {
18 local SYMLINKS=$( find "$1" -type l )
19 if [ -n "$SYMLINKS" ]; then
20 echo "ERROR: Folder $1 contains unexpected symlink(s): $SYMLINKS"
21 exit 10
22 fi
23 }
24
25 ## Copy files from one dir into another dir
26 ## usage: dm_install_dir <from-dir> <to-dir>
27 function dm_install_dir() {
28 local from="$1"
29 local to="$2"
30
31 if [ ! -d "$to" ]; then
32 mkdir -p "$to"
33 fi
34 ${DM_RSYNC:-rsync} -avC --exclude=.git --exclude=.svn "$from/./" "$to/./"
35 }
36
37 ## Copy listed files
38 ## usage: dm_install_files <from-dir> <to-dir> <file1> <file2>...
39 function dm_install_files() {
40 local from="$1"
41 shift
42 local to="$1"
43 shift
44
45 for file in "$@" ; do
46 [ -f "$from/$file" ] && cp -f "$from/$file" "$to/$file"
47 done
48 }
49
50 ## usage: dm_remove_files <directory> <file1> <file2>...
51 function dm_remove_files() {
52 local tgt="$1"
53 shift
54
55 for file in "$@" ; do
56 [ -f "$tgt/$file" -o -L "$tgt/$file" ] && rm -f "$tgt/$file"
57 done
58 }
59
60 ## Copy all bower dependencies
61 function dm_install_bower() {
62 local repo="$1"
63 local to="$2"
64
65 local excludes_rsync=""
66 for exclude in .git .svn {T,t}est{,s} {D,d}oc{,s} {E,e}xample{,s} ; do
67 excludes_rsync="--exclude=${exclude} ${excludes_rsync}"
68 done
69
70 [ ! -d "$to" ] && mkdir "$to"
71 ${DM_RSYNC:-rsync} -avC $excludes_rsync "$repo/./" "$to/./"
72 }
73
74 ## Copy all core files
75 ## usage: dm_install_core <core_repo_path> <to_path>
76 function dm_install_core() {
77 local repo="$1"
78 local to="$2"
79
80 for dir in ang css i js PEAR templates bin CRM api extern Reports install mixin settings Civi partials release-notes xml setup ; do
81 [ -d "$repo/$dir" ] && dm_install_dir "$repo/$dir" "$to/$dir"
82 done
83
84 dm_install_files "$repo" "$to" {agpl-3.0,agpl-3.0.exception,gpl,CONTRIBUTORS}.txt
85 dm_install_files "$repo" "$to" composer.json composer.lock package.json Civi.php README.md release-notes.md extension-compatibility.json
86
87 mkdir -p "$to/sql"
88 pushd "$repo" >> /dev/null
89 dm_install_files "$repo" "$to" sql/civicrm*.mysql sql/case_sample*.mysql
90 ## TODO: for master, remove counties.US.SQL.gz
91 popd >> /dev/null
92
93 if [ -d $to/bin ] ; then
94 rm -f $to/bin/setup.sh
95 rm -f $to/bin/setup.php4.sh
96 rm -f $to/bin/setup.bat
97 fi
98
99 set +e
100 rm -rf $to/sql/civicrm_*.??_??.mysql
101 rm -rf $to/mixin/*/example
102 set -e
103 }
104
105 ## Copy built-in extensions
106 ## usage: dm_install_core <core_repo_path> <to_path> <ext-dirs...>
107 function dm_install_coreext() {
108 local repo="$1"
109 local to="$2"
110 shift
111 shift
112
113 for relext in "$@" ; do
114 [ ! -d "$to/ext/$relext" ] && mkdir -p "$to/ext/$relext"
115 ${DM_RSYNC:-rsync} -avC $excludes_rsync --include=core "$repo/ext/$relext/./" "$to/ext/$relext/./"
116 done
117 }
118
119 ## Get a list of default/core extension directories (space-delimited)
120 ## reldirs=$(dm_core_exts)
121 function dm_core_exts() {
122 ## grep to exclude comments and blank lines
123 grep '^[a-zA-Z]' "$DM_SOURCEDIR"/distmaker/core-ext.txt
124 }
125
126 ## Copy all packages
127 ## usage: dm_install_packages <packages_repo_path> <to_path>
128 function dm_install_packages() {
129 local repo="$1"
130 local to="$2"
131
132 local excludes_rsync=""
133 for exclude in .git .svn _ORIGINAL_ SeleniumRC PHPUnit PhpDocumentor SymfonyComponents git-footnote PHP/CodeCoverage ; do
134 excludes_rsync="--exclude=${exclude} ${excludes_rsync}"
135 done
136
137 ## Note: These small folders have items that previously were not published,
138 ## but there's no real cost to including them, and excluding them seems
139 ## likely to cause confusion as the codebase evolves:
140 ## packages/Files packages/PHP packages/Text
141
142 [ ! -d "$to" ] && mkdir "$to"
143 ${DM_RSYNC:-rsync} -avC $excludes_rsync --include=core "$repo/./" "$to/./"
144 }
145
146 ## Copy Drupal-integration module
147 ## usage: dm_install_drupal <drupal_repo_path> <to_path>
148 function dm_install_drupal() {
149 local repo="$1"
150 local to="$2"
151 dm_install_dir "$repo" "$to"
152
153 # Set full version in .info files. See CRM-15768.
154 local MODULE_DIRS=`find "$to" -type f -name "*.info"`
155 for INFO in $MODULE_DIRS; do
156 dm_preg_edit '/version = ([0-9]*\.x)-[1-9.]*/m' "version = \$1-$DM_VERSION" "$INFO"
157 done
158
159 for f in "$to/.gitignore" "$to/.toxic.json" ; do
160 if [ -f "$f" ]; then
161 rm -f "$f"
162 fi
163 done
164 }
165
166 ## Copy Joomla-integration module
167 ## usage: dm_install_joomla <joomla_repo_path> <to_path>
168 function dm_install_joomla() {
169 local repo="$1"
170 local to="$2"
171 dm_install_dir "$repo" "$to"
172
173 ## Before this change, the zip file included the joomla-integration
174 ## modules twice. The two were basically identical -- except that
175 ## one included .gitignore and the omitted it. We'll now omit it
176 ## consistently.
177
178 for f in "$to/.gitignore" "$to/.toxic.json" ; do
179 if [ -f "$f" ]; then
180 rm -f "$f"
181 fi
182 done
183 }
184
185 ## usage: dm_install_l10n <l10n_repo_path> <to_path>
186 function dm_install_l10n() {
187 local repo="$1"
188 local to="$2"
189 dm_install_dir "$repo" "$to"
190 }
191
192 ## Copy composer's "vendor" folder
193 ## usage: dm_install_vendor <from_path> <to_path>
194 function dm_install_vendor() {
195 local repo="$1"
196 local to="$2"
197
198 local excludes_rsync=""
199 ## CRM-21729 - .idea test-cases unit-test come from phpquery package.
200 for exclude in .git .svn {T,t}est{,s} {D,d}oc{,s} {E,e}xample{,s} .idea test-cases unit-test README.rst; do
201 excludes_rsync="--exclude=${exclude} ${excludes_rsync}"
202 done
203
204 [ ! -d "$to" ] && mkdir "$to"
205 ${DM_RSYNC:-rsync} -avC $excludes_rsync "$repo/./" "$to/./"
206 ## We don't this use CLI script in production, and the symlink breaks D7/BD URL installs
207 dm_remove_files "$to" "bin/pscss" "bin/cssmin"
208 }
209
210 ## usage: dm_install_wordpress <wp_repo_path> <to_path>
211 function dm_install_wordpress() {
212 local repo="$1"
213 local to="$2"
214
215 if [ ! -d "$to" ]; then
216 mkdir -p "$to"
217 fi
218 ${DM_RSYNC:-rsync} -avC \
219 --exclude=.git \
220 --exclude=.svn \
221 --exclude=civicrm.config.php.wordpress \
222 --exclude=.toxic.json \
223 --exclude=.gitignore \
224 --exclude=civicrm \
225 "$repo/./" "$to/./"
226 ## Need --exclude=civicrm for self-building on WP site
227
228 dm_preg_edit '/^([ \*]*)Version: [0-9\.]+/m' "\1Version: $DM_VERSION" "$to/civicrm.php"
229 dm_preg_edit "/^define\( *\'CIVICRM_PLUGIN_VERSION\', *'[0-9\.]+/m" "define('CIVICRM_PLUGIN_VERSION', '$DM_VERSION" "$to/civicrm.php"
230 }
231
232 ## Generate the composer "vendor" folder
233 ## usage: dm_generate_vendor <repo_path>
234 function dm_generate_vendor() {
235 local repo="$1"
236 pushd "$repo"
237 ${DM_COMPOSER:-composer} install
238 popd
239 }
240
241 ## Generate civicrm-version.php
242 ## usage: dm_generate_version <file> <ufname>
243 function dm_generate_version() {
244 local to="$1"
245 local ufname="$2"
246
247 # final touch
248 echo "<?php
249 /** @deprecated */
250 function civicrmVersion( ) {
251 return array( 'version' => '$DM_VERSION',
252 'cms' => '$ufname',
253 'revision' => '$DM_REVISION' );
254 }
255 " > "$to"
256 }
257
258 ## Perform a hard checkout on a given report
259 ## usage: dm_git_checkout <repo_path> <tree-ish>
260 function dm_git_checkout() {
261 pushd "$1"
262 git checkout .
263 git checkout "$2"
264 popd
265 }
266
267 ## Download a Civi extension
268 ## usage: dm_install_cvext <full-ext-key> <target-path>
269 function dm_install_cvext() {
270 # cv dl -b '@https://civicrm.org/extdir/ver=4.7.25|cms=Drupal/com.iatspayments.civicrm.xml' --destination=$PWD/iatspayments
271 cv dl -b "@https://civicrm.org/extdir/ver=$DM_VERSION|cms=Drupal/$1.xml" --to="$2"
272 }
273
274 ## Export a list of patch files from a git repo
275 ## usage: dm_export_patches <src-repo> <out-dir> <range>
276 ## ex: dm_export_patches "$HOME/src/somerepo" "/tmp/export" 5.1.2..5.1.6
277 function dm_export_patches() {
278 if [ ! -d "$1" ]; then
279 echo "ignore: $1"
280 return
281 fi
282 echo "Export \"$1\" ($3) to \"$2\""
283 pushd "$1" >> /dev/null
284 git format-patch "$3" -o "$2"
285 popd >> /dev/null
286 }
287
288
289 ## Edit a file by applying a regular expression.
290 ## Note: We'd rather just call "sed", but it differs on GNU+BSD.
291 ## usage: dm_preg_edit <search-pattern> <replacement-pattern> <file>
292 ## example: '/version = \([0-9]*\.x-\)[1-9.]*/' 'version = \1$DM_VERSION'
293 function dm_preg_edit() {
294 env RPAT="$1" RREPL="$2" RFILE="$3" \
295 php -r '$c = file_get_contents(getenv("RFILE")); $c = preg_replace(getenv("RPAT"), getenv("RREPL"), $c); file_put_contents(getenv("RFILE"), $c);'
296 }