Skip to content

Commit

Permalink
Merge pull request #49 from hendriknielaender/zig-style
Browse files Browse the repository at this point in the history
chore: apply zig style
  • Loading branch information
hendriknielaender authored Jul 23, 2024
2 parents 24753c7 + 09bacfb commit 1ddb7fc
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 248 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CrossTargetInfo = struct {
name: []const u8,
};
// Semantic version of your application
const version = std.SemanticVersion{ .major = 0, .minor = 4, .patch = 2 };
const version = std.SemanticVersion{ .major = 0, .minor = 4, .patch = 3 };

const min_zig_string = "0.13.0";

Expand Down
123 changes: 57 additions & 66 deletions src/alias.zig
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const tools = @import("tools.zig");

pub fn setZigVersion(version: []const u8) !void {
const allocator = tools.getAllocator();

pub fn set_zig_version(version: []const u8) !void {
const allocator = tools.get_allocator();
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const arena_allocator = arena.allocator();

const userHome = tools.getHome();

const zigPath = try std.fs.path.join(arena_allocator, &[_][]const u8{ userHome, ".zm", "versions", version });
const symlinkPath = try std.fs.path.join(arena_allocator, &[_][]const u8{ userHome, ".zm", "current" });
const user_home = tools.get_home();
const zig_path = try std.fs.path.join(arena_allocator, &[_][]const u8{ user_home, ".zm", "versions", version });
const symlink_path = try std.fs.path.join(arena_allocator, &[_][]const u8{ user_home, ".zm", "current" });

try updateSymlink(zigPath, symlinkPath);
try verifyZigVersion(allocator, version);
try update_symlink(zig_path, symlink_path);
try verify_zig_version(allocator, version);
}

fn updateSymlink(zigPath: []const u8, symlinkPath: []const u8) !void {
fn update_symlink(zig_path: []const u8, symlink_path: []const u8) !void {
assert(zig_path.len > 0);
assert(symlink_path.len > 0);

if (builtin.os.tag == .windows) {
if (std.fs.path.dirname(symlinkPath)) |dirname| {
var parent_dir = try std.fs.openDirAbsolute(dirname, .{
.iterate = true,
});
if (std.fs.path.dirname(symlink_path)) |dirname| {
var parent_dir = try std.fs.openDirAbsolute(dirname, .{ .iterate = true });
defer parent_dir.close();
try parent_dir.deleteTree(std.fs.path.basename(symlinkPath));
try parent_dir.deleteTree(std.fs.path.basename(symlink_path));
} else {
@panic("sorry, dirname is not avaiable!");
@panic("dirname is not available!");
}
if (doesDirExist(symlinkPath)) try std.fs.deleteDirAbsolute(symlinkPath);
try copyDir(zigPath, symlinkPath);
if (does_dir_exist(symlink_path)) try std.fs.deleteDirAbsolute(symlink_path);
try copy_dir(zig_path, symlink_path);
} else {
if (doesFileExist(symlinkPath)) try std.fs.cwd().deleteFile(symlinkPath);
std.posix.symlink(zigPath, symlinkPath) catch |err| switch (err) {
if (does_file_exist(symlink_path)) try std.fs.cwd().deleteFile(symlink_path);
std.posix.symlink(zig_path, symlink_path) catch |err| switch (err) {
error.PathAlreadyExists => {
try std.fs.cwd().deleteFile(symlinkPath);
try std.posix.symlink(zigPath, symlinkPath);
try std.fs.cwd().deleteFile(symlink_path);
try std.posix.symlink(zig_path, symlink_path);
},
else => return err,
};
}
}

fn copyDir(source_dir: []const u8, dest_dir: []const u8) !void {
var source = try std.fs.openDirAbsolute(
source_dir,
.{ .iterate = true },
);
fn copy_dir(source_dir: []const u8, dest_dir: []const u8) !void {
assert(source_dir.len > 0);
assert(dest_dir.len > 0);

var source = try std.fs.openDirAbsolute(source_dir, .{ .iterate = true });
defer source.close();

// try make dir
std.fs.makeDirAbsolute(dest_dir) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => {
Expand All @@ -59,42 +58,29 @@ fn copyDir(source_dir: []const u8, dest_dir: []const u8) !void {
},
};

var dest = try std.fs.openDirAbsolute(
dest_dir,
.{ .iterate = true },
);
var dest = try std.fs.openDirAbsolute(dest_dir, .{ .iterate = true });
defer dest.close();

var iterate = source.iterate();
const allocator = tools.getAllocator();
const allocator = tools.get_allocator();
while (try iterate.next()) |entry| {
const entry_name = entry.name;

const source_sub_path = try std.fs.path.join(
allocator,
&.{ source_dir, entry_name },
);
const source_sub_path = try std.fs.path.join(allocator, &.{ source_dir, entry_name });
defer allocator.free(source_sub_path);

const dest_sub_path = try std.fs.path.join(
allocator,
&.{ dest_dir, entry_name },
);
const dest_sub_path = try std.fs.path.join(allocator, &.{ dest_dir, entry_name });
defer allocator.free(dest_sub_path);

switch (entry.kind) {
.directory => {
try copyDir(source_sub_path, dest_sub_path);
},
.file => {
try std.fs.copyFileAbsolute(source_sub_path, dest_sub_path, .{});
},
.directory => try copy_dir(source_sub_path, dest_sub_path),
.file => try std.fs.copyFileAbsolute(source_sub_path, dest_sub_path, .{}),
else => {},
}
}
}

fn doesDirExist(path: []const u8) bool {
fn does_dir_exist(path: []const u8) bool {
const result = blk: {
_ = std.fs.openDirAbsolute(path, .{}) catch |err| {
switch (err) {
Expand All @@ -107,7 +93,7 @@ fn doesDirExist(path: []const u8) bool {
return result;
}

fn doesFileExist(path: []const u8) bool {
fn does_file_exist(path: []const u8) bool {
const result = blk: {
_ = std.fs.cwd().openFile(path, .{}) catch |err| {
switch (err) {
Expand All @@ -120,32 +106,37 @@ fn doesFileExist(path: []const u8) bool {
return result;
}

fn verifyZigVersion(allocator: std.mem.Allocator, expectedVersion: []const u8) !void {
const actualVersion = try retrieveZigVersion(allocator);
defer allocator.free(actualVersion);
fn verify_zig_version(allocator: std.mem.Allocator, expected_version: []const u8) !void {
const actual_version = try retrieve_zig_version(allocator);
defer allocator.free(actual_version);

assert(actual_version.len > 0);
assert(std.mem.eql(u8, expected_version, actual_version));

if (!std.mem.eql(u8, expectedVersion, actualVersion)) {
std.debug.print("Expected Zig version {s}, but currently using {s}. Please check.\n", .{ expectedVersion, actualVersion });
if (!std.mem.eql(u8, expected_version, actual_version)) {
std.debug.print("Expected Zig version {s}, but currently using {s}. Please check.\n", .{ expected_version, actual_version });
} else {
std.debug.print("Now using Zig version {s}\n", .{expectedVersion});
std.debug.print("Now using Zig version {s}\n", .{expected_version});
}
}

fn retrieveZigVersion(allocator: std.mem.Allocator) ![]u8 {
const userHome = tools.getHome();
const symlinkPath = try std.fs.path.join(allocator, &[_][]const u8{ userHome, ".zm", "current" });
defer allocator.free(symlinkPath);
fn retrieve_zig_version(allocator: std.mem.Allocator) ![]u8 {
const user_home = tools.get_home();
const symlink_path = try std.fs.path.join(allocator, &[_][]const u8{ user_home, ".zm", "current" });
defer allocator.free(symlink_path);

var childProcess = std.process.Child.init(&[_][]const u8{ "zig", "version" }, allocator);
var child_process = std.process.Child.init(&[_][]const u8{ "zig", "version" }, allocator);

childProcess.stdin_behavior = .Close;
childProcess.stdout_behavior = .Pipe;
childProcess.stderr_behavior = .Close;
child_process.stdin_behavior = .Close;
child_process.stdout_behavior = .Pipe;
child_process.stderr_behavior = .Close;

try childProcess.spawn();
try child_process.spawn();

if (childProcess.stdout) |stdout| {
return try stdout.reader().readUntilDelimiterOrEofAlloc(allocator, '\n', 100) orelse return error.EmptyVersion;
if (child_process.stdout) |stdout| {
const version = try stdout.reader().readUntilDelimiterOrEofAlloc(allocator, '\n', 100) orelse return error.EmptyVersion;
assert(version.len > 0);
return version;
}

return error.FailedToReadVersion;
Expand Down
19 changes: 9 additions & 10 deletions src/architecture.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@ fn archToString(arch: std.Target.Cpu.Arch) ?[]const u8 {
}

pub fn detect(allocator: std.mem.Allocator, params: DetectParams) !?[]u8 {
const osStr = osToString(params.os) orelse return error.UnsupportedSystem;
const archStr = archToString(params.arch) orelse return error.UnsupportedSystem;

const len = osStr.len + archStr.len + 1; // +1 for the '-'
const os_str = osToString(params.os) orelse return error.UnsupportedSystem;
const arch_str = archToString(params.arch) orelse return error.UnsupportedSystem;

const len = os_str.len + arch_str.len + 1; // +1 for the '-'
const result = try allocator.alloc(u8, len);

if (params.reverse) {
@memcpy(result[0..archStr.len], archStr);
result[archStr.len] = '-';
@memcpy(result[archStr.len + 1 ..], osStr);
@memcpy(result[0..arch_str.len], arch_str);
result[arch_str.len] = '-';
@memcpy(result[arch_str.len + 1 ..], os_str);
} else {
@memcpy(result[0..osStr.len], osStr);
result[osStr.len] = '-';
@memcpy(result[osStr.len + 1 ..], archStr);
@memcpy(result[0..os_str.len], os_str);
result[os_str.len] = '-';
@memcpy(result[os_str.len + 1 ..], arch_str);
}

return result;
Expand Down
50 changes: 25 additions & 25 deletions src/command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@ pub const Command = enum {
Unknown,
};

pub fn handleCommands(cmd: Command, params: ?[]const u8) !void {
pub fn handle_commands(cmd: Command, params: ?[]const u8) !void {
switch (cmd) {
Command.List => {
try handleList();
.List => {
try handle_list();
},
Command.Install => {
try installVersion(params);
.Install => {
try install_version(params);
},
Command.Use => {
try useVersion(params);
.Use => {
try use_version(params);
},
Command.Default => {
try setDefault();
.Default => {
try set_default();
},
Command.Version => {
try getVersion();
.Version => {
try get_version();
},
Command.Help => {
try displayHelp();
.Help => {
try display_help();
},
Command.Unknown => {
try handleUnknown();
.Unknown => {
try handle_unknown();
},
}
}

fn handleList() !void {
const allocator = tools.getAllocator();
fn handle_list() !void {
const allocator = tools.get_allocator();
var version_list = try versions.VersionList.init(allocator);
defer version_list.deinit();

Expand All @@ -52,32 +52,32 @@ fn handleList() !void {
}
}

fn installVersion(params: ?[]const u8) !void {
fn install_version(params: ?[]const u8) !void {
if (params) |version| {
try install.fromVersion(version);
try install.from_version(version);
} else {
std.debug.print("Error: Please specify a version to install using 'install <version>'.\n", .{});
}
}

fn useVersion(params: ?[]const u8) !void {
fn use_version(params: ?[]const u8) !void {
if (params) |version| {
try alias.setZigVersion(version);
try alias.set_zig_version(version);
} else {
std.debug.print("Error: Please specify a version to use with 'use <version>'.\n", .{});
}
}

fn setDefault() !void {
fn set_default() !void {
std.debug.print("Handling 'default' command.\n", .{});
// Your default code here
}

fn getVersion() !void {
fn get_version() !void {
std.debug.print("zvm {}\n", .{options.zvm_version});
}

fn displayHelp() !void {
fn display_help() !void {
const help_message =
\\Usage:
\\ zvm <command> [args]
Expand All @@ -100,6 +100,6 @@ fn displayHelp() !void {
std.debug.print(help_message, .{});
}

fn handleUnknown() !void {
fn handle_unknown() !void {
std.debug.print("Unknown command. Use 'zvm --help' for usage information.\n", .{});
}
Loading

0 comments on commit 1ddb7fc

Please sign in to comment.