Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom installation destination #32

Merged
merged 3 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ RUN bash ./scripts/install.sh

ENV http_proxy="" https_proxy=""

ENV PATH="/root/.cache/homecli/miniconda/bin:${PATH}"
ENV PATH="/root/.homecli/miniconda/bin:${PATH}"

WORKDIR /workspace
VOLUME ["/workplace"]
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ My Personal Home Directory.

install
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/install.sh)"
HOMECLI_INSTALL_DIR=$HOME/.homecli /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/install.sh)"
```
update
```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/update.sh)"
HOMECLI_INSTALL_DIR=$HOME/.homecli /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/update.sh)"
```


uninstall

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/uninstall.sh)"
HOMECLI_INSTALL_DIR=$HOME/.homecli /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/uninstall.sh)"
```

pack

```bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/pack.sh)"
HOMECLI_INSTALL_DIR=$HOME/.homecli /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/BrightXiaoHan/HOME/main/scripts/pack.sh)"
```

unpack
Expand All @@ -34,7 +34,7 @@ unpack
* Run `install.sh`

```bash
bash install.sh unpack home-cli.tar
HOMECLI_INSTALL_DIR=$HOME/.homecli bash install.sh unpack homecli.tar.gz
```

### macos
Expand Down
19 changes: 12 additions & 7 deletions general/fish/config-linux.fish
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
set -l INSTALL_DIR (set -q HOMECLI_INSTALL_DIR; and echo $HOMECLI_INSTALL_DIR; or echo $HOME/.homecli)

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
if test -f ~/.cache/homecli/miniconda/bin/conda
eval ~/.cache/homecli/miniconda/bin/conda "shell.fish" "hook" $argv | source
if test -f $INSTALL_DIR/miniconda/bin/conda
eval $INSTALL_DIR/miniconda/bin/conda "shell.fish" "hook" $argv | source
end
# <<< conda initialize <<<

set -gx PATH ~/.cache/homecli/bin $PATH
set -gx PATH $INSTALL_DIR/bin $PATH
# Mamba
set -gx MAMBA_ROOT_PREFIX ~/.cache/homecli/miniconda
set -gx MAMBA_ROOT_PREFIX $INSTALL_DIR/miniconda

# pipx
set -gx PIPX_HOME $INSTALL_DIR/pipx

# pyenv
set -gx PYENV_ROOT ~/.cache/homecli/pyenv
set -gx PYENV_ROOT $INSTALL_DIR/pyenv
set -gx PATH $PYENV_ROOT/bin $PATH
# if pyenv exists, initialize it
if test -f $PYENV_ROOT/bin/pyenv
Expand All @@ -27,5 +32,5 @@ if not nvim --headless -c quit > /dev/null 2>&1
alias nvim='nvim --appimage-extract-and-run'
end

set -gx CC ~/.cache/homecli/miniconda/bin/gcc
set -gx CXX ~/.cache/homecli/miniconda/bin/g++
set -gx CC $INSTALL_DIR/miniconda/bin/gcc
set -gx CXX $INSTALL_DIR/miniconda/bin/g++
10 changes: 5 additions & 5 deletions general/fish/config.fish
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ alias ip="curl http://ip-api.com/json/?lang=zh-CN"
alias sethttpproxy="set HTTPS_PROXY 'http://127.0.0.1:7890'"
alias unsethttpproxy="set -e HTTPS_PROXY"

set LOCAL_CONFIG (dirname (status --current-filename))/config-local.fish
if test -f $LOCAL_CONFIG
source $LOCAL_CONFIG
end

switch (uname)
case Darwin
source (dirname (status --current-filename))/config-osx.fish
case Linux
source (dirname (status --current-filename))/config-linux.fish
end

set LOCAL_CONFIG (dirname (status --current-filename))/config-local.fish
if test -f $LOCAL_CONFIG
source $LOCAL_CONFIG
end

# if starship and zoxide installed, init them
if type starship > /dev/null 2>&1
starship init fish | source
Expand Down
5 changes: 4 additions & 1 deletion homecli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
PLATFORM = platform.system()
ARCHITECTURE = platform.machine()

CACHE_DIR = os.path.join(os.path.expanduser("~"), ".cache", __app_name__)
if os.getenv("HOMECLI_INSTALL_DIR"):
CACHE_DIR = os.getenv("HOMECLI_INSTALL_DIR")
else:
CACHE_DIR = os.path.join(os.path.expanduser("~"), "." + __app_name__)
BIN_DIR = os.path.join(CACHE_DIR, "bin")
os.makedirs(BIN_DIR, exist_ok=True)

