一、為什么要編譯源代碼?
當然是為了進一步深入了解asp.net core,比如我最近對身份認證組件比較感興趣,網上看了幾篇文章沒有什么收獲的時候,那么直接上源碼,進行運行調試。
學習交流群:307564339
二、下載apsnetcore源代碼
1.下載到本地
#下載項目
git clone --recursive https://github.com/dotnet/aspnetcore
# 進入到項目
cd aspnetcore
2.查看標簽
git tag
1.0.0 . . . v5.0.4
3.切換到v5.0.2
git checkout v5.0.2
4.下載子模塊
git submodule update --init --recursive
三、還原和構建
1.微軟構建說明
請閱讀這篇文章查看自己的系統、環境等最低要求
https://github.com/dotnet/aspnetcore/blob/main/docs/BuildFromSource.md
2.下載JDK和Node
1.安裝jdk
https://www.oracle.com/java/technologies/javase-jdk15-downloads.html
2.設置環境變量
JAVA_HEOM=C:\Program Files\Java\jdk-15.0.2
PAHT=;%JAVA_HOME%\bin
3.安裝NodeJs
https://nodejs.org/zh-cn/
3.安裝yarn
npm install -g yarn
4.修改一些項目的編碼問題
打開aspnetcore\src\Servers\IIS\IISIntegration.slnf查看警告
雙擊警告,把這些文件重新另存為:文件-xxx.cpp另存為-保存(三角箭頭)-編碼保存-使用Unicode(utf-8帶簽名的方式保存,不行就換GB2312)
編譯CommonLibTests不保存,大功告成!!!
修改:aspnetcore\src\Razor\Razor\test\TagHelpers\DefaultTagHelperContentTest.cs文件
修改aspnetcore/eng/Versions.props,去掉版本后面的“servicing.20611.20”
然后重新運行/aspnetcore/build.cmd
5.下載sdk
修改/aspnetcore/build.cmd文件
@ECHO OFF SETLOCAL PowerShell -NoProfile -NoLogo -ExecutionPolicy Bypass -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = ''; try { & '%~dp0build.ps1' %*; exit $LASTEXITCODE } catch { write-host $_; exit 1 }"
# 在EXIT命令之前加上PAUSE,便於調試
PAUSE
SET exit_code=%ERRORLEVEL%
ECHO build.cmd completed
EXIT /b %exit_code%
進入到項目目錄以管理員身份運行"aspnetcore/build.cmd"批處理文件
WARNING: No default group of projects was specified, so building the managed and native projects and their dependencies. Run `build.cmd -help` for more details. Building of NodeJS projects is disabled since node is not detected on Path and no BuildNodeJs or NoBuildNodeJs setting is set explicitly. WARNING: Some managed projects depend on NodeJS projects. Building NodeJS is disabled so the managed projects will fallback to using the output from previous builds. The output may not be correct or up to date. GET https://dot.net/v1/dotnet-install.ps1 dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where: dotnet-install: - The SDK needs to be installed without user interaction and without admin rights. dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs. dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer. dotnet-install: Downloading primary link https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.103/dotnet-sdk-5.0.103-win-x64.zip
會發現代碼卡住不動了,因為這個項目是多個小組開發,使用的sdk也有許多,比如java和node環境等等,甚至是不同的sdk版本,我們可以通過復制上面提示的鏈接進行手動下載
下載,我這邊復制到瀏覽器發現需要6個小時才能下載好。網速還是不滿意的可以采用idm下載器進行下載,我只用了3分鍾就下載好了。。。
在我們運行aspnetcore/build.cmd腳本之后會在項目路徑下生成一個.dotnet文件夾,同時在里面生成一個dotnet-install.ps1腳本文件,打開dotnet-install.ps1在643行找到
DownloadFile函數,修改下面紅色的地方
# $Source:資源路徑(可以是網絡文件資源,也可以是本地文件資源) # $OutPath:輸出路徑 function DownloadFile($Source, [string]$OutPath) {
//新增這樣的if,意思是把我們剛才下載好的網絡文件,指向我們剛才下載好的本地路徑 if ($Source -eq "https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.103/dotnet-sdk-5.0.103-win-x64.zip") { $Source = "C:\Users\wang\Downloads\Compressed\dotnet-sdk-5.0.103-win-x64.zip" } if ($Source -notlike "http*") { # Using System.IO.Path.GetFullPath to get the current directory # does not work in this context - $pwd gives the current directory if (![System.IO.Path]::IsPathRooted($Source)) { $Source = $(Join-Path -Path $pwd -ChildPath $Source) } $Source = Get-Absolute-Path $Source Say "Copying file from $Source to $OutPath" Copy-Item $Source $OutPath return } ..... }
保存dotnet-install.ps1在次運行build.cmd每次提示:dotnet-install: Downloading primary link https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.103/dotnet-sdk-5.0.103-win-x64.zip
我們就使用IDM下載這個資源到本地在修改dotnet-install.ps1腳本,把url替換成我們下載好的本地路徑,在次運行build.cmd腳本