Skip to content

Commit

Permalink
[ci] add reformat workflow to automatically do reformat
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin committed Oct 15, 2024
1 parent 80b905c commit cc035aa
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 76 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/auto-reformat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: reformat
on:
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
- labeled

# Cancel the current workflow when new commit pushed
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
run:
name: "Reformat all codes"
# Don't run reformat when:
# - user cancel
# - PR from outside repository ( we don't have permission to push commit into fork repository )
if: ${{ !cancelled() && github.event.pull_request.head.repo.full_name == github.repository }}
runs-on: [self-hosted, linux, nixos]
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.head_ref }}
- name: "Reformat all T1 module codes"
run: nix develop ".#t1.elaborator.editable" -c mill -i _.reformat
- name: "Reformat all Rust codes"
run: |
cd difftest
nix shell ".#cargo" ".#rustfmt" -c cargo fmt
- name: "Commit formats"
run: |
git config user.name github-actions
git config user.email [email protected]
changed_cases=$(git diff --name-only)
if [ -n "$changed_cases" ]; then
echo -e "Reformat codes: $changed_cases"
git add .
git commit -m "[chore] reformat all T1 modules"
git push origin ${{ github.head_ref }}
else
echo "No format error detected"
fi
9 changes: 3 additions & 6 deletions .github/workflows/t1rocket.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
name: T1RocketEmu VCS Test
on:
pull_request:
workflow_run:
workflows: [reformat]
types:
- opened
- synchronize
- reopened
- ready_for_review
- labeled
- completed
env:
USER: runner
EMU_TOP: t1rocketemu
Expand Down
10 changes: 4 additions & 6 deletions .github/workflows/vcs.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
name: T1Emu VCS Test
on:
pull_request:
workflow_run:
workflows: [reformat]
types:
- opened
- synchronize
- reopened
- ready_for_review
- labeled
- completed

env:
USER: runner
EMU_TOP: t1emu
Expand Down
3 changes: 0 additions & 3 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import $file.dependencies.`berkeley-hardfloat`.common
import $file.dependencies.rvdecoderdb.common
import $file.common

// Required for scalafmt to recognize which file to format
def buildSources = T.sources(os.pwd / "build.sc")

object v {
val scala = "2.13.15"
val mainargs = ivy"com.lihaoyi::mainargs:0.5.0"
Expand Down
12 changes: 0 additions & 12 deletions difftest/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{ lib
, rustPlatform
, rustfmt
, libspike
, libspike_interfaces
, rtlDesignMetadata
Expand Down Expand Up @@ -36,17 +35,6 @@ rustPlatform.buildRustPackage {
];
};

nativeBuildInputs = [
rustfmt
];

postConfigure = ''
if ! cargo fmt --check; then
echo "Please run 'cd difftest && cargo fmt' before building emulator!" >&2
exit 1
fi
'';

buildFeatures = [ ] ++ lib.optionals (lib.hasPrefix "dpi" moduleType) [ "dpi_common/${emuType}" ] ++ lib.optionals enableTrace [ "dpi_common/trace" ];
buildAndTestSubdir = "./${moduleType}";

Expand Down
10 changes: 0 additions & 10 deletions nix/pkgs/mill-builder.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ let
mill -i __.prepareOffline
mill -i __.scalaCompilerClasspath
# mill doesn't put scalafmt version into module ivy deps.
# It downloads scalafmt only when checkFormat/reformat is explicitly trigger.
# And we don't want to wait too long for a dependencies task, so here is the solution:
# "checkFormat" the "build.sc" file so that mill will download scalafmt for us,
# and we don't need to wait too long for formatting.
if ! mill -i mill.scalalib.scalafmt.ScalafmtModule/checkFormatAll buildSources; then
echo "[ERROR] build.sc is misformatted, please run 'mill -i mill.scalalib.scalafmt.ScalafmtModule/reformatAll buildSources'" >&2
exit 1
fi
runHook postBuild
'';

Expand Down
29 changes: 1 addition & 28 deletions nix/t1/mill-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ let
fileset = unions [
./../../build.sc
./../../common.sc
./../../.scalafmt.conf
];
};
millDepsHash = "sha256-pixG96IxJsYlgIU+DVxGHky6G5nMfHXphEq5A/xLP7Q=";
millDepsHash = "sha256-XvGLNLOC7OEwfC7SB5zBdB64VjROBkwgIcHx+9FHmSs=";
nativeBuildInputs = [ dependencies.setupHook ];
};

Expand Down Expand Up @@ -84,32 +83,6 @@ let

outputs = [ "out" "omreader" "elaborator" "t1package" ];

# Check code format before starting build, so that we can enforce all developer run reformat before build.
configurePhase = ''
runHook preConfigure
_targetsToCheck=(
"elaborator"
"omreader"
"omreaderlib"
"rocketemu"
"rocketv"
"t1"
"t1emu"
"t1rocket"
"t1rocketemu"
)
for _t in ''${_targetsToCheck[@]}; do
if ! mill -i "$_t".checkFormat; then
echo "[ERROR] Please run 'mill -i $_t.reformat' before elaborate!" >&2
exit 1
fi
done
unset _targetsToCheck
runHook postConfigure
'';

buildPhase = ''
runHook preBuild
Expand Down
3 changes: 0 additions & 3 deletions script/build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import mill.scalalib.TestModule.Utest
import mill.util.Jvm
import coursier.maven.MavenRepository

// Required for scalafmt to recognize which file to format
def buildSources = T.sources(os.pwd / "build.sc")

trait ScriptModule extends ScalaModule with ScalafmtModule {
val scala3 = "3.3.3"
val mainargs = ivy"com.lihaoyi::mainargs:0.5.0"
Expand Down
9 changes: 1 addition & 8 deletions script/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ let
root = ./.;
fileset = unions [
./build.sc
./.scalafmt.conf
];
};
millDepsHash = "sha256-y8tAFwctiU6ehghuf7KP73DuWCGCnAAdIXOIPwT+QOo=";
millDepsHash = "sha256-QQ5gCbvovC55t9MmfCNTvNFdD6FcNqmLmfhT9qJhQQc=";
};

passthru.withLsp = self.overrideAttrs (old: {
Expand Down Expand Up @@ -66,12 +65,6 @@ let
buildPhase = ''
runHook preBuild
# Not debug build, check source format
if (( $enableNativeExe )); then
echo "Checking format"
mill -i ${moduleName}.checkFormat
fi
echo "Building JAR"
mill -i ${moduleName}.assembly
Expand Down

0 comments on commit cc035aa

Please sign in to comment.