Up: Ignore   [Index]


B.2.1 Create and Update Script :dependencies:

This code is a script file to create a new project from this template, and also to update a project with updated scripts. It is tangled into the $DEV/bin directory and is called from the command line as org-template <project> [<author>] [git] or as org-template -u | --update. Its create mode takes one required, and up to two optional arguments. The required argument is the name of the project. One optional argument is the name of the author. The other optional argument is the term ‘git’, meaning to initialize a ‘git’ repository for the project. To update a project, call org-template --update from the project root.

Here are the steps it takes:

  1. It checks for a call to ‘-h|--help’, and if found, prints the USAGE message.
  2. It next checks for ‘-u|--update’, and if found, calls the template-update function. Otherwise, it calls the template-create function.
  3. It creates a new directory in the current working directory using the ‘project’ argument.
  4. It copies this template into it (using the environment variable ‘SYNC_ORG_TEMPLATE’ to find it) as a new Org file using, again, the name of the project.
  5. It then updates the title to the project name, the date, sets the version number to ‘0.0.0’, and optionally adds the author, using the ‘author’ argument if it was given.
  6. It then deletes this script from the new Org project file, as it is not needed by a project file.
  7. It then tangles Boot Makefile and the default README.md into the project.
  8. If the term ‘'git'’ is supplied as an argument, it initializes a new Git repository, creating a basic .gitignore file in it, adding the Org file and the README.md file and finally making an initial Git commit.
  9. Last, it prints an outline of the project’s structure using the tree command.

##+header: :tangle /usr/local/dev/bin/org-template

  1  # org-template: create and update projects
  2  
  3  USAGE='
  4  '"$0..."'
  5  org-template -h | --help
  6  org-template <Project> [<Author>] [git]
  7  org-template -u | --update
  8  '
  9  
 10  template-create () {
 11      mkdir -v "$1"
 12      printf "copy "
 13      cp -v ${SYNC_ORG_TEMPLATE} "$1/$1.org"
 14      printf "${CLEAR}\n"
 15  
 16      sed -i '' -Ee '/^\#\+(title|TITLE):/ s/TITLE/'"$1"'/' \
 17          -Ee '/^\#\+(date|DATE):\s*(.*)$/ s/$2/$(date '"+%F %R"')/' \
 18          -Ee '/^\#\+(macro|MACRO):version Version/ s/[[:digit:].]+/0.0.0/' \
 19          -Ee '/^\#\+(texinfo_printed_title|TEXINFO_PRINTED_TITLE):/ s/PRINTED TITLE/'"$1"'/' \
 20          "$1/$1.org"
 21  
 22      [[ $# -ge 2 ]] && \
 23          sed -i '' -Ee '/^\#\+(author|AUTHOR):/ s/AUTHOR/'"$2"'/' "$1/$1.org"
 24  
 25      printf "${CYAN}"
 26      ${EDITOR} --eval \
 27      "(with-current-buffer (find-file-noselect \"$1/$1.org\")
 28         (save-excursion
 29          (goto-char (point-min))
 30          (re-search-forward \"#[+]name:project-readme$\")
 31          (org-babel-tangle (quote (4)))
 32          (search-forward \"** Create Script\")
 33          (org-cut-subtree)
 34          (save-buffer)
 35          (re-search-forward \"^#[+]name:boot-template$\")
 36          (org-babel-tangle (quote (4)))))"
 37      printf "${CLEAR}\n"
 38  
 39      if [[ (($# -eq 2) || ($# -eq 3)) && (($2 == 'git') || ($3 == 'git')) ]]
 40      then
 41          cd $1 && {
 42              rm *~
 43              printf "${YELLOWBOLD}"
 44              git init
 45              printf "${CLEAR}"
 46  
 47              echo "\
 48  .gitignore
 49  Makefile
 50  *~
 51  .*~
 52  *.texi
 53  *.info
 54  *.html
 55  *-old
 56  tools" > .gitignore
 57  
 58              git add .
 59              git commit -m "Initial commit of Project $1"
 60          }
 61          cd ..
 62      fi
 63      rm "$1"/*~
 64  
 65      printf "${PURPLEBOLD}\n"
 66      pwd
 67      printf "${CLEAR}"
 68      tree -aI .git $1
 69  
 70      return 0
 71  }
 72  
 73  template-update() {
 74      ${EDITOR} -u --eval \
 75               "(progn
 76                 (with-current-buffer (find-file-noselect (getenv \"SYNC_ORG_TEMPLATE\"))
 77                  (save-excursion
 78                   (goto-char (point-min))
 79                   (org-link-search \"#build-tools\")
 80                   (org-copy-subtree 2)))
 81                 (with-current-buffer (car (find-file-noselect \"./*.org\" nil nil t))
 82                  (save-excursion
 83                    (goto-char (point-min))
 84                    (org-link-search \"#build-tools\")
 85                    (org-paste-subtree 1 nil t t)
 86                    (org-cut-subtree 2)
 87                    (save-buffer))))"
 88  
 89      return 0;
 90  }
 91  
 92  main () {
 93      case $1 in
 94          create)
 95  
 96              printf "${PURPLE}"
 97              read -n 1 -p "Create new project: $2 (y/n) ?"
 98              printf "${CLEAR}\n\n"
 99              [[ $REPLY =~ [yY] ]] && template-create "$2" || exit 0
100              ;;
101  
102          update)
103  
104              template-update
105              ;;
106      esac
107      exit 0
108  }
109  
110  
111  if [[ $1 =~ ^-(h|-help)$ ]]; then
112          printf "HELP:\n$USAGE"
113          exit 0
114  fi
115  
116  if [[ $1 =~ ^-(u|-update)$ ]]; then
117          printf "UPDATE:\n"
118          main update
119  fi
120  
121  [[ ($# -ge 1) && ($# -le 3) ]] && { printf "CREATE:\n";  main create $1; } || {
122      printf "${RED}ARGUMENTS ERROR:${CLEAR}$USAGE\n"
123      exit 1
124  }
125  

Up: Ignore   [Index]