2020-02-25 15:33:34 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-09-23 15:33:30 +02:00
|
|
|
"encoding/base64"
|
2020-02-25 15:33:34 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-23 15:33:30 +02:00
|
|
|
"net/http"
|
2020-03-11 16:15:00 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
2021-12-14 15:43:02 +02:00
|
|
|
"path/filepath"
|
2021-10-20 16:03:16 +02:00
|
|
|
"strconv"
|
2020-03-11 16:15:00 +02:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
2021-11-16 16:02:57 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/buildsettings"
|
2020-07-31 15:41:28 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/npm"
|
|
|
|
|
2020-02-25 15:33:34 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
|
|
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/maven"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2020-05-05 15:54:50 +02:00
|
|
|
"github.com/ghodss/yaml"
|
2021-09-23 15:33:30 +02:00
|
|
|
"github.com/pkg/errors"
|
2020-02-25 15:33:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const templateMtaYml = `_schema-version: "3.1"
|
|
|
|
ID: "{{.ID}}"
|
|
|
|
version: {{.Version}}
|
|
|
|
|
|
|
|
parameters:
|
|
|
|
hcp-deployer-version: "1.1.0"
|
|
|
|
|
|
|
|
modules:
|
|
|
|
- name: {{.ApplicationName}}
|
|
|
|
type: html5
|
|
|
|
path: .
|
|
|
|
parameters:
|
|
|
|
version: {{.Version}}-${timestamp}
|
|
|
|
name: {{.ApplicationName}}
|
|
|
|
build-parameters:
|
|
|
|
builder: grunt
|
|
|
|
build-result: dist`
|
|
|
|
|
|
|
|
// MTABuildTarget ...
|
|
|
|
type MTABuildTarget int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NEO ...
|
|
|
|
NEO MTABuildTarget = iota
|
|
|
|
// CF ...
|
|
|
|
CF MTABuildTarget = iota
|
|
|
|
//XSA ...
|
|
|
|
XSA MTABuildTarget = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValueOfBuildTarget ...
|
|
|
|
func ValueOfBuildTarget(str string) (MTABuildTarget, error) {
|
|
|
|
switch str {
|
|
|
|
case "NEO":
|
|
|
|
return NEO, nil
|
|
|
|
case "CF":
|
|
|
|
return CF, nil
|
|
|
|
case "XSA":
|
|
|
|
return XSA, nil
|
|
|
|
default:
|
2021-05-19 08:45:38 +02:00
|
|
|
return -1, fmt.Errorf("Unknown Platform: '%s'", str)
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String ...
|
|
|
|
func (m MTABuildTarget) String() string {
|
|
|
|
return [...]string{
|
|
|
|
"NEO",
|
|
|
|
"CF",
|
|
|
|
"XSA",
|
|
|
|
}[m]
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
type mtaBuildUtils interface {
|
|
|
|
maven.Utils
|
|
|
|
|
|
|
|
SetEnv(env []string)
|
|
|
|
AppendEnv(env []string)
|
|
|
|
|
|
|
|
Abs(path string) (string, error)
|
|
|
|
FileRead(path string) ([]byte, error)
|
|
|
|
FileWrite(path string, content []byte, perm os.FileMode) error
|
|
|
|
|
|
|
|
DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string) error
|
|
|
|
|
|
|
|
SetNpmRegistries(defaultNpmRegistry string) error
|
|
|
|
InstallAllDependencies(defaultNpmRegistry string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type mtaBuildUtilsBundle struct {
|
|
|
|
*command.Command
|
|
|
|
*piperutils.Files
|
|
|
|
*piperhttp.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bundle *mtaBuildUtilsBundle) SetNpmRegistries(defaultNpmRegistry string) error {
|
|
|
|
npmExecutorOptions := npm.ExecutorOptions{DefaultNpmRegistry: defaultNpmRegistry, ExecRunner: bundle}
|
|
|
|
npmExecutor := npm.NewExecutor(npmExecutorOptions)
|
|
|
|
return npmExecutor.SetNpmRegistries()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bundle *mtaBuildUtilsBundle) InstallAllDependencies(defaultNpmRegistry string) error {
|
|
|
|
npmExecutorOptions := npm.ExecutorOptions{DefaultNpmRegistry: defaultNpmRegistry, ExecRunner: bundle}
|
|
|
|
npmExecutor := npm.NewExecutor(npmExecutorOptions)
|
|
|
|
return npmExecutor.InstallAllDependencies(npmExecutor.FindPackageJSONFiles())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bundle *mtaBuildUtilsBundle) DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string) error {
|
|
|
|
return maven.DownloadAndCopySettingsFiles(globalSettingsFile, projectSettingsFile, bundle)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMtaBuildUtilsBundle() mtaBuildUtils {
|
|
|
|
utils := mtaBuildUtilsBundle{
|
|
|
|
Command: &command.Command{},
|
|
|
|
Files: &piperutils.Files{},
|
|
|
|
Client: &piperhttp.Client{},
|
|
|
|
}
|
|
|
|
utils.Stdout(log.Writer())
|
|
|
|
utils.Stderr(log.Writer())
|
|
|
|
return &utils
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:33:34 +02:00
|
|
|
func mtaBuild(config mtaBuildOptions,
|
|
|
|
telemetryData *telemetry.CustomData,
|
|
|
|
commonPipelineEnvironment *mtaBuildCommonPipelineEnvironment) {
|
|
|
|
log.Entry().Debugf("Launching mta build")
|
2020-11-10 18:14:55 +02:00
|
|
|
utils := newMtaBuildUtilsBundle()
|
2020-06-18 17:30:17 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err := runMtaBuild(config, commonPipelineEnvironment, utils)
|
2020-02-25 15:33:34 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().
|
|
|
|
WithError(err).
|
|
|
|
Fatal("failed to execute mta build")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runMtaBuild(config mtaBuildOptions,
|
|
|
|
commonPipelineEnvironment *mtaBuildCommonPipelineEnvironment,
|
2020-11-10 18:14:55 +02:00
|
|
|
utils mtaBuildUtils) error {
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err = handleSettingsFiles(config, utils)
|
2020-05-15 14:20:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-25 15:33:34 +02:00
|
|
|
|
2021-09-23 15:33:30 +02:00
|
|
|
err = handleActiveProfileUpdate(config, utils)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
err = utils.SetNpmRegistries(config.DefaultNpmRegistry)
|
2020-02-25 15:33:34 +02:00
|
|
|
|
2021-12-14 15:43:02 +02:00
|
|
|
mtaYamlFile := filepath.Join(getSourcePath(config), "mta.yaml")
|
2020-11-10 18:14:55 +02:00
|
|
|
mtaYamlFileExists, err := utils.FileExists(mtaYamlFile)
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mtaYamlFileExists {
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
if err = createMtaYamlFile(mtaYamlFile, config.ApplicationName, utils); err != nil {
|
2020-02-25 15:33:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
log.Entry().Infof("\"%s\" file found in project sources", mtaYamlFile)
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
if err = setTimeStamp(mtaYamlFile, utils); err != nil {
|
2020-02-25 15:33:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
mtarName, err := getMtarName(config, mtaYamlFile, utils)
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var call []string
|
|
|
|
|
2021-05-19 08:45:38 +02:00
|
|
|
platform, err := ValueOfBuildTarget(config.Platform)
|
|
|
|
if err != nil {
|
2020-07-31 15:41:28 +02:00
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
2021-05-19 08:45:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
call = append(call, "mbt", "build", "--mtar", mtarName, "--platform", platform.String())
|
|
|
|
if len(config.Extensions) != 0 {
|
|
|
|
call = append(call, fmt.Sprintf("--extensions=%s", config.Extensions))
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
2021-12-14 15:43:02 +02:00
|
|
|
|
|
|
|
call = append(call, "--source", getSourcePath(config))
|
|
|
|
call = append(call, "--target", getAbsPath(getMtarFileRoot(config)))
|
2020-02-25 15:33:34 +02:00
|
|
|
|
2021-10-20 16:03:16 +02:00
|
|
|
if config.Jobs > 0 {
|
|
|
|
call = append(call, "--mode=verbose")
|
|
|
|
call = append(call, "--jobs="+strconv.Itoa(config.Jobs))
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
if err = addNpmBinToPath(utils); err != nil {
|
2020-02-25 15:33:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:06:17 +02:00
|
|
|
if len(config.M2Path) > 0 {
|
2020-11-10 18:14:55 +02:00
|
|
|
absolutePath, err := utils.Abs(config.M2Path)
|
2020-07-28 17:06:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 18:14:55 +02:00
|
|
|
utils.AppendEnv([]string{"MAVEN_OPTS=-Dmaven.repo.local=" + absolutePath})
|
2020-07-28 17:06:17 +02:00
|
|
|
}
|
|
|
|
|
2020-02-25 15:33:34 +02:00
|
|
|
log.Entry().Infof("Executing mta build call: \"%s\"", strings.Join(call, " "))
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
if err := utils.RunExecutable(call[0], call[1:]...); err != nil {
|
2020-07-31 15:41:28 +02:00
|
|
|
log.SetErrorCategory(log.ErrorBuild)
|
2020-02-25 15:33:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 08:46:18 +02:00
|
|
|
log.Entry().Debugf("creating build settings information...")
|
|
|
|
stepName := "mtaBuild"
|
2022-03-14 13:33:52 +02:00
|
|
|
dockerImage, err := GetDockerImageValue(stepName)
|
2021-12-01 08:46:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:02:57 +02:00
|
|
|
mtaConfig := buildsettings.BuildOptions{
|
|
|
|
Profiles: config.Profiles,
|
|
|
|
GlobalSettingsFile: config.GlobalSettingsFile,
|
|
|
|
Publish: config.Publish,
|
|
|
|
BuildSettingsInfo: config.BuildSettingsInfo,
|
2021-12-01 08:46:18 +02:00
|
|
|
DefaultNpmRegistry: config.DefaultNpmRegistry,
|
|
|
|
DockerImage: dockerImage,
|
2021-11-16 16:02:57 +02:00
|
|
|
}
|
2021-12-01 08:46:18 +02:00
|
|
|
buildSettingsInfo, err := buildsettings.CreateBuildSettingsInfo(&mtaConfig, stepName)
|
2021-11-16 16:02:57 +02:00
|
|
|
if err != nil {
|
2021-12-01 08:46:18 +02:00
|
|
|
log.Entry().Warnf("failed to create build settings info: %v", err)
|
2021-11-16 16:02:57 +02:00
|
|
|
}
|
2021-12-01 08:46:18 +02:00
|
|
|
commonPipelineEnvironment.custom.buildSettingsInfo = buildSettingsInfo
|
2021-11-16 16:02:57 +02:00
|
|
|
|
2021-12-14 15:43:02 +02:00
|
|
|
commonPipelineEnvironment.mtarFilePath = filepath.ToSlash(getMtarFilePath(config, mtarName))
|
|
|
|
commonPipelineEnvironment.custom.mtaBuildToolDesc = filepath.ToSlash(mtaYamlFile)
|
2020-06-15 12:46:54 +02:00
|
|
|
|
2020-06-22 10:39:52 +02:00
|
|
|
if config.InstallArtifacts {
|
|
|
|
// install maven artifacts in local maven repo because `mbt build` executes `mvn package -B`
|
2020-11-10 18:14:55 +02:00
|
|
|
err = installMavenArtifacts(utils, config)
|
2020-06-22 10:39:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// mta-builder executes 'npm install --production', therefore we need 'npm ci/install' to install the dev-dependencies
|
2020-11-10 18:14:55 +02:00
|
|
|
err = utils.InstallAllDependencies(config.DefaultNpmRegistry)
|
2020-06-22 10:39:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-23 15:33:30 +02:00
|
|
|
|
|
|
|
if config.Publish {
|
|
|
|
log.Entry().Infof("publish detected")
|
|
|
|
if (len(config.MtaDeploymentRepositoryPassword) > 0) && (len(config.MtaDeploymentRepositoryUser) > 0) &&
|
|
|
|
(len(config.MtaDeploymentRepositoryURL) > 0) {
|
|
|
|
if (len(config.MtarGroup) > 0) && (len(config.Version) > 0) {
|
2021-09-29 19:07:00 +02:00
|
|
|
httpClient := &piperhttp.Client{}
|
2021-09-23 15:33:30 +02:00
|
|
|
|
|
|
|
credentialsEncoded := "Basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", config.MtaDeploymentRepositoryUser, config.MtaDeploymentRepositoryPassword)))
|
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("Authorization", credentialsEncoded)
|
|
|
|
|
|
|
|
config.MtarGroup = strings.ReplaceAll(config.MtarGroup, ".", "/")
|
|
|
|
|
|
|
|
mtarArtifactName := mtarName
|
|
|
|
|
|
|
|
mtarArtifactName = strings.ReplaceAll(mtarArtifactName, ".mtar", "")
|
|
|
|
|
|
|
|
config.MtaDeploymentRepositoryURL += config.MtarGroup + "/" + mtarArtifactName + "/" + config.Version + "/" + fmt.Sprintf("%v-%v.%v", mtarArtifactName, config.Version, "mtar")
|
|
|
|
|
|
|
|
commonPipelineEnvironment.custom.mtarPublishedURL = config.MtaDeploymentRepositoryURL
|
|
|
|
|
|
|
|
log.Entry().Infof("pushing mtar artifact to repository : %s", config.MtaDeploymentRepositoryURL)
|
|
|
|
|
2022-04-13 14:50:13 +02:00
|
|
|
data, err := os.Open(getMtarFilePath(config, mtarName))
|
2021-09-29 19:07:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to open mtar archive for upload")
|
|
|
|
}
|
|
|
|
_, httpErr := httpClient.SendRequest("PUT", config.MtaDeploymentRepositoryURL, data, headers, nil)
|
|
|
|
|
2021-09-23 15:33:30 +02:00
|
|
|
if httpErr != nil {
|
|
|
|
return errors.Wrap(err, "failed to upload mtar to repository")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("mtarGroup, version not found and must be present")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return errors.New("mtaDeploymentRepositoryUser, mtaDeploymentRepositoryPassword and mtaDeploymentRepositoryURL not found , must be present")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Entry().Infof("no publish detected, skipping upload of mtar artifact")
|
|
|
|
}
|
2020-06-15 12:46:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-23 15:33:30 +02:00
|
|
|
func handleActiveProfileUpdate(config mtaBuildOptions, utils mtaBuildUtils) error {
|
|
|
|
if len(config.Profiles) > 0 {
|
|
|
|
return maven.UpdateActiveProfileInSettingsXML(config.Profiles, utils)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func installMavenArtifacts(utils mtaBuildUtils, config mtaBuildOptions) error {
|
|
|
|
pomXMLExists, err := utils.FileExists("pom.xml")
|
2020-06-15 12:46:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pomXMLExists {
|
2020-11-10 18:14:55 +02:00
|
|
|
err = maven.InstallMavenArtifacts(&maven.EvaluateOptions{M2Path: config.M2Path}, utils)
|
2020-06-15 12:46:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 15:33:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func addNpmBinToPath(utils mtaBuildUtils) error {
|
2020-03-11 16:15:00 +02:00
|
|
|
dir, _ := os.Getwd()
|
|
|
|
newPath := path.Join(dir, "node_modules", ".bin")
|
2020-02-25 15:33:34 +02:00
|
|
|
oldPath := os.Getenv("PATH")
|
|
|
|
if len(oldPath) > 0 {
|
2020-03-11 16:15:00 +02:00
|
|
|
newPath = newPath + ":" + oldPath
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
2020-11-10 18:14:55 +02:00
|
|
|
utils.SetEnv([]string{"PATH=" + newPath})
|
2020-02-25 15:33:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func getMtarName(config mtaBuildOptions, mtaYamlFile string, utils mtaBuildUtils) (string, error) {
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
mtarName := config.MtarName
|
|
|
|
if len(mtarName) == 0 {
|
|
|
|
|
|
|
|
log.Entry().Debugf("mtar name not provided via config. Extracting from file \"%s\"", mtaYamlFile)
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
mtaID, err := getMtaID(mtaYamlFile, utils)
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2020-07-31 15:41:28 +02:00
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
2020-02-25 15:33:34 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mtaID) == 0 {
|
2020-07-31 15:41:28 +02:00
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
2020-05-19 22:02:47 +02:00
|
|
|
return "", fmt.Errorf("Invalid mtar ID. Was empty")
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Entry().Debugf("mtar name extracted from file \"%s\": \"%s\"", mtaYamlFile, mtaID)
|
|
|
|
|
2020-05-19 22:02:47 +02:00
|
|
|
mtarName = mtaID + ".mtar"
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 22:02:47 +02:00
|
|
|
return mtarName, nil
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func setTimeStamp(mtaYamlFile string, utils mtaBuildUtils) error {
|
2020-02-25 15:33:34 +02:00
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
mtaYaml, err := utils.FileRead(mtaYamlFile)
|
2020-02-25 15:33:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mtaYamlStr := string(mtaYaml)
|
|
|
|
|
|
|
|
timestampVar := "${timestamp}"
|
|
|
|
if strings.Contains(mtaYamlStr, timestampVar) {
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
if err := utils.FileWrite(mtaYamlFile, []byte(strings.ReplaceAll(mtaYamlStr, timestampVar, getTimestamp())), 0644); err != nil {
|
2020-07-31 15:41:28 +02:00
|
|
|
log.SetErrorCategory(log.ErrorConfiguration)
|
2020-02-25 15:33:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Entry().Infof("Timestamp replaced in \"%s\"", mtaYamlFile)
|
|
|
|
} else {
|
2020-02-26 17:21:40 +02:00
|
|
|
log.Entry().Infof("No timestamp contained in \"%s\". File has not been modified.", mtaYamlFile)
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimestamp() string {
|
|
|
|
t := time.Now()
|
2020-09-21 09:14:48 +02:00
|
|
|
return fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func createMtaYamlFile(mtaYamlFile, applicationName string, utils mtaBuildUtils) error {
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
log.Entry().Debugf("mta yaml file not found in project sources.")
|
|
|
|
|
|
|
|
if len(applicationName) == 0 {
|
|
|
|
return fmt.Errorf("'%[1]s' not found in project sources and 'applicationName' not provided as parameter - cannot generate '%[1]s' file", mtaYamlFile)
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
packageFileExists, err := utils.FileExists("package.json")
|
2020-02-25 15:33:34 +02:00
|
|
|
if !packageFileExists {
|
|
|
|
return fmt.Errorf("package.json file does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
var result map[string]interface{}
|
2020-11-10 18:14:55 +02:00
|
|
|
pContent, err := utils.FileRead("package.json")
|
2020-02-25 15:33:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
json.Unmarshal(pContent, &result)
|
|
|
|
|
|
|
|
version, ok := result["version"].(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Version not found in \"package.json\" (or wrong type)")
|
|
|
|
}
|
|
|
|
|
|
|
|
name, ok := result["name"].(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Name not found in \"package.json\" (or wrong type)")
|
|
|
|
}
|
|
|
|
|
|
|
|
mtaConfig, err := generateMta(name, applicationName, version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-21 09:04:21 +02:00
|
|
|
if err := utils.FileWrite(mtaYamlFile, []byte(mtaConfig), 0644); err != nil {
|
|
|
|
return fmt.Errorf("failed to write %v: %w", mtaYamlFile, err)
|
|
|
|
}
|
2020-02-25 15:33:34 +02:00
|
|
|
log.Entry().Infof("\"%s\" created.", mtaYamlFile)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func handleSettingsFiles(config mtaBuildOptions, utils mtaBuildUtils) error {
|
|
|
|
return utils.DownloadAndCopySettingsFiles(config.GlobalSettingsFile, config.ProjectSettingsFile)
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateMta(id, applicationName, version string) (string, error) {
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
|
|
|
return "", fmt.Errorf("Generating mta file: ID not provided")
|
|
|
|
}
|
|
|
|
if len(applicationName) == 0 {
|
|
|
|
return "", fmt.Errorf("Generating mta file: ApplicationName not provided")
|
|
|
|
}
|
|
|
|
if len(version) == 0 {
|
|
|
|
return "", fmt.Errorf("Generating mta file: Version not provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, e := template.New("mta.yaml").Parse(templateMtaYml)
|
|
|
|
if e != nil {
|
|
|
|
return "", e
|
|
|
|
}
|
|
|
|
|
|
|
|
type properties struct {
|
|
|
|
ID string
|
|
|
|
ApplicationName string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
props := properties{ID: id, ApplicationName: applicationName, Version: version}
|
|
|
|
|
|
|
|
var script bytes.Buffer
|
2022-07-21 09:04:21 +02:00
|
|
|
if err := tmpl.Execute(&script, props); err != nil {
|
|
|
|
log.Entry().Warningf("failed to execute template: %v", err)
|
|
|
|
}
|
2020-02-25 15:33:34 +02:00
|
|
|
return script.String(), nil
|
|
|
|
}
|
|
|
|
|
2020-11-10 18:14:55 +02:00
|
|
|
func getMtaID(mtaYamlFile string, utils mtaBuildUtils) (string, error) {
|
2020-02-25 15:33:34 +02:00
|
|
|
|
|
|
|
var result map[string]interface{}
|
2020-11-10 18:14:55 +02:00
|
|
|
p, err := utils.FileRead(mtaYamlFile)
|
2020-02-25 15:33:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(p, &result)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, ok := result["ID"].(string)
|
|
|
|
if !ok || len(id) == 0 {
|
2020-11-25 14:47:26 +02:00
|
|
|
return "", fmt.Errorf("Id not found in mta yaml file (or wrong type)")
|
2020-02-25 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
2021-12-14 15:43:02 +02:00
|
|
|
|
|
|
|
// the "source" path locates the project's root
|
|
|
|
func getSourcePath(config mtaBuildOptions) string {
|
|
|
|
path := config.Source
|
|
|
|
if path == "" {
|
|
|
|
path = "./"
|
|
|
|
}
|
|
|
|
return filepath.FromSlash(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// target defines a subfolder of the project's root
|
|
|
|
func getTargetPath(config mtaBuildOptions) string {
|
|
|
|
path := config.Target
|
|
|
|
if path == "" {
|
|
|
|
path = "./"
|
|
|
|
}
|
|
|
|
return filepath.FromSlash(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the "mtar" path resides below the project's root
|
|
|
|
// path=<config.source>/<config.target>/<mtarname>
|
|
|
|
func getMtarFileRoot(config mtaBuildOptions) string {
|
|
|
|
sourcePath := getSourcePath(config)
|
|
|
|
targetPath := getTargetPath(config)
|
|
|
|
|
|
|
|
return filepath.FromSlash(filepath.Join(sourcePath, targetPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMtarFilePath(config mtaBuildOptions, mtarName string) string {
|
|
|
|
root := getMtarFileRoot(config)
|
|
|
|
|
|
|
|
if root == "" || root == filepath.FromSlash("./") {
|
|
|
|
return mtarName
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.FromSlash(filepath.Join(root, mtarName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAbsPath(path string) string {
|
|
|
|
abspath, err := filepath.Abs(path)
|
|
|
|
// ignore error, pass customers path value in case of trouble
|
|
|
|
if err != nil {
|
|
|
|
abspath = path
|
|
|
|
}
|
|
|
|
return filepath.FromSlash(abspath)
|
|
|
|
}
|