Search in sources :

Example 1 with NpmInstallTask

use of com.moowork.gradle.node.npm.NpmInstallTask in project crnk-framework by crnk-project.

the class TSGeneratorPlugin method setupPackageTasks.

void setupPackageTasks(Project project, Task generateTask) {
    final File buildDir = new File(project.getBuildDir(), "npm_compile");
    final File distDir = getNpmOutputDir(project);
    project.getTasks().create(PublishTypescriptStubsTask.NAME, PublishTypescriptStubsTask.class);
    TSGeneratorConfig config = project.getExtensions().getByType(TSGeneratorConfig.class);
    Copy copySources = project.getTasks().create("processTypescript", Copy.class);
    copySources.from(config.getGenDir());
    copySources.into(buildDir);
    copySources.dependsOn(generateTask);
    // copy .npmrc file from root to working directory if available
    final File npmrcFile = new File(project.getProjectDir(), ".npmrc");
    if (npmrcFile.exists()) {
        copySources.getInputs().file(npmrcFile);
        copySources.doFirst(new Action<Task>() {

            @Override
            public void execute(Task task) {
                File targetFile = new File(buildDir, ".npmrc");
                buildDir.mkdirs();
                TypescriptUtils.copyFile(npmrcFile, targetFile);
            }
        });
    }
    CompileTypescriptStubsTask compileTypescriptTask = project.getTasks().create(CompileTypescriptStubsTask.NAME, CompileTypescriptStubsTask.class);
    try {
        NpmInstallTask npmInstall = (NpmInstallTask) project.getTasks().getByName("npmInstall");
        npmInstall.setWorkingDir(buildDir);
        npmInstall.dependsOn(copySources);
        npmInstall.getInputs().files(new File(buildDir, "package.json"));
        npmInstall.getOutputs().dir(new File(buildDir, "node_modules"));
        compileTypescriptTask.dependsOn(npmInstall);
    } catch (UnknownTaskException e) {
        LOGGER.warn("task not found, ok in testing", e);
    }
    ConfigurableFileTree fileTree = project.fileTree(buildDir);
    fileTree.include("package.json");
    fileTree.include(".npmrc");
    fileTree.include("**/*.ts");
    fileTree.exclude("**/*.d.ts");
    compileTypescriptTask.getInputs().files(fileTree);
    compileTypescriptTask.setWorkingDir(buildDir);
    compileTypescriptTask.getOutputs().dir(buildDir);
    ConfigurableFileTree assembleFileTree = project.fileTree(new File(buildDir, "src"));
    assembleFileTree.include("**/*.ts");
    assembleFileTree.include("**/*.js");
    assembleFileTree.include("**/*.js.map");
    Copy assembleSources = project.getTasks().create("assembleTypescript", Copy.class);
    assembleSources.from(assembleFileTree);
    assembleSources.from(new File(buildDir, "package.json"));
    assembleSources.into(distDir);
    assembleSources.dependsOn(compileTypescriptTask);
}
Also used : UnknownTaskException(org.gradle.api.UnknownTaskException) Task(org.gradle.api.Task) NpmInstallTask(com.moowork.gradle.node.npm.NpmInstallTask) NpmInstallTask(com.moowork.gradle.node.npm.NpmInstallTask) Copy(org.gradle.api.tasks.Copy) ConfigurableFileTree(org.gradle.api.file.ConfigurableFileTree) File(java.io.File)

Aggregations

NpmInstallTask (com.moowork.gradle.node.npm.NpmInstallTask)1 File (java.io.File)1 Task (org.gradle.api.Task)1 UnknownTaskException (org.gradle.api.UnknownTaskException)1 ConfigurableFileTree (org.gradle.api.file.ConfigurableFileTree)1 Copy (org.gradle.api.tasks.Copy)1