Gumby.File.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<#
.SYNOPSIS
    Values describing a text file encoding.
#>

enum TextFileEncoding {
    UTF8
    Unicode
    UTF32
    UTF7
    ASCII
}

# change to test publishing automation

<#
.SYNOPSIS
    Gets text file encoding.
 
.PARAMETER Path
    Path and name of the text file whose encoding is to be determined.
 
.OUTPUTS
    'TextFileEncoding' enum value describing the text file encoding.
 
.DESCRIPTION
    The Get-FileEncoding function determines encoding by looking at the Byte Order Mark (BOM).
    It assumes the specified file ('Path' parameter) is an existing, non-zero length file.
#>

function Get-TextFileEncoding ([string] $Path) {
    [byte[]] $bytes = Get-Content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path

    if ($bytes[0] -eq 0xef -and $bytes[1] -eq 0xbb -and $bytes[2] -eq 0xbf) {
        return [TextFileEncoding]::UTF8
    }
    elseif ($bytes[0] -eq 0xfe -and $bytes[1] -eq 0xff) {
        return [TextFileEncoding]::Unicode
    }
    elseif ($bytes[0] -eq 0 -and $bytes[1] -eq 0 -and $bytes[2] -eq 0xfe -and $bytes[3] -eq 0xff) {
        return [TextFileEncoding]::UTF32
    }
    elseif ($bytes[0] -eq 0x2b -and $bytes[1] -eq 0x2f -and $bytes[2] -eq 0x76) {
        return [TextFileEncoding]::UTF7
    }
    else {
        return [TextFileEncoding]::ASCII
    }
}

<#
.SYNOPSIS
Copies a file, creating target directories if needed.
 
.PARAMETER Source
File to copy.
 
.PARAMETER Target
Path to copy the file to.
#>

function CopyFile($Source, $Target) {
    if (!(Test-Path $Source)) { throw "file '$Source' can't be found" }

    $targetPath = Split-Path -Path $Target
    if (!(Test-Path $targetPath)) { md $targetPath | Out-Null }
    Copy-Item $Source $Target
}

<#
.SYNOPSIS
Creates a symbolic link.
 
.PARAMETER Target
Target of the link being created.
 
.PARAMETER Link
Path of the link being created.
#>

function CreateSymbolicLink($Target, $Link) {
    [void](New-Item -Path $Link -ItemType SymbolicLink -Value $Target)
}

function IsSymbolicLink ($Item) {
    if ($Item -is [string]) { $Item = Get-Item $Item }
    return (($null -ne $Item.LinkType) -and ($Item.LinkType -eq 'SymbolicLink'))
}

function IsDirectory($Path) {
    return (Test-Path -PathType Container $Path)
}

<#
.SYNOPSIS
    .
 
.PARAMETER Text
    .
#>

filter IsNotDirectory {
    if($_.Attributes -ne [IO.FileAttributes]::Directory) { $_ }
}

<#
.SYNOPSIS
Sets the read-only property of an item.
 
.PARAMETER Path
Path of the item on which to set the read-only property.
 
.PARAMETER Value
Value of the read-only property.
#>

function SetReadOnly([string] $Path, [bool] $Value = $false) {
    Set-ItemProperty -Name IsReadOnly -Value $Value -Path $Path
}

<#
.SYNOPSIS
Like Test-Path, but with error messages suppressed.
 
.PARAMETER Path
Path to test.
 
.OUTPUTS
Boolean indicating whether the item identified by $Path exists.
#>

function TestPath2([string] $Path) {
    Test-Path -ErrorAction SilentlyContinue $Path
}