Working Saturnin-based meta-command

stub 27KB

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