diff --git a/README.md b/README.md index 20cd129..a54a891 100644 --- a/README.md +++ b/README.md @@ -19,26 +19,22 @@ ### Install Env ``` +# 中国大陆网络: +wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_ubuntu.sh + +# 其他地区网络: wget https://raw.githubusercontent.com/RT-Thread/env/master/install_ubuntu.sh + chmod 777 install_ubuntu.sh ./install_ubuntu.sh rm install_ubuntu.sh ``` -对于中国大陆用户,请使用以下命令 - -``` -wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_ubuntu.sh -chmod 777 install_ubuntu.sh -./install_ubuntu.sh --gitee -rm install_ubuntu.sh -``` +请根据自身网络地区选择对应的下载地址来下载安装脚本。安装脚本会自动识别网络区域,使用相应镜像下载后续仓库,完成后将仓库远程地址统一设为 GitHub。 ### Prepare Env -PLAN A: Whenever start the ubuntu system, you need to type command `source ~/.env/env.sh` to activate the environment variables. - -or PLAN B: open `~/.bashrc` file, and attach the command `source ~/.env/env.sh` at the end of the file. It will be automatically executed when you log in the ubuntu, and you don't need to execute that command any more. +Run `source ~/.env/env.sh` to activate Env. The script creates and activates its Python virtual environment on first use. To activate Env automatically, add this command to `~/.bashrc`. ### Use Env @@ -63,13 +59,7 @@ set-executionpolicy remotesigned .\install_windows.ps1 ``` -对于中国大陆用户,请使用以下命令: - -```powershell -wget https://gitee.com/RT-Thread-Mirror/env/raw/master/install_windows.ps1 -O install_windows.ps1 -set-executionpolicy remotesigned -.\install_windows.ps1 --gitee -``` +安装脚本会自动识别网络区域,使用相应镜像下载仓库,完成后将仓库远程地址统一设为 GitHub。 注意: @@ -79,14 +69,4 @@ set-executionpolicy remotesigned ### Prepare Env -方案 A:每次重启 PowerShell 时,都需要输入命令 `~/.env/env.ps1`,以激活环境变量。(PLAN A: Each time you restart PowerShell, you need to enter the command `~/.env/env.ps1` to activate the environment variable.) - -方案 B (推荐):打开 `C:\Users\user\Documents\WindowsPowerShell`,如果没有`WindowsPowerShell`则新建该文件夹。新建文件 `Microsoft.PowerShell_profile.ps1`,然后写入 `~/.env/env.ps1` 内容即可,它将在你重启 PowerShell 时自动执行,无需再执行方案 A 中的命令。(or PLAN B (recommended): Open `C:\Users\user\Documents\WindowsPowerShell` and create a new file `Microsoft.PowerShell_profile.ps1`. Then write `~/.env/env.ps1` to the file. It will be executed automatically when you restart PowerShell, without having to execute the command in scenario A.) - -### 常见问题 - -对于中国大陆用户,请注意首次激活 Env 时可能出现错误,这可能是当前网络下使用的镜像(默认清华源)连接失败,修复方法: - -1. 再次进入安装 Env 的目录,运行`.\install_windows.ps1 --gitee`重新安装,并在**安装完成后不要激活 Env**。 -2. 打开 `~/.env/env.ps1` 文件,修改 `python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple` 和 `pip install -i https://pypi.tuna.tsinghua.edu.cn/simple "$PSScriptRoot\tools\scriptse` 中的镜像地址 `https://pypi.tuna.tsinghua.edu.cn/simple` 为其他可用的镜像。 -3. 激活 Env。 \ No newline at end of file +Run `~/.env/env.ps1` to activate Env. The script creates and activates its Python virtual environment on first use. To activate Env automatically, add `~/.env/env.ps1` to your PowerShell profile. \ No newline at end of file diff --git a/env.sh b/env.sh index 5c5280f..428bae4 100644 --- a/env.sh +++ b/env.sh @@ -1 +1,24 @@ -export PATH=~/.env/tools/scripts:$PATH +# shellcheck shell=sh +VENV_ROOT="$HOME/.env/.venv" + +if [ ! -d "$VENV_ROOT" ]; then + echo "Create Python venv for RT-Thread..." + if ! python3 -m venv "$VENV_ROOT"; then + echo "Failed to create Python venv for RT-Thread." + return 1 + fi + if ! "$VENV_ROOT/bin/python" -m pip install --upgrade pip; then + echo "Failed to upgrade pip for RT-Thread." + rm -rf "$VENV_ROOT" + return 1 + fi + if ! "$VENV_ROOT/bin/pip" install "$HOME/.env/tools/scripts"; then + echo "Failed to install RT-Thread dependencies." + rm -rf "$VENV_ROOT" + return 1 + fi +fi + +. "$VENV_ROOT/bin/activate" +export PATH="$HOME/.env/tools/scripts:$PATH" +export RTT_EXEC_PATH=/usr/bin diff --git a/install_arch.sh b/install_arch.sh index cc6fe9f..cde77d7 100755 --- a/install_arch.sh +++ b/install_arch.sh @@ -1,5 +1,16 @@ #!/usr/bin/env bash +TOUCH_ENV_URL=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh +COUNTRY=$(wget -qO- --timeout=3 https://ipinfo.io/country 2>/dev/null) +if [ "$COUNTRY" = "CN" ]; then + TOUCH_ENV_URL=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh +elif [ -z "$COUNTRY" ] && [ -t 0 ]; then + read -r -p "Unable to detect network region. Use Gitee mirror? (y/N, default: GitHub) " use_gitee + if [[ "$use_gitee" =~ ^[Yy]$ ]]; then + TOUCH_ENV_URL=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh + fi +fi + # 函数:从 AUR 安装 rt-thread-env-meta 包 install_from_aur() { echo "正在从 AUR 安装 rt-thread-env-meta 包..." @@ -59,12 +70,7 @@ esac echo "安装完成。" -url=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh -if [ $1 ] && [ $1 = --gitee ]; then - url=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh -fi - -wget $url -O touch_env.sh +wget "$TOUCH_ENV_URL" -O touch_env.sh chmod 777 touch_env.sh -./touch_env.sh $@ +./touch_env.sh rm touch_env.sh diff --git a/install_macos.sh b/install_macos.sh index 6098479..a9ab0c4 100755 --- a/install_macos.sh +++ b/install_macos.sh @@ -66,11 +66,7 @@ if ! [ -x "$(command -v arm-none-eabi-gcc)" ]; then brew install gnu-arm-embedded fi -url=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh -if [ $1 ] && [ $1 = --gitee ]; then - url=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh -fi -curl $url -o touch_env.sh +curl https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh -o touch_env.sh chmod 777 touch_env.sh -./touch_env.sh $@ +./touch_env.sh rm touch_env.sh diff --git a/install_suse.sh b/install_suse.sh index a127718..63a5204 100755 --- a/install_suse.sh +++ b/install_suse.sh @@ -1,17 +1,23 @@ #!/usr/bin/env bash +TOUCH_ENV_URL=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh +COUNTRY=$(wget -qO- --timeout=3 https://ipinfo.io/country 2>/dev/null) +if [ "$COUNTRY" = "CN" ]; then + TOUCH_ENV_URL=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh +elif [ -z "$COUNTRY" ] && [ -t 0 ]; then + read -r -p "Unable to detect network region. Use Gitee mirror? (y/N, default: GitHub) " use_gitee + if [[ "$use_gitee" =~ ^[Yy]$ ]]; then + TOUCH_ENV_URL=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh + fi +fi + sudo zypper update -y -sudo zypper install python3 python3-pip gcc git ncurses-devel cross-arm-none-gcc11-bootstrap cross-arm-binutils qemu qemu-arm qemu-extra -y +sudo zypper install python3 python3-pip python3-venv gcc git ncurses-devel cross-arm-none-gcc11-bootstrap cross-arm-binutils qemu qemu-arm qemu-extra -y python3 -m pip install scons requests tqdm kconfiglib python3 -m pip install -U pyocd -url=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh -if [ $1 ] && [ $1 = --gitee ]; then - url=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh -fi - -wget $url -O touch_env.sh +wget "$TOUCH_ENV_URL" -O touch_env.sh chmod 777 touch_env.sh -./touch_env.sh $@ +./touch_env.sh rm touch_env.sh diff --git a/install_ubuntu.sh b/install_ubuntu.sh index 2d27e97..8174bcf 100755 --- a/install_ubuntu.sh +++ b/install_ubuntu.sh @@ -1,15 +1,21 @@ #!/usr/bin/env bash +TOUCH_ENV_URL=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh +COUNTRY=$(wget -qO- --timeout=3 https://ipinfo.io/country 2>/dev/null) +if [ "$COUNTRY" = "CN" ]; then + TOUCH_ENV_URL=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh +elif [ -z "$COUNTRY" ] && [ -t 0 ]; then + read -r -p "Unable to detect network region. Use Gitee mirror? (y/N, default: GitHub) " use_gitee + if [[ "$use_gitee" =~ ^[Yy]$ ]]; then + TOUCH_ENV_URL=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh + fi +fi + sudo apt-get update -sudo apt-get -qq install python3 python3-pip gcc git libncurses5-dev -y +sudo apt-get -qq install python3 python3-pip python3-venv gcc git libncurses5-dev -y pip install scons requests tqdm kconfiglib pyyaml -url=https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.sh -if [ $1 ] && [ $1 = --gitee ]; then - url=https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.sh -fi - -wget $url -O touch_env.sh +wget "$TOUCH_ENV_URL" -O touch_env.sh chmod 777 touch_env.sh -./touch_env.sh $@ +./touch_env.sh rm touch_env.sh diff --git a/install_windows.ps1 b/install_windows.ps1 index 8ed3b77..23131bb 100644 --- a/install_windows.ps1 +++ b/install_windows.ps1 @@ -29,9 +29,14 @@ if (!$?) { echo "Python environment has installed. Jump this step." } +try { + $useGitee = (Invoke-RestMethod -Uri "https://ipinfo.io/json" -UseBasicParsing -TimeoutSec 3).country -eq "CN" +} catch { + $useGitee = $false +} + $git_url = "https://github.com/git-for-windows/git/releases/download/v2.39.2.windows.1/Git-2.39.2-64-bit.exe" -if ($args[0] -eq "--gitee") { - echo "Use gitee mirror server!" +if ($useGitee) { $git_url = "https://registry.npmmirror.com/-/binary/git-for-windows/v2.39.2.windows.1/Git-2.39.2-64-bit.exe" } @@ -53,7 +58,7 @@ if (!(Test-Command git)) { $PIP_SOURCE = "https://pypi.org/simple" $PIP_HOST = "pypi.org" -if ($args[0] -eq "--gitee") { +if ($useGitee) { $PIP_SOURCE = "http://mirrors.aliyun.com/pypi/simple" $PIP_HOST = "mirrors.aliyun.com" } @@ -116,13 +121,13 @@ if (!$?) { } $url = "https://raw.githubusercontent.com/RT-Thread/env/master/touch_env.ps1" -if ($args[0] -eq "--gitee") { +if ($useGitee) { $url = "https://gitee.com/RT-Thread-Mirror/env/raw/master/touch_env.ps1" } wget $url -O touch_env.ps1 echo "run touch_env.ps1" -./touch_env.ps1 $args[0] +./touch_env.ps1 if ($args.Count -ge 2 -and $args[1] -eq "-y") { echo "Windows Env environment installment has finished. (auto mode, no pause)" diff --git a/touch_env.ps1 b/touch_env.ps1 index c2d3ce9..8189361 100644 --- a/touch_env.ps1 +++ b/touch_env.ps1 @@ -2,8 +2,13 @@ $DEFAULT_RTT_PACKAGE_URL = "https://github.com/RT-Thread/packages.git" $ENV_URL = "https://github.com/RT-Thread/env.git" $SDK_URL = "https://github.com/RT-Thread/sdk.git" -if ($args[0] -eq "--gitee") { - echo "Using gitee service." +try { + $useGitee = (Invoke-RestMethod -Uri "https://ipinfo.io/json" -UseBasicParsing -TimeoutSec 3).country -eq "CN" +} catch { + $useGitee = $false +} + +if ($useGitee) { $DEFAULT_RTT_PACKAGE_URL = "https://gitee.com/RT-Thread-Mirror/packages.git" $ENV_URL = "https://gitee.com/RT-Thread-Mirror/env.git" $SDK_URL = "https://gitee.com/RT-Thread-Mirror/sdk.git" @@ -12,8 +17,9 @@ if ($args[0] -eq "--gitee") { $env_dir = "$HOME\.env" if (Test-Path -Path $env_dir) { - $option = Read-Host ".env directory already exists. Would you like to remove and recreate .env directory? (Y/N) " option -} if (( $option -eq 'Y' ) -or ($option -eq 'y')) { + $option = Read-Host ".env directory already exists. Would you like to remove and recreate .env directory? (Y/N) " +} +if (( $option -eq 'Y' ) -or ($option -eq 'y')) { Get-ChildItem $env_dir -Recurse | Remove-Item -Force -Recurse rm -r $env_dir } @@ -29,6 +35,9 @@ if (!(Test-Path -Path $env_dir)) { echo 'source "$PKGS_DIR/packages/Kconfig"' | Out-File -FilePath $env_dir/packages/Kconfig -Encoding ASCII git clone $SDK_URL $env_dir/packages/sdk --depth=1 git clone $ENV_URL $env_dir/tools/scripts --depth=1 + git -C $env_dir/packages/packages remote set-url origin https://github.com/RT-Thread/packages.git + git -C $env_dir/packages/sdk remote set-url origin https://github.com/RT-Thread/sdk.git + git -C $env_dir/tools/scripts remote set-url origin https://github.com/RT-Thread/env.git copy $env_dir/tools/scripts/env.ps1 $env_dir/env.ps1 } else { echo ".env folder has exsited. Jump this step." diff --git a/touch_env.sh b/touch_env.sh index c35dc4d..d87121a 100755 --- a/touch_env.sh +++ b/touch_env.sh @@ -4,8 +4,7 @@ DEFAULT_RTT_PACKAGE_URL=https://github.com/RT-Thread/packages.git ENV_URL=https://github.com/RT-Thread/env.git SDK_URL="https://github.com/RT-Thread/sdk.git" -if [ $1 ] && [ $1 = --gitee ]; then - gitee=1 +if [ "$(wget -qO- --timeout=3 https://ipinfo.io/country 2>/dev/null)" = "CN" ]; then DEFAULT_RTT_PACKAGE_URL=https://gitee.com/RT-Thread-Mirror/packages.git ENV_URL=https://gitee.com/RT-Thread-Mirror/env.git SDK_URL="https://gitee.com/RT-Thread-Mirror/sdk.git" @@ -29,5 +28,12 @@ if ! [ -d $env_dir ]; then echo 'source "$PKGS_DIR/packages/Kconfig"' >$env_dir/packages/Kconfig git clone $SDK_URL $env_dir/packages/sdk --depth=1 git clone $ENV_URL $env_dir/tools/scripts --depth=1 - echo -e 'export PATH=`python3 -m site --user-base`/bin:$HOME/.env/tools/scripts:$PATH\nexport RTT_EXEC_PATH=/usr/bin' >$env_dir/env.sh + git -C $env_dir/packages/packages remote set-url origin https://github.com/RT-Thread/packages.git + git -C $env_dir/packages/sdk remote set-url origin https://github.com/RT-Thread/sdk.git + git -C $env_dir/tools/scripts remote set-url origin https://github.com/RT-Thread/env.git + if ! cp $env_dir/tools/scripts/env.sh $env_dir/env.sh; then + echo "Failed to set up Env activation script." + rm -rf $env_dir + exit 1 + fi fi