Skip to content

Commit b95ed5f

Browse files
committed
config_helper
1 parent 2ac41f8 commit b95ed5f

File tree

11 files changed

+551
-0
lines changed

11 files changed

+551
-0
lines changed

.github/workflows/pr.yml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
on: pull_request
2+
name: Review
3+
jobs:
4+
changelog:
5+
runs-on: ubuntu-latest
6+
name: Changelog should be updated
7+
strategy:
8+
fail-fast: false
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
with:
13+
fetch-depth: 2
14+
15+
- name: Git fetch
16+
run: git fetch
17+
18+
- name: Check that changelog has been updated.
19+
run: git diff --exit-code origin/${{ github.base_ref }} -- CHANGELOG.md && exit 1 || exit 0
20+
21+
test-composer-files:
22+
name: Validate composer
23+
runs-on: ubuntu-latest
24+
strategy:
25+
matrix:
26+
php-versions: [ '8.3' ]
27+
dependency-version: [ prefer-lowest, prefer-stable ]
28+
steps:
29+
- uses: actions/checkout@master
30+
- name: Setup PHP, with composer and extensions
31+
uses: shivammathur/setup-php@v2
32+
with:
33+
php-version: ${{ matrix.php-versions }}
34+
extensions: json
35+
coverage: none
36+
tools: composer:v2
37+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
38+
- name: Get composer cache directory
39+
id: composer-cache
40+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
41+
- name: Cache dependencies
42+
uses: actions/cache@v2
43+
with:
44+
path: ${{ steps.composer-cache.outputs.dir }}
45+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
46+
restore-keys: ${{ runner.os }}-composer-
47+
- name: Validate composer files
48+
run: |
49+
composer validate --strict composer.json
50+
# Check that dependencies resolve.
51+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
52+
- name: Check that composer file is normalized
53+
run: |
54+
composer normalize --dry-run
55+
56+
php-coding-standards:
57+
name: PHP coding standards
58+
runs-on: ubuntu-latest
59+
strategy:
60+
matrix:
61+
php-versions: [ '8.3' ]
62+
steps:
63+
- uses: actions/checkout@master
64+
- name: Setup PHP, with composer and extensions
65+
uses: shivammathur/setup-php@v2
66+
with:
67+
php-version: ${{ matrix.php-versions }}
68+
extensions: json
69+
coverage: none
70+
tools: composer:v2
71+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
72+
- name: Get composer cache directory
73+
id: composer-cache
74+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
75+
- name: Cache dependencies
76+
uses: actions/cache@v2
77+
with:
78+
path: ${{ steps.composer-cache.outputs.dir }}
79+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
80+
restore-keys: ${{ runner.os }}-composer-
81+
- name: Install Dependencies
82+
run: |
83+
composer install --no-interaction --no-progress
84+
- name: PHPCS
85+
run: |
86+
composer coding-standards-check/phpcs
87+
88+
php-code-analysis:
89+
name: PHP code analysis
90+
runs-on: ubuntu-latest
91+
strategy:
92+
matrix:
93+
php-versions: [ '8.3' ]
94+
steps:
95+
- uses: actions/checkout@master
96+
- name: Setup PHP, with composer and extensions
97+
uses: shivammathur/setup-php@v2
98+
with:
99+
php-version: ${{ matrix.php-versions }}
100+
extensions: json
101+
coverage: none
102+
tools: composer:v2
103+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
104+
- name: Get composer cache directory
105+
id: composer-cache
106+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
107+
- name: Cache dependencies
108+
uses: actions/cache@v2
109+
with:
110+
path: ${{ steps.composer-cache.outputs.dir }}
111+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
112+
restore-keys: ${{ runner.os }}-composer-
113+
- name: Code analysis
114+
run: |
115+
./scripts/code-analysis
116+
117+
coding-standards-markdown:
118+
name: Markdown coding standards
119+
runs-on: ubuntu-latest
120+
steps:
121+
- name: Checkout
122+
uses: actions/checkout@v4
123+
124+
- name: Coding standards
125+
run: |
126+
docker run --rm --volume "$(pwd):/md" peterdavehello/markdownlint markdownlint --ignore LICENSE.md '**/*.md'

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
vendor

