اشتراک‌ها
Visual Studio 2017 15.7 منتشر شد
Visual Studio 2017 15.7 منتشر شد
مطالب
PowerShell 7.x - قسمت یازدهم - یک مثال
به عنوان یک تمرین میخواهیم یک پروژه گیت ساده را به صورت زیر visualise کنیم: 


برای تولید چنین نموداری از میتوانیم Mermaid استفاده کنیم. یکی از انواع دیاگرام‌هایی را که پشتیبانی میکند، Gitgraph میباشد. دیاگرام‌ها در Mermaid با کمک یک DSL ساخته میشوند. سینکس آن نیز خیلی ساده است؛ ابتدا نوع دیاگرامی را که میخواهیم ترسیم کنیم، تعیین میکنیم و سپس محتویات را براساس نوع دیاگرام، تعیین میکنیم. به عنوان مثال برای Gitgraph سینتکس آن به این صورت است: 

gitGraph
   commit
   commit
   branch develop
   checkout develop
   commit
   commit
   checkout main
   merge develop
   commit
   commit

در ساختار فوق ابتدا دو کامیت بر روی برنچ اصلی (main) انجام شده‌است؛ سپس یک برنچ جدید را با نام develop، ایجاده کرده‌ایم و بلافاصله به آن checkout کرده‌ایم. در ادامه تعدادی کامیت را بر روی این برنچ انجام داده و در نهایت برنچ موردنظر را بر روی main، مرج کرده‌ایم. در نهایت نیز دو کامیت دیگر را بر روی main ایجاد کرده‌ایم. تعریف فوق، منجر به ساخت چنین نموداری خواهد شد: 


از ادیتور آنلاین Mermaid نیز میتوانید برای تست سینکس استفاده کنید. در ادامه میخواهیم با کمک PowerShell، از روی یک پروژه‌ی گیت، DSL موردنیاز برای ساخت دیاگرام را ایجاد کنیم. برای اینکار ابتدا توسط تابع زیر یک پروژه‌ی گیت را با تعدادی فایل نمونه ایجاد خواهیم کرد: 

Function New-RandomRepo {
    Function RandomFiles($branch = "main") {
        1..3 | ForEach-Object {
            New-Item -ItemType File -Name "file_$($_).txt"
            Set-Content -Path "file_$($_).txt" -Value "This is file $($_) on branch $branch"
            git add .
            git commit -m "Commit $($_) on branch $branch"
        }
    }
    Set-Location ~/Desktop
    New-Item -ItemType Directory "random_git_repo"
    Set-Location "random_git_repo"
    git init -b main
    Write-Output "This is the main branch" | Set-Content -Path "main.txt"
    git add .
    git commit -m "Initial commit"
    1..3 | ForEach-Object { 
        git checkout -b "branch_$($_)"
        RandomFiles "branch_$($_)"
        git checkout main
    }
}

در ادامه تابع New-GitRepoDiagram را برای تولید ساختار مورد نیاز نوشته‌ایم: 

Function New-GitRepoDiagram {
    $commitIds = @()
    $branches = git branch | ForEach-Object {
        $default = $false
        $activeBranch = git symbolic-ref --short HEAD
        $currentBranch = ($_.Replace("* ", " ")).Trim()
        if ($currentBranch -eq $activeBranch) {
            $default = $true
        }
        @{
            name         = $currentBranch
            isMainBranch = $default
        } | ConvertTo-Json
    } | ConvertFrom-Json
    
    $defaultBranch = $branches | Where-Object { $_.isMainBranch -eq $true } | Select-Object -ExpandProperty name
    $mermaidFile = "%%{init: { 'gitGraph': {'mainBranchName': '$defaultBranch' } } }%%" + [Environment]::NewLine
    $mermaidFile += 'gitGraph' + [Environment]::NewLine

    foreach ($branch in ($branches | Sort-Object -Property isMainBranch -Descending)) {
        $name = $branch.name
        $notIncludeTheMainCommits = $name -ne $defaultBranch ? "--not $(git merge-base $defaultBranch $name)" : ""
        $notIncludeTheMainCommits
        $logs = git log --pretty=format:'{"commit": "%h", "author": "%an", "message": "%s"}' --reverse $name | ConvertFrom-Json
        if ($name -ne $defaultBranch) {
            $mermaidFile += '   branch "$name"'.Replace('$name', $name) + [Environment]::NewLine
        }
        foreach ($log in $logs) {
            $commit = $log.commit
            if ($commitIds -contains $commit) {
                continue
            }
            $commitToAdd = '   commit id: "$commit"'.Replace('$commit', $commit) + [Environment]::NewLine
            $mermaidFile += $commitToAdd
            $commitIds += $commit
        }
        if ($name -ne $defaultBranch) {
            $mermaidFile += '   checkout main' + [Environment]::NewLine
        }
    }
    Write-Host $mermaidFile
}

