-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.ps1
114 lines (91 loc) · 3.71 KB
/
play.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
$playground_rootdir=""
if ($IsWindows) {
$playground_rootdir = Split-Path -Parent $MyInvocation.MyCommand.Path
} else {
$playground_rootdir = Split-Path -Parent (wslpath -w $MyInvocation.MyCommand.Path)
}
$playground_template_dir = Join-Path $playground_rootdir "templates"
$playground_projects_dir = Join-Path $playground_rootdir "projects"
function ConvertTo-AsciiHex {
param (
[Parameter(Mandatory = $true)]
[string]$inputString
)
$hexString = ""
foreach ($char in $inputString.ToCharArray()) {
$hexString += [System.Convert]::ToByte($char).ToString("X2")
}
return $hexString
}
function Playground() {
param (
[CmdletBinding()]
[Parameter(Mandatory = $false)]
[string]$project_name = "go-playground",
[Parameter(Mandatory = $false)]
[ValidateScript(
{ $_ -in (Get-PlaygroundTemplates) },
ErrorMessage = "Invalid template name. Use Tab autocompletion or Get-PlaygroundTemplates to see available templates."
)]
[string]$template = "go-dev"
)
$project_dir = Join-Path $playground_projects_dir $project_name
$template_dir = Join-Path $playground_template_dir $template
if (!(Test-Path $template_dir -PathType Container)) {
throw "Template $template does not exist"
exit 1
}
# Make sure projects directory exists
New-Item -ItemType Directory -Path $playground_projects_dir -Force | Out-Null
# Check if project already exists, if it's not create it
if (!(Test-Path $project_dir -PathType Container)) {
Write-Host "Creating project $project_name"
Copy-Item -Path $template_dir -Destination $project_dir -Recurse -Force
# Replace project name in relevant files in .devcontainer directory
foreach ($file in @("docker-compose.yaml", "devcontainer.json")) {
# Do something with the file
Replace-In-File -path "$project_dir\.devcontainer\$file" -pattern "<PROJECT_NAME>" -replacement "$project_name"
}
} else {
Write-Host "Project $project_name already exists. Will open it now."
}
Write-Host "Opening project $project_name from directory $project_dir"
# Get workdir
$workdir = (Get-Content "$project_dir\.devcontainer\devcontainer.json" | ConvertFrom-Json).workspaceFolder
# Open project in VSCode as a dev container
$project_name_hex = ConvertTo-AsciiHex $project_dir
& code --folder-uri vscode-remote://dev-container+$project_name_hex/$workdir
}
New-Alias -Name play -Value Playground
Function Replace-In-File {
param (
[Parameter(Mandatory = $true)]
[string]$path,
[Parameter(Mandatory = $true)]
[string]$pattern,
[Parameter(Mandatory = $true)]
[string]$replacement
)
(Get-Content $path) -replace $pattern, $replacement | Set-Content $path
}
Function Get-Subdirs {
param (
[Parameter(Mandatory = $true)]
[string]$path
)
Get-ChildItem -Path $path -Directory | Select-Object -ExpandProperty Name
}
Function Get-PlaygroundProjects {
Get-Subdirs $playground_projects_dir
}
Function Get-PlaygroundTemplates {
Get-Subdirs $playground_template_dir
}
Register-ArgumentCompleter -CommandName Playground -ParameterName project_name -ScriptBlock {
param($commandName, $parameterName, $stringMatch)
Get-PlaygroundProjects | Where-Object { $_ -like "$stringMatch*" }
}
Register-ArgumentCompleter -CommandName Playground -ParameterName template -ScriptBlock {
param($commandName, $parameterName, $stringMatch)
Get-PlaygroundTemplates | Where-Object { $_ -like "$stringMatch*" }
}