diff --git a/README.md b/README.md index 22810a6e..0a75ca27 100644 --- a/README.md +++ b/README.md @@ -1,246 +1,349 @@ -# Frontend Gradle plugin - -[![Latest release 1.1.0](https://img.shields.io/badge/Latest%20release-1.1.0-blue.svg)](https://github.com/Siouan/frontend-gradle-plugin/releases/tag/v1.1.0) -[![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0) - -[![Build status](https://travis-ci.org/Siouan/frontend-gradle-plugin.svg?branch=master)](https://travis-ci.org/Siouan/frontend-gradle-plugin) -[![Quality gate status](https://sonarcloud.io/api/project_badges/measure?project=Siouan_frontend-gradle-plugin&metric=alert_status)](https://sonarcloud.io/dashboard?id=Siouan_frontend-gradle-plugin) -[![Code coverage](https://sonarcloud.io/api/project_badges/measure?project=Siouan_frontend-gradle-plugin&metric=coverage)](https://sonarcloud.io/dashboard?id=Siouan_frontend-gradle-plugin) -[![Reliability](https://sonarcloud.io/api/project_badges/measure?project=Siouan_frontend-gradle-plugin&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=Siouan_frontend-gradle-plugin) - -This plugin integrates frontend build tasks into a Gradle build. It is inspired by the -[frontend-maven-plugin][frontend-maven-plugin]. - -Detailed changes for each release are documented in the [release notes][release-notes]. The project uses [semantic -versioning][semantic-versioning] for its releases. - -## Summary - -- [Requirements](#requirements) -- [Installation](#installation) - - [Activation](#activation) - - [Using Gradle DSL](#using-gradle-dsl) - - [Using Gradle build script block](#using-gradle-build-script-block) - - [Configuration](#configuration) - - [DSL reference](#dsl-reference) - - [Typical configuration with NPM](#typical-configuration-with-npm) - - [Typical configuration with Yarn](#typical-configuration-with-yarn) - - [Final steps](#final-steps) -- [Tasks](#tasks) - - [Dependencies](#dependencies) - - [Install Node](#install-node) - - [Install Yarn](#install-yarn) - - [Install frontend dependencies](#install-frontend-dependencies) - - [Clean frontend](#clean-frontend) - - [Assemble frontend](#assemble-frontend) - - [Check frontend](#check-frontend) - - [Run custom NPM/Yarn script](#run-custom-npmyarn-script) -- [Contributing][contributing] - -## Requirements - -The plugin is officially supported with: -- Gradle 5.1+ -- JDK 8+ 64 bits - -## Installation - -### Activation - -2 options are available. - -#### Using [Gradle DSL][gradle-dsl] - -This is the modern and recommended approach. - -```gradle -// build.gradle -plugins { - id 'org.siouan.frontend' version '1.1.0' -} -``` - -#### Using [Gradle build script block][gradle-build-script-block] - -This approach is the legacy way to resolve and apply plugins. - -```gradle -// build.gradle -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'org.siouan:frontend-gradle-plugin:1.1.0' - } -} - -apply plugin: 'org.siouan.frontend' -``` - -### Configuration - -#### DSL reference - -All settings are introduced hereafter, with default value for each property. - -```gradle -// build.gradle -frontend { - // NODE SETTINGS - // Version of the distribution to download, used to build the URL to download the distribution, if not set. - nodeVersion = '10.15.3' - - // [Optional] Sets this property to force the download from a custom website. By default, this property is 'null', - // and the plugin attempts to download the distribution compatible with the current platform from the Node website. - nodeDistributionUrl = 'https://nodejs.org/dist/vX.Y.Z/node-vX.Y.Z-win-x64.zip' - - // [Optional] Install directory where the distribution archive shall be exploded. - nodeInstallDirectory = "${projectDir}/node" - - // YARN SETTINGS - // Whether Yarn shall be used instead of NPM when executing frontend tasks. Consequently, a Yarn distribution will - // be downloaded and installed by the plugin. - yarnEnabled = false - - // Yarn version, used to build the URL to download the corresponding distribution, if not set. If a custom - // 'yarnDistributionUrl' property is set, the version of the distribution is expected to be the same as the one set - // in the 'yarnVersion' property, or this may lead to unexpected results. This property is mandatory when the - // property 'yarnEnabled' is true. - yarnVersion = '1.15.2' - - // [Optional] Sets this property to force the download from a custom website. By default, this property is 'null', - // and the plugin attempts to download the distribution compatible with the current platform from the Yarn website. - yarnDistributionUrl = 'https://github.com/yarnpkg/yarn/releases/download/vX.Y.Z/yarn-vX.Y.Z.tar.gz' - - // [Optional] Install directory where the distribution archive shall be exploded. - yarnInstallDirectory = "${projectDir}/yarn" - - // OTHER SETTINGS - // Name of the NPM/Yarn scripts (see 'package.json' file) that shall be executing depending on the Gradle lifecycle - // task. The values below are passed as argument of the 'npm' or 'yarn' executable. - - // [Optional] Use this property only if frontend's compiled resources are generated out of the '${project.buildDir}' - // directory. Default value is . This property is directly used by the 'cleanFrontend' task. The task is - // run when the Gradle built-in 'clean' task is run. - cleanScript = 'run clean' - - // [Optional] Script called to build frontend's artifacts. Default value is . This property is directly used - // by the 'assembleFrontend' task. The task also run when the Gradle built-in 'assemble' task is run. - assembleScript = 'run assemble' - - // [Optional] Script called to run frontend's tests. Default value is . This property is directly used by the - // 'checkFrontend' task. The task is run when the Gradle built-in 'check' task is run. - checkScript = 'run check' -} -``` - -#### Typical configuration with NPM - -```gradle -// build.gradle -frontend { - nodeVersion = '' - cleanScript = 'run clean' - assembleScript = 'run assemble' - checkScript = 'run check' -} -``` - -#### Typical configuration with Yarn - -```gradle -// build.gradle -frontend { - yarnEnabled = true - nodeVersion = '' - yarnVersion = '' - cleanScript = 'run clean' - assembleScript = 'run assemble' - checkScript = 'run check' -} -``` - -### Final steps - -If the developer needs to use Node/NPM/Yarn apart from Gradle, it is mandatory to apply the following steps: - -- Create a `NODEJS_HOME` environment variable containing the real path set in the `nodeInstallationDirectory` -property. -- Add the `$NODEJS_HOME` (Unix-like OS) or `%NODEJS_HOME%` (Windows OS) path to the `PATH` environment variable. - -If Yarn is enabled, apply these latest steps. - -- Create a `YARN_HOME` environment variable containing the real path set in the -`yarnInstallationDirectory` property. -- Add the `$YARN_HOME/bin` (Unix-like OS) or `%YARN_HOME%\bin` (Windows OS) path to the `PATH` -environment variable. - -## Tasks - -The plugin registers multiple tasks, some having dependencies with other, and also with Gradle lifecycle tasks defined -in the [Gradle base plugin][gradle-base-plugin]. - -### Dependencies - -![Task dependencies][task-dependencies] - -### Install Node - -The `installNode` task downloads a Node distribution. If the `distributionUrl` property is ommitted, the URL is -guessed using the `version` property. Use the property `nodeInstallationDirectory` to set the directory where the -distribution shall be installed, which, by default is the `${projectDir}/node` directory. - -### Install Yarn - -The `installYarn` task downloads a Yarn distribution, if `yarnEnabled` property is `true`. If the `distributionUrl` -property is ommitted, the URL is guessed using the `version` property. Use the property `yarnInstallationDirectory` -to set the directory where the distribution shall be installed, which, by default is the `${projectDir}/yarn` directory. - -### Install frontend dependencies - -Depending on the value of the `yarnEnabled` property, the task `installFrontend` issues either a `npm install` command -or a `yarn` command. If a `package.json` file is found in the project's directory, the command shall install -dependencies and tools for frontend development. - -### Clean frontend - -The `cleanFrontend` task does nothing by default, considering frontend generated resources (pre-processed Typescript -files, SCSS stylesheets...) are written in the `${project.buildDir}` directory. If it is not the case, this task may be -useful to clean the relevant directory. To do so, a clean script must be defined in the project's `package.json` file, -and the `cleanScript` property must be set to the corresponding NPM/Yarn command. - -### Assemble frontend - -The `assembleFrontend` task shall be used to integrate a frontend's build script into Gradle builds. The build script -must be defined in the project's `package.json` file, and the `assembleScript` property must be set to the corresponding -NPM/Yarn command. - -### Check frontend - -The `checkFrontend` task shall be used to integrate a frontend's check script into Gradle builds. The check script must -be defined in the project's `package.json` file, and the `checkscript` property must be set with the corresponding -NPM/Yarn command. A typical check script defined in the project's `package.json` file may lint frontend source files, -execute tests, and perform additional analysis tasks. - -### Run custom NPM/Yarn script - -The `runScriptFrontend` task is provided as a task type, to create custom tasks. The `script` property must be set with -the corresponding NPM/Yarn command. For instance, the code below added in the `build.gradle` file allows to run -frontend's end-to-end tests in a custom task: - -```gradle -tasks.register('e2e', org.siouan.frontendgradleplugin.tasks.RunScriptTask) { - dependsOn tasks.named('installFrontend') - script = 'run e2e' -} -``` - -[contributing]: (Contributing to this project) -[frontend-maven-plugin]: (Frontend Maven plugin) -[gradle-base-plugin]: (Gradle Base plugin) -[gradle-build-script-block]: (Gradle build script block) -[gradle-dsl]: (Gradle DSL) -[release-notes]: (Release notes) -[semantic-versioning]: (Semantic versioning) -[task-dependencies]: +# Frontend Gradle plugin + +[![Latest release 1.1.1](https://img.shields.io/badge/Latest%20release-1.1.1-blue.svg)](https://github.com/Siouan/frontend-gradle-plugin/releases/tag/v1.1.1) +[![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0) + +[![Build status](https://travis-ci.org/Siouan/frontend-gradle-plugin.svg?branch=master)](https://travis-ci.org/Siouan/frontend-gradle-plugin) +[![Quality gate status](https://sonarcloud.io/api/project_badges/measure?project=Siouan_frontend-gradle-plugin&metric=alert_status)](https://sonarcloud.io/dashboard?id=Siouan_frontend-gradle-plugin) +[![Code coverage](https://sonarcloud.io/api/project_badges/measure?project=Siouan_frontend-gradle-plugin&metric=coverage)](https://sonarcloud.io/dashboard?id=Siouan_frontend-gradle-plugin) +[![Reliability](https://sonarcloud.io/api/project_badges/measure?project=Siouan_frontend-gradle-plugin&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=Siouan_frontend-gradle-plugin) + +This plugin integrates frontend build tasks into a Gradle build. It is inspired by the +[frontend-maven-plugin][frontend-maven-plugin]. + +Detailed changes for each release are documented in the [release notes][release-notes]. The project uses [semantic +versioning][semantic-versioning] for its releases. + +## Summary + +- [Requirements](#requirements) +- [Installation](#installation) + - [Activation](#activation) + - [Using Gradle DSL](#using-gradle-dsl) + - [Using Gradle build script block](#using-gradle-build-script-block) + - [Configuration](#configuration) + - [DSL reference](#dsl-reference) + - [Typical configuration with NPM](#typical-configuration-with-npm) + - [Typical configuration with Yarn](#typical-configuration-with-yarn) + - [Final steps](#final-steps) + - [Install Node/NPM/Yarn/frontend dependencies](#install-nodenpmyarnfrontend-dependencies) + - [Use Node/NPM/Yarn apart from Gradle](#use-nodenpmyarn-apart-from-gradle) +- [Tasks reference](#tasks-reference) + - [Dependencies](#dependencies) + - [Install Node](#install-node) + - [Install Yarn](#install-yarn) + - [Install frontend dependencies](#install-frontend-dependencies) + - [Clean frontend](#clean-frontend) + - [Assemble frontend](#assemble-frontend) + - [Check frontend](#check-frontend) + - [Run custom NPM/Yarn script](#run-custom-npmyarn-script) +- [Usage guidelines](#usage-guidelines) + - [How to assemble a frontend and a Java backend in a single artifact?](#how-to-assemble-a-frontend-and-a-java-backend-in-a-single-artifact) + - [What kind of script should I attach to the `checkFrontend` task?](#what-kind-of-script-should-i-attach-to-the-checkfrontend-task) +- [Contributing][contributing] + +## Requirements + +The plugin supports: +- [Gradle][gradle] 5.1+ +- [JDK][jdk] 8+ 64 bits +- [Node][node] 6.2.1+ +- [Yarn][yarn] 1.0.0+ + +## Installation + +### Activation + +2 options are available. + +#### Using [Gradle DSL][gradle-dsl] + +This is the modern and recommended approach. + +```gradle +// build.gradle +plugins { + id 'org.siouan.frontend' version '1.1.1' +} +``` + +#### Using [Gradle build script block][gradle-build-script-block] + +This approach is the legacy way to resolve and apply plugins. + +```gradle +// build.gradle +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'org.siouan:frontend-gradle-plugin:1.1.1' + } +} + +apply plugin: 'org.siouan.frontend' +``` + +### Configuration + +#### DSL reference + +All settings are introduced hereafter, with default value for each property. + +```gradle +// build.gradle +frontend { + // NODE SETTINGS + // Version of the distribution to download, used to build the URL to download the distribution, if not set. + nodeVersion = '10.15.3' + + // [Optional] Sets this property to force the download from a custom website. By default, this property is 'null', + // and the plugin attempts to download the distribution compatible with the current platform from the Node website. + nodeDistributionUrl = 'https://nodejs.org/dist/vX.Y.Z/node-vX.Y.Z-win-x64.zip' + + // [Optional] Install directory where the distribution archive shall be exploded. + nodeInstallDirectory = "${projectDir}/node" + + // YARN SETTINGS + // Whether Yarn shall be used instead of NPM when executing frontend tasks. Consequently, a Yarn distribution will + // be downloaded and installed by the plugin. + yarnEnabled = false + + // Yarn version, used to build the URL to download the corresponding distribution, if not set. If a custom + // 'yarnDistributionUrl' property is set, the version of the distribution is expected to be the same as the one set + // in the 'yarnVersion' property, or this may lead to unexpected results. This property is mandatory when the + // property 'yarnEnabled' is true. + yarnVersion = '1.15.2' + + // [Optional] Sets this property to force the download from a custom website. By default, this property is 'null', + // and the plugin attempts to download the distribution compatible with the current platform from the Yarn website. + yarnDistributionUrl = 'https://github.com/yarnpkg/yarn/releases/download/vX.Y.Z/yarn-vX.Y.Z.tar.gz' + + // [Optional] Install directory where the distribution archive shall be exploded. + yarnInstallDirectory = "${projectDir}/yarn" + + // OTHER SETTINGS + // Name of the NPM/Yarn scripts (see 'package.json' file) that shall be executing depending on the Gradle lifecycle + // task. The values below are passed as argument of the 'npm' or 'yarn' executable. + + // [Optional] Use this property only if frontend's compiled resources are generated out of the '${project.buildDir}' + // directory. Default value is . This property is directly used by the 'cleanFrontend' task. The task is + // run when the Gradle built-in 'clean' task is run. + cleanScript = 'run clean' + + // [Optional] Script called to build frontend's artifacts. Default value is . This property is directly used + // by the 'assembleFrontend' task. The task also run when the Gradle built-in 'assemble' task is run. + assembleScript = 'run assemble' + + // [Optional] Script called to run frontend's tests. Default value is . This property is directly used by the + // 'checkFrontend' task. The task is run when the Gradle built-in 'check' task is run. + checkScript = 'run check' +} +``` + +#### Typical configuration with NPM + +```gradle +// build.gradle +frontend { + nodeVersion = '' + cleanScript = 'run clean' + assembleScript = 'run assemble' + checkScript = 'run check' +} +``` + +#### Typical configuration with Yarn + +```gradle +// build.gradle +frontend { + yarnEnabled = true + nodeVersion = '' + yarnVersion = '' + cleanScript = 'run clean' + assembleScript = 'run assemble' + checkScript = 'run check' +} +``` + +### Final steps + +#### Install Node/NPM/Yarn/frontend dependencies + +Working with the frontend application requires the Node distribution, the Yarn distribution - if enabled, and +the frontend dependencies declared in the `package.json` file are all installed/up-to-date. If this is not the case, it +is recommended to run the `installFrontend` task. Open a terminal, and execute the following command in the project's +directory: + +```sh +gradlew installFrontend +``` + +#### Use Node/NPM/Yarn apart from Gradle + +If Node/NPM/Yarn may be used apart from Gradle, it is mandatory to apply the following steps: + +- Create a `NODEJS_HOME` environment variable containing the real path set in the `nodeInstallDirectory` property. +- Add the `$NODEJS_HOME` (Unix-like OS) or `%NODEJS_HOME%` (Windows OS) path to the `PATH` environment variable. + +If Yarn is enabled, apply also the steps below: + +- Create a `YARN_HOME` environment variable containing the real path set in the `yarnInstallDirectory` property. +- Add the `$YARN_HOME/bin` (Unix-like OS) or `%YARN_HOME%\bin` (Windows OS) path to the `PATH` +environment variable. + +## Tasks reference + +The plugin registers multiple tasks, some having dependencies with other, and also with Gradle lifecycle tasks defined +in the [Gradle base plugin][gradle-base-plugin]. + +### Dependencies + +![Task dependencies][task-dependencies] + +### Install Node + +The `installNode` task downloads a Node distribution. If the `distributionUrl` property is ommitted, the URL is +guessed using the `version` property. Use the property `nodeInstallDirectory` to set the directory where the +distribution shall be installed, which, by default is the `${projectDir}/node` directory. + +This task should not be executed directly. It will be called automatically by Gradle, if another task depends on it. + +### Install Yarn + +The `installYarn` task downloads a Yarn distribution, if `yarnEnabled` property is `true`. If the `distributionUrl` +property is ommitted, the URL is guessed using the `version` property. Use the property `yarnInstallDirectory` to set +the directory where the distribution shall be installed, which, by default is the `${projectDir}/yarn` directory. + +This task should not be executed directly. It will be called automatically by Gradle, if another task depends on it. + +### Install frontend dependencies + +Depending on the value of the `yarnEnabled` property, the `installFrontend` task issues either a `npm install` command +or a `yarn install` command. If a `package.json` file is found in the project's directory, the command shall install +dependencies and tools for frontend development. + +This task may be executed directly, especially if the Node distribution and/or the Yarn distribution must be downloaded +again. + +### Clean frontend + +The `cleanFrontend` task does nothing by default, considering frontend generated resources (pre-processed Typescript +files, SCSS stylesheets...) are written in the `${project.buildDir}` directory. If it is not the case, this task may be +useful to clean the relevant directory. To do so, a clean script must be defined in the project's `package.json` file, +and the `cleanScript` property must be set to the corresponding NPM/Yarn command. + +### Assemble frontend + +The `assembleFrontend` task shall be used to integrate a frontend's build script into Gradle builds. The build script +must be defined in the project's `package.json` file, and the `assembleScript` property must be set to the corresponding +NPM/Yarn command. + +### Check frontend + +The `checkFrontend` task shall be used to integrate a frontend's check script into Gradle builds. The check script must +be defined in the project's `package.json` file, and the `checkscript` property must be set with the corresponding +NPM/Yarn command. A typical check script defined in the project's `package.json` file may lint frontend source files, +execute tests, and perform additional analysis tasks. + +### Run custom NPM/Yarn script + +The `runScriptFrontend` task is provided as a task type, to create custom tasks. The `script` property must be set with +the corresponding NPM/Yarn command. For instance, the code below added in the `build.gradle` file allows to run +frontend's end-to-end tests in a custom task: + +```gradle +tasks.register('e2e', org.siouan.frontendgradleplugin.tasks.RunScriptTask) { + dependsOn tasks.named('installFrontend') + script = 'run e2e' +} +``` + +## Usage guidelines + +### How to assemble a frontend and a Java backend in a single artifact? + +If you plan to serve your frontend with a Java backend (e.g. a [Spring Boot][spring-boot] application), you will +probably use other Gradle plugins, such as the [Gradle Java plugin][gradle-java-plugin], the +[Gradle Spring Boot plugin][gradle-spring-boot-plugin], or other ones of your choice. + +In this configuration, you may package your full-stack application as a JAR/WAR artifact. To do so, the frontend must be +assembled before the backend, and generally provided in a special directory for the backend packaging task (e.g. +`jar`/`war`/`bootJar`/`bootWar`... tasks). + +Assembling the frontend before the backend shall not be difficult to setup in Gradle. Below is the task tree of the +`assemble` task when this plugin is used with the [Gradle Java plugin][gradle-java-plugin] or a plugin depending on it: + +```sh +gradlew taskTree --no-repeat assemble + +:build +:assemble ++--- :assembleFrontend +| \--- :installFrontend +| +--- :installNode +| \--- :installYarn ++--- :jar + \--- :classes + +--- :compileJava + \--- :processResources +``` + +1. Considering the frontend assembling script generates the frontend artifacts (HTML, CSS, JS...) in the +`${frontendBuildDir}` directory, these artifacts must be copied, generally in the +`${project.buildDir}/resources/main/public` directory, so as they can be served by the backend. + +Let's create a custom task for this. Add the following lines in the `build.gradle` file: + +```groovy +tasks.register('processFrontendResources', Copy) { + description 'Process frontend resources' + from "${frontendBuildDir}" + into "${project.buildDir}/resources/main/public" + dependsOn tasks.named('assembleFrontend') +} +``` + +Finally, you should: + +- Replace the `${frontendBuildDir}` variable by any relevant directory, depending on where the frontend artifacts are +generated by your assembling script. +- Adapt the target directory under `${project.buildDir}/resources/main`, depending on the Java artifact built. + +2. The frontend must be assembled and the generated resources must be copied before the backend packaging task. + +Our recommendation is the `processResources` task depends on the `processFrontendResources` task. + +```groovy +tasks.named('processResources').configure { + dependsOn tasks.named('processFrontendResources') +} +``` + +### What kind of script should I attach to the `checkFrontend` task? + +The `checkFrontend` task is a dependency of the `check` task. The Gradle official documentation states that the `check` +task shall be used to `attach [...] verification tasks, such as ones that run tests [...]`. It's enough vague to let you +consider any verification task. The script mapped to the `checkFrontend` task may run either automated unit tests, or +functional tests, or a linter, or any other verification action, or even combine some or all of them. Every combination +is possible, since you can define a script in your `package.json` file that executes sequentially the actions of your +choice. + +[contributing]: (Contributing to this project) +[frontend-maven-plugin]: (Frontend Maven plugin) +[gradle]: (Gradle) +[gradle-base-plugin]: (Gradle Base plugin) +[gradle-build-script-block]: (Gradle build script block) +[gradle-dsl]: (Gradle DSL) +[gradle-java-plugin]: (Gradle Java plugin) +[gradle-spring-boot-plugin]: (Gradle Spring Boot plugin) +[j2ee]: (J2EE) +[jdk]: (Java Development Kit) +[node]: (Node.js) +[release-notes]: (Release notes) +[semantic-versioning]: (Semantic versioning) +[spring-boot]: (Spring Boot) +[task-dependencies]: +[yarn]: (Yarn) diff --git a/build.gradle b/build.gradle index 00dd6eca..0ca346bb 100644 --- a/build.gradle +++ b/build.gradle @@ -41,7 +41,7 @@ dependencies { } group 'org.siouan' -version '1.1.0' +version '1.1.1' description 'Integrate your frontend NPM/Yarn build into Gradle.' sourceCompatibility = JavaVersion.VERSION_1_8 diff --git a/sonar-project.properties b/sonar-project.properties index c9bc416d..bc2a8806 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,6 +1,6 @@ sonar.projectKey=Siouan_frontend-gradle-plugin sonar.projectName=frontend-gradle-plugin -sonar.projectVersion=1.1.0 +sonar.projectVersion=1.1.1 sonar.links.homepage=https://github.com/Siouan/frontend-gradle-plugin sonar.links.ci=https://travis-ci.org/Siouan/frontend-gradle-plugin diff --git a/src/main/java/org/siouan/frontendgradleplugin/FrontendGradlePlugin.java b/src/main/java/org/siouan/frontendgradleplugin/FrontendGradlePlugin.java index a6fd0fc9..3be6fda2 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/FrontendGradlePlugin.java +++ b/src/main/java/org/siouan/frontendgradleplugin/FrontendGradlePlugin.java @@ -11,20 +11,77 @@ import org.siouan.frontendgradleplugin.tasks.CleanTask; import org.siouan.frontendgradleplugin.tasks.InstallTask; import org.siouan.frontendgradleplugin.tasks.NodeInstallTask; -import org.siouan.frontendgradleplugin.tasks.RunScriptTask; import org.siouan.frontendgradleplugin.tasks.YarnInstallTask; /** - * Main plugin class which bootstraps the plugin by declaring its DSL and its tasks. + * Main plugin class that bootstraps the plugin by declaring its DSL and its tasks. + *
    + *
  • The plugin applies the Gradle Base plugin, to attach its tasks to the Gradle lifecyle task.
  • + *
  • Tasks are registered lazily thanks to the use of the configuration avoidance API.
  • + *
  • Task properties are mapped the plugin extension (DSL) using the lazy configuration API, allowing there + * calculation is delayed until it is required.
  • + *
