Skip to content

Commit

Permalink
feat: ✨ Introduce configuration file (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
robvanderleek authored Dec 8, 2024
1 parent 4fd60af commit 0dc5f69
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 57 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ jobs:
- name: 'Checkout sources'
uses: actions/checkout@v4

- name: 'Set up Python'
uses: actions/setup-python@v4
with:
python-version: 3.12

- name: 'Set up Poetry'
uses: snok/install-poetry@v1

- name: 'Install dependencies'
run: poetry install --no-interaction --no-root

- name: 'Lint code with Ruff'
run: |
pipx install ruff
ruff check .
run: poetry run ruff check .

- name: 'Type cheking with MyPy'
run: |
pipx install mypy
mypy --ignore-missing-imports codelimit/
run: poetry run mypy codelimit/
8 changes: 8 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,35 @@ jobs:
steps:
- name: 'Checkout sources'
uses: actions/checkout@v3

- name: 'Set up Python'
uses: actions/setup-python@v4
with:
python-version: 3.12

- name: 'Set up Poetry'
uses: snok/install-poetry@v1

- name: 'Install dependencies'
run: poetry install --no-interaction --no-root

- name: 'Run unit-tests with coverage'
run: poetry run pytest --cov --cov-report=xml

- name: 'Run codelimit'
run: |
poetry run codelimit scan .
- name: 'Build and run codelimit binary'
run: |
poetry run pyinstaller -n codelimit -F codelimit/__main__.py
./dist/codelimit scan .
- name: 'Run Code Limit action'
uses: getcodelimit/codelimit-action@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
upload: true

- name: 'Upload coverage reports to Codecov'
uses: codecov/codecov-action@v3
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@
Code Limit is a tool for developers with one goal: _it tells the developer when
it’s time to refactor_.

Check out the [documentation](https://codelimit-docs.vercel.app) and start
Check out the [documentation](https://getcodelimit.github.io) and start
using Code Limit today to keep your code maintainable.

## Quickstart

Depending on your development workflow, Code Limit can run in many different
ways (e.g.: pre-commit hook, GitHub Action, standalone, etc.). See the
[Quickstart documentation](https://codelimit-docs.vercel.app/quickstart/) for
[Quickstart documentation](https://getcodelimit.github.io/quickstart/) for
examples.

## Standalone usage

Code Limit can run as a standalone program to check and inspect a codebase, see
the [Standalone Usage documentation](https://codelimit-docs.vercel.app/usage/)
to get started.
the [Standalone Usage documentation](https://getcodelimit.github.io/usage/) to
get started.

## Configuration

Code Limit aims to be zero-configuration. However, sometimes the exception
proves the rule. Check out the [Configuration
documentation](https://codelimit-docs.vercel.app/configuration/) for all
documentation](https://getcodelimit.github.io/configuration/) for all
configuration options.

## Development

See the [Development
documentation](https://codelimit-docs.vercel.app/development) if you want to
documentation](https://getcodelimit.github.io/development) if you want to
extend or contribute to Code Limit.

## Feedback, suggestions and bug reports
Expand Down
4 changes: 4 additions & 0 deletions codelimit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def check(
):
if exclude:
Configuration.excludes.extend(exclude)
Configuration.load(Path('.'))
check_command(paths, quiet)


Expand All @@ -50,6 +51,7 @@ def scan(
):
if exclude:
Configuration.excludes.extend(exclude)
Configuration.load(path)
scan_command(path)


Expand All @@ -64,6 +66,7 @@ def report(
ReportFormat, typer.Option("--format", help="Output format")
] = ReportFormat.text,
):
Configuration.load(path)
report_command(path, full, totals, fmt)


Expand All @@ -86,6 +89,7 @@ def main(
] = None,
):
"""Code Limit: Your refactoring alarm."""

if verbose:
Configuration.verbose = True
if version:
Expand Down
47 changes: 18 additions & 29 deletions codelimit/common/Configuration.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
class Configuration:
excludes = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"test",
"tests",
]
from pathlib import Path

from yaml import load, FullLoader


class Configuration:
excludes: list[str] = []
verbose = False

@classmethod
def load(cls, root: Path):
config_path = root.joinpath(".codelimit.yml")
if not config_path.exists():
return
with open(config_path) as f:
d = load(f, Loader=FullLoader)
if "excludes" in d:
cls.excludes.extend(d["excludes"])
if "verbose" in d:
cls.verbose = d["verbose"]
52 changes: 41 additions & 11 deletions codelimit/common/Scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def scan_codebase(path: Path, cached_report: Union[Report, None] = None) -> Code
print_header(cached_report, path)
scan_totals = ScanTotals()
with Live(refresh_per_second=2) as live:

def add_file_entry(entry: SourceFileEntry):
scan_totals.add(entry)
table = ScanResultTable(scan_totals)
Expand Down Expand Up @@ -82,12 +81,13 @@ def print_refactor_candidates(scan_totals: ScanTotals):


def _scan_folder(
codebase: Codebase,
folder: Path,
cached_report: Union[Report, None] = None,
add_file_entry: Union[Callable[[SourceFileEntry], None], None] = None,
codebase: Codebase,
folder: Path,
cached_report: Union[Report, None] = None,
add_file_entry: Union[Callable[[SourceFileEntry], None], None] = None,
):
excludes = Configuration.excludes.copy()
excludes = DEFAULT_EXCLUDES.copy()
excludes.extend(Configuration.excludes)
gitignore_excludes = _read_gitignore(folder)
if gitignore_excludes:
excludes.extend(gitignore_excludes)
Expand Down Expand Up @@ -115,11 +115,11 @@ def _scan_folder(


def _scan_file(
codebase: Codebase,
lexer: Lexer,
root: Path,
path: str,
cached_report: Union[Report, None] = None,
codebase: Codebase,
lexer: Lexer,
root: Path,
path: str,
cached_report: Union[Report, None] = None,
) -> SourceFileEntry:
checksum = calculate_checksum(path)
rel_path = relpath(path, root)
Expand Down Expand Up @@ -190,3 +190,33 @@ def _read_gitignore(path: Path) -> list[str] | None:

def is_excluded(path: Path, spec: PathSpec):
return spec.match_file(path)


DEFAULT_EXCLUDES = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"test",
"tests",
]
Loading

0 comments on commit 0dc5f69

Please sign in to comment.