repos.sh 5.19 KB
#!/bin/bash

# export ADD_REMOTE="NO"
# export REMOTE_BRANCH="8.0"
# export PATH_PREFIX=".repos"
# export PATH_START="addons"
# export SYMLINKS_PATH="odoo-addons"
# export ADHOC_PATH="addons-adhoc"
# export CREATE_LINKS=1
# export DEF_REPO="XX"

# Build environment from env.txt file
while read line; do
	export "$line"
done < env.txt

export GIT_EDITOR=true

read -e -p "Enter a list of github repos, separated by comma; or enter for update: " -i "" ACTION

if [ "$ACTION" != "" ]; then
	ADD_ADDON_REPOS=($(printf "$ACTION" | tr "," "\n"))
	ACTION=""
else
	UPDATE="1"
	i=0
	j=0
	while read line; do
    	if [ "$i" -gt "6" ] && [ "$line" != "" ]; then
    		if [ "$line" != "Unsorted (inserted by repos.sh)" ] && [ "$line" != "===============================" ]; then
    			ADD_ADDON_REPOS[$j]=$line
    			j=$[j+1]
    		fi
    	fi
    	i=$[i+1]
	done < ADD.md
	# set some vars for updating:
	ADD_REMOTE="NO"
	read -e -p "Trying to update $[j+1] repos; enter to continue, input to quit: " -i "" ACTION
fi

if [ "$ACTION" != "" ]; then
	exit 0
