Skip to content

Instantly share code, notes, and snippets.

@adrianmgg
Last active March 18, 2024 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmgg/5d14a0a0aa465328952efa8d00541d96 to your computer and use it in GitHub Desktop.
Save adrianmgg/5d14a0a0aa465328952efa8d00541d96 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
quickly initialize a new rust project tailored for doing quick temporary testing of stuff
.PARAMETER ProjectTemplate
one of: Bin, Lib
.PARAMETER OpenInVSCode
open created project in a new vscode window
.EXAMPLE
PS> create_temp_rust_project
.EXAMPLE
PS> create_temp_rust_project -ProjectTemplate Bin -OpenInVSCode
#>
[CmdletBinding()]
param (
[Parameter()]
[switch]$OpenInVSCode,
[Parameter()]
[String]
[ValidateSet('Bin', 'Lib', IgnoreCase=$true)]
$ProjectTemplate = 'Bin',
[Parameter()]
[string]$Name = ''
)
$ErrorActionPreference = 'Stop'
# some hardcoded vars for where i keep my stuff
$ProjectsBaseDir = 'D:\code\rust'
enum ProjectTemplate {
Lib
Bin
}
# New-Variable ProjectTemplate ([ProjectTemplate]$ProjectTemplate) -Force
# create folder for it
$outdir = mkdir (join-path $ProjectsBaseDir "$(if($Name -ne '') { "$Name" } else { 'thing' })-$(get-date -format 'yyyyMMdd-HHmmss')") -ErrorAction Stop -Verbose
#
Push-Location $outdir -ErrorAction Stop -Verbose
cargo init . --vcs git $(switch([ProjectTemplate]$ProjectTemplate) {
Lib { '--lib' }
Bin { '--bin' }
Default { Write-Error "unexpected ProjectTemplate value: ($($ProjectTemplate))" }
}) $(if($Name -ne '') {
'--name'; $Name
})
git config commit.gpgsign false
git add .
git commit -m 'initial commit (cargo init)'
if($OpenInVSCode) {
# code .
code --new-window . (gci ./src/ *.rs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment