Skip to content

Commit

Permalink
feat: add zvm ls zig and zvm ls zls
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Aug 2, 2024
1 parent 7cd8355 commit b8a6cbd
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 141 deletions.
3 changes: 2 additions & 1 deletion src/alias.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const tools = @import("tools.zig");
const config = @import("config.zig");

/// try to set zig version
/// this will use system link on unix-like
Expand Down Expand Up @@ -122,7 +123,7 @@ fn verify_zig_version(expected_version: []const u8) !void {
/// try to get zig version
fn retrieve_zig_version(allocator: std.mem.Allocator) ![]u8 {
const home_dir = tools.get_home();
const current_zig_path = try std.fs.path.join(allocator, &.{ home_dir, ".zm", "current", tools.zig_name });
const current_zig_path = try std.fs.path.join(allocator, &.{ home_dir, ".zm", "current", config.zig_name });
defer allocator.free(current_zig_path);

// here we must use the absolute path, we can not just use "zig"
Expand Down
43 changes: 37 additions & 6 deletions src/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ const std = @import("std");
const builtin = @import("builtin");
const options = @import("options");

const versions = @import("versions.zig");
const install = @import("install.zig");
const alias = @import("alias.zig");
const tools = @import("tools.zig");
const meta = @import("meta.zig");
const config = @import("config.zig");

// Command types
pub const Command = enum {
Expand Down Expand Up @@ -125,13 +126,43 @@ fn handle_alias(params: []const []const u8) !void {
return std.process.execv(allocator, new_params);
}

fn handle_list(_: ?[]const u8) !void {
// TODO:
fn handle_list(param: ?[]const u8) !void {
const allocator = tools.get_allocator();
var version_list = try versions.VersionList.init(allocator, .zig);
defer version_list.deinit();

for (version_list.slice()) |version| {
const version_list: [][]const u8 = blk: {
if (param) |p| {
// when zls
if (tools.eql_str(p, "zls")) {
const res = try tools.http_get(allocator, config.zls_url);
defer allocator.free(res);

var zls_meta = try meta.Zls.init(res, allocator);
defer zls_meta.deinit();

const version_list = try zls_meta.get_version_list(allocator);
break :blk version_list;
} else
// when not zig
if (!tools.eql_str(p, "zig")) {
std.debug.print("Error param, you can specify zig or zls\n", .{});
return;
}
}

// when param is null
const res = try tools.http_get(allocator, config.zig_url);
defer allocator.free(res);

var zig_meta = try meta.Zig.init(res, allocator);
defer zig_meta.deinit();

const version_list = try zig_meta.get_version_list(allocator);
break :blk version_list;
};

defer tools.free_str_array(version_list, allocator);

for (version_list) |version| {
std.debug.print("{s}\n", .{version});
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/config.zig
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// ! this file just store some config meta data
const std = @import("std");
const builtin = @import("builtin");

var allocator: std.mem.Allocator = undefined;
var home_dir: []const u8 = undefined;
/// global allocator
pub var allocator: std.mem.Allocator = undefined;
/// home dir environment variable
pub var home_dir: []const u8 = undefined;

/// zig meta data url
pub const zig_meta_url: []const u8 = "https://ziglang.org/download/index.json";
/// zls meta data url
pub const zls_meta_url: []const u8 = "https://zigtools-releases.nyc3.digitaloceanspaces.com/zls/index.json";

/// parsed zig url
pub const zig_url = std.Uri.parse(zig_meta_url) catch unreachable;
/// parsed zls url
pub const zls_url = std.Uri.parse(zls_meta_url) catch unreachable;

/// zig file name
pub const zig_name = switch (builtin.os.tag) {
.windows => "zig.exe",
.linux => "zig",
.macos => "zig",
.linux, .macos => "zig",
else => @compileError("not support current platform"),
};

pub const archive_ext = if (builtin.os.tag == .windows) "zip" else "tar.xz";
/// zig archive_ext
pub const zig_archive_ext = if (builtin.os.tag == .windows) "zip" else "tar.xz";
11 changes: 9 additions & 2 deletions src/download.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const tools = @import("tools.zig");
const sha2 = std.crypto.hash.sha2;
const architecture = @import("architecture.zig");
const alias = @import("alias.zig");
const hash = @import("hash.zig");
const lib = @import("extract.zig");
const config = @import("config.zig");

pub fn content(allocator: std.mem.Allocator, version: []const u8, url: []const u8) !?[32]u8 {
assert(version.len > 0);
Expand Down Expand Up @@ -111,7 +111,14 @@ fn download_and_extract(
.reverse = false,
}) orelse unreachable;

const file_name = try std.mem.concat(allocator, u8, &[_][]const u8{ "zig-", platform_str, "-", version, ".", tools.archive_ext });
const file_name = try std.mem.concat(allocator, u8, &.{
"zig-",
platform_str,
"-",
version,
".",
config.zig_archive_ext,
});
defer allocator.free(file_name);

const total_size: usize = @intCast(req.response.content_length orelse 0);
Expand Down
1 change: 1 addition & 0 deletions src/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ pub const Zls = struct {
const slice = try list.toOwnedSlice();

std.mem.reverse([]const u8, slice);
return slice;
}
};
71 changes: 49 additions & 22 deletions src/tools.zig
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
//! this file just contains util function
const std = @import("std");
const builtin = @import("builtin");
const config = @import("config.zig");

const testing = std.testing;

var allocator: std.mem.Allocator = undefined;
var home_dir: []const u8 = undefined;

pub const log = std.log.scoped(.zvm);

pub const zig_name = switch (builtin.os.tag) {
.windows => "zig.exe",
.linux => "zig",
.macos => "zig",
else => @compileError("not support current platform"),
};

pub const archive_ext = if (builtin.os.tag == .windows) "zip" else "tar.xz";

/// Initialize the data.
pub fn data_init(tmp_allocator: std.mem.Allocator) !void {
allocator = tmp_allocator;
home_dir = if (builtin.os.tag == .windows)
try std.process.getEnvVarOwned(allocator, "USERPROFILE")
config.allocator = tmp_allocator;
config.home_dir = if (builtin.os.tag == .windows)
try std.process.getEnvVarOwned(config.allocator, "USERPROFILE")
else
std.posix.getenv("HOME") orelse ".";
}

/// Deinitialize the data.
pub fn data_deinit() void {
if (builtin.os.tag == .windows)
allocator.free(home_dir);
config.allocator.free(config.home_dir);
}

/// Get home directory.
pub fn get_home() []const u8 {
return home_dir;
return config.home_dir;
}

/// Get the allocator.
pub fn get_allocator() std.mem.Allocator {
return allocator;
return config.allocator;
}

/// get zvm path segment
/// Get zvm path segment
pub fn get_zvm_path_segment(tmp_allocator: std.mem.Allocator, segment: []const u8) ![]u8 {
return std.fs.path.join(
tmp_allocator,
&[_][]const u8{ get_home(), ".zm", segment },
);
}

/// for verify hash
/// Free str array
pub fn free_str_array(str_arr: []const []const u8, allocator: std.mem.Allocator) void {
for (str_arr) |str|
allocator.free(str);

allocator.free(str_arr);
}

/// For verifying hash
pub fn verify_hash(computed_hash: [32]u8, actual_hash_string: []const u8) bool {
if (actual_hash_string.len != 64) return false; // SHA256 hash should be 64 hex characters

Expand Down Expand Up @@ -86,3 +82,34 @@ test "verify_hash basic test" {
try testing.expect(verify_hash(sample_hash, &sample_hash_hex));
try testing.expect(!verify_hash(sample_hash, "incorrect_hash"));
}

/// http get
pub fn http_get(allocator: std.mem.Allocator, uri: std.Uri) ![]const u8 {

// create a http client
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();

// we ceate a buffer to store the http response
var buf: [1024]u8 = undefined; // 256 * 1024 = 262kb

// try open a request
var req = try client.open(.GET, uri, .{ .server_header_buffer = &buf });
defer req.deinit();

// send request and wait response
try req.send();
try req.wait();

if (req.response.status != .ok) {
return error.ListResponseNotOk;
}

const res = try req.reader().readAllAlloc(allocator, 256 * 1024);
return res;
}

/// eql str
pub fn eql_str(str1: []const u8, str2: []const u8) bool {
return std.mem.eql(u8, str1, str2);
}
105 changes: 0 additions & 105 deletions src/versions.zig

This file was deleted.

0 comments on commit b8a6cbd

Please sign in to comment.