else
	printf "...................................................START...................................................\n"
	if [ "$DEF_REPO" ]; then
		if [ "$DEF_REPO" != "XX" ]; then
			printf "set var 'ADD_ADDON_REPOS'\n"
			ADD_ADDON_REPOS=("$DEF_REPO")
		fi
	fi
	if [ ! "$REMOTE_BRANCH" ]; then
		REMOTE_BRANCH="8.0"
	fi
	if [ ! "$SYMLINKS_PATH" ]; then
		SYMLINKS_PATH="odoo-addons"
	fi
	if [ ! "$PATH_START" ]; then
		PATH_START="addons"
	fi
	if [ ! "$PATH_PREFIX" ]; then
		PATH_PREFIX=".repos"
	fi
	if [ ! "$ADD_REMOTE" ]; then
		ADD_REMOTE="YES"
	fi
	if [ ! "$CREATE_LINKS" ]; then
		CREATE_LINKS=1
	fi
	# :' = Multiline Comment
	# ADDED_REPOS=":'\n"
	ADDED_REPOS=""
	NOT_ADDED_REPOS=""
	
	MAIN_BRANCH=${REMOTE_BRANCH}
	
	for i in "${ADD_ADDON_REPOS[@]}"; do
		printf "repo '$i'\n"

		# Defaults
		CREATE_LINKS=1


		j=($(printf "$i" | tr "~!~" "\n"))
		if [ ${j[1]} ]; then
			i=${j[0]}
			if [ ${j[1]} = "CREATE_LINKS=2" ]; then
				CREATE_LINKS=2
			fi
		fi

		j=($(printf "$i" | tr ":" "\n"))
		if [ ${j[1]} ]; then
			i=${j[0]}
			REMOTE_BRANCH=${j[1]}
		fi
		printf "~~~~~~~~~~~~ USING BRANCH: $REMOTE_BRANCH ~~~~~~~~~~~~\n"

		REPO=${i} && REPL="--" && REMOTE=${REPO/\//$REPL}
		if [ "$UPDATE" = "1" ]; then
			git fetch --no-tags remote--${REMOTE}
			SUBTREE_CMD="pull"
			git subtree ${SUBTREE_CMD} --prefix=${PATH_PREFIX}/${REPO} remote--${REMOTE} ${REMOTE_BRANCH} --squash -q

#			while read line; do
#				if [[ ${REPO} == ${line} ]]; then
#					printf "++++++ MATCH ++++++\n"
#					CREATE_LINKS=0
#				fi
#				printf "+++++ NOMATCH +++++ ${REPO} \n"
#				#printf ${REPO}
#			done < ignore.txt

		elif [ "$ADD_REMOTE" = "YES" ]; then
			git remote add -f --no-tags remote--${REMOTE} https://github.com/${REPO}.git
			SUBTREE_CMD="add"
			git subtree ${SUBTREE_CMD} --prefix=${PATH_PREFIX}/${REPO} remote--${REMOTE}/${REMOTE_BRANCH} --squash
		fi
		ADDED_REPOS+=${REPO}
		ADDED_REPOS+='\n'

		if [ "$CREATE_LINKS" != 0 ]; then
			if [ "$CREATE_LINKS" = 1 ]; then
				for j in ${PATH_PREFIX}/${REPO}/* ; do
					if [ -d "$j" ]; then
						n=${j##*/}
						if [ $n != "__unported__" ]; then
							if [ -h ${SYMLINKS_PATH}/$n ]; then
								printf "symlink exists '${REPO}/$n'\n"
								if [ "$ADD_REMOTE" = "YES" ]; then
									NOT_ADDED_REPOS="$NOT_ADDED_REPOS\n${REPO}/$n"
								fi
							else
								printf "create symlink 1: '${REPO}/$n'\n"
								ln -s "../../${PATH_PREFIX}/${REPO}/$n" "${SYMLINKS_PATH}/$n"
							fi
							if [ -h ${ADHOC_PATH}/$n ]; then
								printf "delete adhoc\n"
								rm ${ADHOC_PATH}/$n
							fi
						fi
					fi
				done
			elif [ "$CREATE_LINKS" = 2 ]; then
				printf "create symlink 2: '${REPO}/'\n"
				REPO_SEP=($(printf "$REPO" | tr "/" "\n"))
				if [ ${REPO_SEP[1]} ]; then
					REPO=${REPO_SEP[1]}
					PATH_PREFIX+="/${REPO_SEP[0]}"
				fi
				ln -s "../../${PATH_PREFIX}/${REPO}" "${SYMLINKS_PATH}/${REPO}"
				PATH_PREFIX=".repos"
			elif [ "$CREATE_LINKS" = 3 ]; then
				ln -s ${SYMLINKS_PATH}/${REPO} ${PATH_START}/
			else
				ln -s ${SYMLINKS_PATH}/${REPO} ${PATH_START}/${CREATE_LINKS}
			fi
			CREATE_LINKS=1
		fi
		
		REMOTE_BRANCH=${MAIN_BRANCH}
		
		printf "..................................................DONE.................................................\n\n"
	done

	if [ "$UPDATE" = "1" ]; then
		printf 'Repos updated:\n---------------\n' + "$ADDED_REPOS" + '\n'
	elif [ "$ADDED_REPOS" != "" ]; then
		printf "$ADDED_REPOS\n" >> ADD.md
	fi

	if [ "$NOT_ADDED_REPOS" != "" ]; then
		printf "$NOT_ADDED_REPOS" >> exists.txt
	fi

	find -name "*.pyc" -delete
	find -name "*.pyo" -delete
	find -name "*.pyd" -delete
	find -name "*.py~" -delete
	find -name "*.mo" -delete
	find -name "pip-log.txt" -delete
	find -name "pip-delete-this-directory.txt" -delete
	find -name "Thumbs.db" -delete
	find -name ".directory" -delete
	find -name ".DS_Store" -delete
	find -name ".*.kate-swp" -delete
	find -name ".swp.*" -delete
	find -name "*.~1~" -delete
	find -name "*.egg-info" -delete
	find -name ".installed.cfg" -delete
	find -name "*.egg" -delete
	find -name "*.manifest" -delete
	find -name "*.spec" -delete
	find -name "*.so" -delete
	find -name "__MACOSX" -exec rm -r "{}" \;
	find -name "__pycache__" -exec rm -r "{}" \;
	find -name "develop-eggs" -exec rm -r "{}" \;
fi