PSGitDotfiles.psm1

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
Function Invoke-GitDotfiles {
  <#
  .SYNOPSIS
  Invokes a git command in your dotfiles repository.
 
  .DESCRIPTION
  Invokes a git command in the dotfiles repo stored in ~/.dotfiles/, using
  ~ as the working tree. All arguments are treated as if they were passed
  to the git command directly.
 
  .EXAMPLE
  Invoke-GitDotfiles add .gitconfig
 
  .EXAMPLE
  Invoke-GitDotfiles commit -m 'managing git config'
 
  .EXAMPLE
  Invoke-GitDotfiles push
  #>

  & git --git-dir `"$(Get-Home)/.dotfiles/`" --work-tree=`"$(Get-Home)`" $args
}

Function Initialize-GitDotfiles {
  <#
  .SYNOPSIS
  Initializes a new git dotfies repository.
 
  .DESCRIPTION
  Initializes a new git dotfiles repository as a bare repo in ~/.dotfiles/.
  The ~ will be used as the working tree. Untracked files will not be shown
  by the status command since most of your home directory won't be managed.
 
  .PARAMETER Uri
  URI to the remote you'll use as the origin. This adds the remote but does not push/pull
  or set an upstream.
 
  .EXAMPLE
  Initialize-GitDotfiles
 
  .EXAMPLE
  Initialize-GitDotfiles -Uri https://github.com/ajoberstar/PSGitDotfiles.git
  #>

  [CmdletBinding()]
  Param(
    [string] $Uri
  )
  & git init --bare "$(Get-Home)/.dotfiles/"
  Invoke-GitDotfiles config --local status.showuntrackedfiles no
  Invoke-GitDotfiles remote add origin "$Uri"
}

Function Install-GitDotfiles {
  <#
  .SYNOPSIS
  Installs a new git dotfies repository from a remote.
 
  .DESCRIPTION
  Installs a new git dotfiles repository as a bare repo in ~/.dotfiles/.
  The ~ will be used as the working tree. Untracked files will not be shown
  by the status command since most of your home directory won't be managed.
 
  The repo will be cloned from the provided URI.
 
  .PARAMETER Uri
  URI to the remote to clone from.
 
  .PARAMETER Branch
  Branch to checkout after clone. Defaults to master.
 
  .EXAMPLE
  Install-GitDotfiles -Uri https://github.com/ajoberstar/PSGitDotfiles.git
  #>

  [CmdletBinding()]
  Param(
    [Parameter(Mandatory = $True)]
    [string] $Uri,

    [Parameter()]
    [string] $Branch = "master"
  )
  & git clone --bare "$Uri" "$(Get-Home)/.dotfiles/"
  Invoke-GitDotfiles config --local status.showuntrackedfiles no
  Invoke-GitDotfiles checkout --force $Branch
}

Function Uninstall-GitDotfiles {
  <#
  .SYNOPSIS
  Delete the git dotfiles repository.
 
  .DESCRIPTION
  Delete the git dotfiles repository in ~/.dotfiles/.
 
  .EXAMPLE
  Uninstall-GitDotfiles
  #>

  [CmdletBinding()]
  Param()

  Remove-Item -Path "$(Get-Home)/.dotfiles/" -Recurse -Force
}

Function Get-Home {
  If ($PSVersionTable.Platform -eq 'Unix') {
    $env:HOME
  } Else {
    $env:USERPROFILE
  }
}

New-Alias -Name dot -Value Invoke-GitDotfiles