Skip to content

Commit

Permalink
enable configuring mtar file name
Browse files Browse the repository at this point in the history
  • Loading branch information
allaVolkov committed Feb 25, 2019
1 parent b20966c commit 2f34573
Show file tree
Hide file tree
Showing 27 changed files with 113 additions and 66 deletions.
2 changes: 1 addition & 1 deletion cmd/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func assembly(source, target, platform string, getWd func() (string, error)) err
return errors.Wrap(err, "assembly of the MTA project failed when generating the meta information")
}
// generate mtar
err = artifacts.ExecuteGenMtar(source, target, strconv.FormatBool(target != ""), dir.Dep, getWd)
err = artifacts.ExecuteGenMtar(source, target, strconv.FormatBool(target != ""), dir.Dep, "", getWd)
if err != nil {
return errors.Wrap(err, "assembly of the MTA project failed when generating the MTA archive")
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var mtarCmdSrc string
var mtarCmdTrg string
var mtarCmdDesc string
var mtarCmdTrgProvided string
var mtarCmdMtarName string

// init - inits flags of init command
func init() {
Expand Down Expand Up @@ -53,6 +54,8 @@ func init() {
"the path to the MBT results folder; the current path is default")
mtarCmd.Flags().StringVarP(&mtarCmdDesc, "desc", "d", "",
"the MTA descriptor; supported values: dev (development descriptor, default value) and dep (deployment descriptor)")
mtarCmd.Flags().StringVarP(&mtarCmdMtarName, "mtar", "m", "*",
"the archive name")
mtarCmd.Flags().StringVarP(&mtarCmdTrgProvided, "target_provided", "", "",
"the MTA target provided indicator; supported values: true, false")
}
Expand Down Expand Up @@ -95,7 +98,7 @@ var mtarCmd = &cobra.Command{
Long: "Generates MTA archive from the folder with all artifacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
err := artifacts.ExecuteGenMtar(mtarCmdSrc, mtarCmdTrg, mtarCmdTrgProvided, mtarCmdDesc, os.Getwd)
err := artifacts.ExecuteGenMtar(mtarCmdSrc, mtarCmdTrg, mtarCmdTrgProvided, mtarCmdDesc, mtarCmdMtarName, os.Getwd)
logError(err)
return err
},
Expand Down
1 change: 1 addition & 0 deletions cmd/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var _ = Describe("Commands", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
mtarCmdSrc = getTestPath("mtahtml5")
mtarCmdMtarName = ""
Ω(metaCmd.RunE(nil, []string{})).Should(Succeed())
Ω(mtarCmd.RunE(nil, []string{})).Should(Succeed())
Ω(getTestPath("result", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile())
Expand Down
3 changes: 1 addition & 2 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ package main
//go:generate go run ./internal/buildtools/embed.go -source=./configs/content_type_cfg.yaml -target=./internal/conttype/content_type_cfg.go -name=ContentTypeConfig -package=conttype
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/manifest.txt -target=./internal/tpl/manifest.go -name=Manifest -package=tpl
//go:generate go run ./internal/buildtools/embed.go -source=./configs/builder_type_cfg.yaml -target=./internal/commands/builder_type_cfg.go -name=BuilderTypeConfig -package=commands
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/base_post_default.txt -target=./internal/tpl/base_post_default.go -name=basePostDefault -package=tpl
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/base_post_verbose.txt -target=./internal/tpl/base_post_verbose.go -name=basePostVerbose -package=tpl
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/base_post.txt -target=./internal/tpl/base_post.go -name=basePost -package=tpl
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/base_pre_default.txt -target=./internal/tpl/base_pre_default.go -name=basePreDefault -package=tpl
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/base_pre_verbose.txt -target=./internal/tpl/base_pre_verbose.go -name=basePreVerbose -package=tpl
//go:generate go run ./internal/buildtools/embed.go -source=./internal/tpl/make_default.txt -target=./internal/tpl/make_default.go -name=makeDefault -package=tpl
Expand Down
11 changes: 6 additions & 5 deletions integration/cloud_mta_build_tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ var _ = Describe("Integration - CloudMtaBuildTool", func() {
os.Remove("./testdata/mta_demo/" + mbtName)
os.Remove("./testdata/mta_demo/Makefile.mta")
os.Remove("./testdata/mta_demo/mtad.yaml")
os.Remove("./testdata/mta_demo/" + archiveName)
os.Remove("./testdata/mta_demo/mta_archives")
os.Remove("./testdata/mta_demo/abc.mtar")
os.RemoveAll("./testdata/mta_demo/mta_archives")
os.RemoveAll("./testdata/mta_assemble/mta_archives")
resourceCleanup("node")
})

Expand Down Expand Up @@ -103,20 +104,20 @@ var _ = Describe("Integration - CloudMtaBuildTool", func() {
})

var _ = Describe("Generate MTAR", func() {
It("Generate MTAR with provided target", func() {
It("Generate MTAR with provided target and mtar name", func() {
dir, _ := os.Getwd()
os.RemoveAll(filepath.Join(dir, "testdata", "mta_demo", archiveName))
path := dir + filepath.FromSlash("/testdata/mta_demo")
bin := filepath.FromSlash("make")
cmdOut, err, _ := execute(bin, "-f Makefile.mta p=cf t="+path, path)
cmdOut, err, _ := execute(bin, "-f Makefile.mta p=cf mtar=abc t="+path, path)
if len(err) > 0 {
fmt.Println(err)
}
Ω(err).Should(Equal(""))
fmt.Println(cmdOut)
Ω(cmdOut).ShouldNot(BeEmpty())
// Check the archive was generated
Ω(filepath.Join(dir, "testdata", "mta_demo", archiveName)).Should(BeAnExistingFile())
Ω(filepath.Join(dir, "testdata", "mta_demo", "abc.mtar")).Should(BeAnExistingFile())
})

It("Generate MTAR", func() {
Expand Down
17 changes: 15 additions & 2 deletions internal/artifacts/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,34 @@ import (
var _ = Describe("Cleanup", func() {

BeforeEach(func() {
os.MkdirAll(getTestPath("result", "mtahtml5"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp"), os.ModePerm)
})

AfterEach(func() {
os.RemoveAll(getTestPath("result"))
})
It("Sanity", func() {
Ω(ExecuteCleanup(getTestPath("mtahtml5"), getResultPath(), "dev", os.Getwd)).Should(Succeed())
Ω(getTestPath("result", "mtahtml5_mta_build_tmp")).ShouldNot(BeADirectory())
Ω(getTestPath("result", ".mtahtml5_mta_build_tmp")).ShouldNot(BeADirectory())
})
It("Fails on location initialization", func() {
Ω(ExecuteCleanup("", getTestPath("result"), "dev", func() (string, error) {
return "", errors.New("err")
})).Should(HaveOccurred())
})
It("Fails on RemoveAll, another go routine creates file in the folder to be cleaned", func() {
messages := make(chan string)
messages1 := make(chan string)
go func() {
file, _ := os.OpenFile(getTestPath("result", ".mtahtml5_mta_build_tmp", "abc.txt"), os.O_CREATE, 0666)
messages1 <- "ping1"
<-messages
file.Close()
}()
<-messages1
Ω(ExecuteCleanup(getTestPath("mtahtml5"), getResultPath(), "dev", os.Getwd)).Should(HaveOccurred())
messages <- "ping"
})
})

var _ = Describe("Cleanup", func() {
Expand Down
2 changes: 1 addition & 1 deletion internal/artifacts/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ cli_version:["x"]
if err != nil {
fmt.Println(err)
}
mtarPath, err := generateMtar(&ep, &ep, &ep, true)
mtarPath, err := generateMtar(&ep, &ep, &ep, true, "")
if err != nil {
fmt.Println(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/artifacts/mtad.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func ExecuteGenMtad(source, target, platform string, wdGetter func() (string, er

func validatePlatform(platform string) error {
if platform != "xsa" && platform != "cf" && platform != "neo" {
return fmt.Errorf("the %s deployment platform is not supported; supported values: cf, xsa, neo", platform)
return fmt.Errorf(`the invalid target platform "%s"; supported platforms are: "cf", "neo", "xsa"`, platform)
}
return nil
}
Expand Down
20 changes: 16 additions & 4 deletions internal/artifacts/mtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import (
"github.com/SAP/cloud-mta-build-tool/internal/archive"
"github.com/SAP/cloud-mta-build-tool/internal/logs"
"strconv"
"github.com/SAP/cloud-mta/mta"
)

const (
mtarExtension = ".mtar"
)

// ExecuteGenMtar - generates MTAR
func ExecuteGenMtar(source, target, targetProvided, desc string, wdGetter func() (string, error)) error {
func ExecuteGenMtar(source, target, targetProvided, desc, mtarName string, wdGetter func() (string, error)) error {
logs.Logger.Info("generating the MTA archive...")
loc, err := dir.Location(source, target, desc, wdGetter)
if err != nil {
return errors.Wrap(err, "generation of the MTA archive failed when initializing the location")
}
path, err := generateMtar(loc, loc, loc, isTargetProvided(target, targetProvided))
path, err := generateMtar(loc, loc, loc, isTargetProvided(target, targetProvided), mtarName)
if err != nil {
return err
}
Expand All @@ -41,7 +42,8 @@ func isTargetProvided(target, provided string) bool {
}

// generateMtar - generate mtar archive from the build artifacts
func generateMtar(targetLoc dir.ITargetPath, targetArtifacts dir.ITargetArtifacts, parser dir.IMtaParser, targetProvided bool) (string, error) {
func generateMtar(targetLoc dir.ITargetPath, targetArtifacts dir.ITargetArtifacts, parser dir.IMtaParser,
targetProvided bool, mtarName string) (string, error) {
// get MTA object
m, err := parser.ParseFile()
if err != nil {
Expand All @@ -59,10 +61,20 @@ func generateMtar(targetLoc dir.ITargetPath, targetArtifacts dir.ITargetArtifact
`generation of the MTA archive failed when creating the "%s" folder`, mtarFolderPath)
}
// archive building artifacts to mtar
mtarPath := filepath.Join(mtarFolderPath, m.ID+"_"+m.Version+mtarExtension)
mtarPath := filepath.Join(mtarFolderPath, getMtarFileName(m, mtarName))
err = dir.Archive(targetTmpDir, mtarPath)
if err != nil {
return "", errors.Wrap(err, "generation of the MTA archive failed when archiving")
}
return mtarPath, nil
}

func getMtarFileName(m *mta.MTA, mtarName string) string {
if mtarName == "" || mtarName == "*" {
return m.ID + "_" + m.Version + mtarExtension
}
if filepath.Ext(mtarName) != "" {
return mtarName
}
return mtarName + mtarExtension
}
31 changes: 21 additions & 10 deletions internal/artifacts/mtar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

"github.com/SAP/cloud-mta/mta"
"github.com/SAP/cloud-mta-build-tool/internal/archive"
)

Expand All @@ -23,26 +24,26 @@ var _ = Describe("Mtar", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
Ω(ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", "cf", true, os.Getwd)).Should(Succeed())
Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "true", "dev", os.Getwd)).Should(Succeed())
Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "true", "dev", "", os.Getwd)).Should(Succeed())
Ω(getTestPath("result", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile())
})

It("Sanity, target not provided", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
Ω(ExecuteGenMeta(getTestPath("mtahtml5"), getResultPath(), "dev", "cf", true, os.Getwd)).Should(Succeed())
Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "false", "dev", os.Getwd)).Should(Succeed())
Ω(ExecuteGenMtar(getTestPath("mtahtml5"), getResultPath(), "false", "dev", "", os.Getwd)).Should(Succeed())
Ω(getTestPath("result", "mta_archives", "mtahtml5_0.0.1.mtar")).Should(BeAnExistingFile())
})

It("Fails on location initialization", func() {
Ω(ExecuteGenMtar("", getResultPath(), "true", "dev", func() (string, error) {
Ω(ExecuteGenMtar("", getResultPath(), "true", "dev", "", func() (string, error) {
return "", errors.New("err")
})).Should(HaveOccurred())
})

It("Fails - wrong source", func() {
Ω(ExecuteGenMtar(getTestPath("mtahtml6"), getResultPath(), "true", "dev", os.Getwd)).Should(HaveOccurred())
Ω(ExecuteGenMtar(getTestPath("mtahtml6"), getResultPath(), "true", "dev", "", os.Getwd)).Should(HaveOccurred())
})
})

Expand All @@ -51,33 +52,33 @@ var _ = Describe("Mtar", func() {
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "testapp"), os.ModePerm)
os.MkdirAll(getTestPath("result", ".mtahtml5_mta_build_tmp", "ui5app2"), os.ModePerm)
Ω(generateMeta(&ep, &ep, &ep, false, "cf", true)).Should(Succeed())
mtarPath, err := generateMtar(&ep, &ep, &ep, true)
mtarPath, err := generateMtar(&ep, &ep, &ep, true, "")
Ω(err).Should(Succeed())
Ω(mtarPath).Should(BeAnExistingFile())
})

It("Generate Mtar - Fails on wrong source", func() {
ep := dir.Loc{SourcePath: getTestPath("not_existing"), TargetPath: getResultPath()}
ep1 := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
_, err := generateMtar(&ep, &ep1, &ep1, true)
_, err := generateMtar(&ep, &ep1, &ep1, true, "")
Ω(err).Should(HaveOccurred())
})

It("Generate Mtar - Invalid mta", func() {
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaBroken.yaml"}
_, err := generateMtar(&ep, &ep, &ep, true)
_, err := generateMtar(&ep, &ep, &ep, true, "")
Ω(err).Should(HaveOccurred())
})
It("Generate Mtar - Mta not exists", func() {
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath(), MtaFilename: "mtaNotExists.yaml"}
_, err := generateMtar(&ep, &ep, &ep, true)
_, err := generateMtar(&ep, &ep, &ep, true, "")
Ω(err).Should(HaveOccurred())
})
It("Generate Mtar - results file exists, folder results can't be created ", func() {
file, _ := os.Create(getTestPath("result"))
defer file.Close()
ep := dir.Loc{SourcePath: getTestPath("mtahtml5"), TargetPath: getResultPath()}
_, err := generateMtar(&ep, &ep, &ep, true)
_, err := generateMtar(&ep, &ep, &ep, true, "")
Ω(err).Should(HaveOccurred())
})
DescribeTable("isTargetProvided", func(target, provided string, expected bool) {
Expand All @@ -92,7 +93,7 @@ var _ = Describe("Mtar", func() {
var _ = Describe("Target Failures", func() {
var _ = DescribeTable("Invalid location", func(loc *testMtarLoc) {
ep := dir.Loc{}
_, err := generateMtar(loc, &ep, &ep, true)
_, err := generateMtar(loc, &ep, &ep, true, "")
Ω(err).Should(HaveOccurred())
},
Entry("Fails on GetTargetTmpDir", &testMtarLoc{
Expand All @@ -105,6 +106,16 @@ var _ = Describe("Mtar", func() {
}))
})
})

var _ = DescribeTable("getMtarFileName", func(mtarName, expected string) {
m := mta.MTA{ID: "proj", Version: "0.1.5"}
Ω(getMtarFileName(&m, mtarName)).Should(Equal(expected))
},
Entry("default mtar", "", "proj_0.1.5.mtar"),
Entry("default supporting make file", "*", "proj_0.1.5.mtar"),
Entry("default supporting make file", "abc", "abc.mtar"),
Entry("default supporting make file", "abc.zip", "abc.zip"),
)
})

type testMtarLoc struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/tpl/base_post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package tpl

// basePost - do not edit
var basePost = []byte{0x7b, 0x7b, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65, 0x20, 0x22, 0x62, 0x61, 0x73, 0x65, 0x5f, 0x70, 0x6f, 0x73, 0x74, 0x22, 0x7d, 0x7d, 0xd, 0xa, 0x23, 0x20, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x20, 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46, 0x20, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x4d, 0x41, 0x4e, 0x49, 0x46, 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46, 0x20, 0x26, 0x20, 0x6d, 0x74, 0x61, 0x64, 0x2e, 0x79, 0x61, 0x6d, 0x6c, 0xd, 0xa, 0x6d, 0x65, 0x74, 0x61, 0x3a, 0x20, 0x24, 0x28, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x29, 0xd, 0xa, 0x7b, 0x7b, 0x22, 0x5c, 0x74, 0x22, 0x7d, 0x7d, 0x40, 0x6d, 0x62, 0x74, 0x20, 0x67, 0x65, 0x6e, 0x20, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x2d, 0x64, 0x3d, 0x7b, 0x7b, 0x2e, 0x44, 0x65, 0x70, 0x7d, 0x7d, 0x20, 0x2d, 0x70, 0x3d, 0x24, 0x7b, 0x70, 0x7d, 0x20, 0x2d, 0x74, 0x3d, 0x24, 0x7b, 0x74, 0x7d, 0xd, 0xa, 0xd, 0xa, 0x23, 0x20, 0x50, 0x61, 0x63, 0x6b, 0x20, 0x61, 0x73, 0x20, 0x4d, 0x54, 0x41, 0x52, 0x20, 0x61, 0x72, 0x74, 0x69, 0x66, 0x61, 0x63, 0x74, 0xd, 0xa, 0x6d, 0x74, 0x61, 0x72, 0x3a, 0x20, 0x24, 0x28, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x73, 0x29, 0x20, 0x6d, 0x65, 0x74, 0x61, 0xd, 0xa, 0x7b, 0x7b, 0x22, 0x5c, 0x74, 0x22, 0x7d, 0x7d, 0x40, 0x6d, 0x62, 0x74, 0x20, 0x67, 0x65, 0x6e, 0x20, 0x6d, 0x74, 0x61, 0x72, 0x20, 0x2d, 0x64, 0x3d, 0x7b, 0x7b, 0x2e, 0x44, 0x65, 0x70, 0x7d, 0x7d, 0x20, 0x2d, 0x74, 0x3d, 0x24, 0x7b, 0x74, 0x7d, 0x20, 0x2d, 0x2d, 0x6d, 0x74, 0x61, 0x72, 0x3d, 0x24, 0x7b, 0x6d, 0x74, 0x61, 0x72, 0x7d, 0x20, 0x2d, 0x2d, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x64, 0x3d, 0x24, 0x7b, 0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x5f, 0x70, 0x72, 0x6f, 0x76, 0x69, 0x64, 0x65, 0x64, 0x7d, 0xd, 0xa, 0xd, 0xa, 0x63, 0x6c, 0x65, 0x61, 0x6e, 0x75, 0x70, 0x3a, 0x20, 0x6d, 0x74, 0x61, 0x72, 0xd, 0xa, 0x23, 0x20, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x20, 0x74, 0x6d, 0x70, 0x20, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, 0xd, 0xa, 0x7b, 0x7b, 0x22, 0x5c, 0x74, 0x22, 0x7d, 0x7d, 0x40, 0x6d, 0x62, 0x74, 0x20, 0x63, 0x6c, 0x65, 0x61, 0x6e, 0x20, 0x2d, 0x74, 0x3d, 0x24, 0x7b, 0x74, 0x7d, 0xd, 0xa, 0x7b, 0x7b, 0x65, 0x6e, 0x64, 0x7d, 0x7d}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ meta: $(modules)

# Pack as MTAR artifact
mtar: $(modules) meta
{{"\t"}}@mbt gen mtar -d={{.Dep}} -t=${t} --target_provided=${target_provided}
{{"\t"}}@mbt gen mtar -d={{.Dep}} -t=${t} --mtar=${mtar} --target_provided=${target_provided}

cleanup: mtar
# Remove tmp folder
Expand Down
4 changes: 0 additions & 4 deletions internal/tpl/base_post_default.go

This file was deleted.

13 changes: 0 additions & 13 deletions internal/tpl/base_post_default.txt

This file was deleted.

Loading

0 comments on commit 2f34573

Please sign in to comment.