Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Make NativeCmd step available in Gradle plugin
Browse files Browse the repository at this point in the history
Maven support had been added in diffplug#949.
  • Loading branch information
carhartl committed Oct 11, 2021
1 parent d21a7bc commit 21689ce
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.diffplug.spotless.generic.IndentStep;
import com.diffplug.spotless.generic.LicenseHeaderStep;
import com.diffplug.spotless.generic.LicenseHeaderStep.YearMode;
import com.diffplug.spotless.generic.NativeCmdStep;
import com.diffplug.spotless.generic.PipeStepPair;
import com.diffplug.spotless.generic.ReplaceRegexStep;
import com.diffplug.spotless.generic.ReplaceStep;
Expand Down Expand Up @@ -394,6 +395,11 @@ public void indentWithTabs() {
addStep(IndentStep.Type.TAB.create());
}

/** Ensures formatting of files via native binary. */
public void nativeCmd(String name, File pathToExe, List<String> arguments) {
addStep(NativeCmdStep.create(name, pathToExe, arguments));
}

/**
* Created by {@link FormatExtension#licenseHeader(String, String)} or {@link FormatExtension#licenseHeaderFile(Object, String)}.
* For most language-specific formats (e.g. java, scala, etc.) you can omit the second {@code delimiter} argument, because it is supplied
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021 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.gradle.spotless;

import static org.assertj.core.api.Assumptions.assumeThat;

import java.io.File;
import java.io.IOException;

import org.junit.jupiter.api.Test;

class NativeCmdIntegrationTest extends GradleIntegrationHarness {
@Test
void nativeCmd() throws IOException {
// This will only work if /usr/bin/sed is available
assumeThat(new File("/usr/bin/sed")).exists();

setFile("build.gradle").toLines(
"plugins {",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" format 'test', {",
" target '**/*.txt'",
" nativeCmd('sed', '/usr/bin/sed', ['s/placeholder/replaced/g'])",
" }",
"}");
setFile("test.txt").toResource("native_cmd/dirty.txt");
gradleRunner().withArguments("spotlessApply").build();
assertFile("test.txt").sameAsResource("native_cmd/clean.txt");
}
}
2 changes: 2 additions & 0 deletions testlib/src/main/resources/native_cmd/clean.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
replaced foo
bar replaced
2 changes: 2 additions & 0 deletions testlib/src/main/resources/native_cmd/dirty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
placeholder foo
bar placeholder

0 comments on commit 21689ce

Please sign in to comment.