توضیحات:

  • در تابع فوق، ابتدا یک آرایه‌ی خالی برای ذخیره‌ی کامیت آی‌دی‌ها، اضافه شده؛ از این آرایه برای جلوگیری از اضافه شدن کامیت تکراری در هر برنچ استفاده شده‌است.
  • سپس با کمک دستور git branch، لیست تمام برنچ‌ها، دریافت شده‌است (همانطور که در قسمت قبل بررسی شد +).
  • برای هر برنچ، تابع تعیین میکند که آیا برنچ جاری است یا خیر. اینکار با کمک دستور git symbolic-ref انجام شده‌است.
  • در ادامه متغیر mermaidFile$ برای ایجاد یک گراف جدید Git مقداردهی میشود. در اینجا نام برنچ اصلی برابر با نام برنچ پیش‌فرض، تنظیم می‌شود.
  • سپس لیست کامیت‌های هر برنچ را با کمک دستور git log (همان سینتکسی که در قسمت‌های قبل بررسی شد +) استخراج میکنیم.
  • و در نهایت به ازای هر کامیت، یک commit id تولید کرده‌ایم.

با فراخوانی تابع فوق، اینچنین ساختاری برایمان تولید خواهد شد: 

%%{init: { 'gitGraph': {'mainBranchName': 'main' } } }%%
gitGraph
   commit id: "765100f"
   branch "branch_1"
   commit id: "c88c441"
   commit id: "44149d9"
   commit id: "a660fe3"
   checkout main
   branch "branch_2"
   commit id: "2dcb572"
   commit id: "b043ad1"
   commit id: "92cafc0"
   checkout main
   branch "branch_3"
   commit id: "559e381"
   commit id: "f72957f"
   commit id: "c066e72"
   checkout main

خروجی فوق دقیقاً دیاگرامی است که در ابتدای مطلب نشان داده شد:

اشتراک‌ها
تجریبات بازنشستگی یک برنامه‌نویس Apple

I retired a year and a half ago after having worked for twenty-six years as a programmer for Apple. I’m not sure which would have been more surprising: if I had continued programming in my spare time after I had retired or if I never programmed again. 

تجریبات بازنشستگی یک برنامه‌نویس Apple
اشتراک‌ها
دوره سه ساعته آموزش Blazor

00:00:00 Introduction
00:03:07 Blazor Project Structure
00:12:14 How Blazor Works
00:20:28 What is an Inventory Management System
00:23:47 Introduction to Clean Architecture
00:30:58 Null Reference Type in .NET 6
00:36:44 Write the View Inventories Use Case
00:45:16 Implement the View Inventories Use Case
00:59:27 Create a Plugin with Dependency Injection
01:08:45 Inject the Use Case in Razor Component
01:17:35 Dependency Injection in Blazor
01:28:08 Page Component - Create the Inventory List Page
01:34:34 SPA Components Best Practice
01:38:07 Databinding and EventCallback in Search Inventory Component
01:52:59 Component Parameters in Inventory List Component
02:03:52 Null Checks
02:06:07 Extract the Inventory List Item Component
02:09:45 Add Inventory Use Case
02:12:10 Implement Add Inventory Repository Methods
02:15:34 NavigationManager
02:17:56 EditForm and Data Validation
02:30:09 Edit Inventory Use Case
02:35:12 Implement Edit Inventory Repository methods
02:43:00 Receive Routing Parameters
02:50:09 Implement Edit Inventory Component
03:03:06 Why we are using Async everywhere 

دوره سه ساعته آموزش Blazor