-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon-functions.ps1
151 lines (126 loc) · 4.89 KB
/
common-functions.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# pmcgrath @ 20/03/2010
# See Profile_Setup_Readme.txt - to make changes effective . $profile.allusersallhosts
# Common functions - should be the first file sourced
function Connect-ToEc2Instance
(
[string] $machineName, # Dns name i.e. ec2-54-228-14-193.eu-west-1.compute.amazonaws.com
[string] $keyFilePath = "$($env:home)\.aws\$($env:username)_aws_keys.ppk"
)
{
# Using putty instead of ssh directly as vim is pretty unusable when connecting using the powershell and ssh combination
putty.exe -ssh -i $keyFilePath "ubuntu@$machineName";
}
function Extend-EnvironmentPath
(
[string[]] $additions
)
{
$additions | % { if (! ($env:Path.Contains($_))) { if (test-path $_) { $env:Path += ";$_"; } } } # Can see existing with $env:Path.Split(';')
}
function Start-SshAgent
{
# This is based on https://github.com/dahlbyk/posh-git
# Ensure ssh agent is already running - will use one per session currently
if (-not $env:SSH_AGENT_PID)
{
ssh-agent | ? { $_ -match '(?<key>[^=]+)=(?<value>[^;]+);' | % { $key = $matches['key']; $value =$matches['value']; set-item env:$key $value; } }
}
}
function Add-SshAgentEntry
(
[string] $keyPath = "$home\.ssh\id_rsa"
)
{
if (-not $env:SSH_AGENT_PID) { throw 'Ssh agent not started !'; }
if (-not (test-path $keyPath)) { throw "keyPath [$keyPath] does not exist !"; }
if (-not (ssh-add -l | ? { $_.contains($keyPath) })) { ssh-add $keyPath; }
}
function Set-RubyEnvironment
(
[string] $version = '2.0.0-p195'
)
{
$newRubyEnvironmentPath = "d:\utilities\rubies\ruby-$version\bin";
if (! (test-path $newRubyEnvironmentPath)) { throw "Path for ruby v$version does not exist ($newRubyEnvironmentPath)"; }
# Can use this to see current version if any's load paths - helps to identify the location : ruby -e 'puts $:'
$existingRubyPath = $env:path.Split(';') | ? { $_ -like "*\ruby*\bin" } | select -first 1; # Ignores the fact that you may have multiple path entries for ruby
if ($existingRubyPath) { $env:Path = $env:Path.Replace($existingRubyPath, $newRubyEnvironmentPath); }
else { Extend-EnvironmentPath @($newRubyEnvironmentPath); }
}
function Test-IsCurrentUserAnAdministrator
{
(new-object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole('Administrators');
}
function Get-GitDirectoryPath
{
# Find .git directory if any in parental hierarchy, null if none found
$directory = get-item .;
while ($directory -ne $null)
{
$potentialGitDirectoryPath = join-path $directory.FullName '.git';
if (test-path $potentialGitDirectoryPath) { return $potentialGitDirectoryPath; }
$directory = $directory.Parent;
}
return $null;
}
function Get-GitBranchCount
(
[string] $gitDirectoryPath
)
{
# Was using "return @(dir (join-path $gitDirectoryPath '/refs/heads') -file).Length;" but git gc deletes files in /ref/heads as part of clean up
return @(git branch).length;
}
function Get-GitBranchName
(
[string] $gitDirectoryPath
)
{
$branchNameLine = get-content (join-path $gitDirectoryPath 'HEAD') | select -first 1;
return $branchNameLine.Replace('ref: refs/heads/', '');
}
function Get-GitInformation
{
# This stuff is just content i have extracted from https://github.com/dahlbyk/posh-git
# It is a much simplier version, which does not cater for all the stuff they do and certainly not submodules etc.
$gitDirectoryPath = Get-GitDirectoryPath;
if (-not $gitDirectoryPath) { return $null; }
$branchCount = Get-GitBranchCount $gitDirectoryPath;
$branchName = Get-GitBranchName $gitDirectoryPath;
if ($branchCount -gt 1) { return "$branchCount*$branchName"; }
return $branchName;
}
function Widen-Window
(
[int] $resizeBy = 25
)
{
$resizeByFactor = 1 + ($resizeBy / 100);
$bufferSize = $host.UI.RawUI.BufferSize;
$windowSize = $host.UI.RawUI.WindowSize;
$bufferSize.Width = $bufferSize.Width * $resizeByFactor;
$windowSize.Width = $windowSize.Width * $resizeByFactor;
$host.UI.RawUI.BufferSize = $bufferSize;
$host.UI.RawUI.WindowSize = $windowSize;
}
function prompt()
{
# If not a file system then cannot be a git reposiotry so exit with default prompt
if (($executionContext.SessionState.Path.CurrentLocation).Provider.Name -ne 'FileSystem')
{
# Can use the following to get existing prompt "(dir function:prompt).Definition" which is what we are returning here
return "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1))";
}
# File system, so could be a git repository
$gitInformation = Get-GitInformation;
$originalForegroundColour = $host.UI.RawUI.ForegroundColor;
write-host "PS $pwd" -nonewline;
if ($gitInformation)
{
$host.UI.RawUI.ForegroundColor = 'DarkCyan';
write-host " [$gitInformation] " -nonewline;
$host.UI.RawUI.ForegroundColor = $originalForegroundColour;
};
# Must return a string
'>';
}