mirror of
				https://github.com/datahub-project/datahub.git
				synced 2025-10-31 10:49:00 +00:00 
			
		
		
		
	 04d0a50118
			
		
	
	
		04d0a50118
		
			
		
	
	
	
	
		
			
			Co-authored-by: Esteban Gutierrez <esteban.gutierrez@acryl.io> Co-authored-by: Chris Collins <chriscollins3456@gmail.com>
		
			
				
	
	
		
			294 lines
		
	
	
		
			8.1 KiB
		
	
	
	
		
			Groovy
		
	
	
	
	
	
			
		
		
	
	
			294 lines
		
	
	
		
			8.1 KiB
		
	
	
	
		
			Groovy
		
	
	
	
	
	
| import java.nio.file.FileSystems
 | |
| import java.nio.file.Paths
 | |
| 
 | |
| plugins {
 | |
|   id 'java'
 | |
|   id 'distribution'
 | |
|   id 'com.github.node-gradle.node'
 | |
| }
 | |
| 
 | |
| apply from: '../gradle/versioning/versioning.gradle'
 | |
| 
 | |
| node {
 | |
| 
 | |
|   // If true, it will download node using above parameters.
 | |
|   // If false, it will try to use globally installed node.
 | |
| 
 | |
|   if (project.hasProperty('useSystemNode')) {
 | |
|     download = ! project.getProperty('useSystemNode').toBoolean()
 | |
|   } else {
 | |
|     download = true
 | |
|   }
 | |
| 
 | |
|   // Version of node to use.
 | |
|   version = '22.16.0'
 | |
| 
 | |
|   // Version of Yarn to use.
 | |
|   yarnVersion = '1.22.22'
 | |
| 
 | |
|   // Base URL for fetching node distributions (set nodeDistBaseUrl if you have a mirror).
 | |
|   if (project.hasProperty('nodeDistBaseUrl')) {
 | |
|     distBaseUrl = project.getProperty('nodeDistBaseUrl')
 | |
|   } else {
 | |
|     distBaseUrl = 'https://nodejs.org/dist'
 | |
|   }
 | |
| 
 | |
|   // Set the work directory for unpacking node
 | |
|   workDir = file("${project.projectDir}/.gradle/nodejs")
 | |
| 
 | |
|   // Set the work directory for NPM
 | |
|   yarnWorkDir = file("${project.projectDir}/.gradle/yarn")
 | |
| 
 | |
|   // Set the work directory where node_modules should be located
 | |
|   nodeProjectDir = file("${project.projectDir}")
 | |
| 
 | |
| }
 | |
| 
 | |
| /*
 | |
|   Wrappers around Yarn Tasks.
 | |
|  */
 | |
| task yarnInstall(type: YarnTask) {
 | |
|   args = ['install', '--network-timeout', '300000']
 | |
| 
 | |
|   // The node_modules directory can contain built artifacts, so
 | |
|   // it's not really safe to cache it.
 | |
|   outputs.cacheIf { false }
 | |
| 
 | |
|   inputs.files(
 | |
|     file('yarn.lock'),
 | |
|     file('package.json'),
 | |
|   )
 | |
|   outputs.dir('node_modules')
 | |
| }
 | |
| 
 | |
| task yarnGenerate(type: YarnTask, dependsOn: yarnInstall) {
 | |
|   args = ['run', 'generate']
 | |
| 
 | |
|   outputs.cacheIf { true }
 | |
| 
 | |
|   inputs.files(
 | |
|     yarnInstall.inputs.files,
 | |
|     file('codegen.yml'),
 | |
|     project.fileTree(dir: "../datahub-graphql-core/src/main/resources/", include: "*.graphql"),
 | |
|     project.fileTree(dir: "src", include: "**/*.graphql"),
 | |
|   )
 | |
| 
 | |
|   outputs.files(
 | |
|     project.fileTree(dir: "src", include: "**/*.generated.ts"),
 | |
|   )
 | |
| 
 | |
|   doFirst{
 | |
|     delete fileTree(dir: 'src', include: '**/*.generated.ts')
 | |
|   }
 | |
| }
 | |
| 
 | |
| task yarnServe(type: YarnTask, dependsOn: [yarnInstall, yarnGenerate]) {
 | |
|   environment.put('REACT_APP_PROXY_TARGET', 'http://localhost:9001')
 | |
|   args = ['run', 'start']
 | |
| }
 | |
| 
 | |
| task yarnPreview(type: YarnTask, dependsOn: [yarnInstall, yarnGenerate]) {
 | |
|   environment.put('REACT_APP_PROXY_TARGET', project.findProperty('proxy') ?: 'http://localhost:9001')
 | |
|   args = ['run', 'preview']
 | |
| }
 | |
