#This is my current script how do you suggest making the code better
1 messages · Page 1 of 1 (latest)
too long discord didn't allow it
const std = @import("std");
const print = std.debug.print;
const eql = std.mem.eql;
pub fn main(init: std.process.Init) !void {
const gpa = init.arena.allocator();
const argsVector = try init.minimal.args.toSlice(gpa);
if (argsVector.len != 3) {
std.log.err("Usage: build <ipa|apk|aab> <isDev>\n", .{});
defer std.process.exit(1);
}
// argsVector[0] is the executable name
const artifactName: [:0]const u8 = argsVector[1];
if (!isValidArtifactName(artifactName)) {
std.log.err("Usage: build <ipa|apk|aab> <isDev>\n", .{});
defer std.process.exit(1);
}
_ = parseBool(argsVector[2]) catch {
std.log.err("Usage: build <ipa|apk|aab> <isDev>\n", .{});
defer std.process.exit(1);
};
var child = try std.process.spawn(init.io, .{ .argv = &.{ "flutter", "doctor", "-v" }, .stdin = .pipe, .stdout = .pipe, .stderr = .pipe });
const child_stdout_file = &child.stdout;
var read_buf: [1024]u8 = undefined;
var stdout_reader_state = child_stdout_file.*.?.readerStreaming(init.io, &read_buf);
const stdout_reader = &stdout_reader_state.interface;
var line_no: usize = 0;
while (try stdout_reader.takeDelimiter('\n')) |line| {
line_no += 1;
print("{s}\n", .{line});
}
print("{}", .{line_no});
}
fn parseBool(str: []const u8) anyerror!bool {
if (eql(u8, str, "true") or eql(u8, str, "1") or eql(u8, str, "yes")) {
return true;
} else if (eql(u8, str, "false") or eql(u8, str, "0") or eql(u8, str, "no")) {
return false;
} else {
return error.InvalidBooleanString;
}
}
test "parseBool tests" {
try std.testing.expectEqual(parseBool("true"), true);
try std.testing.expectEqual(parseBool("1"), true);
try std.testing.expectEqual(parseBool("yes"), true);
try std.testing.expectEqual(parseBool("false"), false);
try std.testing.expectEqual(parseBool("0"), false);
try std.testing.expectEqual(parseBool("no"), false);
try std.testing.expectError(error.InvalidBooleanString, parseBool("unknown"));
try std.testing.expectError(error.InvalidBooleanString, parseBool("random stuff"));
}
fn isValidArtifactName(str: []const u8) bool {
if (eql(u8, str, "ipa") or eql(u8, str, "apk") or eql(u8, str, "aab")) {
return true;
}
return false;
}
test "isValidArtifactName tests" {
try std.testing.expect(isValidArtifactName("ipa"));
try std.testing.expect(isValidArtifactName("apk"));
try std.testing.expect(isValidArtifactName("aab"));
try std.testing.expect(!isValidArtifactName("unknown"));
}
fn isValidCombination(str: []const u8, isDev: bool) bool {
switch (isDev) {
true => return eql(u8, str, "ipa") or eql(u8, str, "apk"),
false => return eql(u8, str, "ipa") or eql(u8, str, "aab"),
}
}
test "isValidCombination tests" {
try std.testing.expect(isValidCombination("ipa", true));
try std.testing.expect(isValidCombination("ipa", false));
try std.testing.expect(isValidCombination("apk", true));
try std.testing.expect(!isValidCombination("apk", false));
try std.testing.expect(!isValidCombination("aab", true));
try std.testing.expect(isValidCombination("aab", false));
}
dont use anyerror, it obscures the possible errors from the caller and compiler
i did fn parseBool(str: []const u8) error.InvalidBooleanString!bool { in the start but the compiler wasn't happy with it
i know i can use !bool but i think error.InvalidBooleanString!bool is more clear since only one error can be returned but i cannot do that
error.Name is an error value, you need to provide an error set
via error { A, B, C}
or just infer it like you do with main
wdym infer it like you do in main ? like !bool ?
i did define an error set, and use it so it's ok i guess
const Invalid = error{
InvalidBooleanString,
};
are you aware how can i handle the case when buffer is too small for a line?
while (try stdout_reader.takeDelimiter('\n')) |line| {
line_no += 1;
print("{s}\n", .{line});
}
you could just set stdout to .inherit
or you could get a writer to your own stdout/err and streamDelimiter (it leaves the delimiter in the buffer so you need to reader.toss(1) in the loop)
if you need to do something over arbitrary line lengths, stream into an allocating writer.
or just choose a buffer size large enough that you reasonably wont encounter this issue
yes, by infer the error i mean !bool.
though i dont actually recomend that but that is my opinion
not sure how to do that ? (stream into an allocating writer)