Skip to content

Commit 46e32f5

Browse files
authored
Merge pull request #330 from lorencarvalho/isort-black-tasks-take-2
Add an 'isort' & 'black' task. (take two)
2 parents df56b41 + 28a2206 commit 46e32f5

File tree

8 files changed

+206
-19
lines changed

8 files changed

+206
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016 LinkedIn Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.linkedin.gradle.python.extension;
17+
18+
import com.linkedin.gradle.python.extension.internal.DefaultExternalTool;
19+
20+
21+
public class BlackExtension extends DefaultExternalTool {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2016 LinkedIn Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.linkedin.gradle.python.extension;
17+
18+
import com.linkedin.gradle.python.extension.internal.DefaultExternalTool;
19+
20+
21+
public class IsortExtension extends DefaultExternalTool {
22+
}

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/extension/MypyExtension.java

+2-18
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,8 @@
1515
*/
1616
package com.linkedin.gradle.python.extension;
1717

18+
import com.linkedin.gradle.python.extension.internal.DefaultExternalTool;
1819

19-
public class MypyExtension {
20-
private boolean run;
21-
private String[] arguments = null;
2220

23-
public boolean isRun() {
24-
return run;
25-
}
26-
27-
public void setRun(boolean run) {
28-
this.run = run;
29-
}
30-
31-
public void setArguments(String argumentString) {
32-
arguments = argumentString.split("\\s+");
33-
}
34-
35-
public String[] getArguments() {
36-
return arguments;
37-
}
21+
public class MypyExtension extends DefaultExternalTool {
3822
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2016 LinkedIn Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.linkedin.gradle.python.extension.internal;
17+
18+
19+
public class DefaultExternalTool {
20+
private boolean run;
21+
private String[] arguments = null;
22+
23+
public boolean isRun() {
24+
return run;
25+
}
26+
27+
public void setRun(boolean run) {
28+
this.run = run;
29+
}
30+
31+
public void setArguments(String argumentString) {
32+
arguments = argumentString.split("\\s+");
33+
}
34+
35+
public String[] getArguments() {
36+
return arguments;
37+
}
38+
}

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/plugin/internal/ValidationPlugin.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
package com.linkedin.gradle.python.plugin.internal;
1717

1818
import com.linkedin.gradle.python.PythonExtension;
19+
import com.linkedin.gradle.python.extension.BlackExtension;
20+
import com.linkedin.gradle.python.extension.IsortExtension;
1921
import com.linkedin.gradle.python.extension.MypyExtension;
2022
import com.linkedin.gradle.python.extension.CoverageExtension;
2123
import com.linkedin.gradle.python.tasks.AbstractPythonMainSourceDefaultTask;
2224
import com.linkedin.gradle.python.tasks.AbstractPythonTestSourceDefaultTask;
25+
import com.linkedin.gradle.python.tasks.BlackTask;
2326
import com.linkedin.gradle.python.tasks.CheckStyleGeneratorTask;
2427
import com.linkedin.gradle.python.tasks.Flake8Task;
2528
import com.linkedin.gradle.python.tasks.MypyTask;
29+
import com.linkedin.gradle.python.tasks.IsortTask;
2630
import com.linkedin.gradle.python.tasks.PyCoverageTask;
2731
import com.linkedin.gradle.python.tasks.PyTestTask;
2832
import com.linkedin.gradle.python.util.ExtensionUtils;
@@ -32,13 +36,15 @@
3236
import org.gradle.api.logging.Logger;
3337
import org.gradle.api.logging.Logging;
3438

39+
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_BLACK;
3540
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_CHECK;
3641
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_CHECKSTYLE;
3742
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_COVERAGE;
3843
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_FLAKE;
3944
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_INSTALL_BUILD_REQS;
4045
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_INSTALL_PROJECT;
4146
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_MYPY;
47+
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_ISORT;
4248
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_PYTEST;
4349

4450
public class ValidationPlugin implements Plugin<Project> {
@@ -112,7 +118,7 @@ public void apply(final Project project) {
112118
/*
113119
* Run mypy.
114120
*
115-
* This uses the mypy.ini file if present to configure mypy.
121+
* This uses the setup.cfg (or mypy.ini) file if present to configure mypy.
116122
*/
117123
MypyExtension mypy = ExtensionUtils.maybeCreate(project, "mypy", MypyExtension.class);
118124
project.getTasks().create(TASK_MYPY.getValue(), MypyTask.class,
@@ -122,6 +128,28 @@ public void apply(final Project project) {
122128
project.getTasks().getByName(TASK_CHECK.getValue())
123129
.dependsOn(project.getTasks().getByName(TASK_MYPY.getValue()));
124130

131+
/*
132+
* Run isort.
133+
*/
134+
IsortExtension isort = ExtensionUtils.maybeCreate(project, "isort", IsortExtension.class);
135+
project.getTasks().create(TASK_ISORT.getValue(), IsortTask.class,
136+
task -> task.onlyIf(it -> project.file(settings.srcDir).exists() && isort.isRun()));
137+
138+
// Make task "check" depend on isort task.
139+
project.getTasks().getByName(TASK_CHECK.getValue())
140+
.dependsOn(project.getTasks().getByName(TASK_ISORT.getValue()));
141+
142+
/*
143+
* Run black.
144+
*/
145+
BlackExtension black = ExtensionUtils.maybeCreate(project, "black", BlackExtension.class);
146+
project.getTasks().create(TASK_BLACK.getValue(), BlackTask.class,
147+
task -> task.onlyIf(it -> project.file(settings.srcDir).exists() && black.isRun()));
148+
149+
// Make task "check" depend on black task.
150+
project.getTasks().getByName(TASK_CHECK.getValue())
151+
.dependsOn(project.getTasks().getByName(TASK_BLACK.getValue()));
152+
125153
/*
126154
* Create checkstyle styled report from flake
127155
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016 LinkedIn Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.linkedin.gradle.python.tasks;
17+
18+
import com.linkedin.gradle.python.extension.BlackExtension;
19+
import com.linkedin.gradle.python.extension.PythonDetails;
20+
import com.linkedin.gradle.python.util.ExtensionUtils;
21+
import org.gradle.api.Project;
22+
import org.gradle.process.ExecResult;
23+
24+
25+
public class BlackTask extends AbstractPythonMainSourceDefaultTask {
26+
27+
public void preExecution() {
28+
PythonDetails blackDetails = getPythonDetails();
29+
args(blackDetails.getVirtualEnvironment().findExecutable("black").getAbsolutePath());
30+
31+
Project project = getProject();
32+
BlackExtension black = ExtensionUtils.getPythonComponentExtension(project, BlackExtension.class);
33+
34+
String[] arguments = black.getArguments();
35+
36+
if (arguments == null) {
37+
// Default to longer line length (160) than the default (88)
38+
// Default to check only
39+
arguments = new String[] {"--check", "-l", "160", getPythonExtension().srcDir, getPythonExtension().testDir};
40+
}
41+
args(arguments);
42+
}
43+
44+
public void processResults(ExecResult results) {
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016 LinkedIn Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.linkedin.gradle.python.tasks;
17+
18+
import com.linkedin.gradle.python.extension.IsortExtension;
19+
import com.linkedin.gradle.python.extension.PythonDetails;
20+
import com.linkedin.gradle.python.util.ExtensionUtils;
21+
import org.gradle.api.Project;
22+
import org.gradle.process.ExecResult;
23+
24+
25+
public class IsortTask extends AbstractPythonMainSourceDefaultTask {
26+
27+
public void preExecution() {
28+
PythonDetails isortDetails = getPythonDetails();
29+
args(isortDetails.getVirtualEnvironment().findExecutable("isort").getAbsolutePath());
30+
31+
Project project = getProject();
32+
IsortExtension isort = ExtensionUtils.getPythonComponentExtension(project, IsortExtension.class);
33+
34+
String[] arguments = isort.getArguments();
35+
36+
if (arguments == null) {
37+
// Default to --check-only --recursive src/ test/
38+
arguments = new String[]{"--check-only", "--recursive", getPythonExtension().srcDir, getPythonExtension().testDir};
39+
}
40+
args(arguments);
41+
}
42+
43+
public void processResults(ExecResult results) {
44+
}
45+
}

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/util/StandardTextValues.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public enum StandardTextValues {
3030
CONFIGURATION_TEST("test"),
3131
CONFIGURATION_VENV("venv"),
3232
CONFIGURATION_WHEEL("wheel"),
33+
TASK_BLACK("runBlack"),
3334
TASK_BUILD_DOCS("buildDocs"),
3435
TASK_CLEAN_SAVE_VENV("cleanSaveVenv"),
3536
TASK_CHECK("check"),
@@ -41,6 +42,7 @@ public enum StandardTextValues {
4142
TASK_INSTALL_PROJECT("installProject"),
4243
TASK_INSTALL_PYTHON_REQS("installPythonRequirements"),
4344
TASK_INSTALL_TEST_REQS("installTestRequirements"),
45+
TASK_ISORT("runIsort"),
4446
TASK_MYPY("runMypy"),
4547
TASK_PACKAGE_DOCS("packageDocs"),
4648
TASK_PACKAGE_JSON_DOCS("packageJsonDocs"),

0 commit comments

Comments
 (0)