-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.sh
160 lines (147 loc) · 4.83 KB
/
pull.sh
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
#!/usr/bin/env bash
# Copyright (C) 2023 Duck McSouls
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Generate the script pull.js, which is used to download all scripts into our
# game.
# Output JavaScript code to this file.
SCRIPT=pull.js && declare -r SCRIPT
# Prefix, i.e. everything before the tree structure of script files.
cat << EOF > "$SCRIPT"
/**
* Copyright (C) 2023 Duck McSouls
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// NOTE: This script is auto-generated by pull.sh.
// Do not import anything into this script. The script should be self-contained
// and independent.
/**
* A function for assertion.
*
* @param {boolean} cond Assert that this condition is true.
* @returns {exception} Throw an error if the given condition is false.
*/
function assert(cond) {
if (!cond) {
throw new Error("Assertion failed.");
}
}
/**
* The directory structure under "src/" on github.com.
*
* @returns {array<string>} All files under "src/" on github.com.
*/
function dir_structure() {
const filesystem = [
// Insert directory tree here. Should contain sample scripts for
// playing Bitburner.
EOF
# The tree structure of script files.
find src/ | grep "\.js" | sed s/^"src\/"/' "'/g | sed s/$/'",'/g >> "$SCRIPT"
# Suffix, i.e. everything after the tree structure of script files.
cat << EOF >> "$SCRIPT"
];
assert(filesystem.length > 0);
return filesystem;
}
/**
* A formatted name of the file where we want to save the downloaded file. The
* terminal command "wget" behaves differently from the API function
* "ns.wget()". The command "wget" is happy to create the required directory
* if we do any of the following:
*
* wget /URL/to/src/file.js src/file.js
* wget /URL/to/src/file.js /src/file.js
*
* The API function "ns.wget()" is happy with this
*
* await ns.wget("/URL/to/src/file.js", "/src/file.js", "home");
*
* but cannot handle this
*
* await ns.wget("/URL/to/src/file.js", "src/file.js", "home");
*
* That is, we must have the leading forward slash "/" character for the
* function to work properly. Here are the relevant issues on github.com:
*
* https://github.com/danielyxie/bitburner/issues/1935
* https://github.com/danielyxie/bitburner/issues/2115
*
* @param {string} f A file name. Cannot be empty string.
* @returns {string} A possibly new file name with the leading forward slash "/"
* character added.
*/
function target_name(f) {
assert(f !== "");
const fname = f.toString();
const prefix = "/quack";
const slash = "/";
return [prefix, slash, fname].join("");
}
/**
* Print the usage information.
*
* @param {NS} ns The Netscript API.
*/
function usage(ns) {
const msg = "Usage: run pull.js";
ns.tprintf(msg);
}
/**
* Pull all files (on github.com) under the directory quacksouls/bitwalk/src
* into the game.
*
* Usage: run pull.js
*
* @param {NS} ns The Netscript API.
*/
export async function main(ns) {
// Sanity check.
// The script does not accept any command line arguments.
if (ns.args.length > 0) {
usage(ns);
return;
}
// Pull files into our home server.
const home = "home";
// The base URL where files are found.
const github = "https://raw.githubusercontent.com/";
const quack = "quacksouls/bitwalk/main/src/";
const prefix = github + quack;
// Pull files into home server.
for (const f of dir_structure()) {
const file = prefix + f;
const target = target_name(f);
const success = await ns.wget(file, target, home);
if (success) {
ns.tprintf(file);
}
}
const msg = "Download complete";
ns.tprintf(msg);
ns.toast(msg);
}
EOF