+ * + * @see Task configuration + * avoidance + * @see Lazy configuration */ public class FrontendGradlePlugin implements Plugin { + /** + * Name of the task that assembles the frontend. + */ + public static final String ASSEMBLE_TASK_NAME = "assembleFrontend"; + + /** + * Name of the task that checks the frontend. + */ + public static final String CHECK_TASK_NAME = "checkFrontend"; + + /** + * Name of the task that cleans the frontend. + */ + public static final String CLEAN_TASK_NAME = "cleanFrontend"; + + /** + * Name of the task that installs a Node distribution. + */ public static final String DEFAULT_NODE_INSTALL_DIRNAME = "node"; + /** + * Name of the task that installs a Yarn distribution. + */ public static final String DEFAULT_YARN_INSTALL_DIRNAME = "yarn"; + /** + * Name of the task that installs frontend dependencies. + */ + public static final String INSTALL_TASK_NAME = "installFrontend"; + + /** + * Name of the task that installs a Node distribution. + */ + public static final String NODE_INSTALL_TASK_NAME = "installNode"; + + /** + * Name of the task that installs a Yarn distribution. + */ + public static final String YARN_INSTALL_TASK_NAME = "installYarn"; + + public static final String GRADLE_ASSEMBLE_TASK_NAME = "assemble"; + + public static final String GRADLE_CHECK_TASK_NAME = "check"; + + public static final String GRADLE_CLEAN_TASK_NAME = "clean"; + + /** + * Root name of the plugin extension. + */ private static final String EXTENSION_NAME = "frontend"; + /** + * The Gradle group in which all this plugin's tasks will be categorized. + */ private static final String TASK_GROUP = "Frontend"; public void apply(final Project project) { @@ -37,26 +94,28 @@ public void apply(final Project project) { extension.getYarnInstallDirectory().convention(new File(project.getProjectDir(), DEFAULT_YARN_INSTALL_DIRNAME)); final TaskContainer projectTasks = project.getTasks(); - projectTasks - .register(NodeInstallTask.DEFAULT_NAME, NodeInstallTask.class, task -> configureTask(task, extension)); - - projectTasks - .register(YarnInstallTask.DEFAULT_NAME, YarnInstallTask.class, task -> configureTask(task, extension)); + projectTasks.register(NODE_INSTALL_TASK_NAME, NodeInstallTask.class, task -> configureTask(task, extension)); - projectTasks.register(InstallTask.DEFAULT_NAME, InstallTask.class, task -> configureTask(task, extension)); + projectTasks.register(YARN_INSTALL_TASK_NAME, YarnInstallTask.class, task -> configureTask(task, extension)); - projectTasks.register(CleanTask.DEFAULT_NAME, CleanTask.class, task -> configureTask(task, extension)); - projectTasks.named("clean", task -> task.dependsOn(projectTasks.named(CleanTask.DEFAULT_NAME))); + projectTasks.register(INSTALL_TASK_NAME, InstallTask.class, task -> configureTask(task, extension)); - projectTasks.register(CheckTask.DEFAULT_NAME, CheckTask.class, task -> configureTask(task, extension)); - projectTasks.named("check", task -> task.dependsOn(projectTasks.named(CheckTask.DEFAULT_NAME))); + projectTasks.register(CLEAN_TASK_NAME, CleanTask.class, task -> configureTask(task, extension)); + projectTasks.named(GRADLE_CLEAN_TASK_NAME, task -> task.dependsOn(projectTasks.named(CLEAN_TASK_NAME))); - projectTasks.register(AssembleTask.DEFAULT_NAME, AssembleTask.class, task -> configureTask(task, extension)); - projectTasks.named("assemble", task -> task.dependsOn(projectTasks.named(AssembleTask.DEFAULT_NAME))); + projectTasks.register(CHECK_TASK_NAME, CheckTask.class, task -> configureTask(task, extension)); + projectTasks.named(GRADLE_CHECK_TASK_NAME, task -> task.dependsOn(projectTasks.named(CHECK_TASK_NAME))); - projectTasks.register(RunScriptTask.DEFAULT_NAME, RunScriptTask.class, this::configureTask); + projectTasks.register(ASSEMBLE_TASK_NAME, AssembleTask.class, task -> configureTask(task, extension)); + projectTasks.named(GRADLE_ASSEMBLE_TASK_NAME, task -> task.dependsOn(projectTasks.named(ASSEMBLE_TASK_NAME))); } + /** + * Configures the given task with the plugin extension. + * + * @param task Task. + * @param extension Plugin extension. + */ private void configureTask(final NodeInstallTask task, final FrontendExtension extension) { task.setGroup(TASK_GROUP); task.setDescription("Downloads and installs a Node distribution."); @@ -65,6 +124,12 @@ private void configureTask(final NodeInstallTask task, final FrontendExtension e task.getNodeInstallDirectory().set(extension.getNodeInstallDirectory()); } + /** + * Configures the given task with the plugin extension. + * + * @param task Task. + * @param extension Plugin extension. + */ private void configureTask(final YarnInstallTask task, FrontendExtension extension) { task.setGroup(TASK_GROUP); task.setDescription("Downloads and installs a Yarn distribution."); @@ -74,15 +139,27 @@ private void configureTask(final YarnInstallTask task, FrontendExtension extensi task.getYarnInstallDirectory().set(extension.getYarnInstallDirectory()); } + /** + * Configures the given task with the plugin extension. + * + * @param task Task. + * @param extension Plugin extension. + */ private void configureTask(final InstallTask task, FrontendExtension extension) { task.setGroup(TASK_GROUP); task.setDescription("Installs/updates frontend dependencies."); task.getYarnEnabled().set(extension.getYarnEnabled()); task.getNodeInstallDirectory().set(extension.getNodeInstallDirectory()); task.getYarnInstallDirectory().set(extension.getYarnInstallDirectory()); - task.dependsOn(NodeInstallTask.DEFAULT_NAME, YarnInstallTask.DEFAULT_NAME); + task.dependsOn(NODE_INSTALL_TASK_NAME, YARN_INSTALL_TASK_NAME); } + /** + * Configures the given task with the plugin extension. + * + * @param task Task. + * @param extension Plugin extension. + */ private void configureTask(final CleanTask task, FrontendExtension extension) { task.setGroup(TASK_GROUP); task.setDescription("Cleans frontend resources outside the build directory by running a specific script."); @@ -90,9 +167,15 @@ private void configureTask(final CleanTask task, FrontendExtension extension) { task.getNodeInstallDirectory().set(extension.getNodeInstallDirectory()); task.getYarnInstallDirectory().set(extension.getYarnInstallDirectory()); task.getCleanScript().set(extension.getCleanScript()); - task.dependsOn(InstallTask.DEFAULT_NAME); + task.dependsOn(INSTALL_TASK_NAME); } + /** + * Configures the given task with the plugin extension. + * + * @param task Task. + * @param extension Plugin extension. + */ private void configureTask(final CheckTask task, FrontendExtension extension) { task.setGroup(TASK_GROUP); task.setDescription("Checks frontend by running a specific script."); @@ -100,9 +183,15 @@ private void configureTask(final CheckTask task, FrontendExtension extension) { task.getNodeInstallDirectory().set(extension.getNodeInstallDirectory()); task.getYarnInstallDirectory().set(extension.getYarnInstallDirectory()); task.getCheckScript().set(extension.getCheckScript()); - task.dependsOn(InstallTask.DEFAULT_NAME); + task.dependsOn(INSTALL_TASK_NAME); } + /** + * Configures the given task with the plugin extension. + * + * @param task Task. + * @param extension Plugin extension. + */ private void configureTask(final AssembleTask task, FrontendExtension extension) { task.setGroup(TASK_GROUP); task.setDescription("Assembles the frontend by running a specific script."); @@ -110,12 +199,6 @@ private void configureTask(final AssembleTask task, FrontendExtension extension) task.getNodeInstallDirectory().set(extension.getNodeInstallDirectory()); task.getYarnInstallDirectory().set(extension.getYarnInstallDirectory()); task.getAssembleScript().set(extension.getAssembleScript()); - task.dependsOn(InstallTask.DEFAULT_NAME); - } - - private void configureTask(final RunScriptTask task) { - task.setGroup(TASK_GROUP); - task.setDescription("Runs a frontend script."); - task.dependsOn(InstallTask.DEFAULT_NAME); + task.dependsOn(INSTALL_TASK_NAME); } } diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/AbstractJob.java b/src/main/java/org/siouan/frontendgradleplugin/core/AbstractJob.java index 605f5a47..9d74cce7 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/AbstractJob.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/AbstractJob.java @@ -9,6 +9,11 @@ abstract class AbstractJob { protected final Task task; + /** + * Builds a job instance related to the given task. + * + * @param task Task. + */ protected AbstractJob(final Task task) { this.task = task; } @@ -17,18 +22,38 @@ private String formatMessage(final String message) { return '[' + task.getName() + "] " + message; } + /** + * Shortcut to log a DEBUG message with the task's logger. + * + * @param message Message. + */ protected void logDebug(final String message) { task.getLogger().debug(formatMessage(message)); } + /** + * Shortcut to log an ERROR message with the task's logger. + * + * @param message Message. + */ protected void logError(final String message) { task.getLogger().error(formatMessage(message)); } + /** + * Shortcut to log a LIFECYCLE message with the task's logger. + * + * @param message Message. + */ protected void logLifecycle(final String message) { task.getLogger().lifecycle(formatMessage(message)); } + /** + * Shortcut to log a WARN LIFECYCLE message with the task's logger. + * + * @param message Message. + */ protected void logWarn(final String message, final Throwable throwable) { task.getLogger().warn(formatMessage(message), throwable); } diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/DistributionInstallJob.java b/src/main/java/org/siouan/frontendgradleplugin/core/DistributionInstallJob.java index 2f6efed4..cc039e4d 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/DistributionInstallJob.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/DistributionInstallJob.java @@ -38,6 +38,24 @@ public DistributionInstallJob(final Task task, final DistributionUrlResolver url this.installDirectory = installDirectory; } + /** + * Installs a distribution: + *
    + *
  • Empty the install directory.
  • + *
  • Resolve the URL to download the distribution.
  • + *
  • Download the distribution.
  • + *
  • Validate the downloaded distribution.
  • + *
  • Explode the distribution archive.
  • + *
  • Deletes the distribution archive and all unnecessary files.
  • + *
+ * + * @throws InvalidDistributionException If the distribution is invalid. + * @throws IOException If an I/O occurs when accessing the file system. + * @throws DistributionUrlResolverException If the URL to download the distribution could not be resolved. + * @throws DownloadException If the distribution could not be downloaded. + * @throws UnsupportedDistributionArchiveException If the distribution archive is not supported, and could not be + * exploded. + */ public void install() throws InvalidDistributionException, IOException, DistributionUrlResolverException, DownloadException, UnsupportedDistributionArchiveException { diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/DistributionUrlResolver.java b/src/main/java/org/siouan/frontendgradleplugin/core/DistributionUrlResolver.java index e510329b..97e08221 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/DistributionUrlResolver.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/DistributionUrlResolver.java @@ -6,7 +6,7 @@ * Interface of a component capable to resolve the URL to download a distribution. */ @FunctionalInterface -public interface DistributionUrlResolver { +interface DistributionUrlResolver { /** * Resolves the URL to download the distribution. diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/DistributionValidator.java b/src/main/java/org/siouan/frontendgradleplugin/core/DistributionValidator.java index 00779475..b5c202a2 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/DistributionValidator.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/DistributionValidator.java @@ -7,7 +7,7 @@ * Interface of a component capable to validate a downloaded distribution. */ @FunctionalInterface -public interface DistributionValidator { +interface DistributionValidator { /** * Validates the distribution in the given file. diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/Downloader.java b/src/main/java/org/siouan/frontendgradleplugin/core/Downloader.java index 764edd56..ced8ffe6 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/Downloader.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/Downloader.java @@ -7,7 +7,7 @@ * Functional interface of a component capable to download resources. */ @FunctionalInterface -public interface Downloader { +interface Downloader { /** * Downloads a resource at a given URL, and moves it to a destination file once completed. Before calling this diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/DownloaderImpl.java b/src/main/java/org/siouan/frontendgradleplugin/core/DownloaderImpl.java index f578bb26..e527381f 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/DownloaderImpl.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/DownloaderImpl.java @@ -25,6 +25,7 @@ public DownloaderImpl(final File temporaryDirectory) { this.temporaryDirectory = temporaryDirectory; } + @Override public void download(final URL resourceUrl, final File destinationFile) throws DownloadException { final String resourceName = new File(resourceUrl.getPath()).getName(); final File downloadedFile = new File(temporaryDirectory, resourceName); diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/FileHasher.java b/src/main/java/org/siouan/frontendgradleplugin/core/FileHasher.java index 00357772..75f59f13 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/FileHasher.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/FileHasher.java @@ -6,7 +6,8 @@ /** * Component capable of hashing a file. */ -public interface FileHasher { +@FunctionalInterface +interface FileHasher { /** * Computes the hash of the file if not already done. diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/FileHasherImpl.java b/src/main/java/org/siouan/frontendgradleplugin/core/FileHasherImpl.java index 2cd63449..08a6485d 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/FileHasherImpl.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/FileHasherImpl.java @@ -29,6 +29,7 @@ public FileHasherImpl() throws NoSuchAlgorithmException { this.digest = MessageDigest.getInstance("SHA-256"); } + @Override public String hash(final File inputFile) throws IOException { final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY); try (final FileInputStream inputStream = new FileInputStream(inputFile)) { diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumNotFoundException.java b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumNotFoundException.java index 24ccc6c8..e55c782f 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumNotFoundException.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumNotFoundException.java @@ -1,5 +1,9 @@ package org.siouan.frontendgradleplugin.core; +/** + * Exception thrown when the checksum of a Node distribution could not be found in the file providing checksums for all + * packages of a given release. + */ public class NodeDistributionChecksumNotFoundException extends FrontendException { } diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReader.java b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReader.java index ff3a398c..2547fc6c 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReader.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReader.java @@ -7,6 +7,7 @@ * A checksum reader allows to extract the checksum of a given distribution file, from a Node's file providing all * checksums for a given Node version. */ +@FunctionalInterface interface NodeDistributionChecksumReader { /** diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReaderImpl.java b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReaderImpl.java index c48c5d34..0bc04f0f 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReaderImpl.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionChecksumReaderImpl.java @@ -11,6 +11,7 @@ */ public class NodeDistributionChecksumReaderImpl implements NodeDistributionChecksumReader { + @Override public String readHash(final File nodeDistributionChecksumFile, final String distributionFilename) throws NodeDistributionChecksumNotFoundException, IOException { final String trailingValue = " " + distributionFilename; diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionUrlResolver.java b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionUrlResolver.java index 13ff61e5..82f828d7 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionUrlResolver.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionUrlResolver.java @@ -50,6 +50,7 @@ public NodeDistributionUrlResolver(final String version, final String distributi this.jreArch = jreArch; } + @Override public URL resolve() throws DistributionUrlResolverException { final String urlAsString; if (distributionUrl == null) { diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionValidator.java b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionValidator.java index 7d5fbbc9..8852e3a8 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionValidator.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/NodeDistributionValidator.java @@ -13,14 +13,35 @@ */ public class NodeDistributionValidator extends AbstractJob implements DistributionValidator { + /** + * Downloader used to get the file containing all checksums for a given distribution. + */ private final Downloader downloader; + /** + * Reader that gives the checksum of the distribution from a file. + */ private final NodeDistributionChecksumReader checksumReader; + /** + * Hasher used to check the distribution integrity. + */ private final FileHasher fileHasher; + /** + * Install directory. + */ private final File installDirectory; + /** + * Builds a validator of a Node distribution. + * + * @param task Refering task. + * @param downloader Downloader. + * @param checksumReader Reader of checksum's file. + * @param fileHasher Hasher. + * @param installDirectory Install directory. + */ public NodeDistributionValidator(final Task task, final Downloader downloader, final NodeDistributionChecksumReader checksumReader, final FileHasher fileHasher, final File installDirectory) { super(task); @@ -30,6 +51,7 @@ public NodeDistributionValidator(final Task task, final Downloader downloader, this.installDirectory = installDirectory; } + @Override public void validate(final URL distributionUrl, final File distributionFile) throws InvalidDistributionException { final String distributionUrlAsString = distributionUrl.toString(); diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/ScriptRunJob.java b/src/main/java/org/siouan/frontendgradleplugin/core/ScriptRunJob.java index 33d16147..61d7fb7f 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/ScriptRunJob.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/ScriptRunJob.java @@ -39,7 +39,8 @@ public void run() { task.getProject() .exec(new ExecSpecAction(yarnEnabled, nodeInstallDirectory, yarnInstallDirectory, script, execSpec -> { logDebug(execSpec.getEnvironment().toString()); - logLifecycle("Running '" + execSpec.getExecutable() + ' ' + String.join(" ", execSpec.getArgs()) + '\''); + logLifecycle( + "Running '" + execSpec.getExecutable() + ' ' + String.join(" ", execSpec.getArgs()) + '\''); })).rethrowFailure().assertNormalExitValue(); } } diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/Utils.java b/src/main/java/org/siouan/frontendgradleplugin/core/Utils.java index 545dcc39..523d63ad 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/Utils.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/Utils.java @@ -94,7 +94,7 @@ public static void moveFiles(final File srcDirectory, final File destDirectory) } }); } -} + } /** * Removes the extension of a filename. In case of a compressed TAR archive, the method removes the whole extension diff --git a/src/main/java/org/siouan/frontendgradleplugin/core/YarnDistributionUrlResolver.java b/src/main/java/org/siouan/frontendgradleplugin/core/YarnDistributionUrlResolver.java index 21ea4c39..8145b93f 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/core/YarnDistributionUrlResolver.java +++ b/src/main/java/org/siouan/frontendgradleplugin/core/YarnDistributionUrlResolver.java @@ -28,6 +28,7 @@ public YarnDistributionUrlResolver(final String version, final String distributi this.distributionUrl = distributionUrl; } + @Override public URL resolve() throws DistributionUrlResolverException { final String urlAsString; if (distributionUrl == null) { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/AbstractRunScriptTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/AbstractRunScriptTask.java index c4c76d59..712050a9 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/AbstractRunScriptTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/AbstractRunScriptTask.java @@ -40,6 +40,9 @@ protected AbstractRunScriptTask() { script = getProject().getObjects().property(String.class); } + /** + * Executes the task. If a script has been provided, it is run with NPM/Yarn. Otherwise, the task does nothing. + */ @TaskAction public void execute() { if (script.isPresent()) { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/AssembleTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/AssembleTask.java index d0bfd6fc..ccc17520 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/AssembleTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/AssembleTask.java @@ -9,11 +9,6 @@ */ public class AssembleTask extends AbstractPredefinedRunScriptTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "assembleFrontend"; - @Input @Optional public Property getAssembleScript() { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/CheckTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/CheckTask.java index 0d8658c6..cdbcf4d7 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/CheckTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/CheckTask.java @@ -9,11 +9,6 @@ */ public class CheckTask extends AbstractPredefinedRunScriptTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "checkFrontend"; - @Input @Optional public Property getCheckScript() { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/CleanTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/CleanTask.java index 0caebc21..c62c7f2e 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/CleanTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/CleanTask.java @@ -9,11 +9,6 @@ */ public class CleanTask extends AbstractPredefinedRunScriptTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "cleanFrontend"; - @Input @Optional public Property getCleanScript() { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/InstallTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/InstallTask.java index f7c46522..47c69e51 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/InstallTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/InstallTask.java @@ -5,11 +5,6 @@ */ public class InstallTask extends AbstractPredefinedRunScriptTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "installFrontend"; - public static final String INSTALL_SCRIPT = "install"; public InstallTask() { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTask.java index 1b722d84..c9352d61 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTask.java @@ -26,11 +26,6 @@ */ public class NodeInstallTask extends DefaultTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "installNode"; - /** * Version of the Node distribution to download. */ @@ -69,6 +64,17 @@ public Property getNodeInstallDirectory() { return nodeInstallDirectory; } + /** + * Executes the task: downloads and installs the distribution. + * + * @throws IOException If an I/O error occured. + * @throws InvalidDistributionException If the downloaded distribution is invalid. + * @throws DistributionUrlResolverException If the URL to download the distribution cannot be downloaded. + * @throws UnsupportedDistributionArchiveException If the type of the distribution is not supported. + * @throws DownloadException If a download error occured. + * @throws NoSuchAlgorithmException If the hashing algorithm used to check the distribution integrity is not + * supported. + */ @TaskAction public void execute() throws IOException, InvalidDistributionException, DistributionUrlResolverException, UnsupportedDistributionArchiveException, DownloadException, NoSuchAlgorithmException { diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/RunScriptTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/RunScriptTask.java index 46f5f162..b3d193e6 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/RunScriptTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/RunScriptTask.java @@ -12,23 +12,21 @@ * A typical usage of this task in a 'build.gradle' file would be: *
  * tasks.register('mytask', org.siouan.frontendgradleplugin.tasks.RunScriptTask) {
- *     dependsOn installFrontend
+ *     dependsOn tasks.named('installFrontend')
  *     script = 'myscript'
  * }
  * 
*/ public class RunScriptTask extends AbstractRunScriptTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "runScriptFrontend"; - @Input public Property getScript() { return script; } + /** + * Executes the task by running the script with NPM/Yarn. + */ @Override public void execute() { final FrontendExtension extension = getProject().getExtensions().findByType(FrontendExtension.class); diff --git a/src/main/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTask.java b/src/main/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTask.java index c5b3998d..3731d31e 100644 --- a/src/main/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTask.java +++ b/src/main/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTask.java @@ -21,11 +21,6 @@ */ public class YarnInstallTask extends DefaultTask { - /** - * Default task name. - */ - public static final String DEFAULT_NAME = "installYarn"; - /** * Version of the distribution to download. */ diff --git a/src/test/java/org/siouan/frontendgradleplugin/core/FrontendGradlePluginTest.java b/src/test/java/org/siouan/frontendgradleplugin/core/FrontendGradlePluginTest.java index 33b72169..c8daef5a 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/core/FrontendGradlePluginTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/core/FrontendGradlePluginTest.java @@ -49,29 +49,30 @@ public void shouldRegisterTasksWithDefaultExtensionValuesWhenApplied() { .isEqualTo(new File(project.getProjectDir(), FrontendGradlePlugin.DEFAULT_YARN_INSTALL_DIRNAME)); final NodeInstallTask nodeInstallTask = project.getTasks() - .named(NodeInstallTask.DEFAULT_NAME, NodeInstallTask.class).get(); + .named(FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, NodeInstallTask.class).get(); assertThat(nodeInstallTask.getNodeVersion().isPresent()).isFalse(); assertThat(nodeInstallTask.getNodeDistributionUrl().isPresent()).isFalse(); assertThat(nodeInstallTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); final YarnInstallTask yarnInstallTask = project.getTasks() - .named(YarnInstallTask.DEFAULT_NAME, YarnInstallTask.class).get(); + .named(FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, YarnInstallTask.class).get(); assertThat(yarnInstallTask.isEnabled()).isEqualTo(extension.getYarnEnabled().get()); assertThat(yarnInstallTask.getYarnVersion().isPresent()).isFalse(); assertThat(yarnInstallTask.getYarnDistributionUrl().isPresent()).isFalse(); assertThat(yarnInstallTask.getYarnInstallDirectory().get()) .isEqualTo(extension.getYarnInstallDirectory().get()); - final InstallTask frontendInstallTask = project.getTasks().named(InstallTask.DEFAULT_NAME, InstallTask.class) - .get(); + final InstallTask frontendInstallTask = project.getTasks() + .named(FrontendGradlePlugin.INSTALL_TASK_NAME, InstallTask.class).get(); assertThat(frontendInstallTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendInstallTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); assertThat(frontendInstallTask.getYarnInstallDirectory().get()) .isEqualTo(extension.getYarnInstallDirectory().get()); - final CleanTask frontendCleanTask = project.getTasks().named(CleanTask.DEFAULT_NAME, CleanTask.class).get(); + final CleanTask frontendCleanTask = project.getTasks() + .named(FrontendGradlePlugin.CLEAN_TASK_NAME, CleanTask.class).get(); assertThat(frontendCleanTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendCleanTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); @@ -80,7 +81,7 @@ public void shouldRegisterTasksWithDefaultExtensionValuesWhenApplied() { assertThat(frontendCleanTask.getCleanScript().isPresent()).isFalse(); final AssembleTask frontendAssembleTask = project.getTasks() - .named(AssembleTask.DEFAULT_NAME, AssembleTask.class).get(); + .named(FrontendGradlePlugin.ASSEMBLE_TASK_NAME, AssembleTask.class).get(); assertThat(frontendAssembleTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendAssembleTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); @@ -88,7 +89,8 @@ public void shouldRegisterTasksWithDefaultExtensionValuesWhenApplied() { .isEqualTo(extension.getYarnInstallDirectory().get()); assertThat(frontendAssembleTask.getAssembleScript().isPresent()).isFalse(); - final CheckTask frontendCheckTask = project.getTasks().named(CheckTask.DEFAULT_NAME, CheckTask.class).get(); + final CheckTask frontendCheckTask = project.getTasks() + .named(FrontendGradlePlugin.CHECK_TASK_NAME, CheckTask.class).get(); assertThat(frontendCheckTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendCheckTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); @@ -114,29 +116,30 @@ public void shouldRegisterTasksWithCustomExtensionValuesWhenApplied() { extension.getCheckScript().set("test"); final NodeInstallTask nodeInstallTask = project.getTasks() - .named(NodeInstallTask.DEFAULT_NAME, NodeInstallTask.class).get(); + .named(FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, NodeInstallTask.class).get(); assertThat(nodeInstallTask.getNodeVersion().get()).isEqualTo(extension.getNodeVersion().get()); assertThat(nodeInstallTask.getNodeDistributionUrl().get()).isEqualTo(extension.getNodeDistributionUrl().get()); assertThat(nodeInstallTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); final YarnInstallTask yarnInstallTask = project.getTasks() - .named(YarnInstallTask.DEFAULT_NAME, YarnInstallTask.class).get(); + .named(FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, YarnInstallTask.class).get(); assertThat(yarnInstallTask.isEnabled()).isEqualTo(extension.getYarnEnabled().get()); assertThat(yarnInstallTask.getYarnVersion().get()).isEqualTo(extension.getYarnVersion().get()); assertThat(yarnInstallTask.getYarnDistributionUrl().get()).isEqualTo(extension.getYarnDistributionUrl().get()); assertThat(yarnInstallTask.getYarnInstallDirectory().get()) .isEqualTo(extension.getYarnInstallDirectory().get()); - final InstallTask frontendInstallTask = project.getTasks().named(InstallTask.DEFAULT_NAME, InstallTask.class) - .get(); + final InstallTask frontendInstallTask = project.getTasks() + .named(FrontendGradlePlugin.INSTALL_TASK_NAME, InstallTask.class).get(); assertThat(frontendInstallTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendInstallTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); assertThat(frontendInstallTask.getYarnInstallDirectory().get()) .isEqualTo(extension.getYarnInstallDirectory().get()); - final CleanTask frontendCleanTask = project.getTasks().named(CleanTask.DEFAULT_NAME, CleanTask.class).get(); + final CleanTask frontendCleanTask = project.getTasks() + .named(FrontendGradlePlugin.CLEAN_TASK_NAME, CleanTask.class).get(); assertThat(frontendCleanTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendCleanTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); @@ -145,7 +148,7 @@ public void shouldRegisterTasksWithCustomExtensionValuesWhenApplied() { assertThat(frontendCleanTask.getCleanScript().get()).isEqualTo(extension.getCleanScript().get()); final AssembleTask frontendAssembleTask = project.getTasks() - .named(AssembleTask.DEFAULT_NAME, AssembleTask.class).get(); + .named(FrontendGradlePlugin.ASSEMBLE_TASK_NAME, AssembleTask.class).get(); assertThat(frontendAssembleTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendAssembleTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); @@ -153,7 +156,8 @@ public void shouldRegisterTasksWithCustomExtensionValuesWhenApplied() { .isEqualTo(extension.getYarnInstallDirectory().get()); assertThat(frontendAssembleTask.getAssembleScript().get()).isEqualTo(extension.getAssembleScript().get()); - final CheckTask frontendCheckTask = project.getTasks().named(CheckTask.DEFAULT_NAME, CheckTask.class).get(); + final CheckTask frontendCheckTask = project.getTasks() + .named(FrontendGradlePlugin.CHECK_TASK_NAME, CheckTask.class).get(); assertThat(frontendCheckTask.getYarnEnabled().get()).isEqualTo(extension.getYarnEnabled().get()); assertThat(frontendCheckTask.getNodeInstallDirectory().get()) .isEqualTo(extension.getNodeInstallDirectory().get()); diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/AssembleTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/AssembleTaskFuncTest.java index f82a1098..91723c14 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/AssembleTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/AssembleTaskFuncTest.java @@ -15,14 +15,13 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** * Functional tests to verify the {@link AssembleTask} integration in a Gradle build. */ -public class AssembleTaskFuncTest { - - private static final String GRADLE_ASSEMBLE_TASK = "assemble"; +class AssembleTaskFuncTest { @TempDir protected File projectDirectory; @@ -36,39 +35,39 @@ public void shouldAssembleFrontendWithNpmOrYarn() throws IOException, URISyntaxE properties.put("assembleScript", "run assemble"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result1 = runGradle(projectDirectory, GRADLE_ASSEMBLE_TASK); + final BuildResult result1 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME); - assertTaskOutcome(result1, NodeInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result1, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, GRADLE_ASSEMBLE_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result1, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result2 = runGradle(projectDirectory, GRADLE_ASSEMBLE_TASK); + final BuildResult result2 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME); - assertTaskOutcome(result2, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result2, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result2, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result2, GRADLE_ASSEMBLE_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result2, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME, TaskOutcome.SUCCESS); - Files.delete(projectDirectory.toPath().resolve("package-lock.json")); + Files.deleteIfExists(projectDirectory.toPath().resolve("package-lock.json")); Files.copy(new File(getClass().getClassLoader().getResource("package-yarn.json").toURI()).toPath(), projectDirectory.toPath().resolve("package.json"), StandardCopyOption.REPLACE_EXISTING); properties.put("yarnEnabled", true); properties.put("yarnVersion", "1.15.2"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result3 = runGradle(projectDirectory, GRADLE_ASSEMBLE_TASK); + final BuildResult result3 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME); - assertTaskOutcome(result3, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result3, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, GRADLE_ASSEMBLE_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result3, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result4 = runGradle(projectDirectory, GRADLE_ASSEMBLE_TASK); + final BuildResult result4 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME); - assertTaskOutcome(result4, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, YarnInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result4, GRADLE_ASSEMBLE_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.GRADLE_ASSEMBLE_TASK_NAME, TaskOutcome.SUCCESS); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/CheckTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/CheckTaskFuncTest.java index d9cfcdb8..3d90d378 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/CheckTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/CheckTaskFuncTest.java @@ -15,14 +15,13 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** * Functional tests to verify the {@link CheckTask} integration in a Gradle build. */ -public class CheckTaskFuncTest { - - private static final String GRADLE_CHECK_TASK = "check"; +class CheckTaskFuncTest { @TempDir protected File projectDirectory; @@ -36,39 +35,39 @@ public void shouldCheckFrontendWithNpmOrYarn() throws IOException, URISyntaxExce properties.put("checkScript", "run check"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result1 = runGradle(projectDirectory, GRADLE_CHECK_TASK); + final BuildResult result1 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME); - assertTaskOutcome(result1, NodeInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result1, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, GRADLE_CHECK_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result1, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result2 = runGradle(projectDirectory, GRADLE_CHECK_TASK); + final BuildResult result2 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME); - assertTaskOutcome(result2, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result2, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result2, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result2, GRADLE_CHECK_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result2, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME, TaskOutcome.SUCCESS); - Files.delete(projectDirectory.toPath().resolve("package-lock.json")); + Files.deleteIfExists(projectDirectory.toPath().resolve("package-lock.json")); Files.copy(new File(getClass().getClassLoader().getResource("package-yarn.json").toURI()).toPath(), projectDirectory.toPath().resolve("package.json"), StandardCopyOption.REPLACE_EXISTING); properties.put("yarnEnabled", true); properties.put("yarnVersion", "1.15.2"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result3 = runGradle(projectDirectory, GRADLE_CHECK_TASK); + final BuildResult result3 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME); - assertTaskOutcome(result3, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result3, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, GRADLE_CHECK_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result3, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result4 = runGradle(projectDirectory, GRADLE_CHECK_TASK); + final BuildResult result4 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME); - assertTaskOutcome(result4, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, YarnInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result4, GRADLE_CHECK_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.GRADLE_CHECK_TASK_NAME, TaskOutcome.SUCCESS); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/CleanTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/CleanTaskFuncTest.java index bac42137..bbc7d118 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/CleanTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/CleanTaskFuncTest.java @@ -15,14 +15,13 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** * Functional tests to verify the {@link CleanTask} integration in a Gradle build. */ -public class CleanTaskFuncTest { - - private static final String GRADLE_CLEAN_TASK = "clean"; +class CleanTaskFuncTest { @TempDir protected File projectDirectory; @@ -36,39 +35,39 @@ public void shouldCleanFrontendWithNpmOrYarn() throws IOException, URISyntaxExce properties.put("cleanScript", "run clean"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result1 = runGradle(projectDirectory, GRADLE_CLEAN_TASK); + final BuildResult result1 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME); - assertTaskOutcome(result1, NodeInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result1, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, GRADLE_CLEAN_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result1, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result2 = runGradle(projectDirectory, GRADLE_CLEAN_TASK); + final BuildResult result2 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME); - assertTaskOutcome(result2, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result2, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result2, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result2, GRADLE_CLEAN_TASK, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result2, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME, TaskOutcome.UP_TO_DATE); - Files.delete(projectDirectory.toPath().resolve("package-lock.json")); + Files.deleteIfExists(projectDirectory.toPath().resolve("package-lock.json")); Files.copy(new File(getClass().getClassLoader().getResource("package-yarn.json").toURI()).toPath(), projectDirectory.toPath().resolve("package.json"), StandardCopyOption.REPLACE_EXISTING); properties.put("yarnEnabled", true); properties.put("yarnVersion", "1.15.2"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result3 = runGradle(projectDirectory, GRADLE_CLEAN_TASK); + final BuildResult result3 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME); - assertTaskOutcome(result3, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result3, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, GRADLE_CLEAN_TASK, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result3, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result4 = runGradle(projectDirectory, GRADLE_CLEAN_TASK); + final BuildResult result4 = runGradle(projectDirectory, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME); - assertTaskOutcome(result4, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, YarnInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result4, GRADLE_CLEAN_TASK, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.GRADLE_CLEAN_TASK_NAME, TaskOutcome.UP_TO_DATE); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/InstallTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/InstallTaskFuncTest.java index b68b29f8..1e52ae03 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/InstallTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/InstallTaskFuncTest.java @@ -15,14 +15,13 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** * Functional tests to verify the {@link InstallTask} integration in a Gradle build. */ -public class InstallTaskFuncTest { - - private static final String GRADLE_INSTALL_TASK = "installFrontend"; +class InstallTaskFuncTest { @TempDir protected File projectDirectory; @@ -35,35 +34,35 @@ public void shouldInstallFrontendWithNpmOrYarn() throws IOException, URISyntaxEx properties.put("nodeVersion", "10.15.3"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result1 = runGradle(projectDirectory, GRADLE_INSTALL_TASK); + final BuildResult result1 = runGradle(projectDirectory, FrontendGradlePlugin.INSTALL_TASK_NAME); - assertTaskOutcome(result1, NodeInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result1, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result1, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result2 = runGradle(projectDirectory, GRADLE_INSTALL_TASK); + final BuildResult result2 = runGradle(projectDirectory, FrontendGradlePlugin.INSTALL_TASK_NAME); - assertTaskOutcome(result2, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result2, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result2, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result2, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); - Files.delete(projectDirectory.toPath().resolve("package-lock.json")); + Files.deleteIfExists(projectDirectory.toPath().resolve("package-lock.json")); Files.copy(new File(getClass().getClassLoader().getResource("package-yarn.json").toURI()).toPath(), projectDirectory.toPath().resolve("package.json"), StandardCopyOption.REPLACE_EXISTING); properties.put("yarnEnabled", true); properties.put("yarnVersion", "1.15.2"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result3 = runGradle(projectDirectory, GRADLE_INSTALL_TASK); + final BuildResult result3 = runGradle(projectDirectory, FrontendGradlePlugin.INSTALL_TASK_NAME); - assertTaskOutcome(result3, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result3, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result3, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result4 = runGradle(projectDirectory, GRADLE_INSTALL_TASK); + final BuildResult result4 = runGradle(projectDirectory, FrontendGradlePlugin.INSTALL_TASK_NAME); - assertTaskOutcome(result4, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, YarnInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTaskFuncTest.java index 87299e4f..9ba10b61 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/NodeInstallTaskFuncTest.java @@ -14,6 +14,7 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** @@ -21,8 +22,6 @@ */ class NodeInstallTaskFuncTest { - private static final String GRADLE_NODE_INSTALL_TASK = "installNode"; - @TempDir protected File projectDirectory; @@ -30,18 +29,20 @@ class NodeInstallTaskFuncTest { public void shouldFailInstallingNodeWhenVersionIsNotSet() throws IOException { FunctionalTestHelper.createBuildFile(projectDirectory, Collections.emptyMap()); - final BuildResult result = runGradleAndExpectFailure(projectDirectory, GRADLE_NODE_INSTALL_TASK); + final BuildResult result = runGradleAndExpectFailure(projectDirectory, + FrontendGradlePlugin.NODE_INSTALL_TASK_NAME); - assertTaskOutcome(result, NodeInstallTask.DEFAULT_NAME, TaskOutcome.FAILED); + assertTaskOutcome(result, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.FAILED); } @Test public void shouldFailInstallingNodeWhenDistributionCannotBeDownloadedWithUnknownVersion() throws IOException { FunctionalTestHelper.createBuildFile(projectDirectory, Collections.singletonMap("nodeVersion", "0.76.34")); - final BuildResult result = runGradleAndExpectFailure(projectDirectory, GRADLE_NODE_INSTALL_TASK); + final BuildResult result = runGradleAndExpectFailure(projectDirectory, + FrontendGradlePlugin.NODE_INSTALL_TASK_NAME); - assertTaskOutcome(result, NodeInstallTask.DEFAULT_NAME, TaskOutcome.FAILED); + assertTaskOutcome(result, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.FAILED); } @Test @@ -51,9 +52,10 @@ public void shouldFailInstallingNodeWhenDistributionCannotBeDownloadedWithInvali properties.put("nodeDistributionUrl", "protocol://domain/unknown"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result = runGradleAndExpectFailure(projectDirectory, GRADLE_NODE_INSTALL_TASK); + final BuildResult result = runGradleAndExpectFailure(projectDirectory, + FrontendGradlePlugin.NODE_INSTALL_TASK_NAME); - assertTaskOutcome(result, NodeInstallTask.DEFAULT_NAME, TaskOutcome.FAILED); + assertTaskOutcome(result, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.FAILED); } @Test @@ -63,12 +65,12 @@ public void shouldInstallNodeFirstAndNothingMoreSecondly() throws IOException { properties.put("nodeDistributionUrl", getClass().getClassLoader().getResource("node-v10.15.3.zip").toString()); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result1 = runGradle(projectDirectory, GRADLE_NODE_INSTALL_TASK); + final BuildResult result1 = runGradle(projectDirectory, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME); - assertTaskOutcome(result1, NodeInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result2 = runGradle(projectDirectory, GRADLE_NODE_INSTALL_TASK); + final BuildResult result2 = runGradle(projectDirectory, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME); - assertTaskOutcome(result2, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/RunScriptTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/RunScriptTaskFuncTest.java index 222eaa98..4be26312 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/RunScriptTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/RunScriptTaskFuncTest.java @@ -15,6 +15,7 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** @@ -42,19 +43,19 @@ public void shouldRunScriptFrontendWithNpmOrYarn() throws IOException, URISyntax final BuildResult result1 = runGradle(projectDirectory, customTaskName); - assertTaskOutcome(result1, NodeInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result1, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result1, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result1, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); assertTaskOutcome(result1, customTaskName, TaskOutcome.SUCCESS); final BuildResult result2 = runGradle(projectDirectory, customTaskName); - assertTaskOutcome(result2, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result2, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); - assertTaskOutcome(result2, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result2, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result2, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); assertTaskOutcome(result2, customTaskName, TaskOutcome.SUCCESS); - Files.delete(projectDirectory.toPath().resolve("package-lock.json")); + Files.deleteIfExists(projectDirectory.toPath().resolve("package-lock.json")); Files.copy(new File(getClass().getClassLoader().getResource("package-yarn.json").toURI()).toPath(), projectDirectory.toPath().resolve("package.json"), StandardCopyOption.REPLACE_EXISTING); properties.put("yarnEnabled", true); @@ -63,16 +64,16 @@ public void shouldRunScriptFrontendWithNpmOrYarn() throws IOException, URISyntax final BuildResult result3 = runGradle(projectDirectory, customTaskName); - assertTaskOutcome(result3, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result3, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); - assertTaskOutcome(result3, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result3, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result3, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); assertTaskOutcome(result3, customTaskName, TaskOutcome.SUCCESS); final BuildResult result4 = runGradle(projectDirectory, customTaskName); - assertTaskOutcome(result4, NodeInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, YarnInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); - assertTaskOutcome(result4, InstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result4, FrontendGradlePlugin.NODE_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result4, FrontendGradlePlugin.INSTALL_TASK_NAME, TaskOutcome.SUCCESS); assertTaskOutcome(result4, customTaskName, TaskOutcome.SUCCESS); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTaskFuncTest.java b/src/test/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTaskFuncTest.java index d540ac59..0eab5f2b 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTaskFuncTest.java +++ b/src/test/java/org/siouan/frontendgradleplugin/tasks/YarnInstallTaskFuncTest.java @@ -14,6 +14,7 @@ import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.siouan.frontendgradleplugin.FrontendGradlePlugin; import org.siouan.frontendgradleplugin.util.FunctionalTestHelper; /** @@ -21,8 +22,6 @@ */ class YarnInstallTaskFuncTest { - private static final String GRADLE_YARN_INSTALL_TASK = "installYarn"; - @TempDir protected File projectDirectory; @@ -30,18 +29,19 @@ class YarnInstallTaskFuncTest { public void shouldSkipInstallWhenYarnIsNotEnabled() throws IOException { FunctionalTestHelper.createBuildFile(projectDirectory, Collections.emptyMap()); - final BuildResult result = runGradle(projectDirectory, GRADLE_YARN_INSTALL_TASK); + final BuildResult result = runGradle(projectDirectory, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME); - assertTaskOutcome(result, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SKIPPED); + assertTaskOutcome(result, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SKIPPED); } @Test public void shouldFailInstallingYarnWhenVersionIsNotSet() throws IOException { FunctionalTestHelper.createBuildFile(projectDirectory, Collections.singletonMap("yarnEnabled", true)); - final BuildResult result = runGradleAndExpectFailure(projectDirectory, GRADLE_YARN_INSTALL_TASK); + final BuildResult result = runGradleAndExpectFailure(projectDirectory, + FrontendGradlePlugin.YARN_INSTALL_TASK_NAME); - assertTaskOutcome(result, YarnInstallTask.DEFAULT_NAME, TaskOutcome.FAILED); + assertTaskOutcome(result, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.FAILED); } @Test @@ -51,9 +51,10 @@ public void shouldFailInstallingYarnWhenDistributionCannotBeDownloadedWithUnknow properties.put("yarnVersion", "0.56.3"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result = runGradleAndExpectFailure(projectDirectory, GRADLE_YARN_INSTALL_TASK); + final BuildResult result = runGradleAndExpectFailure(projectDirectory, + FrontendGradlePlugin.YARN_INSTALL_TASK_NAME); - assertTaskOutcome(result, YarnInstallTask.DEFAULT_NAME, TaskOutcome.FAILED); + assertTaskOutcome(result, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.FAILED); } @Test @@ -64,9 +65,10 @@ public void shouldFailInstallingYarnWhenDistributionCannotBeDownloadedWithInvali properties.put("yarnDistributionUrl", "protocol://domain/unknown"); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result = runGradleAndExpectFailure(projectDirectory, GRADLE_YARN_INSTALL_TASK); + final BuildResult result = runGradleAndExpectFailure(projectDirectory, + FrontendGradlePlugin.YARN_INSTALL_TASK_NAME); - assertTaskOutcome(result, YarnInstallTask.DEFAULT_NAME, TaskOutcome.FAILED); + assertTaskOutcome(result, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.FAILED); } @Test @@ -78,12 +80,12 @@ public void shouldInstallYarnFirstAndNothingMoreSecondly() throws IOException { .put("yarnDistributionUrl", getClass().getClassLoader().getResource("yarn-v1.15.2.tar.gz").toString()); FunctionalTestHelper.createBuildFile(projectDirectory, properties); - final BuildResult result1 = runGradle(projectDirectory, GRADLE_YARN_INSTALL_TASK); + final BuildResult result1 = runGradle(projectDirectory, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME); - assertTaskOutcome(result1, YarnInstallTask.DEFAULT_NAME, TaskOutcome.SUCCESS); + assertTaskOutcome(result1, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.SUCCESS); - final BuildResult result2 = runGradle(projectDirectory, GRADLE_YARN_INSTALL_TASK); + final BuildResult result2 = runGradle(projectDirectory, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME); - assertTaskOutcome(result2, YarnInstallTask.DEFAULT_NAME, TaskOutcome.UP_TO_DATE); + assertTaskOutcome(result2, FrontendGradlePlugin.YARN_INSTALL_TASK_NAME, TaskOutcome.UP_TO_DATE); } } diff --git a/src/test/java/org/siouan/frontendgradleplugin/util/FunctionalTestHelper.java b/src/test/java/org/siouan/frontendgradleplugin/util/FunctionalTestHelper.java index 4bf790af..1b250fec 100644 --- a/src/test/java/org/siouan/frontendgradleplugin/util/FunctionalTestHelper.java +++ b/src/test/java/org/siouan/frontendgradleplugin/util/FunctionalTestHelper.java @@ -7,8 +7,10 @@ import java.io.IOException; import java.util.Collections; import java.util.Map; +import java.util.Optional; import org.gradle.testkit.runner.BuildResult; +import org.gradle.testkit.runner.BuildTask; import org.gradle.testkit.runner.GradleRunner; import org.gradle.testkit.runner.TaskOutcome; @@ -33,7 +35,8 @@ private FunctionalTestHelper() { */ public static void assertTaskOutcome(final BuildResult result, final String taskName, final TaskOutcome expectedOutcome) { - assertThat(result.task(':' + taskName).getOutcome()).isEqualTo(expectedOutcome); + assertThat(Optional.ofNullable(result.task(':' + taskName)).map(BuildTask::getOutcome) + .orElseThrow(() -> new RuntimeException("Task not found: " + taskName))).isEqualTo(expectedOutcome); } /** diff --git a/src/test/resources/package-npm.json b/src/test/resources/package-npm.json index 108433e2..84ff9a6a 100644 --- a/src/test/resources/package-npm.json +++ b/src/test/resources/package-npm.json @@ -1,21 +1,21 @@ { - "name": "frontend-gradle-plugin-npm-tests", - "version": "1.0.0", - "description": "Empty project to test frontend scripts integration in a Gradle build with NPM", - "private": true, - "license": "UNLICENSED", - "cacheDirectories": [ - "node_modules" - ], - "dependencies": { - }, - "scripts": { - "another-script": "echo 'Another frontend script executed'", - "assemble": "echo 'Frontend assembled'", - "check": "npm run lint && npm run test", - "clean": "echo 'Frontend cleaned'", - "lint": "echo 'Frontend linted'", - "start": "echo 'Frontend started and stopped'", - "test": "echo 'Frontend tested'" - } + "name": "frontend-gradle-plugin-npm-tests", + "version": "1.0.0", + "description": "Empty project to test frontend scripts integration in a Gradle build with NPM", + "private": true, + "license": "UNLICENSED", + "cacheDirectories": [ + "node_modules" + ], + "dependencies": { + }, + "scripts": { + "another-script": "echo 'Another frontend script executed'", + "assemble": "echo 'Frontend assembled'", + "check": "npm run lint && npm run test", + "clean": "echo 'Frontend cleaned'", + "lint": "echo 'Frontend linted'", + "start": "echo 'Frontend started and stopped'", + "test": "echo 'Frontend tested'" + } } diff --git a/src/test/resources/package-yarn.json b/src/test/resources/package-yarn.json index 84c7d70d..d9d5455a 100644 --- a/src/test/resources/package-yarn.json +++ b/src/test/resources/package-yarn.json @@ -1,21 +1,21 @@ { - "name": "frontend-gradle-plugin-npm-tests", - "version": "1.0.0", - "description": "Empty project to test frontend scripts integration in a Gradle build with NPM", - "private": true, - "license": "UNLICENSED", - "cacheDirectories": [ - "node_modules" - ], - "dependencies": { - }, - "scripts": { - "another-script": "echo 'Another frontend script executed'", - "assemble": "echo 'Frontend assembled'", - "check": "yarn run lint && yarn run test", - "clean": "echo 'Frontend cleaned'", - "lint": "echo 'Frontend linted'", - "start": "echo 'Frontend started and stopped'", - "test": "echo 'Frontend tested'" - } + "name": "frontend-gradle-plugin-npm-tests", + "version": "1.0.0", + "description": "Empty project to test frontend scripts integration in a Gradle build with NPM", + "private": true, + "license": "UNLICENSED", + "cacheDirectories": [ + "node_modules" + ], + "dependencies": { + }, + "scripts": { + "another-script": "echo 'Another frontend script executed'", + "assemble": "echo 'Frontend assembled'", + "check": "yarn run lint && yarn run test", + "clean": "echo 'Frontend cleaned'", + "lint": "echo 'Frontend linted'", + "start": "echo 'Frontend started and stopped'", + "test": "echo 'Frontend tested'" + } }