Skip to content
Snippets Groups Projects
create_documentation 6.88 KiB
#!/bin/zsh
# vim: set shiftwidth=4 softtabstop=4:
#
# This script generates documentation in all submodules.

# It needs:
# - openerp to be installed
#

# Version 20.2.0
here=$(dirname $0)
source $here/common

project_name=${DOC_PROJECT_NAME:-${CI_PROJECT_NAME:-${DRONE_REPO_NAME:-project_name}}}
languages=(${LANGUAGES:-en})

format=html
out_dir=${project_home}/_build/$format

module_error=0
error=0

typeset -a odoo_modules
# Remark: when running the CI, the configuration values from modules might be empty.
# (they are moved in odoo_modules instead)
odoo_modules=("${(@s/ /)$(read_odoo_scripts_expandable_configuration_key modules " ")}")

pushd $project_home

# set addons path
typeset -a addons_path
addons_path=()
for module_path in $odoo_modules ;
do
    module_dir=$(dirname $module_path)
    if (($addons_path[(Ie)$module_dir]));
    then
        # already in
    else
        addons_path+=($module_dir)
    fi
    unset module_dir
done
# Add odoo_modules copied by docker_build_copy and only path available in CI
if [[ -d odoo_modules ]] && [[ -z "$(find odoo_modules -maxdepth 0 -type d -empty 2>/dev/null)" ]]; then
    addons_path+=($(readlink -f odoo_modules))
    typeset -a dirs
    dirs=(odoo_modules/*/)
    dirs=("${dirs[@]%/}")
    for dir in $dirs; do
    odoo_modules+=($(readlink -f $dir))
    done
    unset dirs
fi
export ODOO_ADDONS_PATH=${(j:,:)addons_path}
unset addons_path

# search where to copy modules documentation
for doc_dir in doc docs; do
    if [[ -d "$doc_dir" ]] ; then
        superproject_modules_doc=$project_home/$doc_dir/modules
        [[ -d $superproject_modules_doc ]] || mkdir $superproject_modules_doc
    fi
done

# Generate all modules documentation
for module_path in $odoo_modules ;
do
    if [[ -d "$module_path" ]] ; then
        echo ""
        echo "${INFO} Documentation for ${BOLD_WHITE}$module_path${RESET}"
        pushd $module_path
        module=$(basename $module_path)
        has_doc=""
        for doc_dir in doc docs ; do
            if [[ -d $doc_dir && -e $doc_dir/Makefile ]]; then
                if [ -e ".git" ] || [ -e "../.git" ]; then
                    branch="$(git rev-parse --abbrev-ref HEAD)"
                elif [ -e ".hg" ]; then
                    branch="$(hg identify --branch)"
                else
                    echo "${WARN} Branch name not found"
                    branch="default"
                fi
                echo "${INFO} Branch name: $branch"
                has_doc=True
                echo ${INFO} Makefile found in $module_path/$doc_dir
                pushd $doc_dir
                # TODO read languages value from Makefile (at least available ones)
                for language in $languages ; do
                    make $format LANGUAGE=${language} BUILDDIR=_build
                    if [[ $? -eq 0 ]] ; then
                        dest_dir=$out_dir/$module/$language/$branch
                        echo ${GREEN}OK${RESET}    - Documentation generation done for $module_path/$doc_dir, copy to $dest_dir
                        mkdir -p $dest_dir
                        if [[ -d "_build/$format/$language" ]];
                        then
                            echo "${INFO} found _build/$format/$language/, copying from there"
                            cp --archive --recursive "_build/$format/$language/." "$dest_dir"
                        else
                            # this is only because some modules Makefile are not up to date
                            echo "${INFO} no _build/$format/$language/ found, copying from _build/$format/"
                            cp --archive --recursive "_build/$format/." "$dest_dir"
                        fi
                    else
                        echo ${WARN} Documentation generation failed for $module_path/$doc_dir
                        ((module_error++))
                    fi
                done
                popd
                # copy documentation in super project
                rsync --recursive --copy-links --perms --times --group --owner $doc_dir/ "$superproject_modules_doc/$module"
                # generate autotodo for each module (to update pathes)
                if [[ -e $doc_dir/autotodo.py ]] ; then
                    echo "${INFO} running autotodo again from different path $superproject_modules_doc/$module for ${module_path}"
                    pushd "$superproject_modules_doc/$module"
                    ./autotodo.py $(realpath --relative-to="." "${module_path}") .py TODO,FIXME,XXX
                    popd
                fi
                # TODO translation will not work when copying like that.
            fi
        done
        if [[ -z "$has_doc" ]] ; then
            echo ${WARN} Missing documentation for $module_path
        fi
        popd
    fi
done

# Generate super project documentation
echo ""
echo "${INFO} ${WHITE_BOLD}Super project${RESET} documentation generation"

# determine branch name
if [ -n "$CI_COMMIT_HG_BRANCH" ]; then
    branch="$CI_COMMIT_HG_BRANCH"
elif [ -n "$CI_COMMIT_BRANCH" ]; then
    branch="$CI_COMMIT_BRANCH"
elif [ -e ".git" ]; then
    branch="$(git rev-parse --abbrev-ref HEAD)"
elif [ -e ".hg" ]; then
    branch="$(hg id -b)"
else
    echo "${WARN} Super project branch name not found, using default"
    branch="default"
fi
echo "${INFO} Super project branch name: $branch"

for doc_dir in doc docs; do
    if [[ -d "$doc_dir" ]] ; then
        pushd $doc_dir
        for language in $languages ; do
            # create _build to output warnings into it
            [[ -d _build ]] || mkdir _build/
            make $format LANGUAGE=${language} BUILDDIR=_build SPHINXOPTS="-w _build/warnings.out"
            if [[ $? -eq 0 ]] ; then
                dest_dir=$out_dir/$project_name/$language/$branch
                echo "${INFO} Documentation generation done for $doc_dir (superproject), copy to $dest_dir"
                mkdir -p $dest_dir
                cp --recursive --archive _build/$format/$language/. $dest_dir
            else
                echo "${FATAL} Documentation generation failed for $doc_dir (superproject)"
                ((error++))
            fi
        done
        popd
    fi
done

popd

if [[ $module_error -gt 0 ]]; then
    echo "${WARN} $module_error module(s) with errors"
fi
# create a metrics file
if [[ "$CI" = "true" ]]; then
    echo -e "# HELP odoo_scripts_create_documentation_module_error Number of module in error.\n# TYPE odoo_scripts_create_documentation_module_error gauge\nodoo_scripts_create_documentation_module_error ${module_error}\n" > metrics
    for doc_dir in doc docs; do
        if [[ -e ${doc_dir}/_build/warnings.out ]] ; then
            echo -e "# HELP odoo_scripts_create_documentation_superproject_warnings Number of warnings in superproject.\n# TYPE odoo_scripts_create_documentation_superproject_warnings gauge\nodoo_scripts_create_documentation_superproject_warnings $(grep --count WARNING ${doc_dir}/_build/warnings.out)\n# EOF\n" >> metrics
        fi
    done
fi

exit $error