lib/update.updates
changeset 52 b68b8615c512
child 55 a2d87cfd77e4
equal deleted inserted replaced
51:c486df8ea68a 52:b68b8615c512
       
     1 # vim: set ft=sh :
       
     2 #
       
     3 # Dependency-based updates + utils
       
     4 
       
     5 ## Compare the given output file with all given source files:
       
     6 #
       
     7 #   check_update $out ${deps[@]} && do_update $out ... || ...
       
     8 #
       
     9 # Returns true if the output file needs to be updated.
       
    10 function check_update {
       
    11     # target
       
    12     local out=$1; shift
       
    13 
       
    14     debug "$out"
       
    15 
       
    16     # need update?
       
    17     local update=
       
    18 
       
    19     if [ ${#@} == 0 ]; then
       
    20         debug "  update: unknown deps"
       
    21         update=y
       
    22 
       
    23     elif [ ! -e $out ]; then
       
    24         debug "  update: dest missing"
       
    25         update=y
       
    26         
       
    27     elif [ $UPDATE_FORCE ]; then
       
    28         debug "  update: forced"
       
    29         update=y
       
    30     fi
       
    31 
       
    32     # check deps
       
    33     for dep in "$@"; do
       
    34         # don't bother checking if already figured out
       
    35         [ $update ] && continue
       
    36 
       
    37         # check
       
    38         if [ ! -e $ROOT/$dep ]; then
       
    39             fail "$dst: Missing source: $dep"
       
    40 
       
    41         elif [ $ROOT/$out -ot $ROOT/$dep ]; then
       
    42             debug "  update: $dep"
       
    43             update=y
       
    44         else
       
    45             debug "  check: $dep"
       
    46         fi
       
    47     done
       
    48 
       
    49     [ ! $update ] && debug "  up-to-date"
       
    50 
       
    51     # return
       
    52     [ $update ]
       
    53 }
       
    54 
       
    55 ## Generate updated output file from given command's stdout:
       
    56 #
       
    57 #   do_update $out $BIN/cmd --args
       
    58 #
       
    59 # Writes output to a temporary .new file, optionally shows a diff of changes, and commits
       
    60 # the new version to $out (unless noop'd).
       
    61 function do_update {
       
    62     local out=$1; shift
       
    63     local tmp=$out.new
       
    64 
       
    65     debug "$out"
       
    66     cmd "$@" > $ROOT/$tmp
       
    67 
       
    68     # compare
       
    69     if [ -e $ROOT/$out ] && [ $UPDATE_DIFF ]; then
       
    70         debug "  changes:"
       
    71 
       
    72         # terse
       
    73         indent "        " diff --unified=1 $ROOT/$out $ROOT/$tmp || true
       
    74     fi
       
    75     
       
    76     # deploy
       
    77     if [ $UPDATE_NOOP ]; then
       
    78         # cleanup
       
    79         debug "  no-op"
       
    80 
       
    81         cmd rm $ROOT/$tmp
       
    82     else
       
    83         # commit
       
    84         debug "  deploy"
       
    85 
       
    86         cmd mv $ROOT/$tmp $ROOT/$out
       
    87     fi
       
    88 }
       
    89 
       
    90 ## Look for a link target:
       
    91 #
       
    92 #   find_link   $lnk    $tgt...
       
    93 #
       
    94 # Outputs the first given target to exist, skipping any that are the same as the given $lnk.
       
    95 # If no $tgt matches, outputs the last one, or '-'.
       
    96 function choose_link {
       
    97     local lnk=$1; shift
       
    98     local tgt=-
       
    99 
       
   100     for tgt in "$@"; do
       
   101         [ $tgt != $out ] && [ -e $ROOT/$tgt ] && break
       
   102     done
       
   103     
       
   104     echo $tgt
       
   105 }
       
   106 
       
   107 
       
   108 ## Compare symlink to target:
       
   109 #
       
   110 #   check_link $lnk $tgt && do_link $lnk $tgt || ...
       
   111 #
       
   112 # Tests if the symlink exists, and the target matches.
       
   113 # Fails if the target does not exist.
       
   114 function check_link {
       
   115     local lnk=$1
       
   116     local tgt=$2
       
   117 
       
   118     [ ! -e $ROOT/$tgt ] && fail "$tgt: target does not exist"
       
   119     
       
   120     [ ! -e $ROOT/$lnk ] || [ $(readlink $ROOT/$lnk) != $ROOT/$tgt ]
       
   121 }
       
   122 
       
   123 ## Update symlink to point to target:
       
   124 #
       
   125 #   do_link $lnk $tgt
       
   126 #
       
   127 function do_link {
       
   128     local lnk=$1
       
   129     local tgt=$2
       
   130 
       
   131     cmd ln -sf $ROOT/$tgt $ROOT/$lnk
       
   132 }
       
   133 
       
   134 ## Update .serial number:
       
   135 #
       
   136 #   do_update_serial $serial
       
   137 #
       
   138 # Shows old/new serial on debug.
       
   139 function do_update_serial {
       
   140     local serial=$1
       
   141 
       
   142     # read
       
   143     local old=$(test -e $ROOT/$serial && cat $ROOT/$serial || echo '')
       
   144 
       
   145 
       
   146     cmd $BIN/update-serial $ROOT/$serial
       
   147     
       
   148     # read
       
   149     local new=$(cat $ROOT/$serial)
       
   150         
       
   151     debug "  $old -> $new"
       
   152 }
       
   153 
       
   154 ## Perform `hg commit` for $DATA
       
   155 function do_commit {
       
   156     local msg=$1
       
   157 
       
   158     indent "    " hg_diff
       
   159 
       
   160     hg_commit "$msg"
       
   161 }
       
   162 
       
   163