-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdev.ps1
More file actions
156 lines (137 loc) · 4.96 KB
/
dev.ps1
File metadata and controls
156 lines (137 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[CmdletBinding()]
param(
[string]$TemplateRepoUrl = "https://github.com/dynamsoft-docs/Docs-Template-Repo.git",
[string]$TemplateBranch = "preview",
[int]$Port = 5555,
[string]$BindHost = "localhost",
[switch]$NoTemplateUpdate,
[switch]$NoServe
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Write-Step {
param([string]$Message)
Write-Host "==> $Message"
}
function Ensure-Command {
param([string]$Name)
if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) {
throw "Required command '$Name' was not found in PATH."
}
}
function Invoke-Native {
param(
[Parameter(Mandatory = $true)]
[string]$FilePath,
[string[]]$Arguments = @()
)
& $FilePath @Arguments
$code = $LASTEXITCODE
if ($code -ne 0) {
$argText = ($Arguments -join " ")
throw "Command failed with exit code ${code}: $FilePath $argText"
}
}
function Invoke-Robocopy {
param(
[string]$Source,
[string]$Destination,
[string[]]$ExtraArgs
)
$baseArgs = @(
$Source,
$Destination,
"/R:2",
"/W:2",
"/NFL",
"/NDL",
"/NJH",
"/NJS",
"/NP"
)
$allArgs = $baseArgs + $ExtraArgs
& robocopy @allArgs | Out-Null
$code = $LASTEXITCODE
if ($code -ge 8) {
throw "robocopy failed with exit code $code. Source: $Source Destination: $Destination"
}
}
Ensure-Command "git"
Ensure-Command "robocopy"
Ensure-Command "bundle"
$repoRoot = Split-Path -Parent $PSScriptRoot
$devRoot = Join-Path $repoRoot ".dev"
$templateRoot = Join-Path $devRoot "Docs-Template-Repo"
$docHome = Join-Path $devRoot "DocHome"
$bundlePath = Join-Path $devRoot "vendor\bundle"
$bundleConfig = Join-Path $devRoot ".bundle"
$bundleUserHome = Join-Path $devRoot ".bundle-user"
$jekyllCache = Join-Path $devRoot ".jekyll-cache"
$siteDir = Join-Path $devRoot "_site"
Write-Step "Preparing local workspace in $devRoot"
New-Item -ItemType Directory -Force -Path $devRoot | Out-Null
if (-not (Test-Path $templateRoot)) {
Write-Step "Cloning template repo ($TemplateBranch)"
Invoke-Native -FilePath "git" -Arguments @("clone", "--depth", "1", "--branch", $TemplateBranch, $TemplateRepoUrl, $templateRoot)
} elseif (-not $NoTemplateUpdate) {
Write-Step "Syncing template repo ($TemplateBranch)"
Invoke-Native -FilePath "git" -Arguments @("-C", $templateRoot, "fetch", "origin", $TemplateBranch, "--depth", "1")
Invoke-Native -FilePath "git" -Arguments @("-C", $templateRoot, "reset", "--hard")
Invoke-Native -FilePath "git" -Arguments @("-C", $templateRoot, "clean", "-fd")
Invoke-Native -FilePath "git" -Arguments @("-C", $templateRoot, "checkout", "-B", $TemplateBranch, "origin/$TemplateBranch")
} else {
Write-Step "Skipping template update"
}
Write-Step "Rebuilding merged site workspace"
New-Item -ItemType Directory -Force -Path $docHome | Out-Null
Invoke-Robocopy -Source $repoRoot -Destination $docHome -ExtraArgs @(
"/MIR",
"/XD", ".git", ".dev", ".vs", "node_modules", "_site", ".bundle", ".jekyll-cache", ".sass-cache", "vendor"
)
Invoke-Robocopy -Source $templateRoot -Destination $docHome -ExtraArgs @(
"/E",
"/XD", ".git", ".github", "_site", "node_modules"
)
Write-Step "Applying local assetsPath replacements"
$search = "assetsPath = '/webres/wwwroot'"
$replace = "assetsPath = 'https://www.dynamsoft.com/webres/wwwroot'"
foreach ($dirName in @("_includes", "_layouts")) {
$dirPath = Join-Path $docHome $dirName
if (-not (Test-Path $dirPath)) {
continue
}
Get-ChildItem -Path $dirPath -Recurse -File | ForEach-Object {
$path = $_.FullName
try {
$content = [System.IO.File]::ReadAllText($path)
} catch {
return
}
if ($content.Contains($search)) {
$updated = $content.Replace($search, $replace)
[System.IO.File]::WriteAllText($path, $updated)
}
}
}
Write-Step "Configuring Bundler and Jekyll local paths"
$env:BUNDLE_PATH = $bundlePath
$env:BUNDLE_APP_CONFIG = $bundleConfig
$env:BUNDLE_USER_HOME = $bundleUserHome
$env:BUNDLE_USER_CACHE = Join-Path $bundleUserHome "cache"
$env:JEKYLL_CACHE_DIR = $jekyllCache
$env:JEKYLL_ENV = "development"
New-Item -ItemType Directory -Force -Path $bundlePath, $bundleConfig, $bundleUserHome, $jekyllCache, $siteDir | Out-Null
Push-Location $docHome
try {
Write-Step "Installing Ruby dependencies"
Invoke-Native -FilePath "bundle" -Arguments @("install")
if ($NoServe) {
Write-Step "Build workspace prepared. Start server with:"
Write-Host "bundle exec jekyll serve -P $Port --trace --host=$BindHost --livereload --destination `"$siteDir`""
} else {
Write-Step "Starting Jekyll server on port $Port"
Invoke-Native -FilePath "bundle" -Arguments @("exec", "jekyll", "serve", "-P", "$Port", "--trace", "--host=$BindHost", "--livereload", "--destination", "$siteDir")
}
} finally {
Pop-Location
}