Skip to content

Commit

Permalink
optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
leleliu008 committed Sep 18, 2024
1 parent b146504 commit bbefbff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 123 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,12 @@ a typical hierarchical structure under `~/.ndk-pkg` directory looks like below:

```bash
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo master
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo main
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --branch=master
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --branch=main
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --sync
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --sync --branch=master
ndk-pkg formula-repo-add my_repo https://github.com/leleliu008/ndk-pkg-formula-repository-my_repo --sync --branch=main
```

- **delete a existing formula repository**
Expand Down Expand Up @@ -956,9 +960,7 @@ If a ndk-pkg formula repository is `disabled`, which means ndk-pkg would not sea
## ndk-pkg formula repository management
run `ndk-pkg formula-repo-add ` command to create a new formula repository locally from an exsting remote git repository.
run `ndk-pkg formula-repo-init` command to create a new formula repository locally without taking any further action.
run `ndk-pkg formula-repo-add ` command to create a new formula repository locally.
## ndk-pkg official formula repository
Expand Down
126 changes: 22 additions & 104 deletions ndk-pkg
Original file line number Diff line number Diff line change
Expand Up @@ -510,25 +510,20 @@ version_match() {
##############################################################################
# {{{ ndk-pkg formula-repo-add

# __create_a_formula_repository_then_sync_it <REPO-NAME> <REPO-URL> [--branch=VALUE --pin/--unpin --enable/--disable]
__create_a_formula_repository_then_sync_it() {
# __create_a_formula_repository <REPO-NAME> <REPO-URL> [--branch=VALUE --pin/--unpin --enable/--disable --sync]
__create_a_formula_repository() {
[ -z "$1" ] && abort 1 "please specify a repository name."
[ -z "$2" ] && abort 1 "please specify a repository url."

FORMULA_REPO_NAME="$1"
FORMULA_REPO_URL="$2"

if [ -z "$NDKPKG_URL_TRANSFORM" ] ; then
GIT_FETCH_URL="$FORMULA_REPO_URL"
else
GIT_FETCH_URL="$("$NDKPKG_URL_TRANSFORM" "$FORMULA_REPO_URL")"
fi

shift 2

FORMULA_REPO_BRANCH=
FORMULA_REPO_PINNED=
FORMULA_REPO_ENABLED=
FORMULA_REPO_SYNC=

while [ -n "$1" ]
do
Expand All @@ -548,6 +543,9 @@ version_match() {
--disable)
FORMULA_REPO_ENABLED=0
;;
--sync)
FORMULA_REPO_SYNC=1
;;
*) abort 1 "unrecognized argument: $1"
esac
shift
Expand All @@ -571,90 +569,22 @@ version_match() {
run install -d "$SESSION_DIR"
run cd "$SESSION_DIR"

run git -c init.defaultBranch=master init
run git remote add origin "$GIT_FETCH_URL"
run git -c protocol.version=2 fetch --progress origin "+refs/heads/$FORMULA_REPO_BRANCH:refs/remotes/origin/$FORMULA_REPO_BRANCH"
run git checkout --progress --force -B "$FORMULA_REPO_BRANCH" "refs/remotes/origin/$FORMULA_REPO_BRANCH"

cat > .ndk-pkg-formula-repo.yml <<EOF
url: $FORMULA_REPO_URL
branch: $FORMULA_REPO_BRANCH
pinned: $FORMULA_REPO_PINNED
enabled: $FORMULA_REPO_ENABLED
created: $TIMESTAMP_UNIX
EOF

run install -d "$NDKPKG_FORMULA_REPO_ROOT"
run mv "$SESSION_DIR" "$NDKPKG_FORMULA_REPO_ROOT/"
}

# }}}
##############################################################################
# {{{ ndk-pkg formula-repo-init

# __create_a_formula_repository <REPO-NAME> <REPO-URL> [--branch=VALUE --pin/--unpin --enable/--disable]
__create_a_formula_repository() {
[ -z "$1" ] && abort 1 "please specify a repository name."
[ -z "$2" ] && abort 1 "please specify a repository url."

FORMULA_REPO_NAME="$1"
if [ "$FORMULA_REPO_SYNC" = 1 ] ; the
if [ -z "$NDKPKG_URL_TRANSFORM" ] ; then
GIT_FETCH_URL="$FORMULA_REPO_URL"
else
GIT_FETCH_URL="$("$NDKPKG_URL_TRANSFORM" "$FORMULA_REPO_URL")"
fi

if [ -z "$NDKPKG_URL_TRANSFORM" ] ; then
FORMULA_REPO_URL="$2"
run git -c init.defaultBranch=master init
run git remote add origin "$GIT_FETCH_URL"
run git -c protocol.version=2 fetch --progress origin "+refs/heads/$FORMULA_REPO_BRANCH:refs/remotes/origin/$FORMULA_REPO_BRANCH"
run git checkout --progress --force -B "$FORMULA_REPO_BRANCH" "refs/remotes/origin/$FORMULA_REPO_BRANCH"
else
FORMULA_REPO_URL="$("$NDKPKG_URL_TRANSFORM" "$2")"
fi

shift 2

FORMULA_REPO_BRANCH=
FORMULA_REPO_PINNED=
FORMULA_REPO_ENABLED=

while [ -n "$1" ]
do
case $1 in
--branch=*)
FORMULA_REPO_BRANCH="${1#*=}"
;;
--pin)
FORMULA_REPO_PINNED=1
;;
--unpin)
FORMULA_REPO_PINNED=0
;;
--enable)
FORMULA_REPO_ENABLED=1
;;
--disable)
FORMULA_REPO_ENABLED=0
;;
*) abort 1 "unrecognized argument: $1"
esac
shift
done

