Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new binary zigfetch #32

Merged
merged 15 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .github/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ targets=(
export BUILD_DATE=$(date +'%Y-%m-%dT%H:%M:%S%z')
export GIT_COMMIT=$(git rev-parse --short HEAD)

pandoc -f org -t markdown README.org -o README.md

for target in "${targets[@]}"; do
echo "Building for ${target}..."
filename=zigcli-${VERSION}-${target}
dst_dir=zig-out/${filename}

# 1. Build
zig build -Doptimize=ReleaseSafe -Dtarget="${target}" -p ${dst_dir} \
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE} -Dis_ci=true
if [[ "${target}" = "x86_64-linux" ]];then
zig build -Doptimize=ReleaseSafe -p ${dst_dir} \
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
else
zig build -Dskip_zigfetch=true -Doptimize=ReleaseSafe -Dtarget="${target}" -p ${dst_dir} \
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
fi

# 2. Prepare files
rm -f ${dst_dir}/bin/*demo
cp LICENSE README.md ${dst_dir}
cp LICENSE README.org ${dst_dir}

find zig-out

# 3. Zip final file
pushd zig-out
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
zig.exe fmt --check .
zig.exe build test
zig.exe build
- name: deps(ubuntu)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt update && sudo apt-get install -y libcurl4-openssl-dev
- name: fmt and test(unix)
if: matrix.os != 'windows-latest'
run: |
Expand All @@ -62,6 +66,10 @@ jobs:
- uses: mlugg/setup-zig@v1
with:
version: 0.13.0
- name: deps(ubuntu)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt update && sudo apt-get install -y libcurl4-openssl-dev
- name: Build
run: |
zig build -Dtarget=${{ matrix.targets }}
zig build -Dtarget=${{ matrix.targets }} -Dskip_zigfetch=true
File renamed without changes.
57 changes: 49 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,76 @@ name: Release

on:
workflow_dispatch:
pull_request:
paths:
- "**.zig"
- ".github/workflows/CI.yml"
- ".github/workflows/release.yml"
push:
branches:
- main
paths:
- "**.zig"
- ".github/workflows/release.yml"
tags:
- "v*"


concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: write

jobs:
upload-assets:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: mlugg/setup-zig@v1
with:
version: 0.13.0
- name: Set env
- name: Set env(release)
if: startsWith(github.ref, 'refs/tags/')
run: |
echo "RELEASE_VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
echo "OUT_DIR=/tmp/zigcli-${{ github.ref_name }}" >> $GITHUB_ENV
- name: Install
uses: pandoc/actions/setup@v1
with:
version: 2.19
- name: Build
echo "OUT_DIR=/tmp/zigcli" >> $GITHUB_ENV
- name: Set env(dev)
if: "!startsWith(github.ref, 'refs/tags/')"
run: |
echo "RELEASE_VERSION=unknown" >> $GITHUB_ENV
echo "OUT_DIR=/tmp/zigcli" >> $GITHUB_ENV
- name: Build(Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt update && sudo apt-get install -y libcurl4-openssl-dev
bash .github/build-release.sh
- name: Build(MacOS)
if: matrix.os == 'macos-latest'
run: |
mkdir -p "${OUT_DIR}"
zig build -Doptimize=ReleaseSafe \
-Dgit_commit=${GIT_COMMIT} -Dbuild_date=${BUILD_DATE}
rm -f zig-out/bin/*demo
cp LICENSE README.org zig-out
find zig-out
pushd zig-out
zip -r ${OUT_DIR}/zigcli-${RELEASE_VERSION}-aarch64-macos.zip .
popd
- name: Release
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
files: ${{ env.OUT_DIR }}/*
- name: Upload
if: "!startsWith(github.ref, 'refs/tags/')"
uses: actions/upload-artifact@v4
with:
name: zigcli-${{ matrix.os }}
path: ${{ env.OUT_DIR }}
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.PHONY: init-docs serve test fmt build

build:
zig build -Doptimize=ReleaseFast \
Expand All @@ -22,4 +23,7 @@ init-docs:
serve:
cd docs && hugo serve -D

.PHONY: init-docs serve test fmt build

zf:
zig build run-zigfetch -- \
http://localhost:8000/c0c48df7567ea02458e9fc1f35c4088271b8d4a6.tar.gz
33 changes: 27 additions & 6 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const macos_private_framework = "/Applications/Xcode.app/Contents/Developer/Plat
pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

const skip_zigfetch = b.option(bool, "skip_zigfetch", "Skip zig fetch") orelse false;
var all_tests = std.ArrayList(*Build.Step).init(b.allocator);

try addModules(b, target, &all_tests);
try buildBinaries(b, optimize, target, &all_tests);
try buildBinaries(b, optimize, target, &all_tests, skip_zigfetch);
try buildExamples(b, optimize, target, &all_tests);

const test_all_step = b.step("test", "Run all tests");
Expand Down Expand Up @@ -87,7 +87,7 @@ fn buildExamples(
"simargs-demo",
"pretty-table-demo",
}) |name| {
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests);
try buildBinary(b, .{ .ex = name }, optimize, target, all_tests, false);
}
}

Expand All @@ -96,8 +96,10 @@ fn buildBinaries(
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
skip_zigfetch: bool,
) !void {
inline for (.{
"zigfetch",
"tree",
"loc",
"pidof",
Expand All @@ -107,7 +109,7 @@ fn buildBinaries(
"repeat",
"tcp-proxy",
}) |name| {
try buildBinary(b, .{ .bin = name }, optimize, target, all_tests);
try buildBinary(b, .{ .bin = name }, optimize, target, all_tests, skip_zigfetch);
}

// TODO: move util out of `bin`
Expand All @@ -120,8 +122,9 @@ fn buildBinary(
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
skip_zigfetch: bool,
) !void {
if (makeCompileStep(b, source, optimize, target)) |exe| {
if (makeCompileStep(b, source, optimize, target, skip_zigfetch)) |exe| {
var deps = b.modules.iterator();
while (deps.next()) |dep| {
exe.root_module.addImport(dep.key_ptr.*, dep.value_ptr.*);
Expand Down Expand Up @@ -164,10 +167,11 @@ fn makeCompileStep(
comptime source: Source,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
skip_zigfetch: bool,
) ?*Build.Step.Compile {
const name = comptime source.name();
const path = comptime source.path();
// We canit use `target.result.isDarwin()` here
// We can't use `target.result.isDarwin()` here
// Since when cross compile to darwin, there is no framework in the host!
const is_darwin = @import("builtin").os.tag == .macos;

Expand All @@ -192,6 +196,23 @@ fn makeCompileStep(
exe.linkFramework("SkyLight");
} else if (std.mem.eql(u8, name, "tcp-proxy")) {
exe.linkLibC();
} else if (std.mem.eql(u8, name, "zigfetch")) {
if (skip_zigfetch) {
return null;
}
const host_os = @import("builtin").os.tag;
const build_os = target.result.os.tag;
if (host_os != build_os) { // don't support cross compile
return null;
}
if (host_os == .linux or host_os == .macos) {
const dep_curl = b.dependency("curl", .{ .link_vendor = false });
exe.root_module.addImport("curl", dep_curl.module("curl"));
exe.linkSystemLibrary("curl");
exe.linkLibC();
} else {
return null;
}
} else if (std.mem.eql(u8, name, "pidof")) {
// only build for macOS
if (is_darwin) {
Expand Down
9 changes: 7 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
.{
.name = "zigcli",
.version = "0.1.0",
.version = "0.1.3",
.paths = .{
"src",
"build.zig",
"build.zig.zon",
"README.org",
},
.dependencies = .{},
.dependencies = .{
.curl = .{
.url = "https://github.com/jiacai2050/zig-curl/archive/c8e2f43f8f042f52373c86043ec16b0f2c3388a2.tar.gz",
.hash = "1220e9b279355ce92cd217684a2449bd8024274eb3fc09a576deb33ca1733b9f0a1f",
},
},
}
33 changes: 31 additions & 2 deletions docs/content/_index.org
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#+TITLE: Zigcli
#+DATE: 2023-10-21T12:09:48+0800
#+LASTMOD: 2024-08-17T18:53:40+0800
#+LASTMOD: 2025-01-01T18:00:54+0800
#+TYPE: docs
#+author: Jiacai Liu

[[https://github.com/jiacai2050/zigcli][https://img.shields.io/github/stars/jiacai2050/zigcli.svg]]
[[https://github.com/jiacai2050/loc/actions/workflows/CI.yml][https://github.com/jiacai2050/loc/actions/workflows/CI.yml/badge.svg]]
[[https://github.com/jiacai2050/loc/actions/workflows/binary.yml][https://github.com/jiacai2050/loc/actions/workflows/binary.yml/badge.svg]]
[[https://github.com/jiacai2050/loc/actions/workflows/binary.yml][https://github.com/jiacai2050/loc/actions/workflows/release.yml/badge.svg]]
[[https://img.shields.io/badge/zig%20version-0.13.0-blue.svg]]

#+begin_quote
Expand All @@ -17,6 +17,35 @@ Official website: https://zigcli.liujiacai.net/

It can be imported as [[https://zigcli.liujiacai.net/modules/][Zig modules]] or used directly as [[https://zigcli.liujiacai.net/programs/][binary programs]].

* Install
** Modules
=zigcli= support [[https://ziglang.org/download/0.11.0/release-notes.html#Package-Management][package manager]] introduced in Zig 0.11.

#+begin_src bash
zig fetch --save=zigcli https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz
#+end_src

#+RESULTS:

Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this:

#+begin_src zig
const zigcli = b.dependency("zigcli", .{});

// Currently zigcli provide two modules.
exe.root_module.addImport("simargs", zigcli.module("simargs"));
exe.root_module.addImport("pretty-table", zigcli.module("pretty-table"));
#+end_src
** Binary
Latest pre-built binaries can be downloaded on the [[https://github.com/jiacai2050/zigcli/releases][release page]], or you can build from source:

#+begin_src bash
git clone https://github.com/jiacai2050/zigcli.git
#+end_src
Then build with
#+begin_src bash
make build
#+end_src
* Who's Using
If you're using =zigcli=, and would like to be added here, welcome to [[https://github.com/jiacai2050/zigcli/pulls][open a PR]].

Expand Down
21 changes: 1 addition & 20 deletions docs/content/modules/_index.org
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
#+TITLE: Modules
#+DATE: 2024-08-17T17:58:01+0800
#+LASTMOD: 2024-08-17T18:17:19+0800
#+LASTMOD: 2025-01-01T18:00:20+0800
#+WEIGHT: 10
#+TYPE: docs
#+AUTHOR: Jiacai Liu
#+DESCRIPTION: Zig modules


* Install
=zigcli= support [[https://ziglang.org/download/0.11.0/release-notes.html#Package-Management][package manager]] introduced in Zig 0.11.

#+begin_src bash
zig fetch --save=zigcli https://github.com/jiacai2050/zigcli/archive/${COMMIT}.tar.gz
#+end_src

#+RESULTS:

Replace ~${COMMIT}~ with a real one, then in your =build.zig=, import the module like this:

#+begin_src zig
const zigcli = b.dependency("zigcli", .{});

// Currently zigcli provide two modules.
exe.root_module.addImport("simargs", zigcli.module("simargs"));
exe.root_module.addImport("pretty-table", zigcli.module("pretty-table"));
#+end_src

* Available modules
13 changes: 1 addition & 12 deletions docs/content/programs/_index.org
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
#+TITLE: Programs
#+DATE: 2023-10-21T12:26:45+0800
#+LASTMOD: 2024-09-28T11:50:42+0800
#+LASTMOD: 2025-01-01T18:00:55+0800
#+TYPE: docs
#+WEIGHT: 20
#+DESCRIPTION: Binary programs which can be used directly

* Install
Latest pre-built binaries can be downloaded on the [[https://github.com/jiacai2050/zigcli/releases][release page]], or you can build from source:

#+begin_src bash
git clone https://github.com/jiacai2050/zigcli.git
#+end_src
Then build with
#+begin_src bash
make build
#+end_src

* Available Programs
28 changes: 28 additions & 0 deletions docs/content/programs/zigfetch.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#+TITLE: zigfetch
#+DATE: 2025-01-01T18:01:47+0800
#+LASTMOD: 2025-01-01T19:08:03+0800
#+TYPE: docs
#+DESCRIPTION: Fetch zig package, baked by libcurl.

=zigfetch= behaves similarly to =zig fetch=, utilizing the capabilities of libcurl for its functionality.

Since the HTTP support within Zig's standard library isn't currently stable, this issue leads to various errors during dependency downloads when building Zig projects.

{{< figure src="https://fs.liujiacai.net/cdn-img/zigcli/zig-fetch-errors.webp" caption="Zig fetch errors">}}

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

=zigfetch= is baked by libcurl, so [[https://curl.se/libcurl/c/libcurl-env.html][http_proxy/https_proxy]] env vars work as expected.

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

#+RESULTS:
: USAGE:
: ./zig-out/bin/zigfetch [OPTIONS] [--] [package-dir or url]
:
: OPTIONS:
: -h, --help Show help
: -v, --verbose Show verbose log
: -d, --debug-hash Print hash for each file
Loading
Loading