Unverified Commit 2611d5bd authored by Matt Sturgeon's avatar Matt Sturgeon
Browse files

formats.lua: init

Add a lua format, based on `lib.generators.toLua`.
parent 0be0e752
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -402,6 +402,31 @@ have a predefined type and string generator already declared under
    :   Outputs the given attribute set as an Elixir map, instead of the
        default Elixir keyword list

`pkgs.formats.lua { asBindings ? false, multiline ? true, columnWidth ? 100, indentWidth ? 2, indentUsingTabs ? false }`

:   A function taking an attribute set with values

    `asBindings` (default `false`)

    :   Whether to treat attributes as variable bindings

    `multiline` (default `true`)

    :   Whether to procude a multiline output. The output may still wrap across
        multiple lines if it would otherwise exceed `columnWidth`.

    `columnWidth` (default `100`)

    :   The column width to use to attempt to wrap lines.

    `indentWidth` (default `2`)

    :   The width of a single indentation level.

    `indentUsingTabs` (default `false`)

    :   Whether the indentation should use tabs instead of spaces.

`pkgs.formats.php { finalVariable }` []{#pkgs-formats-php}

:   A function taking an attribute set with values
+61 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ rec {

  inherit (lib) mkOptionType;
  inherit (lib.types) nullOr oneOf coercedTo listOf nonEmptyListOf attrsOf either;
  inherit (lib.types) bool int float str path;
  inherit (lib.types) bool int float str path luaInline;

  json = {}: {

@@ -541,6 +541,66 @@ rec {
      '';
    };

  lua =
    {
      asBindings ? false,
      multiline ? true,
      columnWidth ? 100,
      indentWidth ? 2,
      indentUsingTabs ? false,
    }:
    {
      type =
        let
          valueType =
            nullOr (oneOf [
              bool
              float
              int
              path
              str
              luaInline
              (attrsOf valueType)
              (listOf valueType)
            ])
            // {
              description = "lua value";
              descriptionClass = "noun";
            };
        in
        if asBindings then attrsOf valueType else valueType;
      generate =
        name: value:
        pkgs.callPackage (
          { runCommand, stylua }:
          runCommand name
            {
              nativeBuildInputs = [ stylua ];
              inherit columnWidth;
              inherit indentWidth;
              indentType = if indentUsingTabs then "Tabs" else "Spaces";
              value = lib.generators.toLua { inherit asBindings multiline; } value;
              passAsFile = [ "value" ];
              preferLocalBuild = true;
            }
            ''
              ${lib.optionalString (!asBindings) ''
                echo -n 'return ' >> $out
              ''}
              cat $valuePath >> $out
              stylua \
                --no-editorconfig \
                --line-endings Unix \
                --column-width $columnWidth \
                --indent-width $indentWidth \
                --indent-type $indentType \
                $out
            ''
        ) { };
      # Alias for mkLuaInline
      lib.mkRaw = lib.mkLuaInline;
    };

  # Outputs a succession of Python variable assignments
  # Useful for many Django-based services
  pythonVars = {}: {
+76 −0
Original line number Diff line number Diff line
@@ -601,6 +601,82 @@ in runBuildTests {
    '';
  };

  luaTable = shouldPass {
    format = formats.lua { };
    input = {
      null = null;
      false = false;
      true = true;
      int = 10;
      float = 3.141;
      str = "foo";
      attrs.foo = null;
      list = [
        null
        null
      ];
      path = ./testfile;
      inline = lib.mkLuaInline "hello('world')";
    };
    expected = ''
      return {
        ["attrs"] = {
          ["foo"] = nil,
        },
        ["false"] = false,
        ["float"] = 3.141,
        ["inline"] = (hello("world")),
        ["int"] = 10,
        ["list"] = {
          nil,
          nil,
        },
        ["null"] = nil,
        ["path"] = "${./testfile}",
        ["str"] = "foo",
        ["true"] = true,
      }
    '';
  };

  luaBindings = shouldPass {
    format = formats.lua {
      asBindings = true;
    };
    input = {
      null = null;
      _false = false;
      _true = true;
      int = 10;
      float = 3.141;
      str = "foo";
      attrs.foo = null;
      list = [
        null
        null
      ];
      path = ./testfile;
      inline = lib.mkLuaInline "hello('world')";
    };
    expected = ''
      _false = false
      _true = true
      attrs = {
        ["foo"] = nil,
      }
      float = 3.141
      inline = (hello("world"))
      int = 10
      list = {
        nil,
        nil,
      }
      null = nil
      path = "${./testfile}"
      str = "foo"
    '';
  };

  phpAtoms = shouldPass rec {
    format = formats.php { finalVariable = "config"; };
    input = {