Skip to content

Commit dd0e136

Browse files
docs: Improve documentation (#289)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 2b37923 commit dd0e136

File tree

15 files changed

+610
-181
lines changed

15 files changed

+610
-181
lines changed

Diff for: README.rst

+1-176
Original file line numberDiff line numberDiff line change
@@ -31,189 +31,14 @@ Ansys pre-commit hooks
3131
:alt: pre-commit.ci status
3232

3333
This Ansys repository contains `pre-commit`_ hooks for different purposes.
34-
Currently, these hooks are available:
34+
The following hooks are currently available:
3535

3636
* ``add-license-headers``: Add missing license headers to files by using
3737
`REUSE <https://reuse.software/>`_ . To use this hook, you must
3838
have ``REUSE`` implemented in your repository.
3939
* ``tech-review``: Do a technical review of your repository according to
4040
`Ansys repository requirements <https://dev.docs.pyansys.com/packaging/structure.html>`_
4141

42-
``add-license-headers`` setup
43-
-----------------------------
44-
45-
Add required directories
46-
^^^^^^^^^^^^^^^^^^^^^^^^
47-
48-
If you are using the ansys.jinja2 template and MIT.txt license, skip this step. By default, the hook will make symbolic links
49-
from its "assets" directory containing LICENSES/MIT.txt and .reuse/templates/ansys.jinja2
50-
to your repository when the hook runs. The .reuse and LICENSES directories will be deleted once the hook is
51-
done running.
52-
53-
If you are using a custom template, create a directory named ``.reuse``, and if you are using a custom license, create a directory
54-
named ``LICENSES`` in the root of your repository. The custom template cannot be named ``ansys.jinja2``, otherwise it will be removed
55-
after the hook is done running. The custom license cannot be named ``MIT.txt`` for the same reason. The ``.reuse`` and/or ``LICENSES``
56-
directories will have to be committed to your repository and will not be removed once the hook is done running as long as there
57-
are custom templates or licenses in those directories. Your project should have the following layout:
58-
59-
::
60-
61-
project
62-
├── LICENCES
63-
│ └── license_name.txt
64-
├── .reuse
65-
│ └── templates
66-
│ └── template_name.jinja2
67-
├── src
68-
├── examples
69-
├── tests
70-
├── .pre-commit-config.yaml
71-
├── pyproject.toml
72-
73-
Where ``license_name`` is the name of the license that is being used, for example, MIT.txt, and
74-
``template_name`` is the name of the custom template being used. The jinja2 file contains the
75-
template for the license headers that are added to the files.
76-
77-
Licenses that are supported by ``REUSE`` can be found in the
78-
`spdx/license-list-data <https://github.com/spdx/license-list-data/tree/main/text>`_ repository.
79-
Please select a license text file from that repository, and copy it to the LICENSES directory.
80-
81-
Set custom arguments
82-
^^^^^^^^^^^^^^^^^^^^
83-
84-
.. code:: yaml
85-
86-
- repo: https://github.com/ansys/pre-commit-hooks
87-
rev: v0.5.1
88-
hooks:
89-
- id: add-license-headers
90-
args: ["--custom_copyright", "custom copyright phrase", "--custom_template", "template_name", "--custom_license", "license_name", "--ignore_license_check", "--start_year", "2023"]
91-
92-
``args`` can also be formatted as follows:
93-
94-
.. code:: yaml
95-
96-
args:
97-
- --custom_copyright=custom copyright phrase
98-
- --custom_template=template_name
99-
- --custom_license=license_name
100-
- --ignore_license_check
101-
- --start_year=2023
102-
103-
* ``custom copyright phrase`` is the copyright line you want to include in the license
104-
header. By default, it uses ``"ANSYS, Inc. and/or its affiliates."``.
105-
* ``template_name`` is the name of the .jinja2 file located in ``.reuse/templates/``.
106-
By default, it uses ``ansys``.
107-
* ``license_name`` is the name of the license being used. For example, MIT, ECL-1.0, etc.
108-
To view a list of licenses that are supported by ``REUSE``, see
109-
https://github.com/spdx/license-list-data/tree/main/text. By default it uses ``MIT``.
110-
* ``ignore_license_check`` is whether or not to check for the license in the header. By default,
111-
it is ``False``, meaning the files are checked for both the copyright and licensing information
112-
in the header. Add ``--ignore_license_check`` to ignore checking for licensing information
113-
in the files.
114-
* ``start_year`` is the start year of the copyright statement. By default, the ``start_year`` is
115-
the current year, making the copyright statement
116-
"Copyright (C) 2024 ANSYS, Inc. and/or its affiliates." If you are adding license headers
117-
to packages released before the current year, add the ``start_year`` argument with the year your
118-
package was released. For example, if ``start_year`` is 2023, the copyright statement would be
119-
"Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates." assuming the current year is 2024.
120-
121-
Specify directories to run the hook on
122-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123-
124-
By default, the hook will run on proto files in any directory, as well as python files within
125-
directories named ``src``, ``examples``, and ``tests``. To specify additional files and/or directories
126-
the hook should run on, add the necessary regex to the ``files`` line in your
127-
.pre-commit-config.yaml file:
128-
129-
.. code:: yaml
130-
131-
- repo: https://github.com/ansys/pre-commit-hooks
132-
rev: v0.5.1
133-
hooks:
134-
- id: add-license-headers
135-
files: '(src|examples|tests|newFolder)/.*\.(py|newExtension)|\.(proto|newExtension)'
136-
137-
Ignore specific files or file types
138-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139-
140-
In .pre-commit-config.yaml:
141-
142-
.. code:: yaml
143-
144-
- repo: https://github.com/ansys/pre-commit-hooks
145-
rev: v0.5.1
146-
hooks:
147-
- id: add-license-headers
148-
exclude: |
149-
(?x)^(
150-
path/to/file1.py |
151-
path/to/.*\.(ts|cpp) |
152-
(.folder1|folder2)/.* |
153-
.*\.js |
154-
\..* |
155-
)$
156-
157-
* ``path/to/file1.py`` excludes the stated file.
158-
* ``path/to/.*\.(ts|cpp)`` excludes all .ts and .cpp files within the ``path/to`` directory.
159-
* ``(.folder1|folder2)/.*`` excludes directories named .folder1 and folder2.
160-
* ``.*\.js`` excludes all .js files in all directories.
161-
* ``\..*`` excludes all hidden files.
162-
163-
``tech-review`` setup
164-
---------------------
165-
166-
These are the default values for the arguments of the tech-review hook:
167-
168-
* ``--author_maint_name=ANSYS, Inc.``
169-
170-
* ``--license=MIT``
171-
* ``--url=https://github.com/ansys/{repo-name}``, replacing ``repo-name`` with the name of the repository
172-
173-
The ``--author_maint_name`` is the name of the author and maintainer in the ``pyproject.toml`` file.
174-
By default, it is "Ansys, Inc.".
175-
176-
The ``--author_maint_email`` is the email of the author and maintainer in the ``pyproject.toml`` file.
177-
By default, it is "[email protected]".
178-
179-
The ``--license`` argument is the license that is being used by your repository. By default, it is
180-
MIT.
181-
182-
The ``--url`` argument is automatically rendered based on the repository name. If your repository
183-
is not in the Ansys organization, please add this argument to your configuration in
184-
.pre-commit-config.yaml.
185-
186-
The ``--product`` argument is required if a ``README.rst`` or ``README.md`` file does not
187-
exist in your repository and you want the template to render correctly. The product
188-
for ``PyMechanical`` would be ``mechanical``, for example.
189-
190-
The ``--non_compliant_name`` flag can be used if your repository does not follow the typical
191-
naming convention of ``ansys-*-*``.
192-
193-
Technical review hook in ``ansys/pre-commit-hooks``' .pre-commit-config.yaml file:
194-
195-
.. code:: yaml
196-
197-
- repo: https://github.com/ansys/pre-commit-hooks
198-
rev: v0.5.1
199-
hooks:
200-
- id: tech-review
201-
args:
202-
- --product=pre_commit_hooks
203-
- --non_compliant_name
204-
205-
Technical review hook in ``PyMechanical``'s .pre-commit-config.yaml file:
206-
207-
.. code:: yaml
208-
209-
- repo: https://github.com/ansys/pre-commit-hooks
210-
rev: v0.5.1
211-
hooks:
212-
- id: tech-review
213-
args:
214-
- --product=mechanical
215-
216-
21742
How to install
21843
--------------
21944

Diff for: doc/changelog.d/289.documentation.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve documentation

Diff for: doc/source/_static/images/license-header-dark.PNG

32.8 KB
Loading

Diff for: doc/source/_static/images/license-header-light.png

35.8 KB
Loading

Diff for: doc/source/contribute/add-hook.rst

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. title:: Creating pre-commit hooks
2+
3+
Creating a pre-commit hook
4+
==========================
5+
6+
1. Create a Python file in the `src/ansys/pre_commit_hooks <https://github.com/ansys/pre-commit-hooks/tree/main/src/ansys/pre_commit_hooks>`_
7+
directory. For example, ``new_hook.py``, with a main function:
8+
9+
.. code:: python
10+
11+
def main():
12+
print("New hook!")
13+
return 0
14+
15+
if __name__ == "__main__":
16+
main()
17+
18+
.. note::
19+
20+
The ``main`` function of the hook must return a non-zero exit code if the hook fails. For the
21+
hooks in the Ansys Pre-Commit Hooks repository, the exit code is set to 1 if the hook fails
22+
and 0 if the hook passes.
23+
24+
2. Create a Python file to test the hook in the `tests <https://github.com/ansys/pre-commit-hooks/tree/main/tests>`_
25+
directory. For example, ``test_new_hook.py``.
26+
27+
3. Add the hook to the ``.pre-commit-hooks.yaml`` file:
28+
29+
.. code:: yaml
30+
31+
- id: "new-hook"
32+
name: "New Hook"
33+
description: "A new pre-commit hook for the Ansys Pre-Commit Hooks repository"
34+
entry: new-hook
35+
language: python
36+
37+
In the ``.pre-commit-hooks.yaml`` file, you can include additional information about the hook
38+
in the ``files``, ``requires-serial``, or ``pass_filenames`` fields. For more information, see
39+
the `Creating new hooks <https://pre-commit.com/#creating-new-hooks>`_ page in the
40+
``pre-commit`` documentation.
41+
42+
4. Add the hook to the ``entry_points`` dictionary in the ``setup.py`` file:
43+
44+
.. code:: python
45+
46+
entry_points = {
47+
"console_scripts": [
48+
"new-hook = ansys.pre_commit_hooks.new_hook:main",
49+
],
50+
},
51+
52+
5. After adding code to the pre-commit hook file, for example, ``new_hook.py``, add the hook's
53+
required dependencies to the ``pyproject.toml`` and ``setup.py`` files if they are not already
54+
there:
55+
56+
In the ``pyproject.toml`` file, add the dependency in the ``requires`` list under the
57+
``[build-system]`` section, pinning the dependency to a specific version:
58+
59+
.. code:: toml
60+
61+
[build-system]
62+
requires = [
63+
"setuptools>=42.0",
64+
"GitPython==3.1.44",
65+
]
66+
67+
In the ``setup.py`` file, add the dependency to the ``install_requires`` list, pinning
68+
the dependency to a specific version:
69+
70+
.. code:: python
71+
72+
install_requires = [
73+
"GitPython==3.1.44",
74+
"importlib-metadata==8.6.1",
75+
]

Diff for: doc/source/contribute/index.rst

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.. title:: Contribute
2+
3+
Contribute
4+
==========
5+
6+
Learn how to contribute to the Ansys Pre-Commit Hooks repository.
7+
8+
.. grid:: 3
9+
10+
.. grid-item-card:: Create a pre-commit hook :octicon:`code-square`
11+
:padding: 2 2 2 2
12+
:link: add-hook
13+
:link-type: doc
14+
15+
How to add a pre-commit hook to the repository
16+
17+
:bdg-primary-line:`Create new hook`
18+
19+
.. grid-item-card:: Test a pre-commit hook :octicon:`note`
20+
:padding: 2 2 2 2
21+
:link: testing
22+
:link-type: doc
23+
24+
How to test a pre-commit hook locally
25+
26+
:bdg-primary-line:`Test`
27+
28+
.. toctree::
29+
:hidden:
30+
:maxdepth: 3
31+
32+
add-hook
33+
testing

Diff for: doc/source/contribute/testing.rst

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
.. title:: Testing pre-commit hooks
2+
3+
Testing a pre-commit hook
4+
=========================
5+
6+
Running ``pre-commit`` locally
7+
------------------------------
8+
9+
To ensure a pre-commit hook works as expected, run ``pre-commit`` locally on the repository. First,
10+
install the ``ansys/pre-commit-hooks`` project in a virtual environment:
11+
12+
.. tab-set::
13+
14+
.. tab-item:: Linux
15+
16+
.. code:: bash
17+
18+
python -m venv .venv
19+
source .venv/bin/activate
20+
pip install -e .
21+
22+
.. tab-item:: Windows
23+
24+
.. code:: batch
25+
26+
python -m venv .venv
27+
.venv\Scripts\activate
28+
pip install -e .
29+
30+
Next, add the pre-commit hook to the repository's ``.pre-commit-config.yaml`` file, copying the
31+
details of the hook from the ``.pre-commit-hooks.yaml`` file. For example, to add the ``new-hook``
32+
hook:
33+
34+
.. code:: yaml
35+
36+
- repo: local
37+
hooks:
38+
- id: "new-hook"
39+
name: "New Hook"
40+
description: "A new pre-commit hook for the Ansys Pre-Commit Hooks repository"
41+
entry: new-hook
42+
language: python
43+
44+
Run ``pre-commit`` on the repository:
45+
46+
.. code:: bash
47+
48+
pre-commit run --all-files --verbose
49+
50+
Make sure to test the hook locally on both Linux and Windows operating systems.
51+
52+
Running ``pytest`` tests
53+
------------------------
54+
55+
Create tests using ``pytest`` in the ``tests`` directory to ensure functions in the hook work as
56+
expected. For example, in ``test_new_hook.py``:
57+
58+
.. code:: python
59+
60+
import pytest
61+
import ansys.pre_commit_hooks.new_hook as hook
62+
63+
def test_main():
64+
"""Test the main function of the new-hook."""
65+
# Assert main passes
66+
assert hook.main() == 0
67+
68+
For more information on writing tests, see the
69+
`pytest documentation <https://docs.pytest.org/en/stable/>`_.

0 commit comments

Comments
 (0)