日常開發用得到的指令整理,包含 dotnet CLI、Docker,以及在 PowerShell 設定檔裡自訂的 Git 縮寫與自動化函式。
Dotnet
dotnet run --project ./MyWebApp/
dotnet run --p ./MyWebApp/
dotnet build
確定程式沒改,可以執行這指令:
dotnet run --no-build
dotnet rebuild
launchSettings.json 指定 profiles:
dotnet run --launch-profile 'http'
建立專案
dotnet new <專案範本簡稱> -n <專案名稱>
列出專案範本有哪些
dotnet new list
指定輸出資料夾
dotnet new console -o ./MyApps/ConsoleApp
常用範本
| 範本說明 | 範本簡稱 |
|---|---|
| 主控台應用程式 | console |
| Blazor Web 應用程式 | blazor |
| 類別庫 | classlib |
| Windows Forms 應用程式 | winforms |
| MSTest 測試專案 | mstest |
| ASP.NET Core Web 應用程式(Model-View-Controller) | mvc |
| ASP.NET Core Web 應用程式 (Razor Pages) | webapp,razor |
| ASP.NET Core Web API | webapi |
Docker
docker ps -a --format "table {{.Image}}\t{{.Names}}\t{{.Status}}"
Windows
1. 測試 port 連線(內建)
Test-NetConnection mydomain.com -Port 443
telnet
2.客製化 Windows 指令
※ 用管理員身分執行 PowerShell,再執行以下指令:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
※ 確認檔案路徑:
$PROFILE
※ 如果檔案不存在就建立他:
if (!(Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }; notepad $PROFILE
※ 確認 devenv 路徑(後續會用來開解決方案):
& "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath
※ 實作指令
全域變數
$global:VSPath = 'C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/devenv.exe'
$global:MyVsWorkDir = 'C:/Users/shilvain/Desktop/shilvain/gitRepository'
常用指令縮寫
Git 指令:
function gs { git status }
function gb { git branch }
function gbvv { git branch -vv }
function gaa { git add . }
function gct { git commit -m $args[0] } # 支援參數:gct 'update'
function gph { git push }
function gpl { git pull }
function gf { git fetch }
function gsw { git switch $args[0] }
function glog { git log --pretty=format:'%C(bold yellow)%h%Creset %C(green)%ad%Creset | %C(auto)%d%Creset%s <%C(white)%an%Creset>' --decorate --graph --all --date=short --author-date-order }
function br { cd .. }
function cdgitrepo { cd D:\shilvain\GitRepository }
複合型指令(自動化)
git diff 指定索引的檔案:
function gd {
param(
[Parameter(Mandatory=$true)]
[int]$index
)
$files = git status --porcelain |
Where-Object { $_ -match '^[ MADRCU?]{1,2}\s+' } |
ForEach-Object { $_.Substring(3) }
if ($index -lt 0 -or $index -ge $files.Count) {
Write-Host "索引超出範圍 (0 ~ $($files.Count - 1))" -ForegroundColor Red
return
}
$target = $files[$index]
git diff $target
}
git add 指定索引的檔案:
function ga {
param(
[Parameter(Mandatory=$true)]
[int]$index
)
$files = git status --porcelain |
Where-Object { $_ -match '^[ MARC?]{1,2}\s+' } |
ForEach-Object { $_.Substring(3) }
if ($index -lt 0 -or $index -ge $files.Count) {
Write-Host "索引超出範圍 (0 ~ $($files.Count - 1))" -ForegroundColor Red
return
}
$target = $files[$index]
Write-Host "git add -> $target" -ForegroundColor Green
git add $target
}
在地端從特定分支切一個新分支出來:
function gck {
param(
[string]$ori_branch,
[string]$new_branch
)
git switch $ori_branch
git checkout -b $new_branch
}
開啟資料夾:
function open-folder {
param(
[Parameter(Mandatory=$true)]
[string]$folder_name
)
# 指定資料夾
$folder = $global:MyFolderBaseDir + '\' + $folder_name
# 開啟檔案總管
Start-Process $folder
}
用 VS 開啟資料夾:
function open-folder {
param(
[Parameter(Mandatory=$true)]
[string]$repo_name
)
$fullPath = $global:MyVsWorkDir + '\' + $repo_name
if (-Not (Test-Path $repo_name)) {
Write-Host "資料夾不存在: $fullPath" -ForegroundColor Red
return
}
& $global:VSPath $fullPath
}
用 VS 開解決方案:
function open-sln {
param(
[Parameter(Mandatory=$true)]
[string]$repo_name
)
$fullPath = $global:MyVsWorkDir + '\' + $repo_name
if (-Not (Test-Path $fullPath)) {
Write-Host "資料夾不存在: $fullPath" -ForegroundColor Red
return
}
$sln = Get-ChildItem -Path $fullPath -Filter *.sln | Select-Object -First 1
if ($sln -eq $null) {
Write-Host "找不到 .sln 檔案於資料夾: $fullPath" -ForegroundColor Red
return
}
& $global:VSPath $sln.FullName
}
自定義的 command,參數設定自動補文字(test 跟 oo):
Register-ArgumentCompleter -Native -CommandName open-sln -ScriptBlock {
param($wordToComplete)
'test','oo' |
Where-Object { $_ -like "*$wordToComplete*" } |
ForEach-Object {
[System.Management.Automation.CompletionResult]::new(
$_, $_, 'ParameterValue', $_
)
}
}
Bash
sh 檔
a 文檔開頭必放:
#!/bin/bash
更改文字顏色,ANSI 色碼
FONT_BLACK='\033[30m'
BG_LIGHT_GREEN='\033[102m'
RESET='\033[0m'
printf "$FONT_BLACK$BG_LIGHT_GREEN 測試 $RESET"
批次檔(bat)
- 用 chrome 開網頁:
start chrome.exe http://mydomain/test
- 註解:
REM 這是註解
- 終端機印文字:
echo Waiting for Docker to start...
- 啟動特定 exe:
start c:/myprogram.exe
- 每 5 秒檢查 docker 是否有開啟,有開就啟動特定 container,沒開就繼續等待:
REM 啟動Docker以及Sql Container
echo Waiting for Docker to start...
goto CHECK
:CHECK
docker info >nul 2>&1
if %errorlevel%==0 (
goto DO_ACTION
)
echo Docker not ready, retry in 5 seconds...
timeout /t 5 >nul
goto CHECK
:DO_ACTION
echo Docker is ready,Start Local Sql Server Container...
REM start container
docker start mycontainer
- 用Vs Code 開目錄
call code "C:\\Users\\User\\Desktop\\"
[!NOTE] code 是Vs Code的批次檔指令,所以要用call,不然會回不來原本的批次檔位置。
VS Code
Ctrl + Shift + E => 聚焦到檔案總管 Ctrl + Shift + F => 全域查詢