Created
April 27, 2020 21:45
-
-
Save marcellustavares/61d8874a60fe672bae49672294153033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.jetbrains.python.envs.PythonEnvsPlugin | |
import com.liferay.gradle.util.FileUtil | |
import org.apache.tools.ant.taskdefs.condition.Os | |
buildscript { | |
dependencies { | |
classpath group: "com.liferay", name: "com.liferay.gradle.plugins.defaults", version: "latest.release" | |
classpath group: "gradle.plugin.com.jetbrains.python", name: "gradle-python-envs", version: "0.0.30" | |
} | |
repositories { | |
maven { | |
url "https://repository-cdn.liferay.com/nexus/content/groups/public" | |
} | |
} | |
} | |
configure(subprojects.findAll { FileUtil.exists(it, "src/main/python")}) { | |
apply plugin: PythonEnvsPlugin | |
task configureVirtualEnv() | |
task formatSourcePython() | |
task packagePython() | |
def pythonSource = "src/main/python" | |
envs { | |
bootstrapDirectory = new File(projectDir, '.bootstrap') | |
envsDirectory = new File(buildDir, '.envs') | |
conda "Miniconda3", "Miniconda3-py38_4.8.2", "64", ["yapf"] | |
} | |
configureVirtualEnv { | |
URI requirementsURI = new File(projectDir.path + "/$pythonSource/requirements.txt").toURI() | |
File requirementsFile = FileUtil.get(project, requirementsURI.toString()) | |
project.envs { | |
condaenv project.name, "3.8", "Miniconda3", requirementsFile.readLines() | |
} | |
} | |
formatSourcePython { | |
def pythonEnvDir = "$project.envs.bootstrapDirectory/Miniconda3" | |
doLast { | |
exec { | |
commandLine _executablePath(pythonEnvDir, "python"), "-m", "yapf", "-i", "--recursive", pythonSource | |
} | |
} | |
} | |
packagePython { | |
def buildLibs = new File("$buildDir/libs") | |
def pythonEnvDir = "$buildDir/.envs/$project.name" | |
def pythonTempDir = "$buildDir/python-temp" | |
doLast { | |
copy { | |
from pythonSource | |
into pythonTempDir | |
exclude "requirements-package.txt" | |
} | |
if (!buildLibs.exists()) { | |
buildLibs.mkdirs() | |
} | |
exec { | |
commandLine _executablePath(pythonEnvDir, "pip"), "install", "-r", "$projectDir/$pythonSource/requirements-package.txt", "--target", pythonTempDir | |
} | |
exec { | |
commandLine _executablePath(pythonEnvDir, "python"), "-m", "zipapp", pythonTempDir, "-o", "$buildDir/libs/${project.name}.py" | |
} | |
delete pythonTempDir | |
} | |
} | |
project.afterEvaluate { | |
assemble { | |
dependsOn packagePython | |
} | |
build_envs { | |
dependsOn configureVirtualEnv | |
} | |
formatSource { | |
dependsOn build_envs | |
} | |
packagePython { | |
dependsOn build_envs | |
} | |
} | |
} | |
private String _executablePath(String pythonEnvDir, String executable) { | |
if (Os.isFamily(Os.FAMILY_WINDOWS)) { | |
return "$pythonEnvDir/Scripts/$executable" | |
} | |
else { | |
return "$pythonEnvDir/bin/$executable" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment