Skip to content

Commit

Permalink
When there is content in the version directory, use it directly
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Aug 4, 2024
1 parent f553563 commit 37cf22c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ pub fn install_zig(version: []const u8) !void {

const arena_allocator = arena.allocator();

// get version path
const version_path = try tools.get_zvm_path_segment(arena_allocator, "version");
// get extract path
const extract_path = try std.fs.path.join(arena_allocator, &.{ version_path, version });

if (tools.does_path_exist(extract_path)) {
try alias.set_zig_version(version);
return;
}

// get version data
const version_data: meta.Zig.VersionData = blk: {
const res = try tools.http_get(arena_allocator, config.zig_url);
Expand All @@ -38,8 +48,6 @@ pub fn install_zig(version: []const u8) !void {
break :blk tmp_val orelse return error.UnsupportedVersion;
};

std.debug.print("Install {s}\n", .{version_data.version});

const reverse_platform_str = try architecture.platform_str(architecture.DetectParams{
.os = builtin.os.tag,
.arch = builtin.cpu.arch,
Expand All @@ -56,10 +64,6 @@ pub fn install_zig(version: []const u8) !void {
const new_file = try download.download(parsed_uri, file_name, version_data.shasum, version_data.size);
defer new_file.close();

// get version path
const version_path = try tools.get_zvm_path_segment(arena_allocator, "version");
// get extract path
const extract_path = try std.fs.path.join(arena_allocator, &.{ version_path, version });
try tools.try_create_path(extract_path);
const extract_dir = try std.fs.openDirAbsolute(extract_path, .{});

Expand Down
8 changes: 4 additions & 4 deletions src/tools.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ pub fn get_zig_version(allocator: std.mem.Allocator) ![]u8 {
}

// check dir exist
pub fn does_path_exist(version_path: []const u8) bool {
std.fs.accessAbsolute(version_path, .{}) catch |err| {
pub fn does_path_exist(path: []const u8) bool {
std.fs.accessAbsolute(path, .{}) catch |err| {
if (err == error.FileNotFound) return false;
};
return true;
}

// check dir exist
pub fn does_path_exist2(dir: std.fs.Dir, file_name: []const u8) bool {
dir.access(file_name, .{}) catch |err| {
pub fn does_path_exist2(dir: std.fs.Dir, path: []const u8) bool {
dir.access(path, .{}) catch |err| {
if (err == error.FileNotFound) return false;
};
return true;
Expand Down

0 comments on commit 37cf22c

Please sign in to comment.