@sap/cds-dk
Version:
Command line client and development toolkit for the SAP Cloud Application Programming Model
81 lines (63 loc) • 3.04 kB
JavaScript
const cds = require('../../../cds')
const { exists, path, readFileSync, yaml } = cds.utils
const { Plugin, BuildError } = require('../../../build')
const { join } = path
const { readProject } = require('../../../init/projectReader')
module.exports = class HelmBuildPlugin extends Plugin {
static taskDefaults = { src: "." }
static hasTask() {
if (exists('chart/Chart.yaml')) {
const chartFile = readFileSync(join(cds.root, 'chart', 'Chart.yaml'), 'utf8')
const chartYaml = yaml.parseDocument(chartFile).toJS()
return chartYaml.annotations?.["app.kubernetes.io/managed-by"] === 'cds-dk/helm'
}
return false
}
init() {
if (this.task.src !== cds.root) {
throw new BuildError("Invalid value for property 'src', it must have value '.'")
}
// different from the default build output structure
this.task.dest = join(cds.root, cds.env.build.target !== '.' ? cds.env.build.target : 'gen', 'chart')
}
async build() {
if(!HelmBuildPlugin.hasTask()) {
this.pushMessage("Helm Chart not found. Execute `cds add helm` to add the helm chart.", Plugin.WARNING)
return
}
const { hasApprouter, hasConnectivity, hasHtml5Repo } = readProject()
const chartFile = readFileSync(join(cds.root, 'chart', 'Chart.yaml'), 'utf8')
const chartYaml = yaml.parseDocument(chartFile).toJS()
const isInternal = chartYaml.dependencies?.find(dep => dep.name === 'web-application')?.repository !== undefined
if (!isInternal) {
// copy web appplication helm chart
await this.copy(join(__dirname, 'files', 'subcharts', 'web-application')).to('charts/web-application')
// copy service instance helm chart
if (this._hasSubchart('service-instance')) {
await this.copy(join(__dirname, 'files', 'subcharts', 'service-instance')).to('charts/service-instance')
}
// copy content deployment helm chart
if (this._hasSubchart('content-deployment')) {
await this.copy(join(__dirname, 'files', 'subcharts', 'content-deployment')).to('charts/content-deployment')
}
}
// copy templates
await this.copy(join(__dirname, 'files', 'templates')).to('templates')
const pluginResourcePath = join(__dirname, 'files', 'plugins')
if (hasApprouter) {
await this.copy(join(pluginResourcePath, 'approuter/approuter-configmap.yaml')).to('templates/approuter-configmap.yaml')
}
if (hasConnectivity) {
await this.copy(join(pluginResourcePath, 'connectivity/connectivity-proxy-info.yaml')).to('templates/connectivity-proxy-info.yaml')
}
if (hasHtml5Repo) {
await this.copy(join(pluginResourcePath, 'html5-repo/html5-apps-deployer-configmap.yaml')).to('templates/html5-apps-deployer-configmap.yaml')
}
// copy chart folder to gen/chart
await this.copy(join(cds.root, 'chart')).to(".")
}
_hasSubchart(subchart) {
const chart = exists('chart/Chart.yaml') ? readFileSync(join(cds.root, 'chart/Chart.yaml'), 'utf8') : ''
return chart.includes(subchart)
}
}