.markdownlint.jsonc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"default": true,
3+
// https://github.com/DavidAnson/markdownlint/blob/main/doc/md013.md
4+
"line-length": {
5+
"line_length": 120,
6+
"code_blocks": false,
7+
"tables": false
8+
},
9+
// https://github.com/DavidAnson/markdownlint/blob/main/doc/md024.md
10+
"no-duplicate-heading": {
11+
"siblings_only": true
12+
}
13+
}

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
[Unreleased]: https://github.com/rimi-itk/drupal_config_helper

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11
# Config helper
2+
3+
Adds some useful commands for managing config:
4+
5+
```shell
6+
config:rename Rename config.
7+
config:add-module-dependencies Enforce module dependencies in config.
8+
config:move-module-config Move config info config/install folder in a module.
9+
```
10+
11+
**Note**: The `config:move-module-config` command is somewhat is similar to the
12+
[`config:export:content:type`](https://drupalconsole.com/docs/en/commands/config-export-content-type) command the
13+
[Drupal Console](https://drupalconsole.com/), but that command does not add the dependencies needed for our
14+
requirements.
15+
16+
## Coding standards
17+
18+
Our coding are checked by GitHub Actions (cf.
19+
[.github/workflows/pr.yml](.github/workflows/pr.yml)). Use the commands below to
20+
run the checks locally.
21+
22+
### PHP
23+
24+
```sh
25+
docker run --rm --volume ${PWD}:/app --workdir /app itkdev/php8.3-fpm composer install
26+
# Fix (some) coding standards issues
27+
docker run --rm --volume ${PWD}:/app --workdir /app itkdev/php8.3-fpm composer coding-standards-apply
28+
# Check that code adheres to the coding standards
29+
docker run --rm --volume ${PWD}:/app --workdir /app itkdev/php8.3-fpm composer coding-standards-check
30+
```
31+
32+
### Markdown
33+
34+
```sh
35+
docker run --rm --volume $PWD:/md peterdavehello/markdownlint markdownlint --ignore vendor --ignore LICENSE.md '**/*.md' --fix
36+
docker run --rm --volume $PWD:/md peterdavehello/markdownlint markdownlint --ignore vendor --ignore LICENSE.md '**/*.md'
37+
```
38+
39+
## Code analysis
40+
41+
We use [PHPStan](https://phpstan.org/) for static code analysis.
42+
43+
Running statis code analysis on a standalone Drupal module is a bit tricky, so we use a helper script to run the
44+
analysis:
45+
46+
```sh
47+
docker run --rm --volume ${PWD}:/app --workdir /app itkdev/php8.3-fpm scripts/code-analysis
48+
```

composer.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "itk-dev/drupal_config_helper",
3+
"description": "Config helper",
4+
"license": "GPL-2.0-or-later",
5+
"type": "drupal-module",
6+
"keywords": [
7+
"Drupal"
8+
],
9+
"require": {
10+
"php": "^8.3",
11+
"drush/drush": "^12.5"
12+
},
13+
"require-dev": {
14+
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
15+
"drupal/coder": "^8.3",
16+
"drupal/maillog": "^1.1",
17+
"ergebnis/composer-normalize": "^2.42",
18+
"mglaman/phpstan-drupal": "^1.2",
19+
"phpstan/extension-installer": "^1.3",
20+
"phpstan/phpstan-deprecation-rules": "^1.1"
21+
},
22+
"repositories": {
23+
"drupal": {
24+
"type": "composer",
25+
"url": "https://packages.drupal.org/8"
26+
}
27+
},
28+
"minimum-stability": "stable",
29+
"config": {
30+
"allow-plugins": {
31+
"dealerdirect/phpcodesniffer-composer-installer": true,
32+
"ergebnis/composer-normalize": true,
33+
"phpstan/extension-installer": true
34+
},
35+
"sort-packages": true
36+
},
37+
"scripts": {
38+
"code-analysis": [
39+
"@code-analysis/phpstan"
40+
],
41+
"code-analysis/phpstan": [
42+
"phpstan analyse"
43+
],
44+
"coding-standards-apply": [
45+
"@coding-standards-apply/phpcs"
46+
],
47+
"coding-standards-apply/phpcs": [
48+
"phpcbf --standard=phpcs.xml.dist"
49+
],
50+
"coding-standards-check": [
51+
"@coding-standards-check/phpcs"
52+
],
53+
"coding-standards-check/phpcs": [
54+
"phpcs --standard=phpcs.xml.dist"
55+
]
56+
}
57+
}

config_helper.info.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: 'config_helper'
2+
type: module
3+
description: 'Config helper'
4+
# Used only for development and testing.
5+
hidden: true
6+
core_version_requirement: ^10
7+
package: 'ITK'
8+
9+
# https://www.drupal.org/node/2087879

phpcs.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<description>PHP Code Sniffer configuration</description>
4+
5+
<file>.</file>
6+
<exclude-pattern>vendor/</exclude-pattern>
7+
<exclude-pattern>node_modules/</exclude-pattern>
8+
9+
<!-- Show progress of the run -->
10+
<arg value="p"/>
11+
12+
<arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,yml"/>
13+
<config name="drupal_core_version" value="9"/>
14+
15+
16+
<rule ref="Drupal">
17+
<!-- We want to be able to use "package" and "version" in our custom modules -->
18+
<exclude name="Drupal.InfoFiles.AutoAddedKeys.Project"/>
19+
<exclude name="Drupal.InfoFiles.AutoAddedKeys.Version"/>
20+
</rule>
21+
22+
<rule ref="DrupalPractice"/>
23+
</ruleset>

phpstan.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
6+
ignoreErrors:
7+
- "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ has parameter \\$[a-zA-Z0-9_]+ with no value type specified in iterable type array#"
8+
- "#Method [a-zA-Z0-9\\_\\\\:\\(\\)]+ return type has no value type specified in iterable type array#"
9+
10+
# Local Variables:
11+
# mode: yaml
12+
# End:

scripts/code-analysis

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
script_dir=$(pwd)
3+
module_name=$(basename "$script_dir")
4+
drupal_dir=vendor/drupal-module-code-analysis
5+
# Relative to $drupal_dir
6+
module_path=web/modules/contrib/$module_name
7+
8+
cd "$script_dir" || exit
9+
10+
drupal_composer() {
11+
composer --working-dir="$drupal_dir" --no-interaction "$@"
12+
}
13+
14+
# Create new Drupal 10 project
15+
if [ ! -f "$drupal_dir/composer.json" ]; then
16+
composer --no-interaction create-project drupal/recommended-project:^10 "$drupal_dir"
17+
fi
18+
# Copy our code into the modules folder
19+
mkdir -p "$drupal_dir/$module_path"
20+
# https://stackoverflow.com/a/15373763
21+
rsync --archive --compress . --filter=':- .gitignore' --exclude "$drupal_dir" --exclude .git "$drupal_dir/$module_path"
22+
23+
drupal_composer config minimum-stability dev
24+
25+
# Allow ALL plugins
26+
# https://getcomposer.org/doc/06-config.md#allow-plugins
27+
drupal_composer config --no-plugins allow-plugins true
28+
29+
drupal_composer require wikimedia/composer-merge-plugin
30+
drupal_composer config extra.merge-plugin.include "$module_path/composer.json"
31+
# https://www.drupal.org/project/drupal/issues/3220043#comment-14845434
32+
drupal_composer require --dev symfony/phpunit-bridge
33+
34+
# Run PHPStan
35+
(cd "$drupal_dir" && vendor/bin/phpstan --configuration="$module_path/phpstan.neon")

0 commit comments

Comments
 (0)