Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Boilerplate Send-To
# C++ Boilerplate Generator Send-To Scripts
# Maintained at https://gist.github.com/rpavlik/fbdf1347e52b64b833c5
# Main script
#
# Look at that gist for a bootstrapper script that installs or upgrades this script for you.
# Create shortcuts like
# %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -File C:\Users\Ryan\Dropbox\configs\win-scripts\boilerplate.ps1
# to use this easily - or just use the bootstrapper/installer to make them for you.
[CmdletBinding()]
Param(
[string]$dir = (Convert-Path .), # send-to will often pass this one but if not, and if not explicit, use cwd.
[string]$basename = "",
[string]$ext = "",
[System.Uri]$URL = "http://tools.getosvr.org/boilerplate/",
[string]$Project = ""
)
# Load the VB stuff to be able to run a simple InputBox
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
if ($basename.Length -eq 0) {
# No basename specified, ask for it.
$basename = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a file base name", "File base", "")
}
if ($basename.Length -eq 0) {
# Cancel or nothing entered - bail out
Exit
}
if ($ext.Length -eq 0) {
# No extension specified, ask for it
$ext = [Microsoft.VisualBasic.Interaction]::InputBox("Enter an extension or type", "Extension", "cpp")
}
if ($ext.Length -eq 0) {
# Cancel or nothing entered - bail out
Exit
}
# Extension mapping: asking for ch is actually asking for a file ending in .h that is (old) C-safe.
$outext = $ext;
if ($ext -eq "ch") {
$outext = "h";
}
# Prepare the query: hash table.
$query = @{"filebase" = "$basename"; "ext" = "$ext"}
# To change the author lines, uncomment and do something like this (keep everything un-indented, the generator does that for you):
#
#$query.Add("authorlines", @'
#Ryan Pavlik
#<ryan@sensics.com>
#'@)
# Compute the location to download from.
$uriBuilder = New-Object System.UriBuilder(New-Object System.Uri($URL, "download.php"))
# Turn that hash table into our query string.
$uriBuilder.Query = [string]::Join('&', @( foreach($kv in $query.GetEnumerator()) { "{0}={1}" -f $kv.Key, $kv.Value } ) )
# Compute the target since we have to explicitly specify it: Net.WebClient doesn't have the API to obey the attachment header.
$target = Join-Path $dir "$basename.$outext"
# Do the download of the generated file.
(New-Object Net.WebClient).DownloadFile($uriBuilder.Uri, $target)
# C++ Boilerplate Generator Send-To Scripts
# Maintained at https://gist.github.com/rpavlik/fbdf1347e52b64b833c5
# For use with an install of https://github.com/rpavlik/generate-cpp-boilerplate
# preconfigured to use the OSVR one.
#
# Bootstrap/installer script: downloads (new install or upgrade) script and creates send-to shortcuts for easy usage.
#
# Install directly from the gist (no installer download needed) by running:
# powershell -NoProfile -ExecutionPolicy Bypass -Command "iex (((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/bpbootstrap.ps1'))"
#
# For easiest use with a custom install, (and to see the insides) see bpbootstraplib.ps1.
[CmdletBinding()]
Param(
[Parameter(Position=1)]
[String]$Project = "",
[Parameter(Position=2)]
[System.Uri]$URL = "",
[switch]$SkipDownload,
[switch]$SkipShortcuts
)
# Get the real code that does the installing.
iex ((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/bpbootstraplib.ps1'))
# Call it, with the same parameters if applicable.
Invoke-BPBootstrap @PSBoundParameters
# C++ Boilerplate Generator Send-To Scripts
# Maintained at https://gist.github.com/rpavlik/fbdf1347e52b64b833c5
# For use with an install of https://github.com/rpavlik/generate-cpp-boilerplate
# preconfigured to use the OSVR one.
#
# Bootstrap library/custom installer script: Innards of bpbootstrap, and also used for custom boostraps.
#
# Install directly from the gist (no installer download needed) by running:
# powershell -NoProfile -ExecutionPolicy Bypass -Command "iex (((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/bpbootstrap.ps1'))"
# or generically for your install of generate-cpp-boilerplate with someting like the following, customizing the last part with the two parameters of course (the two lines are equivalent):
# powershell -NoProfile -ExecutionPolicy Bypass -Command "iex (((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/bpbootstraplib.ps1')) + '; Invoke-BPBootstrap -Project VRPN -URL http://dev.osvr.org/boilerplate/')"
# powershell -NoProfile -ExecutionPolicy Bypass -Command "iex (((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/bpbootstraplib.ps1')) + '; Invoke-BPBootstrap VRPN http://dev.osvr.org/boilerplate/')"
function Invoke-BPBootstrap
{
[CmdletBinding()]
Param(
[Parameter(Position=1)]
[String]$Project = "",
[Parameter(Position=2)]
[System.Uri]$URL = "",
[switch]$SkipDownload,
[switch]$SkipShortcuts
)
### Helper Functions
function Download-BPScript($destDir, $scriptDest, $iconDest)
{
### Config values
# Main script location - always points to latest revision
$source = "https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/boilerplate.ps1"
# Icon from the boilerplate generator itself - again, always the latest revision.
$iconSource = "https://raw.githubusercontent.com/rpavlik/generate-cpp-boilerplate/master/favicon.ico"
# Download the script and the icon.
$wc = New-Object Net.WebClient
$wc.DownloadFile($source, $script)
$wc.DownloadFile($iconSource, $icon)
}
# Helper function to avoid an awkward loop.
function Create-BPShortcut
{
[CmdletBinding()]
Param(
[String]$nameDetails = "",
[String]$launchDetails = ""
)
$name = "${namePrefix}Generate"
if ($nameDetails.Length -ne 0) {
$name += " $nameDetails"
}
if ($haveURL) {
$launchDetails += " -URL " + $URL.ToString()
}
if ($Project.Length -ne 0) {
$launchDetails += " -Project $Project"
}
$shortcutPath = Join-Path $sendto "$name.lnk"
# TODO: Overwrite existing shortcuts that do this.
# Right now, we get access denied trying to delete them, and a COM error
# trying to create over top of them, hence the guard.
if (-not (Test-Path $shortcutPath)) {
$shortcut = $wshshell.CreateShortCut($shortcutPath)
$shortcut.TargetPath = "powershell"
$shortcut.Arguments = "-WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -File $script $launchDetails"
$shortcut.Description = "Generate ${namePrefix}boilerplate $nameDetails"
$shortcut.IconLocation = $icon
$shortcut.Save()
}
}
### Body of bootstrapper script.
# Sanitize namePrefix
$namePrefix = $Project
if ($namePrefix.Length -ne 0 -and (-not ($namePrefix.EndsWith(" ")))) {
# We need to add a space at the end of the name prefix.
$namePrefix += " "
}
# Check to see if we were given a URI
$emptyURI = [System.URI]""
$haveURL = $false
if ($URL -ne $emptyURI) {
$haveURL = $true
}
# Get the special "send to" directory
$sendto = [Environment]::GetFolderPath("SendTo")
# Get our install directory (in the user profile) and create it if it's not there.
$dir = Join-Path ([Environment]::GetFolderPath("ApplicationData")) "GenerateBoilerplate"
if (-not (Get-Item $dir)) {
New-Item -Path $dir -ItemType Directory
}
$script = Join-Path $dir "boilerplate.ps1"
$icon = Join-Path $dir "favicon.ico"
if (-not $SkipDownload) {
Download-BPScript -destDir "$dir" -scriptDest "$script" -iconDest "$icon"
}
# Needed for shortcut creation without powershell v5
$wshshell = New-Object -comobject WScript.Shell
if (-not $SkipShortcuts) {
# Creates the non-extension-specific shortcut
# Create-BPShortcut
# Creates the extension-specific shortcuts
Create-BPShortcut -nameDetails "h" -launchDetails "-ext h"
Create-BPShortcut -nameDetails "cpp" -launchDetails "-ext cpp"
Create-BPShortcut -nameDetails "c-safe h" -launchDetails "-ext ch"
}
}
rem You can just copy and paste the following line into the Windows Run dialog or a cmd.exe prompt (no admin needed, it's a local per-user install)
powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/rpavlik/fbdf1347e52b64b833c5/raw/bpbootstrap.ps1'))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment