forked from clibs/clib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclib-install.c
337 lines (279 loc) · 7.2 KB
/
clib-install.c
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//
// clib-install.c
//
// Copyright (c) 2012-2014 clib authors
// MIT licensed
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "asprintf/asprintf.h"
#include "fs/fs.h"
#include "tempdir/tempdir.h"
#include "commander/commander.h"
#include "clib-package/clib-package.h"
#include "http-get/http-get.h"
#include "logger/logger.h"
#include "debug/debug.h"
#include "parson/parson.h"
#include "str-concat/str-concat.h"
#include "str-replace/str-replace.h"
#include "version.h"
debug_t debugger;
struct options {
const char *dir;
int verbose;
int dev;
int save;
int savedev;
};
static struct options opts;
/**
* Option setters.
*/
static void
setopt_dir(command_t *self) {
opts.dir = (char *) self->arg;
debug(&debugger, "set dir: %s", opts.dir);
}
static void
setopt_quiet(command_t *self) {
opts.verbose = 0;
debug(&debugger, "set quiet flag");
}
static void
setopt_dev(command_t *self) {
opts.dev = 1;
debug(&debugger, "set development flag");
}
static void
setopt_save(command_t *self) {
opts.save = 1;
debug(&debugger, "set save flag");
}
static void
setopt_savedev(command_t *self) {
opts.savedev = 1;
debug(&debugger, "set savedev flag");
}
/**
* Install dependency packages at `pwd`.
*/
static int
install_local_packages() {
if (-1 == fs_exists("./package.json")) {
logger_error("error", "Missing package.json");
return 1;
}
debug(&debugger, "reading local package.json");
char *json = fs_read("./package.json");
if (NULL == json) return 1;
clib_package_t *pkg = clib_package_new(json, opts.verbose);
if (NULL == pkg) goto e1;
int rc = clib_package_install_dependencies(pkg, opts.dir, opts.verbose);
if (-1 == rc) goto e2;
if (opts.dev) {
rc = clib_package_install_development(pkg, opts.dir, opts.verbose);
if (-1 == rc) goto e2;
}
free(json);
clib_package_free(pkg);
return 0;
e2:
clib_package_free(pkg);
e1:
free(json);
return 1;
}
#define E_FORMAT(...) ({ \
rc = asprintf(__VA_ARGS__); \
if (-1 == rc) goto cleanup; \
});
static int
executable(clib_package_t *pkg) {
int rc;
char *url = NULL;
char *file = NULL;
char *tarball = NULL;
char *command = NULL;
char *dir = NULL;
char *deps = NULL;
char *tmp = NULL;
char *reponame = NULL;
debug(&debugger, "install executable %s", pkg->repo);
tmp = gettempdir();
if (NULL == tmp) {
logger_error("error", "gettempdir() out of memory");
return -1;
}
if (!pkg->repo) {
logger_error("error", "repo field required to install executable");
return -1;
}
reponame = strrchr(pkg->repo, '/');
if (reponame && *reponame != '\0') reponame++;
else {
logger_error("error", "malformed repo field, must be in the form user/pkg");
return -1;
}
E_FORMAT(&url
, "https://github.com/%s/archive/%s.tar.gz"
, pkg->repo
, pkg->version);
E_FORMAT(&file, "%s-%s.tar.gz", reponame, pkg->version);
E_FORMAT(&tarball, "%s/%s", tmp, file);
rc = http_get_file(url, tarball);
E_FORMAT(&command, "cd %s && gzip -dc %s | tar x", tmp, file);
debug(&debugger, "download url: %s", url);
debug(&debugger, "file: %s", file);
debug(&debugger, "tarball: %s", tarball);
debug(&debugger, "command: %s", command);
// cheap untar
rc = system(command);
if (0 != rc) goto cleanup;
E_FORMAT(&dir, "%s/%s-%s", tmp, reponame, pkg->version);
debug(&debugger, "dir: %s", dir);
if (pkg->dependencies) {
E_FORMAT(&deps, "%s/deps", dir);
debug(&debugger, "deps: %s", deps);
rc = clib_package_install_dependencies(pkg, deps, opts.verbose);
if (-1 == rc) goto cleanup;
}
free(command);
command = NULL;
E_FORMAT(&command, "cd %s && %s", dir, pkg->install);
debug(&debugger, "command: %s", command);
rc = system(command);
cleanup:
free(tmp);
free(dir);
free(command);
free(tarball);
free(file);
free(url);
return rc;
}
#undef E_FORMAT
/**
* Writes out a dependency to package.json
*/
static int
write_dependency(clib_package_t *pkg, char* prefix) {
JSON_Value *packageJson = json_parse_file("package.json");
JSON_Object *packageJsonObject = json_object(packageJson);
JSON_Value *newDepSectionValue = NULL;
if (NULL == packageJson || NULL == packageJsonObject) return 1;
// If the dependency section doesn't exist then create it
JSON_Object *depSection = json_object_dotget_object(packageJsonObject, prefix);
if (NULL == depSection) {
newDepSectionValue = json_value_init_object();
depSection = json_value_get_object(newDepSectionValue);
json_object_set_value(packageJsonObject, prefix, newDepSectionValue);
}
// Add the dependency to the dependency section
json_object_set_string(depSection, pkg->repo, pkg->version);
// Flush package.json
int retCode = json_serialize_to_file_pretty(packageJson, "package.json");
json_value_free(packageJson);
return retCode;
}
/**
* Save a dependency to package.json.
*/
static int
save_dependency(clib_package_t *pkg) {
debug(&debugger, "saving dependency %s at %s", pkg->name, pkg->version);
return write_dependency(pkg, "dependencies");
}
/**
* Save a development dependency to package.json.
*/
static int
save_dev_dependency(clib_package_t *pkg) {
debug(&debugger, "saving dev dependency %s at %s", pkg->name, pkg->version);
return write_dependency(pkg, "development");
}
/**
* Create and install a package from `slug`.
*/
static int
install_package(const char *slug) {
int rc;
clib_package_t *pkg = clib_package_new_from_slug(slug, opts.verbose);
if (NULL == pkg) return -1;
if (pkg->install) {
rc = executable(pkg);
goto check_save;
}
rc = clib_package_install(pkg, opts.dir, opts.verbose);
if (0 == rc && opts.dev) {
rc = clib_package_install_development(pkg, opts.dir, opts.verbose);
}
check_save:
if (opts.save) save_dependency(pkg);
if (opts.savedev) save_dev_dependency(pkg);
clib_package_free(pkg);
return rc;
}
/**
* Install the given `pkgs`.
*/
static int
install_packages(int n, char *pkgs[]) {
for (int i = 0; i < n; i++) {
debug(&debugger, "install %s (%d)", pkgs[i], i);
if (-1 == install_package(pkgs[i])) return 1;
}
return 0;
}
/**
* Entry point.
*/
int
main(int argc, char *argv[]) {
#ifdef _WIN32
opts.dir = ".\\deps";
#else
opts.dir = "./deps";
#endif
opts.verbose = 1;
opts.dev = 0;
debug_init(&debugger, "clib-install");
command_t program;
command_init(&program
, "clib-install"
, CLIB_VERSION);
program.usage = "[options] [name ...]";
command_option(&program
, "-o"
, "--out <dir>"
, "change the output directory [deps]"
, setopt_dir);
command_option(&program
, "-q"
, "--quiet"
, "disable verbose output"
, setopt_quiet);
command_option(&program
, "-d"
, "--dev"
, "install development dependencies"
, setopt_dev);
command_option(&program
, "-S"
, "--save"
, "save dependency in package.json"
, setopt_save);
command_option(&program
, "-D"
, "--save-dev"
, "save development dependency in package.json"
, setopt_savedev);
command_parse(&program, argc, argv);
debug(&debugger, "%d arguments", program.argc);
int code = 0 == program.argc
? install_local_packages()
: install_packages(program.argc, program.argv);
command_free(&program);
return code;
}