Search in sources :

Example 1 with ModelBuilder

use of org.gradle.tooling.ModelBuilder in project liferay-ide by liferay.

the class GradleTooling method getModel.

public static <T> T getModel(Class<T> modelClass, File cacheDir, File projectDir) throws Exception {
    T retval = null;
    GradleConnector connector = GradleConnector.newConnector().forProjectDirectory(projectDir);
    ProjectConnection connection = null;
    try {
        connection = connector.connect();
        ModelBuilder<T> modelBuilder = (ModelBuilder<T>) connection.model(modelClass);
        File depsDir = new File(cacheDir, "deps");
        depsDir.mkdirs();
        String path = depsDir.getAbsolutePath();
        path = path.replaceAll("\\\\", "/");
        _extractJar(depsDir, "com.liferay.blade.gradle.model");
        _extractJar(depsDir, "com.liferay.blade.gradle.plugin");
        String initScriptTemplate = CoreUtil.readStreamToString(GradleTooling.class.getResourceAsStream("init.gradle"));
        String initScriptContents = initScriptTemplate.replaceFirst("%deps%", path);
        File scriptFile = new File(cacheDir, "init.gradle");
        if (FileUtil.notExists(scriptFile)) {
            scriptFile.createNewFile();
        }
        FileUtil.writeFileFromStream(scriptFile, new ByteArrayInputStream(initScriptContents.getBytes()));
        ModelBuilder<T> builder = modelBuilder.withArguments("--init-script", scriptFile.getAbsolutePath());
        retval = builder.get();
    } catch (Exception e) {
        GradleCore.logError("get gradle custom model error", e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    return retval;
}
Also used : ModelBuilder(org.gradle.tooling.ModelBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ProjectConnection(org.gradle.tooling.ProjectConnection) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException) GradleConnector(org.gradle.tooling.GradleConnector)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 JarFile (java.util.jar.JarFile)1 GradleConnector (org.gradle.tooling.GradleConnector)1 ModelBuilder (org.gradle.tooling.ModelBuilder)1 ProjectConnection (org.gradle.tooling.ProjectConnection)1