noosfero-plugins
6.04 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/sh
set -e
program_name=$(basename $0)
if [ -e /etc/default/noosfero ]; then
  . /etc/default/noosfero
else
  this_script=$(readlink -f $0)
  NOOSFERO_DIR=$(dirname $this_script | xargs dirname)
fi
# data
available_plugins_dir="$NOOSFERO_DIR/plugins"
enabled_plugins_dir="$NOOSFERO_DIR/config/plugins"
available_plugins=$(find "$available_plugins_dir" -maxdepth 1 -mindepth 1 -type d -not -name 'template' -printf '%f\n' | sort)
enabled_plugins=$(find -L "$enabled_plugins_dir" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | sort)
disabled_plugins=$(printf "%s\n" $available_plugins $enabled_plugins_dir | sort | uniq -u)
# operation defaults
quiet=false
needs_migrate=false
load_paths="$NOOSFERO_DIR/lib:$(echo $NOOSFERO_DIR/vendor/plugins/*/lib | tr ' ' :)"
_list() {
  for plugin in $available_plugins; do
    echo "$plugin"
  done
}
_status() {
  for plugin in $available_plugins; do
    if [ -h "$enabled_plugins_dir/$plugin" ]; then
      status="*"
    else
      status=" "
    fi
    echo "[$status] $plugin"
  done
}
_usage() {
  echo "$program_name: manages Noosfero plugins system-wide"
  echo
  echo "Usage:"
  echo "  $program_name [OPTIONS] list"
  echo "  $program_name [OPTIONS] status"
  echo "  $program_name [OPTIONS] enable PLUGIN [PLUGIN ...]"
  echo "  $program_name [OPTIONS] disable PLUGIN [PLUGIN ...]"
  echo "  $program_name [OPTIONS] enableall"
  echo "  $program_name [OPTIONS] disableall"
  echo "  $program_name [OPTIONS] new PLUGIN [PLUGIN ...]"
  echo
  echo "Options:"
  echo "  --quiet|-q"
  echo "    Run quietly"
  echo "  --version|-v"
  echo "    Prints version information and exits"
  echo
}
_say(){
  if [ "$quiet" = 'false' ]; then
    echo $@
  fi
}
run(){
  if [ -e "$1" ]; then
    ruby -I$load_paths -e "require '$1'"
    return $?
  fi
}
_enable(){
  plugin="$1"
  cd $enabled_plugins_dir
  source="../../plugins/$plugin"
  target="$enabled_plugins_dir/$plugin"
  run "$source/before_enable.rb"
  if [ -h "$target" ]; then
    _say "$plugin already enabled"
  else
    if [ ! -d "$source" ]; then
      echo "E: $plugin plugin does not exist!"
      return
    fi
    installation_ok=true
    installation_file="$source/install.rb"
    if ! run $installation_file; then
      installation_ok=false
    else
      dependencies_ok=true
      dependencies_file="$source/dependencies.rb"
      if ! run $dependencies_file; then
        dependencies_ok=false
      fi
    fi
    if [ "$installation_ok" = true ] && [ "$dependencies_ok" = true ]; then
      ln -s "$source" "$plugin"
      plugins_public_dir="$NOOSFERO_DIR/public/plugins"
      plugins_features_dir="$NOOSFERO_DIR/features/plugins"
      cd $plugins_public_dir
      test -d "$source/public" && ln -s "$source/public" "$plugin"
      cd $plugins_features_dir
      test -d "$NOOSFERO_DIR/features" && test -d "$source/features" && ln -s "$source/features" "$plugin"
      _say "$plugin enabled"
      run "$source/after_enable.rb"
      needs_migrate=true
    elif [ "$installation_ok" = false ]; then
      echo "W: failed to install $plugin; not enabling"
      echo
    elif [ "$dependencies_ok" = false ]; then
      echo "W: failed to load dependencies for $plugin; not enabling"
      echo
    fi
  fi
}
_disable(){
  plugin="$1"
  source="$available_plugins_dir/$plugin"
  target="$enabled_plugins_dir/$plugin"
  plugins_public_dir="$NOOSFERO_DIR/public/plugins"
  plugins_features_dir="$NOOSFERO_DIR/features/plugins"
  if ! run "$source/before_disable.rb"; then
    echo "W: failed to disabling $plugin"
    echo
  else
    if [ -h "$target" ]; then
      rm "$target"
      test -h "$plugins_public_dir/$plugin" && rm "$plugins_public_dir/$plugin"
      test -h "$plugins_features_dir/$plugin" && rm "$plugins_features_dir/$plugin"
      _say "$plugin disabled"
      run "$source/after_disable.rb"
    else
      _say "$plugin already disabled"
    fi
  fi
}
_new(){
  plugin=$(echo "$1" | tr '[:upper:]' '[:lower:]')
  target="$available_plugins_dir/$plugin"
  if [ -d "$target" ]; then
    _say "There is already a plugin called $plugin"
    exit 1
  else
    template="$available_plugins_dir/template"
    mkdir "$target"
    plugin_name=$(echo "$plugin" | sed -e 's/^./\u&/; s/_\(.\)/\u\1/g')
    for source_file in $(find "$template" -type f); do
      target_file=$(echo "$source_file" | sed -e "s/template/$plugin/g")
      mkdir -p $(dirname "$target_file")
      sed "s/TemplatePlugin/${plugin_name}Plugin/g" "$source_file" > "$target_file"
    done
    _enable "$plugin"
  fi
}
_enableall(){
  for plugin in $available_plugins; do
    _enable "$plugin"
  done
}
_disableall() {
  for plugin in $enabled_plugins; do
    _disable "$plugin"
  done
}
if [ $# -eq 0 ]; then
  _usage
  exit 0
fi
while [ ! -z "$1" ] && [ "${1##-}" != "$1" ]; do
  opt="$1"
  shift
  case "$opt" in
    -q|--quiet)
      quiet=true
      ;;
    -v|--version)
      ruby "-I$NOOSFERO_DIR/lib" -rnoosfero -e "puts \"$program_name version #{Noosfero::VERSION}\""
      exit 0
      ;;
    *)
      echo "Unknown option: $opt"
      _usage
      exit 1
      ;;
  esac
done
command="$1"
if [ -z "$command" ]; then
  _usage
  exit 1
fi
shift
case "$command" in
  enableall|disableall|enable|disable|new)
    if [ ! -w "$enabled_plugins_dir" ]; then
      echo "E: sorry, you don't have the required permissions to manage plugins"
      exit 2
    fi
    ;;
esac
case "$command" in
  list|status|usage|enableall|disableall)
    if [ ! -z "$1" ]; then
      _usage
      exit 1
    fi
    _$command
    ;;
  enable|disable|new)
    for plugin in $@; do
      _$command "$plugin"
    done
    ;;
  *)
    echo "Unknown command: $command"
    _usage
    exit 1
    ;;
esac
if [ "$needs_migrate" = 'true' ] && [ "$quiet" = 'false' ]; then
  cat <<-EOF
====================================================================
To finish the activation of the plugins you have just enabled, you
need to restart Noosfero.
If you installed Noosfero manually, run:
  $ ./script/production restart
If you installed Noosfero using Debian packages, run as root:
  # service noosfero restart
====================================================================
EOF
fi