Skip to content

Add Sway syntax #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,57 @@ options:
--verbose enable verbose output
```

## Using via Docker Image

### Building the Docker Image

You can build the Docker image for this project by running the following command:

```bash
python setup.py build_docker
```

OR directly from root project directory:

```bash
docker build --file docker/Dockerfile -t grep-ast-image .
```

### Calling grep-ast from Docker image

To grep from current working directory we mount it to docker to give access so paths will work also inside docker, example:

```
docker run -it -v "$(pwd)":/app:ro grep-ast-image str grep_ast/*.py
```

### Using Docker image for pytest tests!

To ensure reproducible testing and ease of debugging, you can run pytest tests inside the Docker image. This is particularly useful when collaborating in an open-source manner.

You can use this docker image e.g. to run pytest:

```bash
python setup.py docker_test
```

OR directly:

```
docker run -it --entrypoint bash grep-ast-image -c 'cd /grep-ast; pwd; pytest'
```

This command will build the Docker image and execute pytest tests inside it.

### If you want to debug Docker image itself

If you want to execute commands inside docker image, overwrite entrypoint with bash and give it commands you want, examples:

```bash
$ docker run -it -v "$(pwd)":/app:ro --entrypoint bash grep-ast-image -c 'cd /app; pwd; ls'
$ docker run -it --entrypoint bash grep-ast-image -c 'cd /grep-ast; pwd; ls'
```

## Examples

Here we search for **"encoding"** in the source to this tool:
Expand Down
13 changes: 13 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.10-slim
COPY . /grep-ast

# Unfortunately to build the multi-arch docker image we need `build-essential` for amd64.
# Apparently py-tree-sitter-languages doesn't have a pre-built binary wheel?

RUN apt-get update && \
apt-get install --no-install-recommends -y build-essential git libportaudio2 && \
rm -rf /var/lib/apt/lists/* && \
pip install pytest && \
pip install --no-cache-dir /grep-ast
WORKDIR /app
ENTRYPOINT ["grep-ast"]
3 changes: 2 additions & 1 deletion grep_ast/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
".scala": "scala",
".sql": "sql",
".sqlite": "sqlite",
".sw": "sway", # https://github.com/FuelLabs/tree-sitter-sway
".toml": "toml",
".tsq": "tsq",
".tsx": "typescript",
".ts": "typescript",
".tsx": "typescript",
".yaml": "yaml",
}

Expand Down
57 changes: 56 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import subprocess

from setuptools import find_packages, setup
from setuptools import Command, find_packages, setup

with open("requirements.txt") as f:
requirements = f.read().splitlines()
Expand All @@ -9,7 +10,61 @@
long_description = f.read()
long_description = re.sub(r"\n!\[.*\]\(.*\)", "", long_description)


class BuildDockerImageCommand(Command):
description = "Build Docker image for the project"
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
command = ["docker", "build", "--file", "docker/Dockerfile", "-t", "grep-ast-image", "."]
subprocess.check_call(command)


class DockerTestCommand(Command):
description = "Build Docker image and run pytest inside it"
user_options = []

def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
build_command = [
"docker",
"build",
"--file",
"docker/Dockerfile",
"-t",
"grep-ast-image",
".",
]
test_command = [
"docker",
"run",
"-it",
"--entrypoint",
"bash",
"grep-ast-image",
"-c",
"cd /grep-ast; pytest",
]
subprocess.check_call(build_command)
subprocess.check_call(test_command)


setup(
cmdclass={
"build_docker": BuildDockerImageCommand,
"docker_test": DockerTestCommand,
},
name="grep-ast",
version="0.2.4",
description="A tool to grep through the AST of a source file",
Expand Down