-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandori.pm
264 lines (195 loc) · 4.46 KB
/
bandori.pm
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env false
# To use this module without install:
# use FindBin;
# use lib "$FindBin::Bin/relative/path/to/this/dir";
# use bandori;
package bandori;
use strict;
use warnings;
use Cwd qw(getcwd cwd abs_path);
use File::Basename;
use File::Spec;
use IPC::Open3;
use JSON::PP;
require Exporter;
our @ISA = qw(Exporter);
# Exported to module namespace
our @EXPORT_OK = qw();
# Exported to global and module namespace
our @EXPORT = qw(
readAllText
writeAllText
appendAllText
enumerateFiles
enumerateDirectories
enumerateFileSystemEntries
getRealPath
streamReadToEnd
invokeProcess
trim
trimStart
trimEnd
deserializeObjectJson
serializeObjectJson
);
our $VERSION = '0.01';
#
# File operations
#
# Opens a text file, reads all the text in the file into a string, and then closes the file.
# If the target file cannot be read and a default value is provided, the default value is returned instead.
sub readAllText
{
my ($path, $default) = @_;
open(my $fd, '<', $path) or do {
return $default if (defined $default);
die "Could not open $path: $!\n";
};
my $text = do { local $/; <$fd> };
close($fd);
return $text;
}
# Creates a new file, write the contents to the file, and then closes the file.
# If the target file already exists, it is truncated and overwritten.
sub writeAllText
{
my ($path, $text) = @_;
open(my $fd, '>', $path) or die "Could not open $path: $!\n";
$fd->print($text);
close($fd);
}
# Appends the specified string to the file, creating the file if it does not already exist.
sub appendAllText
{
my ($path, $text) = @_;
open(my $fd, '>>', $path) or die "Could not open $path: $!\n";
$fd->print($text);
close($fd);
}
#
# Directory Operations
#
# Enumerate the given path and return the path to the containing files
sub enumerateFiles
{
my ($path) = @_;
opendir(my $dir, $path) or die "Could not enumerate $path: $!\n";
my @files = grep{ -f $_ } map{ abs_path("$path/$_") } readdir($dir);
closedir($dir);
return \@files;
}
# Enumerate the given path and return the path to the containing directories
sub enumerateDirectories
{
my ($path) = @_;
opendir(my $dir, $path) or die "Could not enumerate $path: $!\n";
my @files = grep{ -d $_ } map{ abs_path("$path/$_") } grep{ ($_ ne ".") && ($_ ne "..") } readdir($dir);
closedir($dir);
return \@files;
}
# Enumerate the given path and return the path to the containing file system entries
sub enumerateFileSystemEntries
{
my ($path) = @_;
opendir(my $dir, $path) or die "Could not enumerate $path: $!\n";
my @files = map{ abs_path("$path/$_") } grep{ ($_ ne ".") && ($_ ne "..") } readdir($dir);
closedir($dir);
return \@files;
}
# Get the real directory and file path of the given file.
sub getRealPath
{
my ($script) = @_;
my $bin;
my $dir;
# Ensure that we are dealing with a file
unless (-f $script) {
return "";
}
# Expand to absolute path
unless (File::Spec->file_name_is_absolute($script)) {
my $cwd = getcwd();
$cwd = cwd() unless(defined $cwd);
$script = File::Spec->catfile(getcwd(), $script)
}
# Resolve $script if it is a link
while (1) {
my $linktext = readlink($script);
($bin, $dir) = fileparse($script);
last unless defined $linktext;
$script = (File::Spec->file_name_is_absolute($linktext))
? $linktext
: File::Spec->catfile($dir, $linktext);
}
$dir = abs_path($dir) if ($dir);
return ($dir, $bin);
}
#
# Stream Operations
#
# Reads all characters from the current position to the end of the stream.
# To use with STDIN, pass \*STDIN as parameter.
sub streamReadToEnd
{
my ($fd) = @_;
my $text = do { local $/; <$fd> };
return $text;
}
#
# Process Operations
#
sub invokeProcess
{
my ($interactive, @args) = @_;
my $retc = 0;
my @stdout;
my @stderr;
unless ($interactive) {
my $pid = open3(undef, \*SUB_OUT, \*SUB_ERR, @args);
return (1, [], []) if (!$pid);
waitpid($pid, 0);
$retc = $?;
chomp(@stdout = <SUB_OUT>);
chomp(@stderr = <SUB_ERR>);
close(SUB_OUT);
close(SUB_ERR);
}
else {
$retc = system(@args);
}
return ($retc >> 8, \@stdout, \@stderr);
}
#
# String Operations
#
sub trim
{
my ($str) = @_;
$str =~ s/^\s+|\s+$//;
return $str;
}
sub trimStart
{
my ($str) = @_;
$str =~ s/^\s+//;
return $str;
}
sub trimEnd
{
my ($str) = @_;
$str =~ s/\s+$//;
return $str;
}
#
# Json Operations
#
sub deserializeObjectJson
{
my ($json) = @_;
return JSON::PP->new->pretty(1)->utf8->decode($json);
}
sub serializeObjectJson
{
my ($data) = @_;
return JSON::PP->new->pretty(1)->utf8->encode($data);
}