From 3d01c20f3e759d4c7fb02af6aea88f59e736e7c8 Mon Sep 17 00:00:00 2001 From: Meghan Date: Sat, 5 Dec 2020 10:06:59 -0800 Subject: [PATCH] add the http dep type --- README.md | 3 ++- src/cmd_fetch.zig | 7 +++++++ src/util/dep_type.zig | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9da4670648e5912642a58ecbc05198af3fd96e58..f1face6b0a1c758cbfc025048cf4fa16d8df45ce 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ zigmod add - `system_lib` - `git` - `hg` +- `http` ### `fetch` command ``` @@ -103,7 +104,7 @@ pub fn build(b: *Builder) void { #### Dep object | Name | Type | Note | Description | |------|------|------|-------------| -| `type` | `string` | required, enum | One of `system_lib`, `git`, `hg` | +| `type` | `string` | required, enum | One of `system_lib`, `git`, `hg`, `http` | | `path` | `string` | required | URL/path to this dependency. depends on the type | | `version` | `string` | only on some types | pin this dependency at a specific version | | `only_os` | `string` | | comma separated list of OS names to add this Dep to | diff --git a/src/cmd_fetch.zig b/src/cmd_fetch.zig index a6df53b05c6bdeeb6bd6d5585b147205423317e7..f9953a1bf5da2fd57aae1d42fcfaf00f1577175d 100644 --- a/src/cmd_fetch.zig +++ b/src/cmd_fetch.zig @@ -130,6 +130,13 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { _ = try d.type.update(p, d.path); } }, + .http => { + const file_name = try u.last(try u.split(d.path, "/")); + if (try u.does_file_exist(p)) { + try u.rm_recv(p); + } + _ = try d.type.pull(d.path, p); + }, } switch (d.type) { .system_lib => { diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index 66a6708b734902b0430b68d55ec5b395760fa8be..a15de572e82beb2abb21eddb0abe34c981371dbb 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -1,3 +1,6 @@ +const std = @import("std"); +const gpa = std.heap.c_allocator; + const u = @import("./index.zig"); // @@ -7,6 +10,7 @@ pub const DepType = enum { system_lib, // std.build.LibExeObjStep.linkSystemLibrary git, // https://git-scm.com/ hg, // https://www.mercurial-scm.org/ + http, // https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol pub fn pull(self: DepType, rpath: []const u8, dpath: []const u8) !void { switch (self) { @@ -18,6 +22,24 @@ pub const DepType = enum { .hg => { _ = try u.run_cmd(null, &[_][]const u8{"hg", "clone", rpath, dpath}); }, + .http => { + try u.mkdir_all(dpath); + _ = try u.run_cmd(dpath, &[_][]const u8{"wget", rpath}); + const f = rpath[std.mem.lastIndexOf(u8, rpath, "/").?+1..]; + if (std.mem.endsWith(u8, f, ".zip")) { + _ = try u.run_cmd(dpath, &[_][]const u8{"unzip", f, "-d", "."}); + try std.fs.deleteFileAbsolute(try std.fs.path.join(gpa, &[_][]const u8{dpath, f})); + } + if ( + std.mem.endsWith(u8, f, ".tar") + or std.mem.endsWith(u8, f, ".tar.gz") + or std.mem.endsWith(u8, f, ".tar.xz") + or std.mem.endsWith(u8, f, ".tar.zst") + ) { + _ = try u.run_cmd(dpath, &[_][]const u8{"tar", "-xf", f, "-C", "."}); + try std.fs.deleteFileAbsolute(try std.fs.path.join(gpa, &[_][]const u8{dpath, f})); + } + }, } } @@ -31,6 +53,9 @@ pub const DepType = enum { .hg => { _ = try u.run_cmd(dpath, &[_][]const u8{"hg", "pull"}); }, + .http => { + // + }, } } }; -- 2.54.0