Skip to content

Instantly share code, notes, and snippets.

@Adobe-Android
Last active September 29, 2019 17:44
Show Gist options
  • Save Adobe-Android/6bc23694098d7421ae5de7a2f60440f5 to your computer and use it in GitHub Desktop.
Save Adobe-Android/6bc23694098d7421ae5de7a2f60440f5 to your computer and use it in GitHub Desktop.
PowerShell script collection (built with Windows 10 and Linux (pengwin - Debian-based) in mind on PowerShell Core 7.0.0-preview.4
#!/usr/bin/pwsh-preview
# Setting default values for some variables
$configVersion = ""
$preferredCompiler = ""
$cppStandard = ""
$additionalFlags = ""
$currentDir = (Get-Item -Path ".\").FullName
$configPath = "$currentDir/cpp_config.xml"
$PSVersion = "PowerShell " + $PSVersionTable.PSEdition + " version: " + $PSVersionTable.PSVersion
Out-Host -InputObject $PSVersion
Out-Host -InputObject "Attempting to read from cpp_config.xml file..."
# Could optimize by providing index of item
function WriteToXML($funcKey, $answer, $xml) {
Out-Host -InputObject "Writing preferences to cpp_config.xml file..."
for ($i = 0; $i -lt $xml.cppConfig.add.Count; $i++) {
$xmlKey = $xml.cppConfig.add[$i].key
if ($xmlKey -eq $funcKey)
{ $xml.cppConfig.add[$i].value = [string]$answer }
}
$xml.Save($configPath)
}
function CreateConfig() {
Out-Host -InputObject "Failed to find an existing cpp_config.xml file..."
Out-Host -InputObject "Generating a new cpp_config.xml file..."
New-Item cpp_config.xml
Set-Content $configPath '<?xml version="1.0" encoding="UTF-8"?>
<cppConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<add key="configVersion" value="1.0" />
<add key="preferredCompiler" value="" />
<add key="cppStandard" value="" />
<add key="additionalFlags" value="" />
</cppConfig>'
}
$bool = Test-Path $configPath
if ($bool) {
$xml = [xml](Get-Content $configPath)
}
else {
CreateConfig
break
}
if ($xml) {
foreach ($item in $xml.cppConfig.add) {
if ($item.key -eq "configVersion")
{ $configVersion = $item.value }
if ($item.key -eq "preferredCompiler")
{ $preferredCompiler = $item.value }
if ($item.key -eq "cppStandard")
{ $cppStandard = $item.value }
if ($item.key -eq "additionalFlags")
{ $additionalFlags = $item.value }
}
}
$configVersionStr = [string]$configVersion
if ($configVersionStr -eq "1.0") {
Out-Host -InputObject "Config version 1.0 set."
}
else {
Out-Host -InputObject "What config version are you using?"
# WriteToXML
}
$preferredCompilerStr = [string]$preferredCompiler
if ($preferredCompilerStr -eq "") {
$preferredCompiler = Read-Host 'What is your preferred C++ compiler? (g++, clang++, MSVC)'
# TODO: Write to XML config
WriteToXML "preferredCompiler" $preferredCompiler $xml
}
$cppStandardStr = [string]$cppStandard
if ($cppStandardStr -eq "") {
$cppStandard = Read-Host 'What C++ standard will you be using? (C++98, 03, 11, 14, 17)'
# TODO: Write to XML config
WriteToXML "cppStandard" $cppStandard $xml
}
# $additionalFlagsStr = [string]$additionalFlags
# if ($additionalFlagsStr -eq "") {
# $additionalFlags = Read-Host 'Please specify any additional flags here. (pipe delimited)'
# Out-Host -InputObject $additionalFlags
# # TODO: Write to XML config
# WriteToXML "additionalFlags" $additionalFlags $xml
# }
function compile {
$fileName = "test.cpp"
$exeName = $fileName.Split(".")[0]
Out-Host -InputObject "Compiling code..."
# Check for OS (shouldn't be able to run msvc from Linux, etc.)
if ($preferredCompiler -eq "g++" -or $preferredCompiler -eq "clang++") {
$cppStd = "-std=$cppStandard"
Out-Host -InputObject "G++ or Clang++"
& $preferredCompiler $fileName -o $exeName $cppStd $additionalFlags
}
elseif ($preferredCompiler -eq "msvc") {
# cl /EHsc /O1 -std:c++17 test.cpp
$preferredCompiler = "cl"
$cppStd = "-std:$cppStandard"
Out-Host -InputObject "MSVC"
$compilerStatement = "$preferredCompiler /EHsc $fileName /out:$exeName.exe $cppStd $additionalFlags"
Out-Host -InputObject $compilerStatement
& $preferredCompiler /EHsc $fileName /Fe $exeName.exe $cppStd $additionalFlags
}
}
compile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment