fork/nix-minecraft

git clone https://git.julia.blue/fork/nix-minecraft

Infinidogelib: properly escape isNormalVersionaca6047

lazymc
3.3 KiB116 linesraw
1{ lib }:
2lib.makeExtensible (
3  self:
4  let
5    inherit (lib)
6      attrNames
7      concatLines
8      concatStrings
9      concatStringsSep
10      drop
11      filter
12      filterAttrs
13      hasSuffix
14      id
15      isList
16      last
17      mapAttrs'
18      removePrefix
19      removeSuffix
20      sort
21      splitString
22      stringToCharacters
23      take
24      versionOlder
25      nameValuePair
26      ;
27    inherit (builtins)
28      match
29      pathExists
30      readDir
31      replaceStrings
32      typeOf
33      ;
34  in
35  rec {
36    chain = {
37      func = id;
38      __functor =
39        self: input:
40        if (typeOf input) == "lambda" then self // { func = e: input (self.func e); } else self.func input;
41    };
42
43    isNormalVersion = v: isList (match "([[:digit:]]+\\.[[:digit:]]+(\\.[[:digit:]]+)?)" v);
44
45    latestVersion =
46      versions: chain (filter isNormalVersion) (sort versionOlder) last (attrNames versions);
47
48    escapeVersion = replaceStrings [ "." " " ] [ "_" "_" ];
49
50    removeVanilla = n: escapeVersion (removePrefix "vanilla-" n);
51
52    # Stolen from digga: https://github.com/divnix/digga/blob/587013b2500031b71959496764b6fdd1b2096f9a/src/importers.nix#L61-L114
53    rakeLeaves =
54      dirPath:
55      let
56        seive =
57          file: type:
58          # Only rake `.nix` files or directories
59          (type == "regular" && hasSuffix ".nix" file) || (type == "directory");
60
61        collect = file: type: {
62          name = removeSuffix ".nix" file;
63          value =
64            let
65              path = dirPath + "/${file}";
66            in
67            if (type == "regular") || (type == "directory" && pathExists (path + "/default.nix")) then
68              path
69            # recurse on directories that don't contain a `default.nix`
70            else
71              rakeLeaves path;
72        };
73
74        files = filterAttrs seive (readDir dirPath);
75      in
76      filterAttrs (n: v: v != { }) (mapAttrs' collect files);
77
78    # Same as collectFiles, but only gathers files from a specific subdirectory
79    # (e.g. "config")
80    collectFilesAt =
81      path: subdir: mapAttrs' (n: nameValuePair ("${subdir}/${n}")) (collectFiles "${path}/${subdir}");
82
83    # Get all files from a path (e.g. a modpack derivation) and return them in the
84    # format expected by the files/symlinks module options.
85    collectFiles =
86      let
87        mapListToAttrs =
88          fn: fv: list:
89          lib.listToAttrs (map (x: nameValuePair (fn x) (fv x)) list);
90      in
91      path:
92      mapListToAttrs (x: builtins.unsafeDiscardStringContext (lib.removePrefix "${path}/" x)) (lib.id) (
93        lib.filesystem.listFilesRecursive "${path}"
94      );
95
96    wrapJarManifest =
97      manifestText:
98      let
99        chunkCharacters' =
100          characters: chunks:
101          if
102            characters != [ ] # 71 characters plus space prefix = 72 line length
103          then
104            chunkCharacters' (drop 71 characters) (chunks ++ [ (take 71 characters) ])
105          else
106            chunks;
107
108        chunkCharacters =
109          characters: # First line gets 72 due to not having space prefix
110          chunkCharacters' (drop 72 characters) [ (take 72 characters) ];
111
112        wrapLine = chain stringToCharacters chunkCharacters (map concatStrings) (concatStringsSep "\n ");
113      in
114      chain (splitString "\n") (map wrapLine) concatLines manifestText;
115  }
116)