-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevWorkSpace.psm1
More file actions
158 lines (132 loc) · 5.17 KB
/
DevWorkSpace.psm1
File metadata and controls
158 lines (132 loc) · 5.17 KB
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
152
153
154
155
156
157
158
function New-Project {
<#
.SYNOPSIS
Powershell script to automate your work flow
.EXAMPLE
New-Project -name "DevWorkSpace" -description "Powershell script to automate your work flow"
Add each of the following to create a private repo, a repo without a wiki page, issues, downloads and to not Auto Initialize the repo
-Private
-NoWiki
-NoIssues
-NoDownloads
-AutoInit
.NOTES
File Name: devworkspace.psm1
Author: Bradon Kelley (@bckelley)
Requires: Powershell Version 5.1.18362.752
The first time you run the script it will create an Environment Variable to save your GitHub Token
.LINK
https://github.com/bckelley/DevWorkSpace
#>
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory, HelpMessage = "Enter the new repository name")]
[ValidateNotNullorEmpty()]
[string]$Name,
[Parameter(Mandatory, HelpMessage = "Enter the new repositories description")]
[ValidateNotNullOrEmpty()]
[string]$Description,
[switch]$Private,
[switch]$NoWiki,
[switch]$NoIssues,
[switch]$NoDownloads,
[switch]$AutoInit,
[Parameter(Mandatory, HelpMessage = "MIT, GPL-3.0 or AGPL-3.0")]
[ValidateSet("MIT","GPL-3.0","AGPL-3.0")]
[string]$License,
[Parameter()]
[string]$UserToken,
[string]$path = "./$name",
#write full native response to the pipeline
[switch]$Raw
)
<#
#
# The follow if statment only runs once unless you delete the
# ENV Variable.
#
#>
if ( -not ( Test-Path Env:DevWorkSpaceTokenGitHub )) {
$UserToken = Read-Host -Prompt "Enter your Github Personal Access Token: "
Write-Host "Writing to ENV VARIABLE ..."
[System.Environment]::SetEnvironmentVariable('DevWorkSpaceTokenGitHub', $UserToken, [System.EnvironmentVariableTarget]::User)
}
if ( ![System.IO.File]::Exists( $path ) ) {
mkdir $path
}
Set-Location $path
$readme = ".\readme.md"
$gitignore = ".\.gitignore"
$title = "Project: $Name`r`n"
$desc = "$Description `r`n"
$licenseDest = ".\License"
<#
This is for choosing license templates
#>
if ( $License -eq "MIT" ) {
$LicenseTemplate = "..\templates\MIT"
} elseif ( $License -eq "AGPL-3.0" ) {
$LicenseTemplate = "..\templates\GNU AGPLv3"
} elseif ( $License -eq "GPL-3.0" ) {
$LicenseTemplate = "..\templates\GNU GPLv3"
} else {
Write-Warning "A License is required!"
}
$repo = "master"
git init -b $repo
Set-Content -Path $readme -value "$title`r`n$desc"
$From = Get-Content -Path "..\templates\gitignore-wordpress"
Add-Content -Path $gitignore -value $From
Copy-Item -Path $LicenseTemplate -Destination $licenseDest
git add $readme $gitignore $copied $licenseDest -A -f
git commit -a -m "Initial Commit"
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "[BEGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*2)$_"}) | Out-String) `n"
$Token = "$($env:DevWorkSpaceTokenGitHub)"
$Base64Token = [System.Convert]::ToBase64String([char[]]$Token)
$head = @{
Authorization = 'Basic {0}' -f $Base64Token
}
$body = @{
name = $Name
description = $Description
private = $Private -AS [boolean]
has_wiki = (-Not $NoWiki)
has_issues = (-Not $NoIssues)
has_downloads = (-Not $NoDownloads)
} | ConvertTo-Json
Write-Verbose "[PROCESS] Sending json"
Write-Verbose $body
if ($PSCmdlet.ShouldProcess("$Name [$Description]")) {
$r = Invoke-RestMethod -Headers $head -Uri https://api.github.com/user/repos -Body $body -Method Post
if ($r.id -AND $Raw) {
Write-Verbose "[PROCESS] Raw result"
$r
} elseif ($r.id) {
Write-Verbose "[PROCESS} Formatted results"
$r | Select-Object @{Name = "Name";Expression = {$_.name}},
@{Name = "Description";Expression = {$_.description}},
@{Name = "Private";Expression = {$_.private}},
@{Name = "Issues";Expression = {$_.has_issues}},
@{Name = "Wiki";Expression = {$_.has_wiki}},
@{Name = "URL";Expression = {$_.html_url}},
@{Name = "Clone";Expression = {$_.clone_url}}
} else {
Write-Warning "Something went wrong with this process"
}
if ($r.clone_url) {
# $msg =
# @"
# To push an existing local repository to Github run these commands:
# -> git remote add $repo $($r.clone_url)"
# -> git push -u $repo master
# "@
# Write-Host $msg -ForegroundColor Green
git remote add $repo $r.clone_url
git push $repo master
}
}
Write-Verbose "[END ] Ending: $($MyInvocation.MyCommand)"
}
Export-ModuleMember -Function New-Project