-
Notifications
You must be signed in to change notification settings - Fork 1
/
oJobIO.yaml
193 lines (170 loc) · 7.27 KB
/
oJobIO.yaml
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
# Copyright 2023 Nuno Aguiar
jobs:
######################
- name: IO Append File
help: >
Appends a line to a file. Expects:
- target (String) A text file to which the line will be appended.
- line (String/Array) The line to be appended.
- separator (String) Optional specify a different separator than '\n'.
exec: |
_$(args.target).isString().$_("A target needs to be provided.");
_$(args.line).$_("A line to be append needs to be provided.");
args.separator = _$(args.separator).isString().default("\n");
if (isDef(args.lines) && isUnDef(args.line)) args.line = args.lines;
if (isString(args.line)) args.line = [ args.line ];
for(var ii in args.line) {
io.writeFileString(args.target, args.line[ii] + args.separator, void 0, true);
}
#########################
- name: IO Find & Replace
help: >
Finds and replaces text on a target. Expects:
- target (String) The text file to be changed.
- search (String) The regular expression used for replacing.
- flags (String) Optional regular expression flags for search.
- replace (String) The replace text.
- separator (String) Optional specify a different separator than '\n'.
- byline (Boolean) If true will load the file to memory as a all and changed it, if false will replace line by line (defaults to true).
exec: |
_$(args.target).isString().$_("A target needs to be provided.");
_$(args.search).isString().$_("A search regexp needs to be provided.");
_$(args.replace).isString().$_("A replace regexp needs to be provided.");
args.flags = _$(args.flags).isString().default("");
args.separator = _$(args.separator).isString().default("\n");
args.byline = _$(args.byline).isBoolean().default(true);
if (!args.byline) {
var iss = io.readFileString(args.target);
iss = iss.replace(new RegExp(args.search, args.flags), args.replace);
io.writeFileString(args.target, iss);
} else {
var is = io.readFileStream(args.target);
var os = io.writeFileStream("tmp_" + args.target + ".tmp");
ioStreamReadLines(is, (line) => {
line += args.separator;
ioStreamWrite(os, String(line).replace(new RegExp(args.search, args.flag), args.replace), line.length, false);
}, args.separator, false);
is.close();
os.close();
io.mv("tmp_" + args.target + ".tmp", args.target);
}
##################
- name: IO MV File
help: >
Moves a source to a target. Expects:
- source (String) A source to move from.
- target (String) A target to move to.
exec: |
_$(args.source).isString().$_("A source to copy from needs to be provided.");
_$(args.target).isString().$_("A target to copy to needs to be provided.");
io.mv(args.source, args.target);
##################
- name: IO RM File
help: >
Removes a file or directory recursively. Expects:
- filepath (String) The filepath (file or directory) to remove.
exec: |
_$(args.filepath).isString().$_("A filepath is needed to remove (either file or directory).");
io.rm(args.filepath);
##################
- name: IO CP File
help: >
Copies a source to a target. Expects:
- source (String) A source to copy from.
- target (String) A target to copy to.
exec: |
_$(args.source).isString().$_("A source to copy from needs to be provided.");
_$(args.target).isString().$_("A target to copy to needs to be provided.");
io.cp(args.source, args.target);
#####################
- name: IO List files
help: >
List files from a local filepath to args.files array. Expects:
- path (String) The filepath to list files from.
- recursive (Boolean) Recursive file list (defaults false)
exec: |
_$(args.path).isString().$_("Please provide a path.");
args.recursive = _$(args.recursive).isBoolean().default(false);
if (args.recursive) {
args.files = io.listFilesRecursive(args.path);
} else {
args.files = io.listFiles(args.path).files;
}
#########################
- name: IO List filenames
help: >
List filenames from a local filepath to args.files array. Expects:
- path (String) The filepath to list files from.
- fullpath (Boolean) Include the fullpath with each file (defaults: true)
exec: |
_$(args.path).isString().$_("Please provide a path.");
args.fullpath = _$(args.fullpath).isBoolean().default(true);
args.files = io.listFilenames(args.path, args.fullpath);
# =========================
- name: IO Modify text file
help: >
Given a file tries to find the first string 'find' and replace with 'replace'. If the file doesn't exist it will create it. It differs from the "IO Find & Replace" job as it doesn't use regular expressions and just looks for exact matches which is usually more pratical for text configuration files. Expects:
- file (String) The file for find/replace
- find (String) The string to find
- replace (String) The string to replace
exec: |
_$(args.file, "file").$_();
_$(args.find, "find").$_();
args.op = "check";
if (!io.fileExists(args.file)) {
args.op = "create";
} else {
args.op = "append";
var lst = args.find.split(/\n/); lst.pop();
var found = false, pfound = false, ptarget = lst.length, target = 0;
var stream = io.readFileStream(args.file);
ioStreamReadLines(stream, line => {
if (line == lst[lst.length - ptarget]) {
ptarget--;
pfound = true;
} else {
pfound = false;
ptarget = lst.length;
}
if (pfound && ptarget <= 0) {
found = true;
args.op = "replace";
target = target - (lst.length -1);
return true;
}
target++;
});
args.found = found;
args.target = target;
}
switch(args.op) {
case "create" :
io.writeFileString(args.file, args.replace);
break;
case "replace" :
var ft = io.createTempFile("tmp_" + nowNano() + ".txt");
var wstream = io.writeFileStream(ft);
var rstream = io.readFileStream(args.file);
var cline = 0, cignore = false, csource = args.find.split(/\n/).length -1;
ioStreamReadLines(rstream, line => {
if (!cignore && cline == args.target) {
ioStreamWrite(wstream, args.replace);
cline++;
csource--;
if (csource > 0) cignore = true;
} else {
if (!cignore) {
ioStreamWrite(wstream, line + "\n");
cline++;
} else {
csource--;
if (csource <= 0) { cignore = false; };
}
}
});
io.mv(ft, args.file);
break;
case "append":
io.writeFileString(args.file, args.replace, void 0, true);
break;
}