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

stub 27KB

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