aboutsummaryrefslogtreecommitdiff
path: root/share/templater/templater/functions
blob: 4507650701b10362f4d3eb596d9daf7189af2fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
#
# Templater basic functions.
#

# Initialize project
function templater_init {
  if [ ! -d "$PROJECT" ]; then
    templater_echo "Initializing $PROJECT..."
    mkdir -p $PROJECT
  fi
}

# Read a parameter from user
function templater_ask {
  local input
  local function="$1"
  local default="n"
  shift 2

  read -rep "Setup $function? (defaults to $default): " input

  if [ "$input" == "y" ]; then
    templater_$function
  fi
}

# Return list of implementations
function templater_implementations {
  # Do not sort this list: the order in which functions are present in the code is important
  #grep "^function templater_" $PROGRAM | cut -d ' ' -f 2 | sed -e 's/templater_//'
  ls $SHARE
}

# Message
function templater_echo {
  #echo ""
  echo "-> $*"
}

# Checkout to develop branch if available
function templater_checkout_develop {
  if git branch --list develop | grep -q develop; then
    git checkout develop
  fi
}

# Copy or append source file into destination
function templater_copy_or_append {
  local implementation="$1"
  local file="$2"

  if [ -z "$file" ]; then
    return
  fi

  if [ ! -e "$file" ]; then
    cp $SHARE/$implementation/files/$file .
  elif ! grep -q -f $SHARE/$implementation/files/$file $file; then
    cat $SHARE/$implementation/files/$file >> $file
  fi
}

# Read a parameter from user
function templater_user_input {
  local input
  local param="$1"
  local default="$2"
  shift 2

  if echo $param | grep -q 'passwd'; then
    read -s -rep "$* (defaults to $default): " input
  else
    read -rep "$* (defaults to $default): " input
  fi

  if [ -z "$input" ]; then
    export $param="$default"
  else
    export $param="$input"
  fi
}

# Install the global Makefile
function templater_install_makefile {
  if [ -z "$1" ]; then
    return
  fi

  local src="$1"
  local name="`basename $src`"

  # First ensure we have the main Makefile
  if [ ! -e "Makefile" ]; then
    cp $SHARE/templater/files/Makefile .
  fi

  # Then copy the custom Makefile
  if [ ! -e "$name" ]; then
    cp $src .
  fi

  #if ! grep -q "^Makefile.local" .gitignore; then
  #  echo Makefile.local >> .gitignore
  #fi
}

# Check differences
function templater_diff {
  local module="$1"
  local cwd="`pwd`"

  if [ -z "$module" ]; then
    return
  fi

  if [ ! -d "$SHARE/$module/files" ]; then
    return
  fi

  (
  cd $SHARE/$module/files

  find . -type f | while read file; do
    # File exists, check differences
    if [ -e "$cwd/$file" ]; then
      diff -u $cwd/$file $file
    else
      # Use a templaterignore instead
      if echo $file | grep -q -v 'example'; then
        templater_echo "Missing $file"
      fi
    fi
  done
  )
}