Skip to content

Commit

Permalink
Move the feature to check for file changes into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
scroix committed Mar 29, 2024
1 parent 027a129 commit d65be71
Showing 1 changed file with 38 additions and 48 deletions.
86 changes: 38 additions & 48 deletions nodel-webui-js/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,53 +52,43 @@ node {
npmInstallCommand = System.getenv("CI") ? 'ci' : 'install'
}

tasks.register('checkNodeModules') {
def nodeModulesDir = file("${projectDir}/node_modules")
outputs.upToDateWhen { nodeModulesDir.exists() }
}

def srcDir = file('src')
def packageJson = file('package.json')
def packageLockJson = file('package-lock.json')
def gruntFile = file('Gruntfile.js')

def filesChanged = false

static def checkFilesChanged(Project project) {
def nodeModulesDir = project.file("${project.projectDir}/node_modules")
def srcDir = project.file('src')
def packageJson = project.file('package.json')
def packageLockJson = project.file('package-lock.json')
def gruntFile = project.file('Gruntfile.js')

if (!nodeModulesDir.exists()) {
// println "node_modules directory does not exist. Setting filesChanged to true."
return true
} else {
def srcDirModified = srcDir.exists() && srcDir.lastModified() > nodeModulesDir.lastModified()
def packageJsonModified = packageJson.exists() && packageJson.lastModified() > nodeModulesDir.lastModified()
def packageLockJsonModified = packageLockJson.exists() && packageLockJson.lastModified() > nodeModulesDir.lastModified()
def gruntFileModified = gruntFile.exists() && gruntFile.lastModified() > nodeModulesDir.lastModified()

def filesChanged = srcDirModified || packageJsonModified || packageLockJsonModified || gruntFileModified

// println "srcDirModified: ${srcDirModified}"
// println "packageJsonModified: ${packageJsonModified}"
// println "packageLockJsonModified: ${packageLockJsonModified}"
// println "gruntFileModified: ${gruntFileModified}"
// println "filesChanged: ${filesChanged}"

return filesChanged
class FileChecker {
def nodeModulesDir
def srcDir
def packageJson
def packageLockJson
def gruntFile

FileChecker(Project project) {
nodeModulesDir = project.file("${project.projectDir}/node_modules")
srcDir = project.file('src')
packageJson = project.file('package.json')
packageLockJson = project.file('package-lock.json')
gruntFile = project.file('Gruntfile.js')
}
}

tasks.named('clean', Delete) {
delete "${projectDir}/node_modules/"
delete "${projectDir}/build/grunt/"
def checkFilesChanged() {
if (!nodeModulesDir.exists()) {
return true
} else {
def srcDirModified = srcDir.exists() && srcDir.lastModified() > nodeModulesDir.lastModified()
def packageJsonModified = packageJson.exists() && packageJson.lastModified() > nodeModulesDir.lastModified()
def packageLockJsonModified = packageLockJson.exists() && packageLockJson.lastModified() > nodeModulesDir.lastModified()
def gruntFileModified = gruntFile.exists() && gruntFile.lastModified() > nodeModulesDir.lastModified()

def filesChanged = srcDirModified || packageJsonModified || packageLockJsonModified || gruntFileModified

return filesChanged
}
}
}

def fileChecker = new FileChecker(project)



tasks.register('installGruntCli', NpmTask) {
def shouldInstall = checkFilesChanged(project)
def shouldInstall = fileChecker.checkFilesChanged()

if (shouldInstall) {
dependsOn clean
Expand All @@ -113,8 +103,7 @@ tasks.register('installGruntCli', NpmTask) {
}

tasks.register('gruntRun', NpxTask) {
def shouldInstall = checkFilesChanged(project)

def shouldInstall = fileChecker.checkFilesChanged()

dependsOn 'installGruntCli'
command = "grunt"
Expand All @@ -126,6 +115,11 @@ tasks.register('gruntRun', NpxTask) {
onlyIf { shouldInstall }
}

tasks.named('clean', Delete) {
delete "${projectDir}/node_modules/"
delete "${projectDir}/build/grunt/"
}

tasks.register('copyContent', Copy) {
dependsOn gruntRun
from file("${project.buildDir}/grunt")
Expand Down Expand Up @@ -174,10 +168,6 @@ tasks.named('compileJava') {
dependsOn zipContentInterface, copyBuildInfo
}

tasks.named('processResources') {
dependsOn zipContentInterface, copyBuildInfo
}

dependencies {
implementation 'joda-time:joda-time:2.10.10'
implementation 'org.joda:joda-convert:2.2.1'
Expand Down

0 comments on commit d65be71

Please sign in to comment.