Skip to content

Commit b22b313

Browse files
authored
Merge pull request #2158 from mblayman/lua
Add Lua language support
2 parents 7a305e5 + 54331dc commit b22b313

File tree

12 files changed

+169
-2
lines changed

12 files changed

+169
-2
lines changed

azure-pipelines.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ jobs:
4242
displayName: install coursier
4343
- bash: testing/get-dart.sh
4444
displayName: install dart
45+
- bash: testing/get-lua.sh
46+
displayName: install lua
4547
- bash: testing/get-swift.sh
4648
displayName: install swift
4749
- bash: testing/get-r.sh
@@ -56,6 +58,8 @@ jobs:
5658
displayName: install coursier
5759
- bash: testing/get-dart.sh
5860
displayName: install dart
61+
- bash: testing/get-lua.sh
62+
displayName: install lua
5963
- bash: testing/get-swift.sh
6064
displayName: install swift
6165
- bash: testing/get-r.sh

pre_commit/languages/all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pre_commit.languages import dotnet
1414
from pre_commit.languages import fail
1515
from pre_commit.languages import golang
16+
from pre_commit.languages import lua
1617
from pre_commit.languages import node
1718
from pre_commit.languages import perl
1819
from pre_commit.languages import pygrep
@@ -51,6 +52,7 @@ class Language(NamedTuple):
5152
'dotnet': Language(name='dotnet', ENVIRONMENT_DIR=dotnet.ENVIRONMENT_DIR, get_default_version=dotnet.get_default_version, healthy=dotnet.healthy, install_environment=dotnet.install_environment, run_hook=dotnet.run_hook), # noqa: E501
5253
'fail': Language(name='fail', ENVIRONMENT_DIR=fail.ENVIRONMENT_DIR, get_default_version=fail.get_default_version, healthy=fail.healthy, install_environment=fail.install_environment, run_hook=fail.run_hook), # noqa: E501
5354
'golang': Language(name='golang', ENVIRONMENT_DIR=golang.ENVIRONMENT_DIR, get_default_version=golang.get_default_version, healthy=golang.healthy, install_environment=golang.install_environment, run_hook=golang.run_hook), # noqa: E501
55+
'lua': Language(name='lua', ENVIRONMENT_DIR=lua.ENVIRONMENT_DIR, get_default_version=lua.get_default_version, healthy=lua.healthy, install_environment=lua.install_environment, run_hook=lua.run_hook), # noqa: E501
5456
'node': Language(name='node', ENVIRONMENT_DIR=node.ENVIRONMENT_DIR, get_default_version=node.get_default_version, healthy=node.healthy, install_environment=node.install_environment, run_hook=node.run_hook), # noqa: E501
5557
'perl': Language(name='perl', ENVIRONMENT_DIR=perl.ENVIRONMENT_DIR, get_default_version=perl.get_default_version, healthy=perl.healthy, install_environment=perl.install_environment, run_hook=perl.run_hook), # noqa: E501
5658
'pygrep': Language(name='pygrep', ENVIRONMENT_DIR=pygrep.ENVIRONMENT_DIR, get_default_version=pygrep.get_default_version, healthy=pygrep.healthy, install_environment=pygrep.install_environment, run_hook=pygrep.run_hook), # noqa: E501