| 
 | |
| task yarnTest(type: YarnTask, dependsOn: [yarnInstall, yarnGenerate]) {
 | |
|   // Explicitly runs in non-watch mode.
 | |
|   args = ['run', project.hasProperty('withCoverage') ? 'test-coverage' : 'test', 'run']
 | |
|   def test_sentinel = "${buildDir}/.yarn-test-sentinel"
 | |
|   outputs.file(test_sentinel)
 | |
|   inputs.files(project.fileTree(dir: 'src', include: ['**/*.ts', '**/*.tsx']))
 | |
|   doLast {
 | |
|     // touch a file with name yarn-lint.txt in the build directory
 | |
|     def file = file(test_sentinel)
 | |
|     if (!file.exists()) {
 | |
|       file.createNewFile()
 | |
|     } else {
 | |
|       file.setLastModified(System.currentTimeMillis())
 | |
|     }
 | |
|   }
 | |
|   outputs.cacheIf { true }
 | |
| }
 | |
| 
 | |
| task yarnLint(type: YarnTask, dependsOn: [yarnInstall, yarnGenerate]) {
 | |
|   def targetFile = project.findProperty('file') ?: ''
 | |
| 
 | |
|   if (targetFile.isEmpty()) {
 | |
|     // Run on all files
 | |
|     args = ['run', 'lint']
 | |
|   } else {
 | |
|     // Run on specific file - run lint, format-check, and type-check
 | |
|     doFirst {
 | |
|       // Run lint-file first
 | |
|       exec {
 | |
|         workingDir projectDir
 | |
|         commandLine 'yarn', 'run', 'lint-file', targetFile
 | |
|       }
 | |
|       // Then run format-check-file
 | |
|       exec {
 | |
|         workingDir projectDir
 | |
|         commandLine 'yarn', 'run', 'format-check-file', targetFile
 | |
|       }
 | |
|       // This does not run full type-check when we run for a single file
 | |
|       // as that would take too long
 | |
|     }
 | |
|     outputs.cacheIf { false }
 | |
|   }
 | |
| }
 | |
| 
 | |
| test.dependsOn([yarnInstall, yarnTest, yarnLint])
 | |
| 
 | |
| task yarnLintFix(type: YarnTask, dependsOn: [yarnInstall, yarnGenerate]) {
 | |
|   def targetFile = project.findProperty('file') ?: ''
 | |
| 
 | |
|   if (targetFile.isEmpty()) {
 | |
|     // Run on all files
 | |
|     args = ['run', 'lint-fix']
 | |
|     def lint_sentinel = "${buildDir}/.yarn-lint-sentinel"
 | |
|     outputs.file(lint_sentinel)
 | |
|     inputs.files(project.fileTree(dir: 'src', include: ['**/*.ts', '**/*.tsx']))
 | |
|     doLast {
 | |
|       // touch a file with name yarn-lint.txt in the build directory
 | |
|       def file = file(lint_sentinel)
 | |
|       if (!file.exists()) {
 | |
|         file.createNewFile()
 | |
|       } else {
 | |
|         file.setLastModified(System.currentTimeMillis())
 | |
|       }
 | |
|     }
 | |
|     outputs.cacheIf { true }
 | |
|   } else {
 | |
|     // Run on specific file - need to run both lint-fix and format
 | |
|     doFirst {
 | |
|       // Run lint-fix-file first
 | |
|       exec {
 | |
|         workingDir projectDir
 | |
|         commandLine 'yarn', 'run', 'lint-fix-file', targetFile
 | |
|       }
 | |
|       // Then run format-file
 | |
|       exec {
 | |
|         workingDir projectDir
 | |
|         commandLine 'yarn', 'run', 'format-file', targetFile
 | |
|       }
 | |
|     }
 | |
|     outputs.cacheIf { false }
 | |
|   }
 | |
| }
 | |
| 
 | |
| task yarnBuild(type: YarnTask, dependsOn: [yarnInstall, yarnGenerate]) {
 | |
|   // Pass DataHub version via environment variable to vite build
 | |
|   environment.put('DATAHUB_APP_VERSION', project.version)
 | |
| 
 | |
|   args = ['run', project.hasProperty('sourcemap') ? 'buildWithSourceMap' :'build']
 | |
| 
 | |
|   outputs.cacheIf { true }
 | |
|   inputs.files(
 | |
|     file('index.html'),
 | |
|     project.fileTree(dir: "src"),
 | |
|     project.fileTree(dir: "public"),
 | |
| 
 | |
|     yarnInstall.inputs.files,
 | |
|     yarnGenerate.outputs.files,
 | |
| 
 | |
|     file('.env'),
 | |
|     file('vite.config.ts'),
 | |
|     file('tsconfig.json'),
 | |
|   )
 | |
|   outputs.dir('dist')
 | |
| }
 | |