FORMULA_REPO_BRANCH="${FORMULA_REPO_BRANCH:-master}"
FORMULA_REPO_PINNED="${FORMULA_REPO_PINNED:-1}"
FORMULA_REPO_ENABLED="${FORMULA_REPO_ENABLED:-1}"

FORMULA_REPO_PATH="$NDKPKG_HOME/repos.d/$FORMULA_REPO_NAME"

if [ -d "$FORMULA_REPO_PATH" ] ; then
abort 1 "formula repository '$FORMULA_REPO_NAME' already exists."
run git -c init.defaultBranch=master init
run git remote add origin "$FORMULA_REPO_URL"
fi

printf '%b\n' "${COLOR_PURPLE}==> ndk-pkg formula repository${COLOR_OFF} ${COLOR_GREEN}$FORMULA_REPO_NAME${COLOR_OFF} ${COLOR_PURPLE} is being created${COLOR_OFF}"

SESSION_DIR="$NDKPKG_HOME/run/$$/$FORMULA_REPO_NAME"

run rm -rf "$SESSION_DIR"
run install -d "$SESSION_DIR"
run cd "$SESSION_DIR"

run git -c init.defaultBranch=master init
run git remote add origin "$FORMULA_REPO_URL"

cat > .ndk-pkg-formula-repo.yml <<EOF
url: $FORMULA_REPO_URL
branch: $FORMULA_REPO_BRANCH
Expand Down Expand Up @@ -840,7 +770,7 @@ __sync_available_formula_repositories() {
}

[ -d "$NDKPKG_FORMULA_REPO_ROOT/official-core" ] || {
__create_a_formula_repository_then_sync_it official-core "$NDKPKG_OFFICIAL_FORMULA_REPO_URL"
__create_a_formula_repository official-core "$NDKPKG_OFFICIAL_FORMULA_REPO_URL" --sync
}
}

Expand Down Expand Up @@ -8817,11 +8747,8 @@ ${COLOR_GREEN}ndk-pkg cleanup${COLOR_OFF}
delete the unused cached files.


${COLOR_GREEN}ndk-pkg formula-repo-init <FORMULA-REPO-NAME> <FORMULA-REPO-URL> [--branch=VALUE --pin/--unpin --enable/--disable]${COLOR_OFF}
create a new empty formula repository.

${COLOR_GREEN}ndk-pkg formula-repo-add <FORMULA-REPO-NAME> <FORMULA-REPO-URL> [--branch=VALUE --pin/--unpin --enable/--disable]${COLOR_OFF}
create a new empty formula repository then sync with server.
${COLOR_GREEN}ndk-pkg formula-repo-add <FORMULA-REPO-NAME> <FORMULA-REPO-URL> [--branch=VALUE --pin/--unpin --enable/--disable --sync]${COLOR_OFF}
create a new empty formula repository. If --sync option is given, sync with the server after the formula repo is created.

${COLOR_GREEN}ndk-pkg formula-repo-del <FORMULA-REPO-NAME>${COLOR_OFF}
delete the given formula repository.
Expand Down Expand Up @@ -9506,23 +9433,14 @@ case $1 in
formula-repo-info) shift; __info_the_given_formula_repository "$@" ;;
formula-repo-conf) shift; __conf_the_given_formula_repository "$@" ;;
formula-repo-sync) shift; __sync_the_given_formula_repository "$@" ;;
formula-repo-init)
shift

case $1 in
official-*) abort 1 "ndk-pkg formula repository name that starts with 'official-' is reserved for ndk-pkg official formula repository, please use other name."
esac

__create_a_formula_repository "$@"
;;
formula-repo-add)
shift

case $1 in
official-*) abort 1 "ndk-pkg formula repository name that starts with 'official-' is reserved for ndk-pkg official formula repository, please use other name."
esac

__create_a_formula_repository_then_sync_it "$@"
__create_a_formula_repository "$@"
;;
formula-repo-del)
shift
Expand Down
16 changes: 2 additions & 14 deletions ndk-pkg-zsh-completion
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ _ndk_pkg_actions=(
'search:search all available packages whose name matches the given regular expression pattern.'
'info-available:show information of the given available package.'
'info-installed:show information of the given installed package.'
'formula-repo-init:create a new empty formula repository.'
'formula-repo-add:create a new empty formula repository then sync with server.'
'formula-repo-add:create a new empty formula repository.'
'formula-repo-del:delete the given formula repository from your local cache.'
'formula-repo-sync:sync the given formula repository.'
'formula-repo-conf:change the config of the given formula repository.'
Expand Down Expand Up @@ -90,18 +89,7 @@ function _ndk_pkg() {
'1:formula-repo-name:(name)' \
'2:formula-repo-url:(https://)' \
'--branch=-[specify the formula repo branch]' \
+ '(pin)' \
'--pin[set pinned to 1]' \
'--unpin[set pinned to 0]' \
+ '(enable)' \
'--enable[set enabled to 1]' \
'--disable[set enabled to 0]'
;;
formula-repo-init)
_arguments \
'1:formula-repo-name:(name)' \
'2:formula-repo-url:(https://)' \
'--branch=-[specify the formula repo branch]' \
'--sync[sync with the server after the formula repo is created]' \
+ '(pin)' \
'--pin[set pinned to 1]' \
'--unpin[set pinned to 0]' \
Expand Down

0 comments on commit bbefbff

Please sign in to comment.