Simple Makefile target helper https://pagure.io/mkit

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #!/bin/bash
  2. # mkit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. init_core() {
  5. #
  6. # Load core modules (or die)
  7. #
  8. #shellcheck disable=SC1090
  9. . "$MKIT_DIR/include/mkit.sh" \
  10. && . "$MKIT_DIR/include/vars.sh" \
  11. && return 0
  12. echo "failed to load core; check if MKIT_DIR is set properly: $MKIT_DIR" >&2
  13. exit 9
  14. }
  15. #
  16. # Path to MKit dir (where 'include' is)
  17. #
  18. MKIT_DIR=${MKIT_DIR:-$(dirname "$0")}
  19. init_core
  20. mkit_import ini
  21. declare -A MKIT_STUB_LICENSES
  22. MKIT_STUB_LICENSES[GPLv1]="http://www.gnu.org/licenses/old-licenses/gpl-1.0.md"
  23. MKIT_STUB_LICENSES[GPLv2]="http://www.gnu.org/licenses/old-licenses/gpl-2.0.md"
  24. MKIT_STUB_LICENSES[GPLv3]="http://www.gnu.org/licenses/gpl-3.0.md"
  25. MKIT_STUB_LICENSES[LGPLv2]="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.md"
  26. MKIT_STUB_LICENSES[LGPLv3]="http://www.gnu.org/licenses/lgpl-3.0.md"
  27. MKIT_STUB_LICENSES[AGPLv3]="http://www.gnu.org/licenses/agpl-3.0.md"
  28. MKIT_STUB_LICENSES[FDLv1.3]="http://www.gnu.org/licenses/fdl-1.3.md"
  29. MKIT_STUB_LICENSES[FDLv1.2]="http://www.gnu.org/licenses/old-licenses/fdl-1.2.md"
  30. MKIT_STUB_LICENSES[FDLv1.1]="http://www.gnu.org/licenses/old-licenses/fdl-1.1.md"
  31. deploy() {
  32. local file=$1 # which known file?
  33. local any_name=${NiceName:-$PackageName}
  34. local tmp
  35. tmp=$(mktemp -t mkit.stub.deploy.XXXXXXX)
  36. mkdir -p "$(dirname "$file")"
  37. case $file in
  38. Makefile)
  39. echo -n "# $any_name"
  40. test -n "$Tagline" && echo -n " - $Tagline"
  41. echo
  42. $MkLicense && echo '# See LICENSE.md file for copyright and license details.'
  43. echo ''
  44. echo 'MKIT_DIR=utils/mkit'
  45. #shellcheck disable=SC2016
  46. echo 'include $(MKIT_DIR)/mkit.mk'
  47. ;;
  48. README.md)
  49. echo "$any_name"
  50. tr -c '=\n' '=' <<<"$any_name"
  51. echo ''
  52. echo ''
  53. if test -n "$Tagline"; then
  54. echo "$Tagline"
  55. else
  56. echo "(Nothing to say about this project.)"
  57. fi
  58. ;;
  59. */mkit.ini|mkit.ini)
  60. echo "[project]"
  61. {
  62. echo "version = $Version"
  63. test -n "$Codename" && echo "codename = $Codename"
  64. test -n "$NiceName" && echo "name = $NiceName"
  65. test -n "$Tagline" && echo "tagline = $Tagline"
  66. test -n "$PackageName" && echo "pkgname = $PackageName"
  67. test -n "$Maintainer" && echo "maintainer = $Maintainer"
  68. test -n "$VcsBrowser" && echo "vcs_browser = $VcsBrowser"
  69. test -n "$RelSrc" && echo "relsrc = $RelSrc"
  70. test -n "$RelDst" && echo "reldst = $RelDst"
  71. } | reformat_section
  72. if updating; then
  73. remake_section dist
  74. remake_section ENV
  75. remake_section roots
  76. remake_section tokens
  77. remake_section modes
  78. remake_section files
  79. else
  80. echo ""
  81. echo "[dist]"
  82. {
  83. $MkLicense && echo "tarball = LICENSE.md"
  84. $MkMakefile && echo "tarball = Makefile"
  85. $MkReadme && echo "tarball = README.md"
  86. echo "tarball = mkit.ini"
  87. $MkPackaging && echo "tarball = packaging"
  88. echo "tarball = src"
  89. echo "tarball = tests"
  90. echo "tarball = utils"
  91. $MkPackaging && echo "rpmstuff = packaging/template.spec"
  92. $MkPackaging && echo "debstuff = packaging/debian"
  93. } | reformat_section
  94. echo ""
  95. echo "[ENV]"
  96. {
  97. echo " PREFIX = /usr/local"
  98. } | reformat_section
  99. echo ""
  100. echo "[roots]"
  101. {
  102. echo "bin = [ENV:PREFIX]/bin"
  103. echo "doc = [ENV:PREFIX]/share/doc/$PackageName"
  104. } | reformat_section
  105. echo ""
  106. echo "[tokens]"
  107. {
  108. echo "__${PackageName^^}_FOO__ = Barr.."
  109. } | reformat_section
  110. echo ""
  111. echo "[modes]"
  112. {
  113. echo "bin = 755"
  114. echo "doc = 644"
  115. } | reformat_section
  116. echo ""
  117. echo "[files]"
  118. {
  119. echo "bin = src/$PackageName"
  120. $MkLicense && echo "doc = LICENSE.md"
  121. $MkReadme && echo "doc = README.md"
  122. } | reformat_section
  123. fi
  124. echo ""
  125. echo "#mkit version=$MKIT_VERSION"
  126. ;;
  127. packaging/template.spec)
  128. echo 'Name: __MKIT_PROJ_PKGNAME__'
  129. echo 'Version: __MKIT_PROJ_VERSION__'
  130. echo 'Release: 1%{?dist}'
  131. echo 'Summary: __MKIT_PROJ_NAME__ - __MKIT_PROJ_TAGLINE__'
  132. $MkLicense && echo "License: $License"
  133. echo ''
  134. echo 'Source0: %{name}-%{version}.tar.gz'
  135. echo 'BuildArch: noarch'
  136. echo ''
  137. echo 'Requires: MKIT_STUB_REQUIRES'
  138. echo '%description'
  139. echo 'MKIT_STUB_DESCRIPTION'
  140. echo ''
  141. echo '%prep'
  142. echo '%setup -q'
  143. echo ''
  144. echo '%build'
  145. echo 'make %{?_smp_mflags} PREFIX=/usr'
  146. echo ''
  147. echo '%install'
  148. echo '%make_install PREFIX=/usr'
  149. echo ''
  150. echo '%files'
  151. echo 'MKIT_STUB_FILELIST'
  152. echo ''
  153. echo '%changelog'
  154. echo ''
  155. echo '# specfile built with MKit __MKIT_SELF_VERSION__'
  156. ;;
  157. packaging/debian/copyright)
  158. echo ""
  159. ;;
  160. packaging/debian/control)
  161. echo 'Source: __MKIT_PROJ_PKGNAME__'
  162. echo 'Maintainer: __MKIT_PROJ_MAINTAINER__'
  163. test -n "$VcsBrowser" && echo 'Vcs-Browser: __MKIT_PROJ_VCS_BROWSER__'
  164. echo 'Section: misc'
  165. echo 'Priority: extra'
  166. echo 'Standards-Version: 3.9.2'
  167. echo 'Build-Depends: debhelper (>= 9)'
  168. echo ''
  169. echo 'Package: __MKIT_PROJ_PKGNAME__'
  170. echo 'Architecture: all'
  171. echo 'Depends: MKIT_STUB_REQUIRES'
  172. echo 'Description: __MKIT_PROJ_NAME__ - __MKIT_PROJ_TAGLINE__'
  173. echo ' MKIT_STUB_DESCRIPTION'
  174. echo ''
  175. echo '# control file built with MKit __MKIT_SELF_VERSION__'
  176. ;;
  177. packaging/debian/changelog)
  178. echo '__MKIT_PROJ_PKGNAME__ (__MKIT_PROJ_VERSION__-1) UNRELEASED; urgency=medium'
  179. echo ''
  180. echo ' * Initial release. (Closes: #XXXXXX)'
  181. echo ''
  182. echo " -- __MKIT_PROJ_MAINTAINER__ $(date -R)"
  183. ;;
  184. packaging/debian/compat)
  185. echo 9
  186. ;;
  187. packaging/debian/rules)
  188. echo '#!/usr/bin/make -f'
  189. echo ''
  190. echo '%:'
  191. echo ''
  192. echo ' dh $@'
  193. echo ''
  194. echo 'override_dh_auto_install:'
  195. echo ''
  196. echo ' make install PREFIX=/usr DESTDIR=debian/tmp'
  197. echo ''
  198. echo 'override_dh_usrlocal:'
  199. echo ''
  200. echo ' @true'
  201. ;;
  202. packaging/debian/source/format)
  203. echo '3.0 (quilt)'
  204. ;;
  205. packaging/debian/*.install)
  206. echo MKIT_STUB_FILELIST
  207. ;;
  208. src/*.skel)
  209. echo 'echo "my version is: __MKIT_PROJ_VERSION__"'
  210. echo 'echo "And that'"'"'s all, folks!"'
  211. ;;
  212. LICENSE.md)
  213. local url # license URL
  214. url="${MKIT_STUB_LICENSES[$License]}"
  215. curl -sf "$url" \
  216. || die "failed to download license: $url"
  217. ;;
  218. .mkit/autoclean)
  219. ;;
  220. MKIT_STUB_README.md)
  221. echo "FINISHING MKIT CONFIGURATION"
  222. echo "============================"
  223. echo ""
  224. echo "Congratulations, your new project has been configured!"
  225. echo ""
  226. echo "However, the *stub* script is not able to figure out"
  227. echo "everything, so few things still need to be done manually."
  228. echo "This document will guide you throught the rest of the"
  229. echo "process."
  230. echo ""
  231. echo ""
  232. echo "Structure"
  233. echo "---------"
  234. echo ""
  235. echo "First, let's go through the directory structure:"
  236. echo ""
  237. echo " * *src* directory - here is your main place to store"
  238. echo " source files. This includes also documents like user"
  239. echo " manuals---IOW, anything intended to end up on user's"
  240. echo " machine should be uder 'src'."
  241. echo ""
  242. echo " Note that during build time, files named ending with"
  243. echo " '.skel' are subject to token expansion, see mkit.ini"
  244. echo " section below for details."
  245. echo ""
  246. echo " * *notes* directory - here you shall store notes"
  247. echo " intended for people contributing to your project,"
  248. echo " for instance, guidelines, coding style documents,"
  249. echo " TODOs, ideas, plans..."
  250. echo ""
  251. echo " * *utils* directory - here you shall store utilities"
  252. echo " and scripts that will help you with project maintenance,"
  253. echo " and that, unlike software like compilers or versioning"
  254. echo " systems, can (and should) be embedded inside the"
  255. echo " repository."
  256. echo ""
  257. echo " MKit itself is one nice example. :)"
  258. if $MkPackaging; then
  259. echo ""
  260. echo " * *packaging* directory contains templates that enable"
  261. echo " MKit create raw stuffs used to create DEB or RPM"
  262. echo " packages. Similar to '.skel' files in 'src', all files"
  263. echo " here are automatically considered for token expansion,"
  264. echo " no matter how they are named (see mkit.ini section"
  265. echo " below)."
  266. echo ""
  267. echo " NOTE: these templates, as well as any packages created by"
  268. echo " them are intended only for experimental, private use and"
  269. echo " testing."
  270. echo ""
  271. echo " Should you have ambition to create 'real' packages for"
  272. echo " OS distribution such as Debian or Fedora (what a great"
  273. echo " idea!), be prepared that you will need to follow their"
  274. echo " guidelines. This will most probably mean huge changes"
  275. echo " to these packages or even changes to your workflow."
  276. echo ""
  277. echo ""
  278. echo "Placeholders"
  279. echo "------------"
  280. echo ""
  281. echo "At places where *stub* script did not have way to get all"
  282. echo "information automatically, it has inserted placeholders."
  283. echo "You will need to go through all of these placeholders and"
  284. echo "replace them with proper data."
  285. echo ""
  286. echo "Please follow instructions:"
  287. echo ""
  288. echo " 1. Look for placeholders starting with \`MKIT_STUB_\`"
  289. echo " prefix by calling this command:"
  290. echo ""
  291. echo " grep -l MKIT_STUB_ -r"
  292. echo ""
  293. echo " 2. Go through each file and locate the placeholder. (You"
  294. echo " will also see placeholders like \`__MKIT_*__\`, you can"
  295. echo " ignore those."
  296. echo ""
  297. echo " 3. Replace placeholder with appropriate information:"
  298. echo ""
  299. echo " * \`MKIT_STUB_REQUIRES\` - Requirements of your"
  300. echo " project."
  301. echo ""
  302. echo " * \`MKIT_STUB_DESCRIPTION\` - Description of your"
  303. echo " project (few sentences to paragraphs)."
  304. echo ""
  305. echo " * \`MKIT_STUB_FILELIST\` - List of full paths to"
  306. echo " your files after installation."
  307. echo ""
  308. echo " Note that in case of debian/package.install files,"
  309. echo " PREFIX based paths (eg. /usr/bin) in this file should"
  310. echo " should be as if PREFIX was /usr."
  311. echo ""
  312. echo " In case of Fedora-based distro, you should make use"
  313. echo " of RPM macros:"
  314. echo ""
  315. echo " https://fedoraproject.org/wiki/Packaging:RPMMacros"
  316. echo ""
  317. echo " Refer to these documents for further details:"
  318. echo ""
  319. echo " http://rpm-guide.readthedocs.io/"
  320. echo " https://www.debian.org/doc/manuals/maint-guide/"
  321. fi
  322. echo ""
  323. echo ""
  324. echo "mkit.ini"
  325. echo "--------"
  326. echo ""
  327. echo "Most sections still need to be reviewed. In order to do"
  328. echo "that, you will need to understand how MKit works:"
  329. echo ""
  330. echo " 1. First, you need to define *roles* your files will play"
  331. echo " when they are installed on user's file systems. These"
  332. echo " roles then imply where and how the files should be"
  333. echo " deployed."
  334. echo ""
  335. echo " Typical example of a role is e.g. 'bin' for commands"
  336. echo " (normally under '/usr/bin' or '/usr/local/bin'), 'doc'"
  337. echo " for documents or 'lib' for libraries."
  338. echo ""
  339. echo " 2. Next, in \`[roots]\` section, you have to set target"
  340. echo " root directory for each role. However, in order to"
  341. echo " enable people to implement local conventions, it is"
  342. echo " considered a good manner to also respect PREFIX"
  343. echo " environment variable. For this reason, most paths"
  344. echo " need to start with \`[ENV:PREFIX]\`."
  345. echo ""
  346. echo " 3. \`[files]\` section is where you assign actual files"
  347. echo " from your repository to their final paths. The format"
  348. echo " is \`ROLE = REPOPATH [RENAMED]\`, where ROLE is file's"
  349. echo " role, REPOPATH is relative path to the file."
  350. echo ""
  351. echo " Final path is then composed by taking path assigned to"
  352. echo " ROLE and appending file's name. However, if you need"
  353. echo " the deployed file to have different name than in the"
  354. echo " codebase, you can specify the other name as RENAMED."
  355. echo ""
  356. echo " Note that you don't need to address every single file"
  357. echo " individually, if in your repo you have a directory with"
  358. echo " 100 files of the same role, you can add just path to the"
  359. echo " directory itself."
  360. echo ""
  361. echo " 4. If some roles require special permissions on your files,"
  362. echo " \`[modes]\` section is your friend. Permissions here"
  363. echo " should be in UNIX octal format."
  364. echo ""
  365. echo " 5. Next, \`[tokens]\` section allows you to define own"
  366. echo " placeholders that will be replaced when your scripts are"
  367. echo " built. Each file in 'src' directory that is named with"
  368. echo " '.skel' suffix, and each file from 'packaging' directory"
  369. echo " (no matter its name), can contain one or more of tokens"
  370. echo " defined here, plus range of tokens automatically defined"
  371. echo " by MKit. During build, these tokens are replaced with"
  372. echo " their actual values."
  373. echo ""
  374. echo " 6. Less interesting, but important section is \`[dist]\`,"
  375. echo " which lists files in your codebase that will be added"
  376. echo " to distribution tarball (part of \"stuffs\" mentioned"
  377. echo " above). Listing directory here will include all its"
  378. echo " contents, and normally it's OK to be very inclusive, so"
  379. echo " most of the time this section should be OK."
  380. echo ""
  381. echo " 7. Even less interesting is section \`[ENV]\`. It is"
  382. echo " special in that it provides *default* value for an"
  383. echo " environment variable. You almost never need to touch"
  384. echo " this."
  385. echo ""
  386. echo " 8. Finally, the most interesting section! \`[project]\`,"
  387. echo " provides most general information for your project such"
  388. echo " as name and version."
  389. echo ""
  390. echo " Note that the \`version\` key is another \"special"
  391. echo " snowflake\" -- it is now set to 0.0.0, and you **should"
  392. echo " not need** to change it manually. When you feel you"
  393. echo " a are ready to release next version of your evolving"
  394. echo " project, simply call \`make vbump\` and MKit will take"
  395. echo " care of everything!"
  396. if $MkMakefile; then
  397. echo ""
  398. echo ""
  399. echo "Makefile"
  400. echo "--------"
  401. echo ""
  402. echo "*stub* script also created a Makefile for you, but all"
  403. echo "it really does is include MKit's own mkit.mk, which in turn"
  404. echo "only maps \`make\` targets to actual mkit script calls."
  405. echo "Unless your project already uses GNU Make, you should not"
  406. echo "need to change this file."
  407. fi
  408. if $MkReadme; then
  409. echo ""
  410. echo ""
  411. echo "README.md"
  412. echo "---------"
  413. echo ""
  414. echo "Each serious project needs a serious README. Which is why"
  415. echo "*stub* has created a 'stub' of one for you."
  416. fi
  417. echo ""
  418. echo ""
  419. echo "The final touch"
  420. echo "---------------"
  421. echo ""
  422. echo "Once you have reviewed everything, just delete this file!"
  423. ;;
  424. esac >"$tmp"
  425. cat "$tmp" > "$file"
  426. rm "$tmp"
  427. }
  428. known_licenses() {
  429. local key
  430. local first=true
  431. for key in "${!MKIT_STUB_LICENSES[@]}"; do
  432. $first && echo "$key" && continue
  433. echo ", $key"
  434. done
  435. }
  436. usage() {
  437. {
  438. echo "Usage:"
  439. echo " stub [options] new PKGNAME"
  440. echo " stub [options] update"
  441. echo " stub -L"
  442. echo ""
  443. echo "Options:"
  444. echo ""
  445. echo " -c CODENAME your project codename"
  446. echo " -t TAGLINE your project tagline"
  447. echo " -b RELSRC pre-release branch"
  448. echo " -B RELDST post-release branch"
  449. echo " -n NICENAME your project's 'nice' name"
  450. echo " -l LICENSE your options's license (see -L)"
  451. echo " -m MAINT project maintainer's name and e-mail"
  452. echo " -v URL URL to public code browser"
  453. echo " -V VERSION initial version (default: 0.0.0)"
  454. echo " -a enable autoclean ('make clean' after"
  455. echo " each 'make install')"
  456. echo " -g make git commits before and adter"
  457. echo " (implies -y)"
  458. echo " -y don't ask, just do it"
  459. echo " -R skip creating README.md"
  460. echo " -M skip creating Makefile"
  461. echo " -P skip creating packaging templates"
  462. echo " -L list know licenses and exit"
  463. echo ""
  464. echo "PKGNAME should be packaging-friendly name, ie. consist"
  465. echo "only of small letters, numbers, underscore and dash."
  466. echo "For your 'real' name, use NICENAME, which can be any"
  467. echo "string."
  468. } >&2
  469. exit 2
  470. }
  471. confirm() {
  472. local response # user's response to our warning
  473. $Force && return 0
  474. {
  475. echo "Warning: This operation can be destructive for your"
  476. echo "current codebase. At least following files will be"
  477. echo "created or overwritten:"
  478. echo ""
  479. $MkPackaging && echo " * 'packaging' directory (pass -P to avoid)"
  480. $MkMakefile && echo " * 'Makefile' (pass -M to avoid)"
  481. $MkReadme && echo " * 'README.md' (pass -R to avoid)"
  482. $MkLicense && echo " * 'LICENSE.md' (omit -l to avoid)"
  483. echo " * 'mkit.ini'"
  484. echo ""
  485. read -p "Type 'yes' to proceed: " -r response
  486. } >&2
  487. test "$response" == "yes" && return 0
  488. warn "Aborting."
  489. return 1
  490. }
  491. mkcommit_backup() {
  492. git ls-files --others \
  493. | grep -qv -e '^utils/mkit$' -e '^utils/mkit/' \
  494. || { warn "nothing to back up"; return 0; }
  495. git add . || return
  496. git rm -r --cached utils/mkit || return
  497. git commit -m "WIP [mkit/stub] backup" || return
  498. }
  499. mkcommit_mkit_code() {
  500. git ls-files --others \
  501. | grep -q -e '^utils/mkit$' -e '^utils/mkit/' \
  502. || return 0
  503. git add utils/mkit || return
  504. git commit -m "WIP [mkit/stub] Add MKit version v$MKIT_VERSION" || return
  505. }
  506. mkcommit_mkit_conf() {
  507. local msg # commit message (the important art
  508. git add . || return
  509. case $Action in
  510. new) msg="Add MKit configuration stub" ;;
  511. update) msg="Update MKit configuration" ;;
  512. esac
  513. git commit -m "WIP [mkit/stub] $msg" || return
  514. }
  515. deploy_packaging() {
  516. rm -rf packaging
  517. deploy packaging/template.spec
  518. deploy packaging/debian/copyright
  519. deploy packaging/debian/control
  520. deploy packaging/debian/changelog
  521. deploy packaging/debian/compat
  522. deploy packaging/debian/rules
  523. deploy packaging/debian/source/format
  524. deploy packaging/debian/"$PackageName".install
  525. }
  526. init_from_existing() {
  527. #
  528. # Initialize variables from old mkit.ini
  529. #
  530. test -f "$MKIT_INI" \
  531. || die "mkit.ini not found; aborting update: $MKIT_INI"
  532. user_gave Codename || Codename=$(ini_raw1v project:codename)
  533. user_gave License || License=$(ini_raw1v project:license)
  534. user_gave Maintainer || Maintainer=$(ini_raw1v project:maintainer)
  535. user_gave NiceName || NiceName=$(ini_raw1v project:name)
  536. user_gave PackageName || PackageName=$(ini_raw1v project:pkgname)
  537. user_gave RelDst || RelDst=$(ini_raw1v project:reldst)
  538. user_gave RelSrc || RelSrc=$(ini_raw1v project:relsrc)
  539. user_gave Tagline || Tagline=$(ini_raw1v project:tagline)
  540. user_gave VcsBrowser || VcsBrowser=$(ini_raw1v project:vcs_browser)
  541. user_gave Version || Version=$(ini_raw1v project:version)
  542. }
  543. ini_raw1v() {
  544. #
  545. # Read raw scalar from mkit.ini
  546. #
  547. local path=$1
  548. MKIT_INI_EXPAND=0 ini 1value "$path"
  549. }
  550. user_gave() {
  551. #
  552. # True if user gave value to variable $1
  553. #
  554. local var=$1 # name of the variable
  555. { test "${UserGave[$var]}" == 1; } 2>/dev/null
  556. }
  557. updating() {
  558. #
  559. # Are we updating?
  560. #
  561. test "$Action" == update
  562. }
  563. remake_section() {
  564. #
  565. # Re-compose mkit.ini section $1
  566. #
  567. local section=$1
  568. local key
  569. local value
  570. ini lskeys "$section" | grep -q . \
  571. || return 1
  572. echo ""
  573. echo "[$section]"
  574. ini lskeys "$section" \
  575. | while read -r key; do
  576. MKIT_INI_EXPAND=0 ini values "$section:$key" \
  577. | while read -r value; do
  578. echo "$key = $value"
  579. done
  580. done \
  581. | reformat_section
  582. }
  583. reformat_section() {
  584. #
  585. # Re-format "k = v" on stdin as "nice" ini section
  586. #
  587. local key
  588. local eq
  589. local value
  590. while read -r key eq value; do
  591. test "$eq" == "=" || {
  592. warn "ignoring malformed ini line: $key $eq $value"
  593. continue
  594. }
  595. echo "$key = $value"
  596. done \
  597. | column -to' ' \
  598. | sed 's/^/ /'
  599. }
  600. main() {
  601. local NiceName # human-readable project name
  602. local PackageName # machine-safe project (package) name
  603. local RelSrc # pre-release branch
  604. local RelDst # post-release branch
  605. local Codename # release codename
  606. local Tagline # project tagline
  607. local Maintainer # project maintainer (Name + e-mail)
  608. local VcsBrowser # VCS browser (eg. GitHub URL)
  609. local Version=0.0.0 # project version
  610. local AutoClean=false # touch .mkit/autoclean?
  611. local MkCommits=false # create pre/post git commits?
  612. local Force=false # go without asking?
  613. local MkReadme=true # create README.md?
  614. local MkMakefile=true # create Makefile?
  615. local MkPackaging=true # create packaging templates?
  616. local MkLicense=false # create LICENSE.md file
  617. local Action # 'update' to respect existing, 'new' to force
  618. # rewrite incl. MKIT_STUB_* placeholders
  619. declare -A UserGave
  620. while true; do case $1 in
  621. -n) NiceName=$2; UserGave[NiceName]=1; shift 2 || usage ;;
  622. -b) RelSrc=$2; UserGave[RelSrc]=1; shift 2 || usage ;;
  623. -B) RelDst=$2; UserGave[RelDst]=1; shift 2 || usage ;;
  624. -c) Codename=$2; UserGave[Codename]=1; shift 2 || usage ;;
  625. -t) Tagline=$2; UserGave[Tagline]=1; shift 2 || usage ;;
  626. -l) License=$2; UserGave[License]=1; shift 2 || usage ;;
  627. -m) Maintainer=$2; UserGave[Maintainer]=1; shift 2 || usage ;;
  628. -v) VcsBrowser=$2; UserGave[VcsBrowser]=1; shift 2 || usage ;;
  629. -V) Version=$2; UserGave[Version]=1; shift 2 || usage ;;
  630. -M) MkMakefile=false; shift ;;
  631. -R) MkReadme=false; shift ;;
  632. -a) AutoClean=true; shift ;;
  633. -y) Force=true; shift ;;
  634. -g) MkCommits=true; shift ;;
  635. -P) MkPackaging=false; shift ;;
  636. -L) known_licenses | tr , '\n'; exit 0 ;;
  637. -*) usage ;;
  638. *) break ;;
  639. esac done
  640. Action=$1; PackageName=$2
  641. case $Action:$PackageName in
  642. new:) usage ;;
  643. new:*) : ;;
  644. update:) : ;;
  645. update:*) usage ;;
  646. *) usage ;;
  647. esac
  648. updating && init_from_existing
  649. if test -n "$License"; then
  650. known_licenses | grep -qxF "$License" \
  651. || die "unknown license (use -L to get list): $License"
  652. MkLicense=true
  653. fi
  654. if $MkCommits; then
  655. mkcommit_backup || die "failed creating backup commit"
  656. Force=true
  657. fi
  658. confirm || return 1
  659. deploy "$MKIT_INI"
  660. deploy src/"$PackageName".skel
  661. $MkMakefile && deploy Makefile
  662. $MkReadme && deploy README.md
  663. $MkLicense && deploy LICENSE.md
  664. $AutoClean && deploy .mkit/autoclean
  665. $MkPackaging && deploy_packaging
  666. if $MkCommits; then
  667. mkcommit_mkit_code || die "failed creating post-commit"
  668. mkcommit_mkit_conf || die "failed creating post-commit"
  669. fi
  670. deploy MKIT_STUB_README.md
  671. warn "Configuration stub built, follow instructions in"
  672. warn "MKIT_STUB_README.md to finish configuration."
  673. return 0
  674. }
  675. main "$@"