mk.sh 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/bin/bash
  2. # saturnin - Smart and ready desktop helper
  3. # See LICENSE file for copyright and license details.
  4. tmp=$(mktemp)
  5. sed -e 's/ = /=/' < config.mk > $tmp
  6. . $tmp
  7. rm -f $tmp
  8. bindir=${DESTDIR}${PREFIX}/bin
  9. shrdir=${DESTDIR}${PREFIX}/share/saturnin
  10. lexdir=${DESTDIR}${PREFIX}/libexec/saturnin
  11. list_of_bins() {
  12. echo bin/saturnin
  13. }
  14. list_of_lexs() {
  15. echo libexec/saturnin-czrates
  16. echo libexec/saturnin-dmenu
  17. echo libexec/saturnin-iam
  18. echo libexec/saturnin-conf
  19. echo libexec/saturnin-ip
  20. echo libexec/saturnin-ln
  21. echo libexec/saturnin-push
  22. echo libexec/saturnin-revert
  23. echo libexec/saturnin-watch
  24. echo libexec/saturnin-www
  25. }
  26. list_of_installed_bins() {
  27. list_of_bins | sed -e "s/bin/$(sed -e 's/\//\\\//g' <<<$bindir)/"
  28. }
  29. list_of_installed_lexs() {
  30. list_of_lexs | sed -e "s/libexec/$(sed -e 's/\//\\\//g' <<<$lexdir)/"
  31. }
  32. die() {
  33. for l in "$@"; do echo "$l" >&2; done
  34. exit 9
  35. }
  36. list_of_shrs() {
  37. echo ffoo/saturnin_www.sh
  38. }
  39. build1() {
  40. local srcpath dstpath
  41. srcpath=$1
  42. dstpath=${srcpath%.skel}
  43. case $dstpath in
  44. *.md) cat $srcpath \
  45. | expand_includes \
  46. | expand_variables \
  47. > $dstpath ;;
  48. *) cat $srcpath \
  49. | expand_variables \
  50. > $dstpath ;;
  51. esac
  52. echo $dstpath >> built.list
  53. }
  54. build() {
  55. local srcpath
  56. find -type f -name '*.skel' \
  57. | while read srcpath;
  58. do
  59. build1 "$srcpath"
  60. done
  61. }
  62. clean() {
  63. test -f built.list && {
  64. cat built.list | xargs rm -f
  65. rm -f built.list
  66. } || :
  67. }
  68. dist() {
  69. local version=$(get_version)
  70. local dirname=saturnin-$version
  71. mkdir -p $dirname
  72. cp -R bin \
  73. config.mk \
  74. ffoo \
  75. Makefile \
  76. README \
  77. LICENSE \
  78. setup \
  79. utils \
  80. $dirname
  81. sed -i -e "s/^VERSION = .*/VERSION = $version/" $dirname/config.mk
  82. tar -cf $dirname.tar $dirname
  83. gzip $dirname.tar
  84. rm -rf $dirname
  85. }
  86. expand_includes() {
  87. #
  88. # Expand include directives
  89. #
  90. # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
  91. #
  92. perl -we '
  93. use strict;
  94. my $text;
  95. while (<>) {
  96. chomp;
  97. if (m/<!-- include4: (\S+) -->/) {
  98. open my $fh, $1 or warn "cannot find: $1";
  99. my $text = do { local($/); <$fh> };
  100. close $fh;
  101. $text =~ s/^(.)/ $1/gm;
  102. chomp $text;
  103. print "$text\n";
  104. } else {
  105. print "$_\n";
  106. }
  107. }
  108. '
  109. }
  110. expand_variables() {
  111. #
  112. # Expand variable values
  113. #
  114. perl -pe "
  115. s|__SATURNIN_CONFIG_LOCAL__|$SATURNIN_CONFIG_PATH_LOCAL|;
  116. s|__SATURNIN_CONFIG_USER__|\\\$HOME/$SATURNIN_CONFIG_PATH_USER|;
  117. s|__SATURNIN_SHARE__|$shrdir|;
  118. s|__SATURNIN_LIBEXEC__|$lexdir|;
  119. s|__VERSION__|$(get_version)|;
  120. "
  121. }
  122. install() {
  123. mkdir -vp $bindir \
  124. $shrdir/ffoo \
  125. $lexdir
  126. list_of_bins | xargs cp -vrt $bindir
  127. list_of_shrs | xargs cp -vrt $shrdir/ffoo
  128. list_of_lexs | xargs cp -vrt $lexdir
  129. list_of_installed_bins | xargs chmod -v 755
  130. list_of_installed_lexs | xargs chmod -v 755
  131. test -f .autoclean && clean || :
  132. }
  133. get_version() {
  134. #
  135. # Build semver version string with build metadata
  136. #
  137. # Build version string from available info using following
  138. # logic:
  139. #
  140. # 1. use VERSION (from config.mk)
  141. # 2. if we are in git, override the version with last tag
  142. # 3. if set, add STAGE (from config.mk) as pre-release ID
  143. # (afer dash)
  144. # 4. if we are at a later commit than the last tag, add commit
  145. # sha1 to build metadata (after plus sign)
  146. # 5. if the tree is "dirty", i.e. has uncommited changes,
  147. # add "dirty" to build metadata
  148. #
  149. # The version is compatible with SemVer 2.0.0.
  150. #
  151. # Examples:
  152. #
  153. # myprog v1.0.7 # all clear
  154. # myprog v1.0.7-alpha # STAGE="alpha"
  155. # myprog v1.0.7-alpha+g1aef811 # ^^ + some commits after
  156. # myprog v1.0.7-alpha+g1aef811.dirty # ^^ + tree edited
  157. # myprog v1.0.7-alpha+dirty # tag OK but tree edited
  158. # myprog v1.0.7+dirty # ^^ but no stage
  159. #
  160. # Note that versions with "dirty" should be perceived as kind of
  161. # dangerous outside developer's own machine. Versions with sha1 are
  162. # safer but must not be released.
  163. #
  164. # I have considered decorating the git commit refs to make them
  165. # sort of sortable (e.g. "r1.g1aef811"), but on second thought,
  166. # I don't think it's good idea to give *any* semantics to meta-data
  167. # at all. First, there is no rule that r1<r2<r3; a commit can be
  168. # removing what other just added and one change can be split to
  169. # multiple commits. Also, the whole thing breaks anyway once you
  170. # rebase your branch (no, it's not a sin). The sole purpose of
  171. # meta-data is to *identify* the code, and provide safe path back
  172. # to tree; commit refs are already perfect for that.
  173. #
  174. local version=$VERSION
  175. local stage=$STAGE
  176. if git rev-parse HEAD >&/dev/null;
  177. then # we are in git repo... so we can get smart
  178. local lasttag=$(git tag | grep ^v | sort -V | tail -n1)
  179. if ! git describe --tags --exact-match HEAD >&/dev/null;
  180. then # we are not at later commit than the last tag
  181. local sha=g$(git describe --always HEAD)
  182. fi
  183. if test "$(git diff --shortstat 2>/dev/null)" != "";
  184. then # thr tree is "dirty", i.e. has been edited
  185. local dirty=dirty
  186. fi
  187. version=${lasttag:1}
  188. local suffix=""
  189. case $stage:$sha:$dirty in
  190. ::) suffix="" ;;
  191. ::dirty) suffix="+$dirty" ;;
  192. :g*:) suffix="+$sha" ;;
  193. :g*:dirty) suffix="+$sha.dirty" ;;
  194. *::) suffix="-$stage" ;;
  195. *::dirty) suffix="-$stage+$dirty" ;;
  196. *:g*:) suffix="-$stage+$sha" ;;
  197. *:g*:dirty) suffix="-$stage+$sha.$dirty" ;;
  198. esac
  199. version=$version$suffix
  200. fi
  201. echo $version
  202. }
  203. uninstall() {
  204. list_of_installed_bins | xargs rm -vf
  205. rm -vrf $shrdir
  206. rm -vrf $lexdir
  207. }
  208. case $1 in
  209. build|clean|dist|install|install_manpages|manpages|uninstall)
  210. $1
  211. ;;
  212. *)
  213. echo "usage: $(basename $0) build|clean|dist|install|uninstall" >&2
  214. esac