pre_commit/languages/lua.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import contextlib
2+
import os
3+
import sys
4+
from typing import Generator
5+
from typing import Sequence
6+
from typing import Tuple
7+
8+
import pre_commit.constants as C
9+
from pre_commit.envcontext import envcontext
10+
from pre_commit.envcontext import PatchesT
11+
from pre_commit.envcontext import Var
12+
from pre_commit.hook import Hook
13+
from pre_commit.languages import helpers
14+
from pre_commit.prefix import Prefix
15+
from pre_commit.util import clean_path_on_failure
16+
from pre_commit.util import cmd_output
17+
18+
ENVIRONMENT_DIR = 'lua_env'
19+
get_default_version = helpers.basic_get_default_version
20+
healthy = helpers.basic_healthy
21+
22+
23+
def _get_lua_version() -> str: # pragma: win32 no cover
24+
"""Get the Lua version used in file paths."""
25+
_, stdout, _ = cmd_output('luarocks', 'config', '--lua-ver')
26+
return stdout.strip()
27+
28+
29+
def get_env_patch(d: str) -> PatchesT: # pragma: win32 no cover
30+
version = _get_lua_version()
31+
so_ext = 'dll' if sys.platform == 'win32' else 'so'
32+
return (
33+
('PATH', (os.path.join(d, 'bin'), os.pathsep, Var('PATH'))),
34+
(
35+
'LUA_PATH', (
36+
os.path.join(d, 'share', 'lua', version, '?.lua;'),
37+
os.path.join(d, 'share', 'lua', version, '?', 'init.lua;;'),
38+
),
39+
),
40+
(
41+
'LUA_CPATH',
42+
(os.path.join(d, 'lib', 'lua', version, f'?.{so_ext};;'),),
43+
),
44+
)
45+
46+
47+
def _envdir(prefix: Prefix) -> str: # pragma: win32 no cover
48+
directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT)
49+
return prefix.path(directory)
50+
51+
52+
@contextlib.contextmanager # pragma: win32 no cover
53+
def in_env(prefix: Prefix) -> Generator[None, None, None]:
54+
with envcontext(get_env_patch(_envdir(prefix))):
55+
yield
56+
57+
58+
def install_environment(
59+
prefix: Prefix,
60+
version: str,
61+
additional_dependencies: Sequence[str],
62+
) -> None: # pragma: win32 no cover
63+
helpers.assert_version_default('lua', version)
64+
65+
envdir = _envdir(prefix)
66+
with clean_path_on_failure(envdir):
67+
with in_env(prefix):
68+
# luarocks doesn't bootstrap a tree prior to installing
69+
# so ensure the directory exists.
70+
os.makedirs(envdir, exist_ok=True)
71+
72+
# Older luarocks (e.g., 2.4.2) expect the rockspec as an arg
73+
for rockspec in prefix.star('.rockspec'):
74+
make_cmd = ('luarocks', '--tree', envdir, 'make', rockspec)
75+
helpers.run_setup_cmd(prefix, make_cmd)
76+
77+
# luarocks can't install multiple packages at once
78+
# so install them individually.
79+
for dependency in additional_dependencies:
80+
cmd = ('luarocks', '--tree', envdir, 'install', dependency)
81+
helpers.run_setup_cmd(prefix, cmd)
82+
83+
84+
def run_hook(
85+
hook: Hook,
86+
file_args: Sequence[str],
87+
color: bool,
88+
) -> Tuple[int, bytes]: # pragma: win32 no cover
89+
with in_env(hook.prefix):
90+
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package = "pre-commit-package"
2+
version = "dev-1"
3+
4+
source = {
5+
url = "git+ssh://git@github.com/pre-commit/pre-commit.git"
6+
}
7+
description = {}
8+
dependencies = {}
9+
build = {
10+
type = "builtin",
11+
modules = {},
12+
}

pre_commit/store.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ def _git_cmd(*args: str) -> None:
188188

189189
LOCAL_RESOURCES = (
190190
'Cargo.toml', 'main.go', 'go.mod', 'main.rs', '.npmignore',
191-
'package.json', 'pre_commit_placeholder_package.gemspec', 'setup.py',
191+
'package.json', 'pre-commit-package-dev-1.rockspec',
192+
'pre_commit_placeholder_package.gemspec', 'setup.py',
192193
'environment.yml', 'Makefile.PL', 'pubspec.yaml',
193194
'renv.lock', 'renv/activate.R', 'renv/LICENSE.renv',
194195
)

testing/gen-languages-all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sys
33

44
LANGUAGES = [
55
'conda', 'coursier', 'dart', 'docker', 'docker_image', 'dotnet', 'fail',
6-
'golang', 'node', 'perl', 'pygrep', 'python', 'r', 'ruby', 'rust',
6+
'golang', 'lua', 'node', 'perl', 'pygrep', 'python', 'r', 'ruby', 'rust',
77
'script', 'swift', 'system',
88
]
99
FIELDS = [

testing/get-lua.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Install the runtime and package manager.
5+
sudo apt install lua5.3 liblua5.3-dev luarocks
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- id: hello-world-lua
2+
name: hello world lua
3+
entry: hello-world-lua
4+
language: lua
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env lua
2+
3+
print('hello world')
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package = "hello"
2+
version = "dev-1"
3+
4+
source = {
5+
url = "git+ssh://git@github.com/pre-commit/pre-commit.git"
6+
}
7+
description = {}
8+
dependencies = {}
9+
build = {
10+
type = "builtin",
11+
modules = {},
12+
install = {
13+
bin = {"bin/hello-world-lua"}
14+
},
15+
}

0 commit comments

Comments
 (0)