Skip to content

Commit

Permalink
Merge pull request #3609 from nojaf/format-fsharp
Browse files Browse the repository at this point in the history
  • Loading branch information
MangelMaxime authored Nov 24, 2023
2 parents 43f34f9 + a2d5854 commit f030cd3
Show file tree
Hide file tree
Showing 186 changed files with 78,116 additions and 33,862 deletions.
8 changes: 7 additions & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
"isRoot": true,
"tools": {
"fantomas": {
"version": "6.2.0",
"version": "6.2.3",
"commands": [
"fantomas"
]
},
"husky": {
"version": "0.6.3",
"commands": [
"husky"
]
}
}
}
13 changes: 11 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ insert_final_newline = true
[*.ts]
indent_size = 2

# Fantomas (see https://github.com/fsprojects/fantomas/blob/master/docs/Documentation.md)
# Fantomas (see https://fsprojects.github.io/fantomas/docs/end-users/Configuration.html)
[*.{fs,fsx,fsi}]
max_line_length = 80
end_of_line = lf
fsharp_alternative_long_member_definitions = true
fsharp_multi_line_lambda_closing_newline = true
fsharp_multiline_bracket_style = stroustrup
fsharp_bar_before_discriminated_union_declaration = true
fsharp_multiline_bracket_style = aligned
fsharp_keep_max_number_of_blank_lines = 2
fsharp_record_multiline_formatter = number_of_items
fsharp_array_or_list_multiline_formatter = number_of_items
fsharp_align_function_signature_to_indentation = true
fsharp_multi_line_lambda_closing_newline = true
fsharp_max_if_then_else_short_width = 0
3 changes: 1 addition & 2 deletions .fantomasignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
src/**
src/fcs-fable
tests/**
tests_external/**
!src/Fable.Build/**
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

# Always use lf for F# files
*.fs text eol=lf
*.fsx text eol=lf
*.fsi text eol=lf
17 changes: 17 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ on:
branches: [ main ]

jobs:
# Separate job that verifies if all code was formatted correctly
# Run `dotnet fantomas .` to format all code.
verify-linting:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3

- name: Restore tools
run: dotnet tool restore

- name: Check F# code
run: dotnet fantomas . --check

# Separate build job for JavaScript
build-javascript:
runs-on: ubuntu-latest
Expand Down
94 changes: 94 additions & 0 deletions .github/workflows/commands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Commands on PR
on:
issue_comment:
types: [created]

permissions:
contents: write
issues: write
pull-requests: write

jobs:
run_command:
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/run')
runs-on: ubuntu-20.04
steps:
- name: Extract command to run
uses: actions/github-script@v3
id: command-extractor
with:
result-encoding: string
script: |
if (context.eventName !== "issue_comment") throw "Error: This action only works on issue_comment events.";
// extract the command to run, allowed characters: a-z, A-Z, digits, hyphen, underscore
const regex = /^\/run ([a-zA-Z\d\-\_]+)/;
command = regex.exec(context.payload.comment.body);
if (command == null) throw "Error: No command found in the trigger phrase.";
return command[1];
- name: Get github ref
uses: actions/github-script@v3
id: get-pr
with:
script: |
const result = await github.pulls.get({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});
return { "ref": result.data.head.ref, "repository": result.data.head.repo.full_name};
- name: Checkout repo
uses: actions/checkout@v2
with:
repository: ${{ fromJson(steps.get-pr.outputs.result).repository }}
ref: ${{ fromJson(steps.get-pr.outputs.result).ref }}
fetch-depth: 0
- name: Install dotnet
uses: actions/setup-dotnet@v3
- name: Install dotnet tools
run: dotnet tool restore
- name: Process fantomas command
if: steps.command-extractor.outputs.result == 'fantomas'
id: fantomas
run: dotnet fantomas . -r
- name: Commit and push changes
if: steps.fantomas.outcome == 'success' || steps.xlf.outcome == 'success'
run: |
git config --local user.name "github-actions[bot]"
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git commit -a -m 'Automated command ran: ${{ steps.command-extractor.outputs.result }}
Co-authored-by: ${{ github.event.comment.user.login }} <${{ github.event.comment.user.id }}+${{ github.event.comment.user.login }}@users.noreply.github.com>'
git push
- name: Post command comment
if: steps.fantomas.outcome == 'success'
uses: actions/github-script@v3
with:
script: |
// Probably, there's more universal way of getting outputs, but my gh-actions-fu is not that good.
var output = ""
if ("${{steps.command-extractor.outputs.result}}" == 'fantomas') {
output = "${{steps.fantomas.outputs.result}}"
} else if("${{steps.command-extractor.outputs.result}}" == 'xlf') {
output = "${{steps.xlf.outputs.result}}"
}
const body = `Ran ${{ steps.command-extractor.outputs.result }}: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}\n${output}`;
await github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
- name: Post command failed comment
if: failure()
uses: actions/github-script@v3
with:
script: |
const body = `Failed to run ${{ steps.command-extractor.outputs.result }}: https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
await github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
22 changes: 22 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

## husky task runner examples -------------------
## Note : for local installation use 'dotnet' prefix. e.g. 'dotnet husky'

## run all tasks
#husky run

### run all tasks with group: 'group-name'
#husky run --group group-name

## run task with name: 'task-name'
#husky run --name task-name

## pass hook arguments to task
#husky run --args "$1" "$2"

## or put your custom commands -------------------
#echo 'Husky.Net is awesome!'

dotnet husky run --name fantomas-format-staged-files
11 changes: 11 additions & 0 deletions .husky/task-runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"tasks": [
{
"name": "fantomas-format-staged-files",
"group": "pre-commit-operations",
"command": "dotnet",
"args": ["fantomas", "${staged}"],
"include": ["**/*.fs", "**/*.fsx", "**/*.fsi"]
}
]
}
Loading

0 comments on commit f030cd3

Please sign in to comment.