Skip to content

Instantly share code, notes, and snippets.

@jonathanpeppers
Last active April 7, 2020 19:07
Show Gist options
  • Save jonathanpeppers/153411a4c81d3dc8ee5f5524ec6c3047 to your computer and use it in GitHub Desktop.
Save jonathanpeppers/153411a4c81d3dc8ee5f5524ec6c3047 to your computer and use it in GitHub Desktop.
My powershell profile I've been using
Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1'
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile))
{
Import-Module "$ChocolateyProfile"
}
### Jon Peppers ###
# Variables
$xa = 'C:\src\xamarin-android'
$xf = 'C:\src\Xamarin.Forms'
$buildtools = 'C:\Users\jopepper\android-toolchain\sdk\build-tools\29.0.0'
$platformtools = 'C:\Users\jopepper\android-toolchain\sdk\platform-tools'
$keystore = "$env:localappdata\Xamarin\Mono for Android\debug.keystore"
$java_home = 'C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\bin'
# Aliases
Set-Alias notepad 'C:\Program Files\Notepad++\notepad++.exe'
Set-Alias msbuild15 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe'
Set-Alias msbuild 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe'
Set-Alias msbuild16 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe'
Set-Alias msbuild-pre 'C:\Program Files (x86)\Microsoft Visual Studio\2019\PublicPreview\MSBuild\Current\Bin\MSBuild.exe'
Set-Alias msbuild-int 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\MSBuild.exe'
Set-Alias msbuild-master 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin\MSBuild.exe'
Set-Alias adb "$platformtools\adb.exe"
Set-Alias dexdump "$buildtools\dexdump.exe"
Set-Alias xabuild "$xa\bin\Debug\bin\xabuild.exe"
Set-Alias xabuildr "$xa\bin\Release\bin\xabuild.exe"
Set-Alias xabuildc "$xa\bin\Control\bin\xabuild.exe"
Set-Alias monodroid 'C:\src\monodroid\external\xamarin-android\bin\Debug\bin\xabuild.exe'
Set-Alias 7z 'C:\Program Files\7-Zip\7z.exe'
Set-Alias java "$java_home\java.exe"
Set-Alias javac "$java_home\javac.exe"
Set-Alias javap "$java_home\javap.exe"
Set-Alias keytool "$java_home\keytool.exe"
Set-Alias grep Select-String
Set-Alias xperf 'C:\tools\xperf\xperf.exe'
Set-Alias nuget 'C:\tools\NuGet.exe'
Set-Alias jit-times 'C:\src\xamarin-android\bin\Debug\lib\xamarin.android\xbuild\Xamarin\Android\jit-times.exe'
Set-Alias ilspy 'C:\ProgramData\chocolatey\bin\ILSpy.exe'
# Functions
$vsbat = 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat'
function vscmd()
{
& cmd /k "$vsbat"
}
function git-nuke()
{
& git clean -dxf
& git submodule foreach --recursive 'git clean -dxf'
}
function bundletool()
{
& java -jar "$xa\bin\Debug\lib\xamarin.android\xbuild\Xamarin\Android\bundletool.jar" $args
}
function logcat()
{
& adb logcat -c
& adb logcat
}
function sign([string] $apk)
{
$signed = $apk.Replace('.apk', '-signed.apk')
Remove-Item $signed -ErrorAction SilentlyContinue
& "$buildtools\zipalign.exe" -p 4 "$apk" "$signed"
& "$buildtools\apksigner.bat" sign --ks "$keystore" --ks-pass pass:android -key-pass pass:android --ks-key-alias androiddebugkey "$signed" JAVA_HOME=$java_home
}
function apksigner()
{
$old_JAVA_HOME = $env:JAVA_HOME
$env:JAVA_HOME = [System.IO.Path]::GetDirectoryName("$java_home")
& "$buildtools\apksigner.bat" @args
$env:JAVA_HOME = $old_JAVA_HOME
}
function gradlew()
{
java -classpath "$xa\build-tools\gradle\gradle\wrapper\gradle-wrapper.jar" org.gradle.wrapper.GradleWrapperMain $args
}
function msbuild-oop-start()
{
Stop-Process -Name MSBuild -ErrorAction SilentlyContinue
Stop-Process -Name xabuild -ErrorAction SilentlyContinue
$env:MSBUILDNOINPROCNODE=1
Write-Host 'MSBuild out-of-process mode enabled :)'
}
function msbuild-oop-stop()
{
Stop-Process -Name MSBuild -ErrorAction SilentlyContinue
Stop-Process -Name xabuild -ErrorAction SilentlyContinue
$env:MSBUILDNOINPROCNODE=''
Write-Host 'MSBuild out-of-process mode disabled :)'
}
function build-xamarin-forms ()
{
& msbuild .\Xamarin.Forms.sln /t:Restore /bl:restore.binlog
& msbuild .\Xamarin.Forms.Build.Tasks\Xamarin.Forms.Build.Tasks.csproj /bl:buildtasks.binlog
& msbuild .\Xamarin.Forms.ControlGallery.Android\Xamarin.Forms.ControlGallery.Android.csproj /t:SignAndroidPackage /bl:android.binlog
}
function copy-shared-runtime ([string] $configuration = 'Debug')
{
$src = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Xamarin\Android'
$dest = ".\bin\$configuration\lib\xamarin.android\xbuild\Xamarin\Android\"
Copy-Item "$src\*.apk" $dest
Copy-Item "$src\Mono.Android.DebugRuntime-debug.xml" $dest
}
function copy-random-files ([string] $configuration = 'Debug')
{
$src = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Xamarin\Android'
$dest = ".\bin\$configuration\lib\xamarin.android\xbuild\Xamarin\Android\"
Copy-Item "$src\jar2xml.jar" $dest
Copy-Item "$src\INIFileParser.dll" $dest
Copy-Item "$src\Xamarin.Installer.*" $dest
}
function touch ([string] $path)
{
if (Test-Path $path -PathType Leaf)
{
$date = (Get-Date)
$date = $date.ToUniversalTime()
$file = Get-Item $path
$file.LastAccessTimeUtc = $date
$file.LastWriteTimeUtc = $date
}
else
{
New-Item $path -ItemType File
}
}
function xperf-start
{
$stack='Profile+CSwitch+ReadyThread+FileCreate+FileClose'
& xperf -on Base+Diag+Latency+FileIO+DISPATCHER+REGISTRY+PERF_COUNTER -stackWalk $stack -f Kernel.etl -BufferSize 1024
& xperf -start AMXperf -on e4b70372-261f-4c54-8fa6-a5a7914d73da+cfeb0608-330e-4410-b00d-56d8da9986e6+8e92deef-5e17-413b-b927-59b2f06a3cfc+751ef305-6c6e-4fed-b847-02ef79d26aef+0A002690-3839-4E3A-B3B6-96D8DF868D99 -f amxperftrace.etl
}
function xperf-stop
{
& xperf -stop -stop AMXperf -d Merged.etl
& "$env:ProgramFiles\Windows Defender\mpcmdrun.exe" -getfiles
}
function dir-size ([string] $dir = '.')
{
"{0:N2} MB" -f ((Get-ChildItem $dir -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB)
}
function redirect ([Parameter(Mandatory=$true)][string] $url)
{
$request = [System.Net.WebRequest]::Create($url)
$request.AllowAutoRedirect = $false
$response = $request.GetResponse()
$response.GetResponseHeader("Location")
}
function Publish-GooglePlay
{
param (
[Parameter(Mandatory=$true)]
[string] $project,
[string] $target = 'SignAndroidPackage',
[string] $configuration = 'Release',
[string] $keystoreFile = 'C:\src\peppers.jks',
[string] $alias = 'peppers',
[string] $storePass = 'peppers',
[string] $keyPass = 'peppers',
[string] $msbuild = 'msbuild'
)
& $msbuild $project /t:$target /restore /bl /p:Configuration=$configuration /p:AndroidPackageFormat=aab /p:AndroidKeyStore=True /p:AndroidSigningKeyStore=$keystoreFile /p:AndroidSigningStorePass=$storePass /p:AndroidSigningKeyAlias=$alias /p:AndroidSigningKeyPass=$keyPass
}
enum App
{
HelloWorld
XamarinForms
HelloForms
}
function Get-Project ([Parameter(Mandatory=$true)][App] $App)
{
if ($App -eq 'HelloWorld')
{
return "$xa\samples\HelloWorld\HelloWorld.csproj"
}
if ($App -eq 'XamarinForms')
{
return "$xa\tests\Xamarin.Forms-Performance-Integration\Droid\Xamarin.Forms.Performance.Integration.Droid.csproj"
}
if ($App -eq 'HelloForms')
{
return "$xf\samples\HelloForms.Android\HelloForms.Android.csproj"
}
}
function Get-CleanDir ([Parameter(Mandatory=$true)][App] $App)
{
if ($App -eq 'HelloWorld')
{
return "$xa\samples\"
}
if ($App -eq 'XamarinForms')
{
return "$xa\tests\Xamarin.Forms-Performance-Integration\"
}
if ($App -eq 'HelloForms')
{
return "$xf\samples\"
}
}
function Get-PackageName ([Parameter(Mandatory=$true)][App] $App)
{
if ($App -eq 'HelloWorld')
{
return 'com.xamarin.android.helloworld'
}
if ($App -eq 'XamarinForms')
{
return 'Xamarin.Forms_Performance_Integration'
}
if ($App -eq 'HelloForms')
{
return "com.xamarin.forms.helloforms"
}
}
function Get-Activity ([Parameter(Mandatory=$true)][App] $App)
{
if ($App -eq 'HelloWorld')
{
return 'example.MainActivity'
}
if ($App -eq 'XamarinForms')
{
return 'xamarin.forms.performance.integration.MainActivity'
}
if ($App -eq 'HelloForms')
{
return "com.xamarin.forms.MainActivity"
}
}
function Profile-Startup
{
param (
[Parameter(Mandatory=$true)]
[App] $App,
[string] $msbuild = 'xabuildr',
[int] $iterations = 10
)
$project = Get-Project -App $App
$clean = Get-CleanDir -App $App
$package = Get-PackageName -App $App
$extraProps = ''
& git clean -dfx $clean
# We need a huge logcat buffer
& adb logcat -G 15M
& adb logcat -c
& $msbuild $project /t:SignAndroidPackage /r /bl /t:Install,_Run $extraProps
for ($i=0; $i -le $iterations; $i++) {
Start-Sleep -Seconds 5
& adb shell am force-stop $package
& $msbuild $project /t:_Run $extraProps
}
& adb logcat -d > .\adb.txt
& adb-grep-timing
}
function simpleperf ([Parameter(Mandatory=$true)][App] $App)
{
$activity = Get-Activity -App $App
$package = Get-PackageName -App $App
$ndk = 'C:\Users\jopepper\android-toolchain\ndk'
$simplperfdir = "$ndk\simpleperf"
Remove-Item -Recurse .\binary_cache\
Remove-Item .\perf.data
Write-Host "Package $package, Activity: $activity"
#-r "-e task-clock:u -f 1000 -g --duration 10"
& python "$simplperfdir\app_profiler.py" -p $package -a $activity --ndk_path="$ndk"
& python "$simplperfdir\report.py"
& python "$simplperfdir\report.py" --sort dso
& python "$simplperfdir\report.py" -g --gui
}
function adb-grep-timing()
{
& adb logcat -d | C:\tools\AdbLogcatTiming\AdbLogcatTiming.exe
}
function pull-profile-data([Parameter(Mandatory=$true)][string] $package)
{
$src = 'files/.__override__/profile.mlpd'
$dest = '/sdcard/Download/profile.mlpd'
& adb shell run-as $package ls -la $src
& adb shell run-as $package mv $src $dest
& adb shell ls -la $dest
& adb pull $dest
& adb shell rm $dest
}
function mprof-report([string] $file = 'profile.mlpd', [string] $sort = 'total')
{
& bash -c "mprof-report --verbose --method-sort=$sort $file"
}
function create-forms-nuget([string] $configuration = 'Release', [string] $version = '9.9.1')
{
Push-Location -Path C:\src\Xamarin.Forms\.nuspec
& nuget pack Xamarin.Forms.nuspec -properties "configuration=$configuration;platform=anycpu" -Version $version
Pop-Location
}
# TODO: doesn't work yet
#function vsixinstaller($path) {
# & cmd /c "$vsbat" vsixinstaller.exe /a "$path"
#}
@jonathanpeppers
Copy link
Author

jonathanpeppers commented Jan 29, 2019

How to?

In your current powershell session, edit your profile with:

notepad $profile

Edit/save, to reload in current session:

.$profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment