Skip to content

Commit 8609a57

Browse files
authored
feat(zigfetch): support fetch package from git (#33)
1 parent 134269c commit 8609a57

File tree

9 files changed

+325
-67
lines changed

9 files changed

+325
-67
lines changed

.github/build-release.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ targets=(
2121
"aarch64-linux"
2222
"x86_64-linux"
2323
"x86-linux"
24-
"aarch64-macos"
24+
# This target is built on CI directly.
25+
# "aarch64-macos"
2526
"x86_64-macos"
2627
"x86_64-windows"
2728
)
@@ -39,7 +40,7 @@ for target in "${targets[@]}"; do
3940
zig build -Doptimize=ReleaseSafe -p ${dst_dir} \
4041
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
4142
else
42-
zig build -Dskip_zigfetch=true -Doptimize=ReleaseSafe -Dtarget="${target}" -p ${dst_dir} \
43+
zig build -Dskip-zigfetch=true -Doptimize=ReleaseSafe -Dtarget="${target}" -p ${dst_dir} \
4344
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
4445
fi
4546

.github/workflows/CI.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ jobs:
5252
zig build test
5353
zig build
5454
find zig-out
55+
- name: zigfetch compare
56+
if: matrix.os == 'macos-latest'
57+
run: |
58+
bash .github/zigfetch.sh
5559
5660
cross-compile:
5761
timeout-minutes: 10
@@ -72,4 +76,4 @@ jobs:
7276
sudo apt update && sudo apt-get install -y libcurl4-openssl-dev
7377
- name: Build
7478
run: |
75-
zig build -Dtarget=${{ matrix.targets }} -Dskip_zigfetch=true
79+
zig build -Dtarget=${{ matrix.targets }} -Dskip-zigfetch=true

.github/zigfetch.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
4+
5+
pkg=https://github.com/jiacai2050/zig-curl/archive/c8e2f43f8f042f52373c86043ec16b0f2c3388a2.tar.gz
6+
7+
zig fetch --debug-hash "${pkg}"
8+
"${script_dir}/../zig-out/bin/zigfetch" "${pkg}"
9+
10+
actual=$("${script_dir}/../zig-out/bin/zigfetch" "${pkg}" 2>&1 | tail -1)
11+
expected="1220e9b279355ce92cd217684a2449bd8024274eb3fc09a576deb33ca1733b9f0a1f"
12+
if [ "${actual}" != "${expected}" ];then
13+
echo "Expected: ${expected}, actual:${actual}"
14+
exit 1
15+
fi

build.zig

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ const macos_private_framework = "/Applications/Xcode.app/Contents/Developer/Plat
66
pub fn build(b: *Build) !void {
77
const optimize = b.standardOptimizeOption(.{});
88
const target = b.standardTargetOptions(.{});
9-
const skip_zigfetch = b.option(bool, "skip_zigfetch", "Skip zig fetch") orelse false;
9+
const skip_zigfetch = b.option(bool, "skip-zigfetch", "Skip zig fetch") orelse false;
10+
const vendor_libcurl = b.option(bool, "vendor-libcurl", "Static link libcurl") orelse false;
1011
var all_tests = std.ArrayList(*Build.Step).init(b.allocator);
1112

1213
try addModules(b, target, &all_tests);
13-
try buildBinaries(b, optimize, target, &all_tests, skip_zigfetch);
14+
try buildBinaries(b, optimize, target, &all_tests, skip_zigfetch, vendor_libcurl);
1415
try buildExamples(b, optimize, target, &all_tests);
1516

1617
const test_all_step = b.step("test", "Run all tests");
@@ -87,7 +88,7 @@ fn buildExamples(
8788
"simargs-demo",
8889
"pretty-table-demo",
8990
}) |name| {
90-
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests, false);
91+
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests, false, false);
9192
}
9293
}
9394

