setup_dev.ps1

发布时间 2023-06-20 19:56:00作者: Ronald Hu

##############################################################################

千里业务windows开发环境安装脚本

##############################################################################

note: 如果在运行中遇到安装权限问题:

以管理员身份打开powershell,并运行如下命令设置安装权限

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

TODO:

1, 提示界面,用户可选择需安装的组件

2,检查系统中已安装的组件,提升用户是否覆盖

3,安装完检查系统是否工作,并打印对于组件的版本号

$ErrorActionPreference = "Inquire"

$current_dir = Split-Path -Parent \(MyInvocation.MyCommand.Definition cd "\)current_dir"
\(install_dir = "D:\msys64_longsight" \)user_name = "longsight"

function Init-Msys {
Write-Host "Installing MSYS2..." -ForegroundColor Cyan

if(Test-Path "$install_dir") {
    Remove-Item "$install_dir" -Recurse -Force
}

# 下载msys2的自安装包到当前目录
Write-Host "Downloading MSYS2 SFX package..."
$msys_url = "http://longsight.inhuawei.com/download/tools/msys2-base-x86_64-20220603.sfx.exe"
$msys_installer = "$current_dir/msys2.exe"
(New-Object System.Net.WebClient).DownloadFile("$msys_url", "$msys_installer")
Write-Host "Extracting MSYS2 SFX pacakge and copy folder to $install_dir ..."
.\msys2.exe -y -o"$current_dir" # Extract to .\msys64
Remove-Item "$msys_installer"  # Delete the archive again
Move-Item .\msys64 "$install_dir" # Move to target folder

Write-Host "Modifying Configuration files of MSYS2 enviromnent..."
# 修改msys2的默认用户名为dev
(Get-Content -Path "$install_dir\etc\nsswitch.conf") -Replace "db_home: cygwin desc", "db_home: /home/$user_name" | Set-Content -Path "$install_dir\etc\nsswitch.conf"
# 更新pacman的镜像网站
Copy-Item .\pacman.conf -Destination "$install_dir\etc\pacman.conf" -Force
Copy-Item .\pacman.d -Recurse -Destination "$install_dir\etc" -Force
Add-Content "$install_dir\etc\profile.d\lang.sh" "export LANG=C"

}

function bash($command) {
Write-Host $command -NoNewline
D:\msys64_longsight\usr\bin\sh.exe --login -c $command
Write-Host " -OK" -ForegroundColor Green
}

function Update-Msys {
# Run for the first time
Write-Host "Initializing MSYS2 bash..."
bash ' '

Write-Host "Updating MSYS2 package..."
bash 'pacman --noconfirm -Syuu'  # Core update (in case any core packages are outdated)
bash 'pacman --noconfirm -Syuu'  # Normal update

}

function Setup-Tools {
Write-Host "Installing development tools..."
bash 'pacman --sync --noconfirm base-devel'
bash 'pacman --sync --noconfirm binutils'
bash 'pacman --sync --noconfirm vim'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-gcc-libs'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-gcc'
bash 'pacman --sync --noconfirm msys/git'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-git-lfs'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-cmake'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-ninja'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-python-pip'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-doxygen'
bash 'pacman --sync --noconfirm mingw-w64-x86_64-graphviz'
bash 'pacman --sync --noconfirm msys/zsh'

bash '/mingw64/bin/pip config set global.index-url http://mirrors.tools.huawei.com/pypi/simple'
bash '/mingw64/bin/pip config set install.trusted-host mirrors.tools.huawei.com'
bash '/mingw64/bin/pip install Sphinx'
bash '/mingw64/bin/pip install sphinx-rtd-theme'
bash '/mingw64/bin/pip install sphinx-sitemap'
bash '/mingw64/bin/pip install breathe'
bash '/mingw64/bin/pip install myst-parser'
bash '/mingw64/bin/pip install conan==1.52.0'

# 配置zsh为默认的shell
(Get-Content -Path "$install_dir\msys2_shell.cmd") -Replace 'LOGINSHELL=bash', 'LOGINSHELL=zsh' | Set-Content -Path "$install_dir\msys2_shell.cmd"
(Get-Content -Path "$install_dir\msys2_shell.cmd") -Replace 'rem set MSYS=winsymlinks:nativestrict', 'set MSYS=winsymlinks:nativestrict' | Set-Content -Path "$install_dir\msys2_shell.cmd"
(Get-Content -Path "$install_dir\msys2_shell.cmd") -Replace 'rem set MSYS2_PATH_TYPE=inherit', 'set MSYS2_PATH_TYPE=inherit' | Set-Content -Path "$install_dir\msys2_shell.cmd"
$call_vcvars = 'call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"'
(Get-Content -Path "$install_dir\msys2_shell.cmd") -Replace "@echo off", "@echo off`n$call_vcvars" | Set-Content -Path "$install_dir\msys2_shell.cmd"
Add-Content "$install_dir\msys2.ini" "SHELL=/usr/bin/zsh"
Write-Host "MSYS2 installed" -ForegroundColor Green

}

function Setup-Dotfiles {
Write-Host "Copy dotfiles to new development enviroment..."
Copy-Item -Path .\dotfiles* -Destination "$install_dir\home$user_name" -Recurse
}

function Setup-Conan {
Copy-Item -Path .\conan.zip -Destination "$install_dir\home$user_name"
bash '/mingw64/bin/conan config install ./conan.zip'
bash '/mingw64/bin/conan user -p Huawei@tesgine123 -r conan-tesgine ate-ax'
}

function Create-Installer {
7z a -sfx -m0=lzma2 -mfb=64 -md=32m -ms=on -mx=9 msys2-longsight-sfx.exe $install_dir
}

function Install-VSCommunity {
Write-Host "Installing VSCommunity 2022..." -ForegroundColor Cyan

# 下载VSCommunity的自安装包到当前目录
Write-Host "Downloading VSCommunity install package..."
$vs_url = "http://longsight.inhuawei.com/download/tools/vs_local.exe"
$vs_boostrap = "$current_dir/vs_local.exe"
(New-Object System.Net.WebClient).DownloadFile("$vs_url", "$vs_boostrap")
Write-Host "Download VSCommunity pacakge done" -ForegroundColor Green
.\vs_local.exe -y  # Extract to .\vs_local

$vs_download = "$current_dir/vs_local/"
$vs_installer = "$vs_download/vs_setup.exe"

$workload_argument = @(
	'--add Microsoft.VisualStudio.Workload.NativeDesktop',
	'--add Microsoft.VisualStudio.Component.VC.Llvm.Clang',
	'--add Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset'
) 

$options_workload          = [string]::Join(" ", $workload_argument )
$options_quiet              = "--quiet"
$options_layout             = "--layout $vs_download"
$optionsIncludeRecommended = "--includeRecommended"
$vs_layout_options = @(
	$options_layout,
	$options_workload,
	"--lang En-us",
	"--includeRecommended",
	"--quiet",
	"--wait"
)
$vs_install_options = @(
	$options_workload,
	"--locale En-us",
	"--includeRecommended",
	"--wait",
	"--passive"
)

Write-Host "Start Installing VSCommunity..."
$process = Start-Process -FilePath "$vs_installer" -ArgumentList $vs_install_options -Wait -PassThru
Write-Host "Install VSCommunity Done" -ForegroundColor Green

#Remove-Item "$vs_boostrap"  # Delete the archive again
#Remove-Item "$vs_download"  # Delete the archive again
Write-Host "VSCommunity 2022 installed" -ForegroundColor Green

}

Init-Msys
Update-Msys
Setup-Tools
Setup-Dotfiles
Setup-Conan
Install-VSCommunity

Create-Installer

Write-Host "Done!!!" -ForegroundColor Green