-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
BookGenShell.ps1
288 lines (247 loc) · 7.12 KB
/
BookGenShell.ps1
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# -----------------------------------------------------------------------------
# BookGen PowerShell Registration script
# Version 3.0
# Last modified: 2024-07-21
# -----------------------------------------------------------------------------
# NodeJS install test
function Test-NodeJs
{
try
{
# Try to get the node version
$nodeVersion = node --version
if ($nodeVersion)
{
return $true
}
}
catch
{
return $false
}
}
# Node commands
function Get-NodePath
{
try
{
$nodePath = (Get-Command node.exe -ErrorAction Stop).Source
$nodeDir = Split-Path $nodePath
return $nodeDir
}
catch
{
Write-Error "node.exe not found in the system PATH."
}
}
function npm {
param (
[string[]]$Args
)
$scriptPath = Get-NodePath
$nodeExe = Join-Path $scriptPath "node.exe"
if (-Not (Test-Path $nodeExe))
{
$nodeExe = "node"
}
$npmPrefixJs = Join-Path $scriptPath "node_modules/npm/bin/npm-prefix.js"
$npmCliJs = Join-Path $scriptPath "node_modules/npm/bin/npm-cli.js"
$npmPrefixNpmCliJs = & $nodeExe $npmPrefixJs
$npmPrefixNpmCliJsPath = Join-Path $npmPrefixNpmCliJs "node_modules/npm/bin/npm-cli.js"
if (Test-Path $npmPrefixNpmCliJsPath)
{
$npmCliJs = $npmPrefixNpmCliJsPath
}
& $nodeExe $npmCliJs @Args
}
function npx {
param (
[string[]]$Args
)
$scriptPath = Get-NodePath
$nodeExe = Join-Path $scriptPath "node.exe"
if (-Not (Test-Path $nodeExe)) {
$nodeExe = "node"
}
$npmPrefixJs = Join-Path $scriptPath "node_modules/npm/bin/npm-prefix.js"
$npxCliJs = Join-Path $scriptPath "node_modules/npm/bin/npx-cli.js"
$npmPrefixNpxCliJsPath = & $nodeExe $npmPrefixJs
$npmPrefixNpxCliJs = Join-Path $npmPrefixNpxCliJsPath "node_modules/npm/bin/npx-cli.js"
if (Test-Path $npmPrefixNpxCliJs) {
$npxCliJs = $npmPrefixNpxCliJs
}
& $nodeExe $npxCliJs @Args
}
function corepack {
param (
[string[]]$Args
)
$scriptPath = Get-NodePath
$nodeExe = Join-Path $scriptPath "node.exe"
$corepackJs = Join-Path $scriptPath "node_modules/corepack/dist/corepack.js"
if (Test-Path $nodeExe) {
& $nodeExe $corepackJs @Args
} else {
$env:PATHEXT = $env:PATHEXT -replace ";.JS;", ";"
& node $corepackJs @Args
}
}
# cdg command
function cdg
{
$argsAsString = $args -join ' '
if ([string]::IsNullOrWhiteSpace($argsAsString))
{
BookGen.Shell.exe "cdg"
}
else
{
BookGen.Shell.exe "cdg" "$argsAsString"
}
$location = [Environment]::GetEnvironmentVariable('cdgPath', 'User')
Push-Location $location
}
# www command
function www
{
$argsAsString = $args -join ' '
if ([string]::IsNullOrWhiteSpace($argsAsString))
{
BookGen.Shell.exe "www"
}
else
{
BookGen.Shell.exe "www" "$argsAsString"
}
}
# organize command
function organize
{
$argsAsString = $args -join ' '
if ([string]::IsNullOrWhiteSpace($argsAsString))
{
BookGen.Shell.exe "organize"
}
else
{
BookGen.Shell.exe "organize" "$argsAsString"
}
}
# info command
function bookgen-info()
{
Clear-Host
Get-Content $env:BookGenPath\getting-started.mdr | Out-Host -Paging
}
# lancher Command
function launcher()
{
BookGen.Launch.exe $(Get-Location).Path
}
# intro message
function intro()
{
clear
bookgen version
Write-Host " ____________________________________________________ "
Write-Host "/ To view the getting started doc type: bookgen-info \"
Write-Host "| To get info on using bookgen type: Bookgen Help |"
Write-Host "| To graphicaly select working directory type: cdg |"
Write-Host "| To start the bookgen launcher type: launcher |"
Write-Host "\ To redisplay this message type: intro /"
Write-Host " ----------------------------------------------------- "
Write-Host " \"
Write-Host " \ \"
Write-Host " \ /\"
Write-Host " ( )"
Write-Host " .( o )."
Bookgen.exe terminalinstall -t
if ($LastExitCode -eq 0)
{
Bookgen.exe terminalinstall -c
if ($LastExitCode -ne 0)
{
Write-Host ""
Write-Host "To install this shell as a windows terminal profile run:";
Write-Host "Bookgen terminalinstall"
}
}
if (Test-NodeJs)
{
$nodeVersion = node --version
Write-Host "Node version: $nodeVersion"
}
}
#Set UTF8 encoding
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[console]::InputEncoding = [System.Text.Encoding]::UTF8
#Set BookGenRoot variable
$env:BookGenPath = $PSScriptRoot
# register scripts folder to the path
$env:Path += ";$PSScriptRoot"
if (-not (Test-NodeJs)) {
#check if it's bundled
$nodeDirs = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object { $_.Name -like "node-*" }
foreach ($dir in $nodeDirs)
{
$nodePath = Join-Path -Path $dir.FullName -ChildPath "node.exe"
if (Test-Path -Path $nodePath)
{
$env:NodeJsDir = "$($dir.FullName)"
$env:NodeExe = $nodePath
$env:Path += ";$($dir.FullName)"
Write-Host "Added $($dir.FullName) to PATH."
break
}
}
}
# set colors
Set-PSReadLineOption -Colors @{
Parameter = 'Red'
String = 'Cyan'
Command = 'Green'
}
# PowerShell parameter completion shim for BookGen
Register-ArgumentCompleter -Native -CommandName BookGen -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
BookGen.exe "Shell" "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# Case invariant registration
Register-ArgumentCompleter -Native -CommandName bookgen -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
BookGen.exe "Shell" "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# PowerShell parameter completion shim for git
Register-ArgumentCompleter -Native -CommandName git -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
BookGen.Shell.exe "git-complete" $cursorPosition "$wordToComplete" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
# set prompt
function prompt {
$git = $(BookGen.Shell.exe "prompt" $(Get-Location).Path)
if (-not [string]::IsNullOrWhiteSpace($git)) {
'PS ' + $(Get-Location) + "`n"+$git+ $(if ($NestedPromptLevel -ge 1) { '>>' }) + ' > '
}
else {
'PS ' + $(Get-Location) + $(if ($NestedPromptLevel -ge 1) { '>>' }) + ' > '
}
}
# if argument given set to startup folder
if ($args.Count -eq 1) {
Set-Location $args[0]
}
# welcome message
intro