Expand Down
7 changes: 7 additions & 0 deletions homecli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile
import urllib.request
Expand Down Expand Up @@ -38,6 +39,7 @@ def download_with_progress(url, path, name=""):
for data in response.iter_content(chunk_size=chunk_size):
f.write(data)
progress(f.tell(), total_size, name)
sys.stderr.write("\n")

except ImportError:

Expand All @@ -52,6 +54,7 @@ def download_with_progress(url, path, name=""):
break
f.write(chunk)
progress(f.tell(), total_size, name)
sys.stderr.write("\n")


def install_neovim(overwrite=True):
Expand Down Expand Up @@ -235,6 +238,9 @@ def install_conda():
check=True,
env=env,
)

env = os.environ.copy()
env["PIPX_HOME"] = os.path.join(CACHE_DIR, "pipx")
for package in ["rich-cli", "git+https://github.com/BrightXiaoHan/ssr-command-client.git@socks2http", "mycli", "mdformat"]:
subprocess.run(
[
Expand All @@ -244,6 +250,7 @@ def install_conda():
package,
],
check=True,
env=env,
)
logging.info("Installing other packages done.")

Expand Down
59 changes: 36 additions & 23 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
# MODE: local-install, online-install or unpack. Default: online-install
MODE=${1:-online-install}
INSTALL_DIR=${HOMECLI_INSTALL_DIR:-$HOME/.homecli}

if [ "$MODE" = "local-install" ]; then
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
DIR="$HOME/.cache/homecli/HOME/general"
mkdir -p ~/.cache/homecli/HOME
cp -r $CWD/.. ~/.cache/homecli/HOME
cd ~/.cache/homecli/HOME
DIR="$INSTALL_DIR/HOME/general"
mkdir -p $INSTALL_DIR/HOME
cp -r $CWD/.. $INSTALL_DIR/HOME
cd $INSTALL_DIR/HOME

elif [ "$MODE" = "unpack" ]; then
TARFILE="$2"
DESTINATION=$HOME/.cache
if [ -z "$DESTINATION" || -z "$TARFILE" ]; then
if [ -z "$TARFILE" ]; then
echo "Usage: install.sh unpack <tarfile>"
exit 1
fi
mkdir -p $DESTINATION/homecli
tar -xvf "$TARFILE" -C "$DESTINATION/homecli"
mkdir -p $DESTINATION/homecli/miniconda
tar -xvf $DESTINATION/homecli/miniconda.tar.gz -C $DESTINATION/homecli/miniconda
DIR="$DESTINATION/homecli/HOME/general"
mkdir -p $INSTALL_DIR
tar -xvf "$TARFILE" -C "$INSTALL_DIR"
mkdir -p $INSTALL_DIR/miniconda
tar -xvf $INSTALL_DIR/miniconda.tar.gz -C $INSTALL_DIR/miniconda
DIR="$INSTALL_DIR/HOME/general"
elif [ "$MODE" = "online-install" ]; then
DIR="$HOME/.cache/homecli/HOME/general"
mkdir -p ~/.cache/homecli
git clone https://github.com/BrightXiaoHan/HOME ~/.cache/homecli/HOME
cd ~/.cache/homecli/HOME
DIR="$INSTALL_DIR/HOME/general"
mkdir -p $INSTALL_DIR
git clone https://github.com/BrightXiaoHan/HOME $INSTALL_DIR/HOME
cd $INSTALL_DIR/HOME
elif [ "$MODE" = "relink" ]; then
DIR="$INSTALL_DIR/HOME/general"
else
echo "Usage: install.sh <mode> [tarfile]"
echo "mode: local-install, online-install, unpack or relink (local-install is default)"
exit 1
fi

# get current dir
Expand Down Expand Up @@ -89,21 +95,23 @@ ln -s $DIR/mambarc ~/.mambarc

if [ "$MODE" = "local-install" ] || [ "$MODE" = "online-install" ]; then
PYTHONPATH="./:$PYTHONPATH" \
PATH="$HOME/.cache/homecli/miniconda/bin:$HOME/.cache/homecli/nodejs/bin:$PATH" \
PATH="$INSTALL_DIR/miniconda/bin:$INSTALL_DIR/nodejs/bin:$PATH" \
python3 homecli/install.py
curl https://pyenv.run | PYENV_ROOT="${HOME}/.cache/homecli/pyenv" bash
curl https://pyenv.run | PYENV_ROOT="$INSTALL_DIR/pyenv" bash
mv $HOME/.local/share/nvim $INSTALL_DIR/nvim
ln -s $INSTALL_DIR/nvim $HOME/.local/share/nvim
elif [ "$MODE" = "unpack" ]; then
mkdir -p ~/.local/share && ln -s $DESTINATION/homecli/nvim/ ~/.local/share/nvim
source $DESTINATION/homecli/miniconda/bin/activate
mkdir -p ~/.local/share && ln -s $INSTALL_DIR/nvim/ ~/.local/share/nvim
source $INSTALL_DIR/miniconda/bin/activate
CRYPTOGRAPHY_OPENSSL_NO_LEGACY=1 conda unpack