| 
 | |
| task copyCapabilitySummary(type: Copy) {
 | |
|   def sourceFile = file('../metadata-ingestion/src/datahub/ingestion/autogenerated/capability_summary.json')
 | |
| 
 | |
|   if (!sourceFile.exists()) {
 | |
|     // We don't want frontend devs to have to run this task
 | |
|     // But still keeping it here to make sure the dependency is there properly in gradle
 | |
|     dependsOn ':metadata-ingestion:capabilitySummary'
 | |
|   }
 | |
| 
 | |
|   from sourceFile
 | |
|   into 'public/assets/ingestion'
 | |
| 
 | |
|   inputs.file(sourceFile)
 | |
|   outputs.file('public/assets/ingestion/capability_summary.json')
 | |
| }
 | |
| 
 | |
| yarnBuild.dependsOn copyCapabilitySummary
 | |
| 
 | |
| // Define a list of configurations for prettier tasks
 | |
| def externalPrettierConfigs = [
 | |
|     [
 | |
|         name: 'graphql',
 | |
|         pattern: 'datahub-graphql-core/src/main/resources/**/*.graphql'
 | |
|     ],
 | |
|     [
 | |
|         name: 'githubActions',
 | |
|         pattern: '.github/**/*.{yml,yaml}'
 | |
|     ],
 | |
|     [
 | |
|         name: 'md',
 | |
|         pattern: '**/*.md'
 | |
|     ]
 | |
| ]
 | |
| 
 | |
| // Iterate through the configurations to create check and write tasks
 | |
| externalPrettierConfigs.each { config ->
 | |
|   tasks.register("${config.name}PrettierCheck", YarnTask) {
 | |
|     dependsOn(tasks.named('yarnInstall'))
 | |
|     args = ['run', 'prettier', '--check', "../${config.pattern}"]
 | |
|   }
 | |
| 
 | |
|   tasks.register("${config.name}PrettierWrite", YarnTask) {
 | |
|     dependsOn(tasks.named('yarnInstall'))
 | |
|     args = ['run', 'prettier', '--write', "../${config.pattern}"]
 | |
|   }
 | |
| 
 | |
|   tasks.register("${config.name}PrettierWriteChanged", YarnTask) {
 | |
|     // Get the list of changed Markdown files using Git
 | |
|     def changedFiles = new ByteArrayOutputStream()
 | |
|     exec {
 | |
|       commandLine 'git', 'diff', '--name-only', '--diff-filter=ACMRT', 'HEAD'
 | |
|       standardOutput = changedFiles
 | |
|     }
 | |
|     def allChangedFiles = changedFiles.toString().trim().split('\n').findAll { it != '' }
 | |
|     def matcher = FileSystems.getDefault().getPathMatcher("glob:${config.pattern}")
 | |
|     def matchingFiles = allChangedFiles.findAll { file -> matcher.matches(Paths.get(file)) }
 | |
| 
 | |
|     if (matchingFiles.isEmpty()) {
 | |
|       logger.lifecycle("No changed files found matching ${config.pattern}.")
 | |
|       return
 | |
|     }
 | |
| 
 | |
|     logger.lifecycle("Running Prettier on ${matchingFiles.size()} changed ${config.name} files")
 | |
|     args = ['run', 'prettier', '--write'] + matchingFiles.collect { "../${it}" }
 | |
|   }
 | |
| }
 | |
| 
 | |
| clean {
 | |
|   delete 'node_modules/.yarn-integrity'
 | |
|   delete 'dist'
 | |
|   delete 'tmp'
 | |
|   delete 'just'
 | |
|   delete fileTree(dir: 'src', include: '**/*.generated.ts')
 | |
|   delete 'public/assets/capability_summary.json'
 | |
| }
 | |
| 
 | |
| configurations {
 | |
|   assets
 | |
| }
 | |
| 
 | |
| distZip {
 | |
|   dependsOn yarnBuild
 | |
|   archiveFileName = "datahub-web-react-${archiveVersion}.${archiveExtension}"
 | |
|   from 'dist'
 | |
| }
 | |
| 
 | |
| jar {
 | |
|   dependsOn yarnBuild
 | |
|   into('public') {
 | |
|     from 'dist'
 | |
|   }
 | |
|   archiveClassifier = 'assets'
 | |
| }
 | |
| build.dependsOn jar
 |