@@ -97,6 +98,7 @@ fn buildBinaries(
9798
target: std.Build.ResolvedTarget,
9899
all_tests: *std.ArrayList(*Build.Step),
99100
skip_zigfetch: bool,
101+
vendor_libcurl: bool,
100102
) !void {
101103
inline for (.{
102104
"zigfetch",
@@ -109,7 +111,15 @@ fn buildBinaries(
109111
"repeat",
110112
"tcp-proxy",
111113
}) |name| {
112-
try buildBinary(b, .{ .bin = name }, optimize, target, all_tests, skip_zigfetch);
114+
try buildBinary(
115+
b,
116+
.{ .bin = name },
117+
optimize,
118+
target,
119+
all_tests,
120+
skip_zigfetch,
121+
vendor_libcurl,
122+
);
113123
}
114124

115125
// TODO: move util out of `bin`
@@ -123,8 +133,16 @@ fn buildBinary(
123133
target: std.Build.ResolvedTarget,
124134
all_tests: *std.ArrayList(*Build.Step),
125135
skip_zigfetch: bool,
136+
vendor_libcurl: bool,
126137
) !void {
127-
if (makeCompileStep(b, source, optimize, target, skip_zigfetch)) |exe| {
138+
if (makeCompileStep(
139+
b,
140+
source,
141+
optimize,
142+
target,
143+
skip_zigfetch,
144+
vendor_libcurl,
145+
)) |exe| {
128146
var deps = b.modules.iterator();
129147
while (deps.next()) |dep| {
130148
exe.root_module.addImport(dep.key_ptr.*, dep.value_ptr.*);
@@ -168,6 +186,7 @@ fn makeCompileStep(
168186
optimize: std.builtin.Mode,
169187
target: std.Build.ResolvedTarget,
170188
skip_zigfetch: bool,
189+
vendor_libcurl: bool,
171190
) ?*Build.Step.Compile {
172191
const name = comptime source.name();
173192
const path = comptime source.path();
@@ -206,9 +225,11 @@ fn makeCompileStep(
206225
return null;
207226
}
208227
if (host_os == .linux or host_os == .macos) {
209-
const dep_curl = b.dependency("curl", .{ .link_vendor = false });
228+
const dep_curl = b.dependency("curl", .{ .link_vendor = vendor_libcurl });
229+
if (!vendor_libcurl) {
230+
exe.linkSystemLibrary("curl");
231+
}
210232
exe.root_module.addImport("curl", dep_curl.module("curl"));
211-
exe.linkSystemLibrary("curl");
212233
exe.linkLibC();
213234
} else {
214235
return null;

docs/content/_index.org

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#+TITLE: Zigcli
1+
#+TITLE: Introduction
22
#+DATE: 2023-10-21T12:09:48+0800
3-
#+LASTMOD: 2025-01-01T19:29:01+0800
3+
#+LASTMOD: 2025-01-02T23:22:49+0800
44
#+TYPE: docs
55
#+author: Jiacai Liu
66

@@ -18,32 +18,7 @@ Official website: https://zigcli.liujiacai.net/
1818
It can be imported as [[https://zigcli.liujiacai.net/packages/][Zig packages]] or used directly as [[https://zigcli.liujiacai.net/programs/][command line programs]].
1919

2020
* Install
21-
** Packages
22-
#+begin_src bash
23-
zig fetch --save=zigcli https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz
24-
#+end_src
25-
26-
#+RESULTS:
27-
28-
Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this:
29-
30-
#+begin_src zig
31-
const zigcli = b.dependency("zigcli", .{});
32-
33-
// Currently zigcli provide two packages.
34-
exe.root_module.addImport("simargs", zigcli.module("simargs"));
35-
exe.root_module.addImport("pretty-table", zigcli.module("pretty-table"));
36-
#+end_src
37-
** CLI Programs
38-
The latest pre-built binaries are available on the [[https://github.com/jiacai2050/zigcli/releases][release page]] or you can build it from source.
39-
40-
#+begin_src bash
41-
git clone https://github.com/jiacai2050/zigcli.git
42-
#+end_src
43-
Then build with zig 0.13.0
44-
#+begin_src bash
45-
make build
46-
#+end_src
21+
See [[https://zigcli.liujiacai.net/install][INSTALL]] page.
4722
* Who's Using
4823
If you're using =zigcli=, and would like to be added here, welcome to [[https://github.com/jiacai2050/zigcli/pulls][open a PR]].
4924

docs/content/install.org

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#+TITLE: Install
2+
#+DATE: 2025-01-02T23:20:23+0800
3+
#+LASTMOD: 2025-01-02T23:21:04+0800
4+
#+TYPE: docs
5+
#+WEIGHT: 10
6+
#+AUTHOR: Jiacai Liu
7+
8+
** Packages
9+
#+begin_src bash
10+
zig fetch --save=zigcli https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz
11+
#+end_src
12+
13+
#+RESULTS:
14+
15+
Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this:
16+
17+
#+begin_src zig
18+
const zigcli = b.dependency("zigcli", .{});
19+
20+
// Currently zigcli provide two packages.
21+
exe.root_module.addImport("simargs", zigcli.module("simargs"));
22+
exe.root_module.addImport("pretty-table", zigcli.module("pretty-table"));
23+
#+end_src
24+
** CLI Programs
25+
The latest pre-built binaries are available on the [[https://github.com/jiacai2050/zigcli/releases][release page]] or you can build it from source.
26+
27+
#+begin_src bash
28+
git clone https://github.com/jiacai2050/zigcli.git
29+
#+end_src
30+
Then build with zig 0.13.0
31+
#+begin_src bash
32+
make build
33+
#+end_src

docs/content/programs/zigfetch.org

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#+TITLE: zigfetch
22
#+DATE: 2025-01-01T18:01:47+0800
3-
#+LASTMOD: 2025-01-01T19:08:03+0800
3+
#+LASTMOD: 2025-01-02T23:02:00+0800
44
#+TYPE: docs
55
#+DESCRIPTION: Fetch zig package, baked by libcurl.
66

@@ -12,7 +12,7 @@ Since the HTTP support within Zig's standard library isn't currently stable, thi
1212

1313
This poses a significant challenge for Chinese developers owing to [[https://en.wikipedia.org/wiki/Great_Firewall][the Great Firewall]].
1414

15-
=zigfetch= is baked by libcurl, so [[https://curl.se/libcurl/c/libcurl-env.html][http_proxy/https_proxy]] env vars work as expected.
15+
So =zigfetch= is born, it's baked by libcurl, so [[https://curl.se/libcurl/c/libcurl-env.html][http_proxy/https_proxy]] env vars work as expected.
1616

1717
#+begin_src bash :results verbatim :exports result :dir ../../..
1818
./zig-out/bin/zigfetch --help

src/bin/pkg/Manifest.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,14 @@ const Parse = struct {
472472
buf.* = buf_managed.moveToUnmanaged();
473473
switch (try result) {
474474
.success => {},
475-
.failure => |err| {
476-
std.log.err("parse str lit failed, err:{any}, token:{any}, bytes:{s}, offset:{d}", .{ err, token, bytes, offset });
475+
.failure => |e| {
476+
std.log.err("parse str lit failed, err:{any}, token:{any}, bytes:{any}, offset:{any}", .{
477+
e,
478+
token,
479+
bytes,
480+
offset,
481+
});
482+
return error.ParseFailure;
477483
},
478484
}
479485
}

0 commit comments

Comments
 (0)