# Re-link broken symlinks
for file in $(find $HOME -type l ! -exec test -e {} \; -print); do
old=$(readlink $file)
# Re-link to the new location with $HOME prefix
# e.g.> /root/.cache/homecli/xxx -> $HOME/.cache/homecli/xxx
# e.g.> /root/.homecli/xxx -> /home/hanbing/.homecli/xxx

# extract str after .cache/homecli
# extract str after .homecli
if [[ $old == *".local/share/nvim"* ]]; then
prefix=$(echo $old | sed 's/\.local\/share\/nvim.*//')
# replace prefix with $HOME
Expand All @@ -112,9 +120,14 @@ elif [ "$MODE" = "unpack" ]; then
ln -s $new $file
fi
done
elif [ "$MODE" = "relink" ]; then
ln -s $INSTALL_DIR/nvim/ ~/.local/share/nvim
fi

# add fish path to .bashrc
if ! grep -q 'export PATH=$HOME/.cache/homecli/miniconda/bin:$PATH' ~/.bashrc; then
echo 'export PATH=$HOME/.cache/homecli/miniconda/bin:$PATH' >> ~/.bashrc
if ! grep -q "export PATH=$INSTALL_DIR/miniconda/bin:\$PATH" ~/.bashrc; then
echo "export PATH=$INSTALL_DIR/miniconda/bin:\$PATH" >> ~/.bashrc
fi

# add HOMECLI_INSTALL_DIR to config-local.fish
echo "set -gx HOMECLI_INSTALL_DIR $INSTALL_DIR" > ~/.config/fish/config-local.fish
10 changes: 3 additions & 7 deletions scripts/pack.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DIR=${HOMECLI_INSTALL_DIR:-$HOME/.homecli}
OUTFILE=${1:-homecli.tar.gz}
DIR=~/.cache/homecli

source $DIR/miniconda/bin/activate
$DIR/miniconda/bin/conda-pack -o $DIR/miniconda.tar.gz
Expand All @@ -10,10 +10,6 @@ echo $CURDIR
OUTFILE=${1:-homecli.tar.gz}
cd $DIR
tar -cvf $CURDIR/$OUTFILE \
HOME bin miniconda.tar.gz pyenv
HOME bin miniconda.tar.gz pyenv pipx nvim
rm miniconda.tar.gz
cd -

cd ~/.local/share/
tar -rvf $CURDIR/$OUTFILE nvim/
cd -
cd -
17 changes: 14 additions & 3 deletions scripts/uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
INSTALL_DIR=${HOMECLI_INSTALL_DIR:-$HOME/.homecli}
REMOVE_CACHE=${1:-true}

# remove config files
rm -rf ~/.config/alacritty \
~/.config/nvim \
Expand All @@ -12,7 +15,15 @@ rm -rf ~/.config/alacritty \
rm -rf ~/.local/share/nvim

# remove cache
rm -rf ~/.cache/homecli
if [ "$REMOVE_CACHE" = "true" ]; then
rm -rf $INSTALL_DIR
elif [ "$REMOVE_CACHE" = "false" ]; then
echo "remove cache skipped"
else
echo "invalid argument: $REMOVE_CACHE (should be true or false)"
echo "usage: $0 [true|false]"
exit 1
fi

# remove export PATH=$HOME/.cache/homecli/miniconda/bin:$PATH' from .bashrc
sed -i '/export PATH=$HOME\/.cache\/homecli\/miniconda\/bin:$PATH/d' ~/.bashrc
# remove export PATH=$INSTALL_DIR/bin:$PATH' from .bashrc
sed -i '/export PATH=.*homecli\/miniconda\/bin:$PATH/d' ~/.bashrc
5 changes: 3 additions & 2 deletions scripts/update.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
DIR=~/.cache/homecli/HOME
DIR=${HOMECLI_INSTALL_DIR:-$HOME/.homecli}

cd $DIR
cd $DIR/HOME

git pull
PYTHONPATH="./:$PYTHONPATH" python homecli/install.py -c update
mamba update --all