create-pot-files.sh 11.7 KB
Newer Older
1
#!/bin/bash
2
set -e
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#####################################################################
## Variables

## Source code path
SRC=

## Output path
POTDIR=

## The component *.pot files are based roughly on CRM/Foo templates/CRM/Foo.
COMPONENT_POTS="Admin Badge Batch Campaign Case Contribute Event Extension Financial Grant Mailing Member PCP Pledge Project Queue Report"

## The adhoc *.pot files are based on clear file list (but may have some special/less predictable rules).
ADHOC_POTS="common-base countries drupal-civicrm install menu provinces"

## The magic *.pot files are derived from other *.pot files.
MAGIC_POTS="common-components"

## List of chosen *.pot files
POTS=

## Header file to prepend to any *.pot
26 27
HEADER_TMPL=bin/header
HEADER=
28 29 30 31 32 33 34 35

## Flags to control which actions are performed
DO_SCAN=
DO_DIGEST=
DO_CLEANUP=
FORCE=

#####################################################################
36 37 38
function usage() {
  cat <<EOT
create-pot-files.sh - builds .pot files for CiviCRM.
39

40
Usage:
41
  ./bin/create-pot-files.sh [options] [srcdir] [destdir] [pot1...]
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
Examples:
  ./bin/create-pot-files.sh ~/repository/civicrm/ ~/repository/l10n/po/pot/
  ./bin/create-pot-files.sh -sd ~/repository/civicrm/ ~/repository/l10n/po/pot/ common-base Admin Contribute

Options:
  -s    Scan targets for strings (Pass #1)
  -d    Digest/dedupe scanned strings (Pass #2)
  -c    Cleanup temp files
  -a    All (scan+digest+cleanup; default)
  -f    Force (Ignore cached results from previous scan)
  -h    Help

Targets:
  $COMPONENT_POTS
  $ADHOC_POTS $MAGIC_POTS
58 59 60 61

Although you should probably not call this directly. Use build-unified-pots.sh
if you are exporting the strings to Transifex.
http://wiki.civicrm.org/confluence/display/CRMDOC/Pushing+new+strings+to+Transifex
62 63 64 65 66 67

EOT

  exit 1;
}

68 69 70
#####################################################################
## Assert that CLI dependencies are met
function check_deps() {
71
  for cmd in sponge civistrings msgcomm msguniq tempfile tr grep cut date sed ; do
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    if ! which $cmd > /dev/null ; then
      echo "Missing required command: $cmd"
      echo
      case "$cmd" in
        sponge)
          echo 'This program uses the "sponge" command which you can get by installing the'
          echo '"moreutils" package under Debian/Ubuntu or by visting'
          echo 'https://joeyh.name/code/moreutils/'
          ;;
        civistrings)
          echo 'This program uses the "civistrings" command which is bundled with buildkit.'
          echo 'You can download it separately from https://github.com/civicrm/civistrings'
      esac
      exit 1
    fi
  done
}

