Force textify.ps1 to read files as UTF-8.
[metze/wireshark/wip.git] / tools / textify.ps1
1 #
2 # Textify - Copy text files and make them useful for Windows users.
3 #
4 # Copyright 2013 Gerald Combs <gerald@wireshark.org>
5 #
6 # Wireshark - Network traffic analyzer
7 # By Gerald Combs <gerald@wireshark.org>
8 # Copyright 1998 Gerald Combs
9 #
10 # SPDX-License-Identifier: GPL-2.0-or-later
11
12 #requires -version 2
13
14 <#
15 .SYNOPSIS
16 Text file conversion script for packaging on Windows.
17
18 .DESCRIPTION
19 This script copies a text file from a source to a destination,
20 converting line endings and adding a ".txt" filename extension
21 if needed. If the destination is a directory the source file
22 name is used. Newer files will not be overwritten.
23
24 The destination file should be double-clickable and usable
25 when Notepad is the default editor.
26
27 .PARAMETER Destination
28 Specifies the destination directory for the text files.
29
30 .PARAMETER SourceFiles
31 The names of the files to copy and convert.
32
33 .INPUTS
34 -Destination Destination directory.
35 -SourceFiles List of files.
36
37 .OUTPUTS
38 Copies of input files, UTF8 encoded with Windows line endings and no BOM in the
39 destination directory.
40
41 .EXAMPLE
42 C:\PS> .\tools\textify.ps1 -Destination wireshark-release-staging COPYING
43 #>
44
45 Param(
46     [Parameter(Mandatory=$true, Position=0)]
47     [ValidateScript({Test-Path $_ -PathType 'Container'})]
48     [String]
49     $Destination,
50
51     [Parameter(Mandatory=$true, Position=1, ValueFromRemainingArguments=$true)]
52     [ValidateScript({Test-Path $_ -PathType 'Leaf'})]
53     [String[]]
54     $SourceFiles
55 )
56
57 $no_bom_encoding = New-Object System.Text.UTF8Encoding($False)
58
59 foreach ($src_file in Get-ChildItem $SourceFiles) {
60     if ($Destination) {
61         $base = Split-Path -Leaf $src_file
62         $dst_file = Join-Path $Destination $base
63     } else {
64         $dst_file = $src_file.FullName
65     }
66
67     if (-not $dst_file.EndsWith(".txt")) {
68         $dst_file += ".txt"
69     }
70
71     $src_modtime = (Get-Item $src_file).LastWriteTime
72
73     if (-not (Test-Path $dst_file) -or ((Get-Item $dst_file).LastWriteTime -lt $src_modtime)) {
74         # "Get-Content -Encoding" is undocumented in PS 2.0, but works
75         # here. If it doesn't work elsewhere we can use:
76         # $contents = [System.IO.File]::ReadAllLines($src_file, $no_bom_encoding)
77         $contents = Get-Content -Encoding UTF8 $src_file
78         # We might want to write this out with a BOM in order to improve
79         # the chances of Notepad's UTF-8 heuristics.
80         # https://blogs.msdn.microsoft.com/oldnewthing/20070417-00/?p=27223
81         [System.IO.File]::WriteAllLines($dst_file, $contents, $no_bom_encoding)
82         Write-Host "Textified $src_file to $dst_file"
83     } else {
84         Write-Host "Skipping $src_file"
85     }
86 }