Skip to content

Commit

Permalink
Merge pull request #330 from lorencarvalho/isort-black-tasks-take-2
Browse files Browse the repository at this point in the history
Add an 'isort' & 'black' task. (take two)
  • Loading branch information
elautz authored Jan 14, 2020
2 parents df56b41 + 28a2206 commit 46e32f5
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* 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.linkedin.gradle.python.extension;

import com.linkedin.gradle.python.extension.internal.DefaultExternalTool;


public class BlackExtension extends DefaultExternalTool {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* 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.linkedin.gradle.python.extension;

import com.linkedin.gradle.python.extension.internal.DefaultExternalTool;


public class IsortExtension extends DefaultExternalTool {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,8 @@
*/
package com.linkedin.gradle.python.extension;

import com.linkedin.gradle.python.extension.internal.DefaultExternalTool;

public class MypyExtension {
private boolean run;
private String[] arguments = null;

public boolean isRun() {
return run;
}

public void setRun(boolean run) {
this.run = run;
}

public void setArguments(String argumentString) {
arguments = argumentString.split("\\s+");
}

public String[] getArguments() {
return arguments;
}
public class MypyExtension extends DefaultExternalTool {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* 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.linkedin.gradle.python.extension.internal;


public class DefaultExternalTool {
private boolean run;
private String[] arguments = null;

public boolean isRun() {
return run;
}

public void setRun(boolean run) {
this.run = run;
}

public void setArguments(String argumentString) {
arguments = argumentString.split("\\s+");
}

public String[] getArguments() {
return arguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
package com.linkedin.gradle.python.plugin.internal;

import com.linkedin.gradle.python.PythonExtension;
import com.linkedin.gradle.python.extension.BlackExtension;
import com.linkedin.gradle.python.extension.IsortExtension;
import com.linkedin.gradle.python.extension.MypyExtension;
import com.linkedin.gradle.python.extension.CoverageExtension;
import com.linkedin.gradle.python.tasks.AbstractPythonMainSourceDefaultTask;
import com.linkedin.gradle.python.tasks.AbstractPythonTestSourceDefaultTask;
import com.linkedin.gradle.python.tasks.BlackTask;
import com.linkedin.gradle.python.tasks.CheckStyleGeneratorTask;
import com.linkedin.gradle.python.tasks.Flake8Task;
import com.linkedin.gradle.python.tasks.MypyTask;
import com.linkedin.gradle.python.tasks.IsortTask;
import com.linkedin.gradle.python.tasks.PyCoverageTask;
import com.linkedin.gradle.python.tasks.PyTestTask;
import com.linkedin.gradle.python.util.ExtensionUtils;
Expand All @@ -32,13 +36,15 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;

import static com.linkedin.gradle.python.util.StandardTextValues.TASK_BLACK;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_CHECK;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_CHECKSTYLE;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_COVERAGE;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_FLAKE;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_INSTALL_BUILD_REQS;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_INSTALL_PROJECT;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_MYPY;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_ISORT;
import static com.linkedin.gradle.python.util.StandardTextValues.TASK_PYTEST;

public class ValidationPlugin implements Plugin<Project> {
Expand Down Expand Up @@ -112,7 +118,7 @@ public void apply(final Project project) {
/*
* Run mypy.
*
* This uses the mypy.ini file if present to configure mypy.
* This uses the setup.cfg (or mypy.ini) file if present to configure mypy.
*/
MypyExtension mypy = ExtensionUtils.maybeCreate(project, "mypy", MypyExtension.class);
project.getTasks().create(TASK_MYPY.getValue(), MypyTask.class,
Expand All @@ -122,6 +128,28 @@ public void apply(final Project project) {
project.getTasks().getByName(TASK_CHECK.getValue())
.dependsOn(project.getTasks().getByName(TASK_MYPY.getValue()));

/*
* Run isort.
*/
IsortExtension isort = ExtensionUtils.maybeCreate(project, "isort", IsortExtension.class);
project.getTasks().create(TASK_ISORT.getValue(), IsortTask.class,
task -> task.onlyIf(it -> project.file(settings.srcDir).exists() && isort.isRun()));

// Make task "check" depend on isort task.
project.getTasks().getByName(TASK_CHECK.getValue())
.dependsOn(project.getTasks().getByName(TASK_ISORT.getValue()));

/*
* Run black.
*/
BlackExtension black = ExtensionUtils.maybeCreate(project, "black", BlackExtension.class);
project.getTasks().create(TASK_BLACK.getValue(), BlackTask.class,
task -> task.onlyIf(it -> project.file(settings.srcDir).exists() && black.isRun()));

// Make task "check" depend on black task.
project.getTasks().getByName(TASK_CHECK.getValue())
.dependsOn(project.getTasks().getByName(TASK_BLACK.getValue()));

/*
* Create checkstyle styled report from flake
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* 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.linkedin.gradle.python.tasks;

import com.linkedin.gradle.python.extension.BlackExtension;
import com.linkedin.gradle.python.extension.PythonDetails;
import com.linkedin.gradle.python.util.ExtensionUtils;
import org.gradle.api.Project;
import org.gradle.process.ExecResult;


public class BlackTask extends AbstractPythonMainSourceDefaultTask {

public void preExecution() {
PythonDetails blackDetails = getPythonDetails();
args(blackDetails.getVirtualEnvironment().findExecutable("black").getAbsolutePath());

Project project = getProject();
BlackExtension black = ExtensionUtils.getPythonComponentExtension(project, BlackExtension.class);

String[] arguments = black.getArguments();

if (arguments == null) {
// Default to longer line length (160) than the default (88)
// Default to check only
arguments = new String[] {"--check", "-l", "160", getPythonExtension().srcDir, getPythonExtension().testDir};
}
args(arguments);
}

public void processResults(ExecResult results) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* 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.linkedin.gradle.python.tasks;

import com.linkedin.gradle.python.extension.IsortExtension;
import com.linkedin.gradle.python.extension.PythonDetails;
import com.linkedin.gradle.python.util.ExtensionUtils;
import org.gradle.api.Project;
import org.gradle.process.ExecResult;


public class IsortTask extends AbstractPythonMainSourceDefaultTask {

public void preExecution() {
PythonDetails isortDetails = getPythonDetails();
args(isortDetails.getVirtualEnvironment().findExecutable("isort").getAbsolutePath());

Project project = getProject();
IsortExtension isort = ExtensionUtils.getPythonComponentExtension(project, IsortExtension.class);

String[] arguments = isort.getArguments();

if (arguments == null) {
// Default to --check-only --recursive src/ test/
arguments = new String[]{"--check-only", "--recursive", getPythonExtension().srcDir, getPythonExtension().testDir};
}
args(arguments);
}

public void processResults(ExecResult results) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum StandardTextValues {
CONFIGURATION_TEST("test"),
CONFIGURATION_VENV("venv"),
CONFIGURATION_WHEEL("wheel"),
TASK_BLACK("runBlack"),
TASK_BUILD_DOCS("buildDocs"),
TASK_CLEAN_SAVE_VENV("cleanSaveVenv"),
TASK_CHECK("check"),
Expand All @@ -41,6 +42,7 @@ public enum StandardTextValues {
TASK_INSTALL_PROJECT("installProject"),
TASK_INSTALL_PYTHON_REQS("installPythonRequirements"),
TASK_INSTALL_TEST_REQS("installTestRequirements"),
TASK_ISORT("runIsort"),
TASK_MYPY("runMypy"),
TASK_PACKAGE_DOCS("packageDocs"),
TASK_PACKAGE_JSON_DOCS("packageJsonDocs"),
Expand Down

0 comments on commit 46e32f5

Please sign in to comment.