Visual Studio Code (VSCode)是一個輕量級但功能強大的源代碼編輯器,可在桌面上運行,適用于Windows,macOS和Linux。 它內置了對JavaScript,TypeScript和Node.js的支持,并具有豐富的其他語言(如C++,C#,Java,Python,PHP,Go)和運行時(如.NET和Unity)的擴展生態系統。
使用這些介紹性視頻開始使用VSCode開始您的旅程:https://code.visualstudio.com/docs/getstarted/introvideos
本文介紹如何使用VSCode進行PostgreSQL開發環境準備
安裝VSCode
1.? 下載VSCode
根據用戶環境,下載合適的VSCode版本下載地址:https://code.visualstudio.com/download
2. ? 安裝VSCode
根據提示進行安裝安裝C/C++編程語言支持
安裝C/C++編程語言支持(C/C++ for Visual Studio Code)
微軟的C/C++擴展**提供了對Visual Studio Code的C/C++支持,以便在Windows,Linux和macOS上使用VS Code進行C和C++開發。Note: C++ Intellisense也可以使用,根據個人喜歡選擇。
可以在VSCode內的Extension中搜索C/C++,找到目標插件后進行安裝。
下載PostgreSQL源代碼
Git下載最新PG代碼
確保您的計算機上安裝了Git。Git的使用幫助網上隨處可見,這里就不贅述了。
$?cd?sandbox
$?git?clone?https://github.com/postgres/postgres.git
$?cd?postgres
##?一般更改代碼都在特定的Branch上進行
$ git checkout -b FEATTURE-NAME
?$?EDIT?YOUR?CODE
?$?git?commit?-a
?$?git?diff?--patience?master?my-feature?>?../my-feature.patch
下載對應版本的PG代碼 (Optional)
?https://www.postgresql.org/ftp/source/運行VSCode
?1.打開源代碼目錄
?菜單 File --> Open
?打開對應的目錄,比如 ~/sandbox/postgres?2.配置命令????
有許多工具來自動執行諸如linting,build,打包,測試或部署軟件系統之類的任務。 比如TypeScript編譯器,再比如ESLint和TSLint這樣的linters以及Make,Ant,Gulp,Jake,Rake和MSBuild等build系統。
這些工具主要是通過命令行來運行的,并在內部軟件開發過程(編輯,編譯,測試和調試)內自動執行任務。 鑒于它們在開發生命周期中的重要性,能夠運行工具并從VS Code中分析其結果非常有幫助。VSCode中的任務可以配置為運行腳本和啟動進程,以便可以在VSCode中使用許多現有工具,而無需輸入命令行或編寫新代碼。工作區或文件夾特定任務是從工作區的.vscode文件夾中的tasks.json文件配置的。
2.1 打開 View --> Command Palette
輸入 Task: , 選擇 Tasks: Configure Task
2.2 選擇 Create tasks.json file from template
2.3 選擇 Others Example to run the arbitrary external command
2.4 現在開始編輯 task.json 文件
到微軟網站上 https://go.microsoft.com/fwlink/?LinkId=733558
查看關于 task.json 的格式文檔注意? 請根據個人需要編輯下面的任務配置文件
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"presentation" : { "reveal": "always" },
"tasks": [
{
"label": "Configure",
"type": "shell",
"command": "./configure --enable-depend --enable-cassert --enable-debug",
"problemMatcher": [
"$eslint-compact"
]
},
{
"label": "Make All",
"type": "shell",
"command": "make -j4 all",
"problemMatcher": [
"$eslint-compact"
]
},
{
"label": "Make Clean All",
"type": "shell",
"command": "make clean",
"problemMatcher": [
"$eslint-compact"
]
},
{
"label": "Make Install",
"type": "shell",
"command": "make install"
}
]
}
?3.運行所配制的命令
打開 View --> Command Palette --> Tasks: Run Task
選擇對應的 Configure、Make 或者 make install 命令來進行PostgreSQL的編譯等任務。
NOTE 可以配置一些快捷方式來方便工作使用VS Code調試PostgreSQL
這里以Mac環境下為例進行說明1.使用LLDB調試
LLDB是XCode下默認的調試工具,它和GDB有很多類似之處,如果你對GDB熟悉,使用LLDB不存在什么問題。這里是LLDB和GDB的一個命令對比(https://lldb.llvm.org/use/map.html)
注意 如果你的開發環境是Linux,請使用apt-get/yum 之間安裝lldb在VS Code中調試PG打開lauch.json
菜單 View -> Command Palette,輸入launch,選擇 Debug: Open launch.json
選擇 C++ (GDB/LLDB 編輯launch.json文件
注意:根據你的PG環境,修改下面 "args"里面的路徑
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) pg Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/backend/postgres",
"args": ["-D", "/Users/grantzhou/pgdata/data"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
},
{
"name": "(lldb) pg Launch help",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/src/backend/postgres",
"args": ["--help", ""],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
},
{
"name": "(lldb) Launch",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/src/backend/postgres",
"MIMode": "lldb"
}
]
}
?開始調試
1.? 打開調試選項卡(或者 ?? ?F5)???
2.?? 調試
當調試會話開始后, ?? ?上面會出現調試工具欄.
??? Continue/Pause F5
??? Step ?? ?Over F10
??? Step Into F11
??? Step ?? ?Out ?F11
??? Restart ??F5
??? Stop ?F52.使用GDB調試 (Mac上不推薦使用)
注意:在最新版的Mac上,gdb 最新版本8.2的安裝和執行非常的繁瑣,并且存在很多無法工作且需要降級到8.0版本的情況,這里不推薦使用。MAC上安裝GDB
與GCC一樣,安裝GDB的最簡單方法是通過Homebrew。 在終端窗口中,運行命令 brew install gdb,并等待它完成。注意:我們需要對GDB可執行文件進行代碼簽名,從而可以根據調試器的需要控制其他進程。對gdb進行代碼簽名
在Keychain中創建一個新證書
1.? 打開 ?? ?Keychain ?? ?Access 程序
2.? 菜單選擇 ?? ?Certificate ?? ?Assistant --> Create a Certificate
????a. 確保Identity ?? ?Type設置為Self ?? ?Signed Root
????b. 將證書類型更改為代碼簽名
????c. ?? ?選中“覆蓋默認值”復選框
????d. 選擇 “Continue” (在彈出提示中再次單擊繼續)。
????e. 在下一個頁面
????Security Number : 1,
????Validity Period : 3650 (最長 20 年)
??? f. 點擊繼續
?????g. 一直繼續,直到讓你選擇保存位置。選擇System
???? h. 根據提示輸入密碼,Done
3.? 回到 Keychain Access 主窗口,選擇左側邊欄中的System keychain,然后從列表中選擇新創建的證書,右鍵選擇 ?? ?Get ?? ?Info并設置為永遠信任。
?簽名
1. 重新啟動Taskgate? access-control服務使用Activity Monitor服務)
?
2. 點擊Quit,并等待其退出,并重新顯示在Activity ?? ?Monitor中 ?? ?(最多等待一到兩分鐘)
3. 簽名完成調試簽名問題
codesign -fs gdbcert /usr/local/bin/gdb
Restart your mac and enable
csrutil enable --without debug
sudo killall taskgated
# Monitor logs
log stream --predicate 'process == "taskgated" OR (process == "kernel" AND eventMessage CONTAINS "macOSTaskPolicy")' --info
其他
1. PostgreSQL后端主流程,初次PG開發人員建議多看一下?
????https://www.postgresql.org/developer/backend
2.Wiki.postgresql.org的開發人員部分? (如何進行代碼貢獻)???
????https://wiki.postgresql.org/wiki/Developer_and_Contributor_Resources
3. 新手從這里開始
????https://wiki.postgresql.org/wiki/So,_you_want_to_be_a_developer%3F常用VS Code功能
內置快捷鍵參考Windows系統
????https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdfLinux系統
????https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdfMac系統?
????https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf配置自定義快捷鍵
VSCode提供了很多定制功能包括快捷鍵的定制。
注意:如果您安裝了許多擴展程序或者已經自定義了鍵盤快捷鍵,則有時會出現鍵綁定沖突,其中相同的鍵盤快捷鍵映射到多個命令。 這可能會導致一些奇怪的現象,比如當您在編輯器中切換文件時,時常會導致進入和超出當前編輯范圍的問題
?File > Preferences > Keyboard Shortcuts (Windows)
?Code > Preferences > Keyboard Shortcuts (MacOS)結束語
本篇日志只是為了讓大家對如何使用VS Code開始PG編程有個初步的了解。
希望感興趣的朋友 Enjoy VS Code, Enjoy PostgreSQL development
PostgreSQL中文社區歡迎廣大技術人員投稿
投稿郵箱:press@postgres.cn