-
Notifications
You must be signed in to change notification settings - Fork 18
/
wrapper.sh
584 lines (523 loc) · 18.1 KB
/
wrapper.sh
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#! /bin/bash
### example
# sh wrapper.sh --repo make-novice --create
# sh wrapper.sh --repo make-novice --import
# sh wrapper.sh --repo make-novice --account GitHubUser --import
# sh wrapper.sh --repo r-novice-gapminder --account swcarpentry-ja --import --webpages
# create (implemented): subroutine to create new PO files from an English lesson not currently being translated
# import (testing): subroutine to pull a lesson being translated from remote to make changes locally
# webpages (testing): subroutine to render webpages from current PO files and export Japanese lessons to remote repos
# update (to-do later): subroutine to pull updates from remote English lesson and merge based on archived ancestor (only new sections need to be translated)
pc_user=`whoami`
echo $pc_user account for pc `hostname`
git_user=`git config user.name`
remote_user=$git_user
git_email=`git config user.email`
remote_account=$git_user
echo $git_user \<${git_email}\> account detected for git
next=false
create=false
import=false
render=false
for op in "$@"; do
if $next; then
next=false;
continue;
fi
case "$op" in
-u|--user)
shift
if [[ $1 != "" ]]; then
# pass different git user name to local account
git_user="${1/%\//}"
# keep username case-sensitive
# git_user=`echo "$git_user" | tr '[:upper:]' '[:lower:]'`
next=true
shift
else
git_user=`git config user.name`
next=true
fi
;;
-c|--create)
shift
create=true
next=true
;;
-i|--import)
shift
import=true
next=true
;;
-a|--account)
shift
if [[ $1 != "" ]]; then
remote_user="${1/%\//}"
next=true
else
echo "specify remote account for: $git_user"
remote_user=$git_user
next=true
fi
shift
;;
-r|--repo)
shift
if [[ $1 != "" ]]; then
repo="${1/%\//}"
next=true
else
all_repo=true
next=true
fi
shift
;;
-w|--webpages)
shift
render=true
next=true
;;
-u|--update)
shift
update=true
next=true
;;
-*)
echo "Error: Invalid option: $op"
exit 1
;;
esac
done
if [[ $import == true ]]; then
if [[ $create == true ]]; then
echo "Warning: it is not recommended to --create and --import at the same time"
fi
create=$create
fi
if [[ $import == true ]] || [[ $create == true ]]; then
if [[ -z $repo ]]; then
echo all available repos to import: $all_repos
else
echo repo to create or import: $repo
fi
fi
echo create ${repo} : $create
echo import ${repo} : $import
echo render webpages : $render
#check if remote i18n repo exists
root_dir=`git ls-remote https://github.com/${remote_user}/i18n.git | grep "ja" | wc -l`
echo i18n repo: $root_dir
if [ $root_dir -eq 1 ]; then
echo "remote found: https://github.com/${remote_user}/i18n.git"
elif [ $root_dir -eq 0 ]; then
echo remote not found for user repo: https://github.com/${remote_user}/i18n.git \n please create a fork
exit 1
else
echo ambiguous repo:
git ls-remote https://github.com/${remote_user}/i18n.git
exit 1
fi
#check if remote repo exists
root_dir=`git ls-remote https://github.com/${remote_user}/${repo}.git | grep "gh-pages" | wc -l`
echo $repo repo: $root_dir
if [ $root_dir -eq 1 ]; then
echo "remote found: https://github.com/${remote_user}/${repo}.git"
elif [ $root_dir -eq 0 ]; then
echo remote not found for user repo: https://github.com/${remote_user}/${repo}.git \n please create a fork
exit 1
else
echo ambiguous repo:
git ls-remote https://github.com/${remote_user}/${repo}.git
exit 1
fi
if [[ $create == true ]]; then
echo "runnning"
if [[ -d i18n ]]; then
cd i18n
fi
#check if current working directory is i18n
wd="${PWD##*/}"
echo $wd
if [[ $wd != "i18n" ]]; then
echo "create i18n directory"
git clone https://github.com/${git_user}/i18n.git
cd i18n
fi
git pull origin ja
#checkout Japanese branch
git checkout ja
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
git remote add remote-repo [email protected]:${git_user}/i18n.git
git pull remote-repo ja
#import submodules
git submodule init
git submodule add [email protected]:${git_user}/${repo}
echo "update local submodules"
git submodule update -f --recursive $repo
#remove _locale directory (only translate English lessons)
for dir in `git submodule | grep "^+" | cut -d" " -f2`
do
if [ -d $dir ]
then
cd $dir
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
#reset all repos to remote
url=https://github.com/${git_user}/${dir}.git
git remote add remote-repo $url
if [[ `git branch -v | grep "master" | wc -l` -ge 1 ]]
then
git pull master -f
fi
git remote add remote-repo $url
if [[ `git branch -v | grep "gh-pages" | wc -l` -ge 1 ]]
then
git branch -D gh-pages
fi
git checkout -b gh-pages `git rev-list HEAD | tail -n 1`
git pull remote-repo gh-pages -f
git add -u
git commit -m "reset branch"
git checkout HEAD
rm -rf _locale
cd ..
fi
done
if [[ ! -z $repo ]]; then
if [[ -d $repo ]]; then
echo submodule $repo found
else
git submodule add https://github.com/swcarpentry/${repo}.git
fi
fi
if [[ -z $repo ]];then
echo "warning $repo not found, specify --repo <lesson name> --create"
exit 1
fi
echo "run import on po4gitbook"
po4gitbook/import.sh > /dev/null 2>&1
echo "upated PO files exported"
if [[ -f po/${repo}.ja.po ]]; then
echo "Warning: file po/${repo}.ja.po already exists: check for conflicts and import"
exit 1
else
echo "creating PO file for $repo"
cp po/${repo}.pot po/${repo}.ja.po
git add po/${repo}.ja.po
#archive PO file from English lessons for merging updates
if [[ -f po/.ancestors/.${repo}.jp.po.ancestor ]]; then
echo "Warning file po/${repo}.ja.po already archived in po/.ancestors"
else
echo "archiving PO file for $repo"
mkdir -p po/.ancestors
cp po/${repo}.pot po/.ancestors/.${repo}.ja.po.ancestor
git add po/.ancestors/.${repo}.ja.po.ancestor
fi
#fill in missing information for Japanese
year=`date +%Y`
sed -i '1s/# SOME DESCRIPTIVE TITLE./# Japanese translation of the Software Carpentry ${repo} Lesson/g' po/${repo}.ja.po
sed -i "2s/# Copyright \(C\) YEAR THE PACKAGE\'S COPYRIGHT HOLDER/# Copyright \(C\) ${year} Software Carpentry Foundation; Japanese Translation Team/g" po/${repo}.ja.po
sed -i '3s/# This file is distributed under the same license as the PACKAGE package./# This file is distributed under the same license as the git4pobook package./g' po/${repo}.ja.po
sed -i '4s/# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR./# ${git_user} <${git_email}>, ${year}./g' po/${repo}.ja.po
sed -i '12s/"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"/"Last-Translator: ${git_user} <${git_email}>\n"/g' po/${repo}.ja.po
sed -i '13s/Language-Team: LANGUAGE <[email protected]>\n/Language-Team: Japanese <[email protected]>\n/g' po/${repo}.ja.po
sed -i '14 i "Language: ja\n"' po/${repo}.ja.po
#remove Japanese from LINGUAS if already exists
sed -i '1s/ ja//g' po/LINGUAS
#add Japanese to LINGUAS
sed -i '1s/$/ ja/g' po/LINGUAS
fi
git add po/${repo}.ja.po
echo "removing extraneous PO files"
rm po/*.pot
echo "run compile on po4gitbook to create new lessson"
po4gitbook/compile.sh > /dev/null 2>&1
echo lesson $repo created in _locale/ja/$repo
#restore _locale directory (only translate English lessons)
for dir in `git submodule | grep "^+" | cut -d" " -f2`
do
if [ -d $dir ]
then
cd $dir
git reset --hard
cd ..
fi
done
fi
if [[ $import == true ]]; then
echo "runnning"
if [[ -d i18n ]]; then
cd i18n
fi
#check if current working directory is i18n
wd="${PWD##*/}"
echo $wd
if [[ $wd != "i18n" ]]; then
echo "create i18n directory"
git clone https://github.com/${git_user}/i18n.git
cd i18n
fi
#checkout Japanese branch
git checkout ja
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
git remote add remote-repo [email protected]:${git_user}/i18n.git
git pull remote-repo ja
#import submodules
git submodule init
git submodule foreach 'git pull remote-repo'
git submodule add [email protected]:${git_user}/${repo}
echo "update local submodules"
git submodule update -f --recursive
#remove _locale directory (only translate English lessons)
for dir in `git submodule | grep "^+" | cut -d" " -f2`
do
if [ -d $dir ]
then
cd $dir
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
#reset all repos to remote
url=https://github.com/${git_user}/${dir}.git
git remote add remote-repo $url
if [[ `git branch -v | grep "master" | wc -l` -ge 1 ]]
then
git pull master -f
fi
git remote add remote-repo $url
if [[ `git branch -v | grep "gh-pages" | wc -l` -ge 1 ]]
then
git branch -D gh-pages
fi
git checkout -b gh-pages `git rev-list HEAD | tail -n 1`
git pull remote-repo gh-pages -f
git add -u
git commit -m "reset branch"
git checkout HEAD
rm -rf _locale
cd ..
fi
done
if [[ ! -z $repo ]]; then
if [[ -d $repo ]]; then
echo "lesson repo found: ${repo} found"
else
echo "lesson repo ${repo} not found, run:\n sh wrapper.sh --repo ${repo} --create"
fi
fi
if [[ -z $repo ]];then
echo "warning $repo not found, specify --repo <lesson name> --import"
exit 1
fi
if [[ -f po/${repo}.ja.po ]]; then
echo "Warning: file po/${repo}.ja.po exists: please edit file and submit pull request"
exit 0
else
echo "Warning: file po/${repo}.ja.po not found, run:\n sh wrapper.sh --repo ${repo} --create"
exit 1
fi
fi
if [[ $render == true ]]; then
echo "runnning"
if [[ -d i18n ]]; then
cd i18n
fi
#check if current working directory is i18n
wd="${PWD##*/}"
echo $wd
if [[ $wd != "i18n" ]]; then
echo "create i18n directory"
git clone https://github.com/${git_user}/i18n.git
cd i18n
fi
#checkout Japanese branch
git checkout ja
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
git remote add remote-repo [email protected]:${git_user}/i18n.git
git pull remote-repo ja
#import submodules
git submodule init
git submodule add [email protected]:${git_user}/${repo}
echo "import local submodules"
git submodule update -f --recursive
if [[ ! -z $repo ]]; then
if [[ -d $repo ]]; then
echo "lesson repo found: ${repo} found"
else
echo "lesson repo ${repo} not found, run:\n sh wrapper.sh --repo ${repo} --create"
fi
fi
if [[ -z $repo ]];then
echo "warning $repo not found, specify --repo <lesson name> --import"
exit 1
fi
if [[ -f po/${repo}.ja.po ]]; then
echo "File po/${repo}.ja.po exists: exporting translated lesson"
else
echo "Warning: file po/${repo}.ja.po not found, run:\n sh wrapper.sh --repo ${repo} --create"
exit 1
fi
#correct dates in headers
year=`date +%Y`
past_year=$(( $year-1 ))
#replace past year with current year
sed -i "2s/$past_year/$year/g" po/*.po
# append current year if different to previous year
sed -i "4s/, ${past_year}\./, ${past_year}, ${year}./g" po/*po
#create all Japanese lessons
echo "run compile on po4gitbook"
po4gitbook/compile.sh > /dev/null 2>&1
#commit updates to source PO files
git pull remote-repo ja
git add -u po/*ja.po
git commit -m "update PO files"
git push remote-repo ja
echo "translated lessons from po/${repo}.ja.po exported to _locale/ja/$repo"
#check if remote translated lesson exists
lesson_dir=`git ls-remote https://github.com/${git_user}/${repo}-ja.git | grep "master" | wc -l`
echo ${repo}-ja repo: $lesson_dir
if [ $lesson_dir -eq 1 ]; then
echo "remote found: https://github.com/${git_user}/${repo}-ja.git"
elif [ $root_dir -eq 0 ]; then
echo remote not found for user repo: https://github.com/${git_user}/${repo}-ja.git please create a new empty repo
exit 1
else
echo ambiguous repo:
git ls-remote https://github.com/${git_user}/${repo}-ja.git
exit 1
fi
#create as submodule
#git submodule add https://github.com/${git_user}/${repo}-ja.git _locale/ja/$repo
##git submodule absorbgitdirs <path>
#create external ja repo
mkdir -p ../${repo}-ja
#add update lessons to remote
cd ../${repo}-ja
git init
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
remotes=`git remote | grep "remote-repo" | wc -l`
if [[ remotes -le 0 ]]; then
git remote add remote-repo https://github.com/$remote_user/$repo-ja.git
fi
git pull remote-repo master
#move files to external repo
rsync -r ../i18n/locale/ja/${repo}/*md ../i18n/locale/ja/${repo}/*/*md .
# remove files provided by template
rm -rf bin/boilerplate
rm -rf _layouts _includes _episodes_rmd assets bin code
git add *
git commit -m "update lesson files"
git push remote-repo master
echo "lesson $repo-ja pushed to ${remote_user}/$repo-ja"
#update original lesson to import translated content
cd ../i18n # or English lesson
# restore to version from remote
git submodule update -f --recursive
# import changes from org repo
git submodule foreach 'case $name in po4gitbook) ;; *) git checkout gh-pages; git pull -f remote-repo gh-pages ;; esac'
#restore _locale lessons (only English lessons translated)
for dir in `git submodule | grep "^+" | cut -d" " -f2`
do
if [ -d $dir ]
then
cd $dir
git checkout gh-pages
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
remotes=`git remote | grep "remote-repo" | wc -l`
if [[ remotes -le 0 ]]; then
git remote add remote-repo https://github.com/$remote_user/${dir}.git
fi
git pull remote-repo gh-pages
cd ..
fi
done
#create external repo
mkdir -p ../${repo}
#add update lessons to remote
cd ../${repo}
git init
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
remotes=`git remote | grep "remote-repo" | wc -l`
if [[ remotes -le 0 ]]; then
git remote add remote-repo https://github.com/$remote_user/${repo}.git
fi
if [[ `git branch -v | grep "gh-pages" | wc -l` -ge 1 ]]; then
git checkout -b gh-pages
fi
git checkout gh-pages
git pull remote-repo gh-pages
git submodule update -f --recursive
#add changes
git add -u
#create lesson in _locale if not existing or update
if [ -d ./_locale/ja ]
then
cd _locale/ja
git init
git checkout master
git add -u
git commit -m "update Japanese lessons"
remotes=`git remote | grep "origin" | wc -l`
if [[ remotes -le 0 ]]; then
git remote add origin https://github.com/$remote_user/${repo}-ja.git
fi
git pull origin master
pwd
cd ../..
else
mkdir -p _locale
url="https://github.com/${remote_user}/${repo}-ja.git"
git submodule add $url ./_locale/ja
fi
cd _locale/ja
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
remotes=`git remote | grep "remote-repo" | wc -l`
if [[ remotes -le 0 ]]; then
git remote add remote-repo https://github.com/${remote_user}/$repo-ja.git
fi
git checkout master
git pull remote-repo master
cd ../..
#push updated _locale lessons to English lesson
git checkout gh-pages
git pull remote-repo gh-pages
git add -u
git commit -m "update Japanese lessons"
if [ `git remote -v | grep "remote-repo" | wc -l` -ge 1 ]
then
git remote remove remote-repo
fi
remotes=`git remote | grep "remote-repo" | wc -l`
if [[ remotes -le 0 ]]; then
git remote add remote-repo https://github.com/${remote_user}/$repo.git
fi
git push remote-repo gh-pages
echo "lesson $repo pushed to ${remote_user}/$repo with locale $repo-ja"
cd ../i18n
git submodule update -f --recursive
fi
echo "run complete!"