Skip to content
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

Add wesnoth-map-diff #6472

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions .github/workflows/map-diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Map Diff

on:
pull_request:
paths:
- '**.map'

jobs:
comment-map-diff:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3.0.0
- uses: actions/setup-node@v3.0.0
with:
node-version: '16'
- name: Package install
run: |
cd ./utils/wesnoth-map-diff
npm install
- name: Package build
run: |
cd ./utils/wesnoth-map-diff
npm run build:prod
- name: Get maps diff
id: get-maps-diff
run: |
cd ./utils/wesnoth-map-diff

## Get Maps
git fetch --depth=1 origin ${{ github.event.pull_request.base.sha }}

map_paths=($(git diff --name-only HEAD ${{ github.event.pull_request.base.sha }} | grep ".map$"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reproduce the issue with git diff --name-only HEAD ${{ github.event.pull_request.base.sha }} '*.map' but I think strictly it should be git diff --name-only HEAD ${{ github.event.pull_request.base.sha }} -- '*.map'

If grep is really necessary then it should be grep '\.map$' since a dot matches any character.

To not use word splitting and unintentionally pathname expansion it'd be better to use mapfile here:
mapfile -t map_paths < <(git diff --name-only HEAD ${{ github.event.pull_request.base.sha }} -- '*.map')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can reproduce the issue using my test repository:

> git clone git@github.com:macabeus/wesnoth.git
> cd wesnoth
> git checkout feat/add-wesnoth-map-diff-backup
> git diff --name-only HEAD 514b8bca9cf -- '*.map'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot reproduce it there either. For me git works as documented.

$ git diff --name-only HEAD 514b8bca9cf -- '*.map'
data/test.map
data/test2.map

Anyway it's not that big a deal to keep the grep as well.


for map_path in "${map_paths[@]}"
do
map_filename=$(basename "$map_path")
git show ${{ github.event.pull_request.base.sha }}:$map_path > $map_filename
macabeus marked this conversation as resolved.
Show resolved Hide resolved
done

## Run map diff
diff_images=()

for map_path in "${map_paths[@]}"
do
map_filename=$(basename "$map_path")
output_filename=$(echo "$map_filename" | perl -pe 's/map$/png/')
macabeus marked this conversation as resolved.
Show resolved Hide resolved

node ./build/index.js "./$map_filename" "../../$map_path" "./$output_filename"

diff_images+=("$output_filename")
done

## Compress images
sudo apt-get install -y pngquant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already utils/woptipng.py

Copy link
Contributor Author

@macabeus macabeus Mar 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice this tool before.

But anyway, utils/woptipng.py isn't standalone.
It depends on a set of different packages (Pillow, advdef, imagemagick/convert, and optipng).

I think it's simpler just to install a single package (pngquant) and run it on this action.
pngquant works well for this kind of image as far as I checked.


for diff_image in "${diff_images[@]}"
do
pngquant "$diff_image" -o "$diff_image" --force
done

## Write comment body
comment_body=""

for i in "${!diff_images[@]}"
do
map_path=${map_paths[$i]}
diff_image=${diff_images[$i]}

image_link=$(curl -X POST "https://api.imgur.com/3/upload" \
-F "image=@\"$diff_image\"" | jq ".data.link" -r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often will this upload images?

Copy link
Contributor Author

@macabeus macabeus Mar 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uploads an image whenever a PR (with a .map file) is opened or updated.


comment_body="$comment_body<h3>$map_path</h3><img src=\"$image_link\" /><br />"
done

echo "::set-output name=COMMENT_BODY::$comment_body"
- name: Add comment
uses: peter-evans/create-or-update-comment@v1.4.5
with:
issue-number: ${{ github.event.pull_request.number }}
edit-mode: replace
body: |
${{ steps.get-maps-diff.outputs.COMMENT_BODY }}
1 change: 1 addition & 0 deletions utils/wesnoth-map-diff/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
81 changes: 81 additions & 0 deletions utils/wesnoth-map-diff/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
overrides: [
{
files: ['**/*.ts'],
},
],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {
'@typescript-eslint/member-delimiter-style': ['error', {
multiline: {
delimiter: 'comma',
requireLast: true,
},
singleline: {
delimiter: 'comma',
requireLast: false,
},
}],
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-empty-interface': [
'error',
{
allowSingleExtends: true,
},
],
'@typescript-eslint/no-var-requires': 'off',
'arrow-parens': 'off',
'comma-dangle': [
'error',
{
arrays: 'always-multiline',
exports: 'always-multiline',
functions: 'never',
imports: 'always-multiline',
objects: 'always-multiline',
},
],
'function-paren-newline': ['error', 'consistent'],
'quote-props': ['error', 'as-needed'],
'max-len': [
'error',
{
code: 120,
ignoreComments: true,
ignoreRegExpLiterals: true,
ignoreStrings: true,
ignoreTemplateLiterals: true,
ignoreTrailingComments: true,
ignoreUrls: true,
tabWidth: 2,
},
],
'multiline-ternary': ['error', 'always-multiline'],
'no-unused-expressions': ['error', { allowTernary: true }],
'no-multiple-empty-lines': [
'error',
{
max: 1,
maxEOF: 1,
},
],
'implicit-arrow-linebreak': 'off',
'import/extensions': 'off',
'import/no-unresolved': 'off',
semi: ['error', 'never'],
'space-before-function-paren': ['error', 'always'],
'object-curly-spacing': ['error', 'always'],
'operator-linebreak': [
'error',
'before',
{ overrides: { '=': 'after', ':': 'before', '?': 'before' } },
],
},
}
4 changes: 4 additions & 0 deletions utils/wesnoth-map-diff/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.eslintcache

node_modules/
build/
39 changes: 39 additions & 0 deletions utils/wesnoth-map-diff/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# wesnoth-map-diff

> 🗺 Print the diff between two maps

## Setup

1 - Make sure you have Node on your system. If you don't have it, install it. If you are using macOS or Linux, you can install Node using [nvm](https://github.com/nvm-sh/nvm). On Windows, use [nvm-windows](https://github.com/coreybutler/nvm-windows).

2 - Open the terminal on `utils/wesnoth-map-diff` folder and run the following command to install the project dependencies:

```
npm i
```

3 - Then, run the following command to build it:

```
npm run build:dev
```

4 - Finally, run it using `node ./build/index.js [path for the old map] [path for the new map] [output filename]`. For instance:

```
node ./build/index.js old.map new.map output.png
```

## Contributing

Run tests:

```
npm run test
```

Run lint:

```
npm run lint
```
6 changes: 6 additions & 0 deletions utils/wesnoth-map-diff/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
}
3 changes: 3 additions & 0 deletions utils/wesnoth-map-diff/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['./setup-jest.js'],
}