refactor: improve code formatting in `mod.rs` and `logger.rs`
[ouch.git] / .github / workflows / build-artifacts-and-run-tests.yml
blobcb3bf305be2d41ad0222f69d6a95c478bbd36fd0
1 # This is a reusable workflow
3 name: Build artifacts and run tests
5 on:
6   workflow_dispatch:
7     inputs:
8       matrix_all_combinations:
9         description: "if matrix should have all combinations of targets and features"
10         type: boolean
11         required: true
12         default: true
13       upload_artifacts:
14         description: "if built artifacts should be uploaded"
15         type: boolean
16         required: true
17         default: true
18   workflow_call:
19     inputs:
20       matrix_all_combinations:
21         description: "if matrix should have all combinations of targets and features"
22         type: boolean
23         required: true
24       upload_artifacts:
25         description: "if built artifacts should be uploaded"
26         type: boolean
27         required: true
29 jobs:
30   build-artifacts-and-run-tests:
31     runs-on: ${{ matrix.os || 'ubuntu-latest' }}
32     env:
33       CARGO: cargo
34     strategy:
35       fail-fast: false
36       matrix:
37         feature-unrar: ${{ inputs.matrix_all_combinations && fromJSON('[true, false]') || fromJSON('[false]')}}
38         feature-use-zlib: ${{ inputs.matrix_all_combinations && fromJSON('[true, false]') || fromJSON('[false]')}}
39         feature-use-zstd-thin: ${{ inputs.matrix_all_combinations && fromJSON('[true, false]') || fromJSON('[false]')}}
40         target:
41           # native
42           - x86_64-unknown-linux-gnu
43           - x86_64-pc-windows-gnu
44           - x86_64-pc-windows-msvc
45           - aarch64-pc-windows-msvc
46           - x86_64-apple-darwin
47           # cross
48           - x86_64-unknown-linux-musl
49           - aarch64-unknown-linux-gnu
50           - aarch64-unknown-linux-musl
51           - armv7-unknown-linux-gnueabihf
52           - armv7-unknown-linux-musleabihf
54         include:
55           # runner overrides
56           - target: x86_64-pc-windows-gnu
57             os: windows-latest
58           - target: x86_64-pc-windows-msvc
59             os: windows-latest
60           - target: aarch64-pc-windows-msvc
61             os: windows-latest
62           - target: x86_64-apple-darwin
63             os: macos-latest
64           # targets that use cross
65           - target: x86_64-unknown-linux-musl
66             use-cross: true
67           - target: aarch64-unknown-linux-gnu
68             use-cross: true
69           - target: aarch64-unknown-linux-musl
70             use-cross: true
71           - target: armv7-unknown-linux-gnueabihf
72             use-cross: true
73           - target: armv7-unknown-linux-musleabihf
74             use-cross: true
75           # features (unless `matrix_all_combinations` is true, we only run these on linux-gnu)
76           - feature-unrar: true
77             target: x86_64-unknown-linux-gnu
78           - feature-use-zlib: true
79             target: x86_64-unknown-linux-gnu
80           - feature-use-zstd-thin: true
81             target: x86_64-unknown-linux-gnu
83     steps:
84       - name: Checkout
85         uses: actions/checkout@v4
87       - name: Install cross
88         if: matrix.use-cross
89         run: |
90           pushd "$(mktemp -d)"
91           wget https://github.com/cross-rs/cross/releases/download/v0.2.4/cross-x86_64-unknown-linux-musl.tar.gz
92           tar xf cross-x86_64-unknown-linux-musl.tar.gz
93           cp cross ~/.cargo/bin
94           popd
95           echo CARGO=cross >> $GITHUB_ENV
97       - name: Concatenate features
98         id: concat-features
99         shell: bash
100         run: |
101           FEATURES=()
102           if [[ "${{ matrix.feature-use-zlib }}" == true ]]; then FEATURES+=(use_zlib); fi
103           if [[ "${{ matrix.feature-use-zstd-thin }}" == true ]]; then FEATURES+=(use_zstd_thin); fi
104           if [[ "${{ matrix.feature-unrar }}" == true ]]; then FEATURES+=(unrar); fi
105           IFS=','
106           echo "FEATURES=${FEATURES[*]}" >> $GITHUB_OUTPUT
108       - name: Set up extra cargo flags
109         env:
110           FEATURES: ${{steps.concat-features.outputs.FEATURES}}
111         shell: bash
112         run: |
113           FLAGS="--no-default-features"
114           if [[ -n "$FEATURES" ]]; then FLAGS+=" --features $FEATURES"; fi
115           echo "EXTRA_CARGO_FLAGS=$FLAGS" >> $GITHUB_ENV
117       - name: Install Rust
118         run: |
119           rustup toolchain install stable --profile minimal -t ${{ matrix.target }}
121       - uses: Swatinem/rust-cache@v2
122         with:
123           key: "${{ matrix.target }}-${{ matrix.feature-unrar }}-${{ matrix.feature-use-zstd-thin }}-${{ matrix.feature-unrar }}"
125       - name: Test on stable
126         # there's no way to run tests for ARM64 Windows for now
127         if: matrix.target != 'aarch64-pc-windows-msvc'
128         run: |
129           ${{ env.CARGO }} +stable test --profile fast --target ${{ matrix.target }} $EXTRA_CARGO_FLAGS
131       - name: Build release artifacts (binary and completions)
132         if: ${{ inputs.upload_artifacts }}
133         run: |
134           ${{ env.CARGO }} +stable build --release --target ${{ matrix.target }} $EXTRA_CARGO_FLAGS
135         env:
136           OUCH_ARTIFACTS_FOLDER: artifacts
138       - name: Upload release artifacts
139         if: ${{ inputs.upload_artifacts }}
140         uses: actions/upload-artifact@v4
141         with:
142           name: ouch-${{ matrix.target }}-${{ steps.concat-features.outputs.FEATURES }}
143           path: |
144             target/${{ matrix.target }}/release/ouch
145             target/${{ matrix.target }}/release/ouch.exe
146             artifacts/