Skip to content

Commit

Permalink
port clang-format to maven plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zashroof committed Jan 22, 2025
1 parent ecd1f16 commit 7d1fe1f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.cpp;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.cpp.ClangFormatStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Clang implements FormatterStepFactory {
@Parameter
private String version;

@Parameter
private String pathToExe;

@Parameter
private String style;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
ClangFormatStep clang = ClangFormatStep.withVersion(version == null ? ClangFormatStep.defaultVersion() : version);

if (pathToExe != null) {
clang = clang.withPathToExe(pathToExe);
}

if (style != null) {
clang = clang.withStyle(style);
}

return clang.create();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 DiffPlug
* Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,10 @@ public void addEclipseCdt(EclipseCdt eclipse) {
addStepFactory(eclipse);
}

public void addClangFormat(Clang clang) {
addStepFactory(clang);
}

@Override
public String licenseHeaderDelimiter() {
return CppDefaults.DELIMITER_EXPR;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2024 DiffPlug
* Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.cpp.Clang;
import com.diffplug.spotless.maven.generic.LicenseHeader;

/**
Expand All @@ -49,4 +50,8 @@ public String licenseHeaderDelimiter() {
public void addBuf(Buf buf) {
addStepFactory(buf);
}

public void addClang(Clang clang) {
addStepFactory(clang);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.cpp;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;
import com.diffplug.spotless.tag.ClangTest;

@ClangTest
class ClangMavenIntegrationTest extends MavenIntegrationHarness {

@Test
@ClangTest
void csharp() throws Exception {
writePomWithCppSteps("<includes>", "<include>", "src/**/*.cs", "</include>", "</includes>",
"<clangFormat>", "<version>", "14.0.0-1ubuntu1.1", "</version>", "</clangFormat>");
setFile("src/test.cs").toResource("clang/example.cs");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("src/test.cs").sameAsResource("clang/example.cs.clean");
}

@Test
@ClangTest
void proto() throws Exception {
writePomWithCppSteps("<includes>", "<include>", "**/*.proto", "</include>", "</includes>",
"<clangFormat>", "<version>", "14.0.0-1ubuntu1.1", "</version>", "</clangFormat>");
setFile("buf.proto").toResource("protobuf/buf/buf.proto");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("buf.proto").sameAsResource("protobuf/buf/buf.proto.clean");
}

}

0 comments on commit 7d1fe1f

Please sign in to comment.