Initialize-Project.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
<#
.SYNOPSIS
    Project bootstrapping.
#>


#Requires -Version 5
Set-StrictMode -Version Latest

# config files embedded, to simplify packaging as a ps module
$Template_editorconfig = @"
# http://editorconfig.org
 
root = true
 
[*]
# force unix endings and utf-8 everywhere
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
 
indent_style = space
indent_size = 2
 
[Makefile]
# tabs are syntax elements in make
indent_style = tab
 
[*.py]
# PEP 8 mandates multiples of four as indentation
indent_size = 4
 
[*.md]
trim_trailing_whitespace = false
max_line_length = off
 
[*.{bat,ps1,psm1,psd1,ps1xml}]
end_of_line = crlf
 
"@


$Template_gitattributes = @"
# enable git auto handling
* text=auto
 
# force endings for some platform specific files
*.md eol=lf
*.sh eol=lf
*.xml eol=lf
*.json eol=lf
*.sql eol=lf
*.yaml eol=lf
*.yml eol=lf
 
*.ps1 eol=crlf
*.psm1 eol=crlf
*.psd1 eol=crlf
*.ps1xml eol=crlf
*.bat eol=crlf
 
*.pdf -text
 
"@


$Template_gitignore = @"
# intellij #
.idea/
*.iml
*.ipr
*.iws
 
# eclipse #
.settings/
.project
.classpath
 
# vagrant #
.vagrant
 
# osx #
.DS_Store
 
"@


function Initialize-Project {
    <#
    .SYNOPSIS
        Create a git-based project at target location.
    .DESCRIPTION
        Sets up the git repository and imports basic config files.
    #>

    [CmdletBinding()]
    Param([Parameter(Mandatory=$false)][string]$Path = $(Get-Location))

    If (-not $(Test-Path $Path)) {
        New-Item -ItemType Directory $Path
    }

    Push-Location -StackName __Initialize-Project__ $Path
    try {
        git init
        __DumpToUnixFile $Template_editorconfig $(Join-Path $Path '.editorconfig')
        __DumpToUnixFile $Template_gitattributes $(Join-Path $Path '.gitattributes')
        __DumpToUnixFile $Template_gitignore $(Join-Path $Path '.gitignore')
        git add .
    } finally {
        Pop-Location -StackName __Initialize-Project__
    }
}

function __DumpToUnixFile($content, [string] $target) {
    <#
    .SYNOPSIS
    write given string to target file using utf8 encoding without BOM and LF line endings.
      
    .PARAMETER content
    to be written to file
     
    .PARAMETER target
    name of target file
    #>

    $encoding = New-Object System.Text.UTF8Encoding($False)
    $content | ForEach-Object { $_.Replace("`r`n","`n") } | ForEach-Object {
        [System.IO.File]::WriteAllText($target, $_, $encoding)
    }
}