Skip to content

Commit ffed3eb

Browse files
committed
Add local dev workspace script and localhost binding
1 parent 2ae06db commit ffed3eb

3 files changed

Lines changed: 190 additions & 11 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ sitemap.xml
4646
Hide_Tree_Page.md
4747
Gemfile.lock
4848
Gemfile
49+
50+
# Local development workspace
51+
.dev/
52+
.bundle/
53+
.jekyll-cache/
54+
.sass-cache/

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,40 @@ This project is to store [Dynamic Web TWAIN](https://www.dynamsoft.com/web-twain
44

55
### Local Development
66

7-
1. Download the docs template repo and the Web TWAIN docs repo with [download-dwt.sh](https://github.com/tony-xlh/misc/raw/refs/heads/main/build-docs/download-dwt.sh).
8-
2. Install Ruby and Jekyll.
9-
3. Go to the docs' folder and install dependencies.
7+
1. Install Ruby, Bundler, and Jekyll.
8+
2. Run the local dev script from this repository root:
109

11-
```bash
12-
bundle install
10+
```powershell
11+
powershell -ExecutionPolicy Bypass -File .\scripts\dev.ps1
1312
```
1413

15-
4. Serve the site.
14+
This script keeps all local-only files in `.dev/`:
15+
- `.dev/Docs-Template-Repo` (template clone)
16+
- `.dev/DocHome` (merged docs + template workspace)
17+
- `.dev/vendor/bundle`, `.dev/.bundle`, `.dev/.bundle-user`, `.dev/.jekyll-cache`, `.dev/_site`
1618

17-
```bash
18-
bundle exec jekyll serve -P 5555 --trace --host=0.0.0.0
19-
```
19+
Optional flags:
2020

21-
### License
21+
```powershell
22+
# Prepare workspace only (do not start server)
23+
powershell -ExecutionPolicy Bypass -File .\scripts\dev.ps1 -NoServe
2224
23-
All documentation is available under the terms of [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/).
25+
# Skip template pull/refresh and use current local template copy
26+
powershell -ExecutionPolicy Bypass -File .\scripts\dev.ps1 -NoTemplateUpdate
27+
28+
# Change port
29+
powershell -ExecutionPolicy Bypass -File .\scripts\dev.ps1 -Port 6001
30+
31+
# Bind to all interfaces (optional)
32+
powershell -ExecutionPolicy Bypass -File .\scripts\dev.ps1 -BindHost 0.0.0.0
33+
```
2434

35+
Local preview URL root:
2536

37+
```text
38+
http://localhost:5555/web-twain/docs/
39+
```
40+
41+
### License
42+
43+
All documentation is available under the terms of [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/).

scripts/dev.ps1

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

0 commit comments

Comments
 (0)