@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
97 lines (84 loc) • 3.86 kB
JavaScript
const fs = require('fs')
const cds = require('../../../cds')
const { exists, isdir, path } = cds.utils
const { FILE_EXT_CDS, CONTENT_LANGUAGE_BUNDLES, CONTENT_DEFAULT_CSN, DEFAULT_CSN_FILE_NAME, FLAVOR_LOCALIZED_EDMX, CONTENT_EDMX, CONTENT_LOCALIZED_EDMX } = require('../../constants')
const { getI18nDefaultFolder, normalizePath } = require('../../util')
const DEFAULT_COMPILE_DEST_FOLDER = path.normalize('src/main/resources/edmx')
const DEBUG = cds.debug('cli|build')
const LOG = cds.log('cli|build')
module.exports = class JavaBuildPlugin extends require('../edmx') {
static get taskDefaults() { return { src: normalizePath(cds.env.folders.srv) } }
static hasTask() {
const src = path.join(cds.root, this.taskDefaults?.src ?? 'srv')
const hasSrc = exists(src) || this.taskDefaults?.src === '.'
return cds.env['project-nature'] === 'java' && hasSrc && !cds.env.extends
}
init() {
super.init()
this.task.options.compileDest = path.resolve(this.task.dest, this.task.options.compileDest || DEFAULT_COMPILE_DEST_FOLDER)
}
async build() {
const compileDest = this.task.options.compileDest
const { src, dest } = this.task
const model = await this.model()
if (!model) {
return
}
await this._compileForJava(model, compileDest)
if (!this.hasBuildOption(CONTENT_EDMX, false)) {
// generate edmx files containing all features - localized edmx for backward compatibility
let localized = this.hasBuildOption(CONTENT_LOCALIZED_EDMX, true)
// Force localized to true for OData v2
if (cds.env.odata.version === 'v2') {
if (!localized) {
LOG.info('Forcing localized EDMX generation for OData v2')
localized = true
}
}
if (localized) {
await this.compileToEdmx(model, compileDest, { [FLAVOR_LOCALIZED_EDMX]: true })
}
// non-localized edmx
await this.compileToEdmx(model, path.join(compileDest, 'odata', cds.env.odata.version), { [FLAVOR_LOCALIZED_EDMX]: this.hasBuildOption(FLAVOR_LOCALIZED_EDMX, true) })
}
if (!this.hasBuildOption(CONTENT_LANGUAGE_BUNDLES, false)) {
// collect and write language bundles containing all features
await this.collectLanguageBundles(model, path.join(compileDest, getI18nDefaultFolder()))
}
if (this.isStagingBuild()) {
await this._copyNativeContent(src, dest)
}
}
async clean() {
if (this.isStagingBuild()) {
return super.clean()
}
DEBUG?.(`Deleting build target folder ${this.task.options.compileDest}`)
await fs.promises.rm(this.task.options.compileDest, { force: true, recursive: true })
}
async _copyNativeContent(src, dest) {
return super.copyNativeContent(src, dest, (entry) => {
if (isdir(entry)) {
return true // using common filter for folders
} else {
const extname = path.extname(entry)
return extname !== FILE_EXT_CDS
}
})
}
async _compileForJava(model, csnDest, compileOptions = {}) {
// csn for service providers
const m = cds.compile.for.java(model, {
...this._options4odata(),
...compileOptions
})
const csnFile = path.join(csnDest, DEFAULT_CSN_FILE_NAME)
// adding csn to build result containing @source and _where persisted properties
if (this.hasBuildOption(CONTENT_DEFAULT_CSN, true)) { //default true or undefined
await this.compileToJson(model, csnFile)
} else {
await this.compileToJson(m, csnFile)
}
return m
}
}