#####################################################################
## civistrings wrapper with some default options
function _civistrings() {
  civistrings --header="$HEADER" "$@"
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109
#####################################################################
## usage: HEADER=$(build_header "$HEADER_TMPL")
function build_header() {
  local tmpl="$1"
  local out="$POTDIR/.header"
  local now=`date +'%F %R%z'`

  cat "$tmpl" \
    | sed "s/NOW/$now/" \
    > "$out"

  echo "$out"
}

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
#####################################################################
## Build an individual POT file
## usage: build_raw_pot <name>
## example: build_raw_pot Mailing
## example: build_raw_pot install
function build_raw_pot() {
  local name="$1"
  local filepath="$POTDIR/.raw-"$(echo $name | tr '[:upper:]' '[:lower:]').pot

  if [ -f "$filepath" -a -z "$FORCE" ]; then
    echo "[[ Found raw strings for ${name} from previous scan. ]]"
    return
  fi

  echo "[[ Building raw strings for ${name} ]]"

  case "$name" in

    ## Adhoc targets, sorted alphabetically

    common-base)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/{ACL,Activity,Block,common,Contact,Core} \
        {CRM,templates/CRM}/{Custom,Dashlet,Dedupe,Export,Form,Friend} \
        {CRM,templates/CRM}/{Group,Import,Logging,Note,Price,Profile} \
        {CRM,templates/CRM}/{Relationship,SMS,Standalone,Tag,UF,Utils} \
136 137 138
        ang/crmApp* \
        ang/crmAttachment* \
        ang/crmUi* \
139 140 141 142 143 144
        xml/templates/civicrm_acl.tpl \
        xml/templates/civicrm_data.tpl \
        xml/templates/languages.tpl \
        xml/templates/civicrm_msg_template.tpl \
        xml/templates/message_templates/friend_* \
        xml/templates/message_templates/uf_notify_* \
145
        packages/HTML/QuickForm \
totten's avatar
totten committed
146
        partials/ \
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
        js/

      ## The CRM/Upgrade folder includes *.tpl files which, for some reason,
      ## have been omitted from past pot's. Omitting these requires more
      ## precise file selection.
      find CRM/Upgrade -name '*.php' | _civistrings -ao "$filepath" -
      _civistrings -ao "$filepath" templates/CRM/Upgrade
      ;;

    common-components)
      ## Not yet; handled in the digest phase. That why it's in MAGIC_POTS
      return
      ;;

    countries)
      cat "$HEADER" > "$filepath"
      grep ^INSERT xml/templates/civicrm_country.tpl \
        | cut -d\" -f4 \
        | while read entry; do
          echo -e "msgctxt \"country\"\nmsgid \"$entry\"\nmsgstr \"\"\n"
        done \
        >> "$filepath"
      ## Hmm, if civicrm_country.tpl used {ts}, then we could just call "civistrings --msgctxt=country"
      ;;

    drupal-civicrm)
      _civistrings -o "$filepath" \
        drupal \
        CRM/Core/Permission.php
      ;;

    install)
      _civistrings -o "$filepath" \
        install/
      ;;

    menu)
      cat "$HEADER" > "$filepath"
      grep -h '<title>' CRM/*/xml/Menu/*.xml \
        | sed 's/^.*<title>\(.*\)<\/title>.*$/\1/' \
        | while read entry; do
          echo -e "msgctxt \"menu\"\nmsgid \"$entry\"\nmsgstr \"\"\n"
        done \
        >> "$filepath"
      _civistrings --msgctxt=menu xml/templates/civicrm_navigation.tpl -ao "$filepath"
      ;;

    provinces)
      cat "$HEADER" > "$filepath"
      grep '^(' xml/templates/civicrm_state_province.tpl \
        | cut -d\" -f4 \
        | while read entry; do
          echo -e "msgctxt \"province\"\nmsgid \"$entry\"\nmsgstr \"\"\n"
        done \
        >> "$filepath"
        ## Hmm, if civicrm_country.tpl used {ts}, then we could just call "civistrings --msgctxt=country"
      ;;

    ## Standard targets, sorted alphabetically

207 208 209 210 211 212 213
    Admin)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        ang/crmCxn* \
        ang/crmStatusPage*
      ;;

214 215 216 217 218 219 220 221 222
    Campaign)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        xml/templates/message_templates/petition_*
      ;;

    Case)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
223
        ang/crmCase* \
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        xml/templates/message_templates/case_*
      ;;

    Contribute)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        xml/templates/message_templates/contribution_* \
        xml/templates/message_templates/test_*
      ;;

    Event)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        xml/templates/message_templates/event_* \
        xml/templates/message_templates/participant_*
      ;;

bgm's avatar
bgm committed
241 242 243 244 245 246
    Mailing)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        ang/crmMailing*
      ;;

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
    Member)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        xml/templates/message_templates/membership_*
      ;;

    PCP)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        xml/templates/message_templates/pcp_*
      ;;

    Pledge)
      _civistrings -o "$filepath" \
        {CRM,templates/CRM}/$name \
        xml/templates/message_templates/pledge_*
      ;;

    *)
      if echo " $COMPONENT_POTS " | grep -q " $name " > /dev/null ; then
        _civistrings -o "$filepath" {CRM,templates/CRM}/$name
      else
        echo "unrecognized pot: $name"
      fi
      ;;

  esac

  find "$filepath" ! -empty | while read f; do
    msguniq "$filepath" | sponge "$filepath"
  done
}

#####################################################################
## usage: make_stat <name>
## example: make_stat Mailing
function make_stat() {
  local name="$1"
  local filepath="$POTDIR/.raw-"$(echo $name | tr '[:upper:]' '[:lower:]').pot
  grep ^msgid "$filepath" | sort -u > "$filepath.msgid"
  grep '^#:' "$filepath" | sed 's/#://' | tr ' ' '\n' | sort -u > "$filepath.files"
}

#####################################################################
## Scan .raw-*.pot for common strings and put them in common-components.pot
## usage: build_common_components
function build_common_components() {
  echo "[[ Building common-components.pot ]]"
  local paths=""
296
  local has_multiple=0
297 298 299 300
  for comp in $COMPONENT_POTS ; do
    local rawfile=".raw-"$(echo $comp | tr '[:upper:]' '[:lower:]').pot
    if [ -f "$rawfile" ]; then
      paths="$paths $rawfile"
301
      has_multiple=1
302 303
    fi
  done
304
  if [ $has_multiple -eq 1 ]; then
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
    msgcomm $paths > .raw-common-components.pot
  else
    cat $HEADER > .raw-common-components.pot
  fi
}

#####################################################################
## example: build_final_pot Mailing
## example: build_final_pot install
function build_final_pot() {
  local name="$1"
  local rawpath="$POTDIR/.raw-"$(echo $name | tr '[:upper:]' '[:lower:]').pot
  local finalpath="$POTDIR/"$(echo $name | tr '[:upper:]' '[:lower:]').pot
  local tmpfile=`tempfile`

  echo "[[ Building final strings for ${name} ]]"

  cp -f "$rawpath" "$finalpath"

  if echo " $COMPONENT_POTS " | grep -q " $name " > /dev/null ; then
    msgcomm "$finalpath" .raw-common-components.pot > $tmpfile
    msgcomm --unique "$finalpath" $tmpfile | sponge "$finalpath"

    msgcomm "$finalpath" .raw-common-base.pot | sponge $tmpfile
    msgcomm --unique "$finalpath" $tmpfile | sponge "$finalpath"

  elif [ "$name" == "install" ]; then
    msgcomm "$finalpath" .raw-common-base.pot | sponge $tmpfile
    msgcomm --unique "$finalpath" $tmpfile | sponge "$finalpath"
  fi

  rm -f "$tmpfile"
}

#####################################################################
## Delete temp files
function do_cleanup() {
  echo "[[ Cleanup temp files ]]"
343
  rm .header .raw*pot -f
344 345 346 347 348
}

#####################################################################
## Main

349 350 351
[ "$1" == "--help" ] && usage
[ "$1" == "-h" ] && usage

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
check_deps

FOUND_ACTION=
while getopts "asfdc" opt; do
  case $opt in
    a)
      DO_SCAN=1
      DO_DIGEST=1
      DO_CLEANUP=1
      FOUND_ACTION=1
      ;;
    s)
      DO_SCAN=1
      FOUND_ACTION=1
      ;;
    d)
      DO_DIGEST=1
      FOUND_ACTION=1
      ;;
    c)
      DO_CLEANUP=1
      FOUND_ACTION=1
      ;;
    f)
      FORCE=1
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

if [ -z "$FOUND_ACTION" ]; then
  DO_SCAN=1
  DO_DIGEST=1
  DO_CLEANUP=1
fi

shift $((OPTIND-1))

397 398 399 400 401 402 403 404
[ "$1" == "" ] && echo 'source dir missing'     && usage
test ! -e "$1" && echo 'source does not exist'  && usage
test ! -d "$1" && echo 'source not a directory' && usage

[ "$2" == "" ] && echo 'target dir missing'     && usage
test ! -e "$2" && echo 'target does not exist'  && usage
test ! -d "$2" && echo 'target not a directory' && usage

405 406 407
# use absolute paths so that we can chdir/pushd
SRC=$(php -r 'echo realpath($argv[1]);' "$1")
POTDIR=$(php -r 'echo realpath($argv[1]);' "$2")
408
HEADER=$(build_header "$HEADER_TMPL")
409 410 411 412 413 414 415
## TODO: substitute "NOW" in HEADER
shift 2

if [ -z "$1" ]; then
  POTS="$COMPONENT_POTS $ADHOC_POTS $MAGIC_POTS"
else
  POTS="$@"
416
fi
417

418 419 420 421 422 423 424
if [ -n "$DO_SCAN" ]; then
  pushd "$SRC" >> /dev/null
    for POT in $POTS ; do
      build_raw_pot "$POT"
    done
  popd >> /dev/null
fi
425

426 427 428 429 430 431 432 433
if [ -n "$DO_DIGEST" ]; then
  pushd "$POTDIR" >> /dev/null
    build_common_components
    for POT in $POTS ; do
      build_final_pot "$POT"
    done
  popd >> /dev/null
fi
434

435 436 437 438 439
if [ -n "$DO_CLEANUP" ]; then
  pushd "$POTDIR" >> /dev/null
    do_cleanup
  popd >> /dev/null
fi