1const std = @import("std");
2const string = []const u8;
3const builtin = @import("builtin");
4const yaml = @import("yaml");
5const extras = @import("extras");
6
7const zigmod = @import("../lib.zig");
8const u = @import("funcs.zig");
9
10//
11//
12
13pub const Dep = struct {
14 const Self = @This();
15
16 type: Type,
17 path: [:0]const u8,
18 id: [48]u8,
19 name: string,
20 main: [:0]const u8,
21 version: string,
22 c_include_dirs: []const string = &.{},
23 c_source_flags: []const string = &.{},
24 c_source_files: []const string = &.{},
25 only_os: []const string = &.{},
26 except_os: []const string = &.{},
27 yaml: ?yaml.Mapping,
28 deps: []zigmod.Dep,
29 keep: bool = false,
30 for_build: bool = false,
31
32 pub const EMPTY = (" " ** 48).*;
33
34 pub const Type = @import("./dep_type.zig").DepType;
35
36 pub fn clean_path(self: Dep, alloc: std.mem.Allocator) ![:0]const u8 {
37 if (self.type == .local) {
38 return self.path;
39 }
40 var p: string = self.path;
41 p = extras.trimPrefix(p, "http://");
42 p = extras.trimPrefix(p, "https://");
43 p = extras.trimPrefix(p, "git://");
44 p = extras.trimSuffix(p, ".git");
45 const np = try std.mem.joinZ(alloc, "/", &.{ @tagName(self.type), p });
46 return np;
47 }
48
49 pub fn clean_path_v(self: Dep, alloc: std.mem.Allocator) !string {
50 if (self.type == .http and self.version.len > 0) {
51 const i = std.mem.indexOf(u8, self.version, "-").?;
52 return std.mem.join(alloc, "/", &.{ "v", try self.clean_path(alloc), self.version[i + 1 .. 15] });
53 }
54 return std.mem.join(alloc, "/", &.{ "v", try self.clean_path(alloc), self.version });
55 }
56
57 pub fn is_for_this(self: Dep) bool {
58 const os = @tagName(builtin.os.tag);
59 if (self.only_os.len > 0) {
60 return extras.containsString(self.only_os, os);
61 }
62 if (self.except_os.len > 0) {
63 return !extras.containsString(self.except_os, os);
64 }
65 return true;
66 }
67
68 pub fn exact_version(self: Dep, alloc: std.mem.Allocator, dpath: [:0]const u8) !string {
69 if (self.version.len == 0) {
70 return try self.type.exact_version(alloc, dpath);
71 }
72 return switch (self.type) {
73 .git => blk: {
74 const vers = try u.parse_split(zigmod.Dep.Type.Version.Git, '-').do(self.version);
75 if (vers.id.frozen()) break :blk self.version;
76 break :blk try self.type.exact_version(alloc, dpath);
77 },
78 else => self.version,
79 };
80 }
81};