ref: master
script/noosfero-plugins
#!/bin/sh set -e program_name=$(basename $0) if [ -e /etc/default/noosfero ]; then . /etc/default/noosfero fi if [ -z "$NOOSFERO_DIR" ]; then 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" base_plugins_dir="$NOOSFERO_DIR/baseplugins" available_plugins=$(find -L "$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) base_plugins=$(find -L "$base_plugins_dir" -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | sort) # 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" -o -h "$base_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 } _install(){ # export so that recursive enables for dependencies inherit this option too export NOOSFERO_BUNDLE_OPTS='install' _enable "$1" } _enable(){ plugin="$1" if [ -d "$available_plugins_dir/$plugin" ]; then source="$available_plugins_dir/$plugin" else if [ ! -d "$plugin" ]; then echo "E: $plugin not found (needs to be an existing directory)" return fi # out-of-tree plugins source="$plugin" plugin=$(basename "$plugin") fi target="$enabled_plugins_dir/$plugin" base="$base_plugins_dir/$plugin" run "$source/before_enable.rb" if [ -h "$target" -o -h "$base" ]; 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 [ -e $source/Gemfile ]; then gemfile=$(mktemp --tmpdir=.) cat $NOOSFERO_DIR/Gemfile $source/Gemfile > $gemfile if [ -z "$NOOSFERO_BUNDLE_OPTS" ]; then NOOSFERO_BUNDLE_OPTS="--local"; fi if ! RUBYOPT='' BUNDLE_GEMFILE="$gemfile" bundle $NOOSFERO_BUNDLE_OPTS --quiet; then dependencies_ok=false else mv "$gemfile".lock Gemfile.lock fi rm -f $gemfile fi if ! run $dependencies_file; then dependencies_ok=false fi fi if [ "$installation_ok" = true ] && [ "$dependencies_ok" = true ]; then ln -s "$source" "$target" plugins_public_dir="$NOOSFERO_DIR/public/plugins" plugins_features_dir="$NOOSFERO_DIR/features/plugins" test -d "$target/public" && ln -s "$target/public" "$plugins_public_dir/$plugin" test -d "$NOOSFERO_DIR/features" && test -d "$target/features" && ln -s "$target/features" "$plugins_features_dir/$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 exit 1 elif [ "$dependencies_ok" = false ]; then echo "W: failed to load dependencies for $plugin; not enabling" echo exit 2 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 [ -h "$base_plugins_dir/$plugin" ]; then _say "$plugin is a base plugin and cannot be disabled" return fi 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 } _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 -and '(' -not -name '*.po' -and -not -name '*.mo' ')'); 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|install|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