|
| 1 | +package githubactions |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/intelops/compage/internal/languages/dotnet/frameworks" |
| 5 | + "github.com/intelops/compage/internal/languages/executor" |
| 6 | + "github.com/intelops/compage/internal/utils" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const CIFile = "ci.yml.tmpl" |
| 11 | +const ReleaseFile = "release.yml.tmpl" |
| 12 | +const Path = "/.github/workflows" |
| 13 | + |
| 14 | +// Copier integrations specific copier |
| 15 | +type Copier struct { |
| 16 | + ProjectDirectoryName string |
| 17 | + NodeDirectoryName string |
| 18 | + NodeName string |
| 19 | + TemplatesRootPath string |
| 20 | + Data map[string]interface{} |
| 21 | +} |
| 22 | + |
| 23 | +func NewCopier(gitPlatformUserName, gitRepositoryName, projectDirectoryName, nodeName, nodeDirectoryName, templatesRootPath string) *Copier { |
| 24 | + // populate map to replace templates |
| 25 | + data := map[string]interface{}{ |
| 26 | + "NodeName": strings.ToLower(nodeName), |
| 27 | + "MicroServiceName": frameworks.GetMicroServiceName(nodeDirectoryName), |
| 28 | + "GitRepositoryName": gitRepositoryName, |
| 29 | + "GitPlatformUserName": gitPlatformUserName, |
| 30 | + } |
| 31 | + |
| 32 | + return &Copier{ |
| 33 | + TemplatesRootPath: templatesRootPath, |
| 34 | + NodeDirectoryName: nodeDirectoryName, |
| 35 | + NodeName: nodeName, |
| 36 | + ProjectDirectoryName: projectDirectoryName, |
| 37 | + Data: data, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// CreateYamls creates required file from language template. |
| 42 | +func (c Copier) CreateYamls() error { |
| 43 | + destGithubActionsDirectory := c.ProjectDirectoryName + Path |
| 44 | + if err := utils.CreateDirectories(destGithubActionsDirectory); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + err := c.createReleaseYaml() |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + err = c.createCIYaml() |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +// createCIYaml creates required file from language template. |
| 60 | +func (c Copier) createCIYaml() error { |
| 61 | + var filePaths []*string |
| 62 | + // copy ci.yaml |
| 63 | + targetCIYamlName := c.ProjectDirectoryName + Path + "/" + c.NodeName + "-" + CIFile |
| 64 | + _, err := utils.CopyFile(targetCIYamlName, c.TemplatesRootPath+Path+"/"+CIFile) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + filePaths = append(filePaths, &targetCIYamlName) |
| 69 | + return executor.ExecuteGhActions(filePaths, c.Data) |
| 70 | +} |
| 71 | + |
| 72 | +// createReleaseYaml creates required file from language template. |
| 73 | +func (c Copier) createReleaseYaml() error { |
| 74 | + var filePaths []*string |
| 75 | + // copy release.yaml |
| 76 | + targetReleaseFileName := c.ProjectDirectoryName + Path + "/" + c.NodeName + "-" + ReleaseFile |
| 77 | + _, err := utils.CopyFile(targetReleaseFileName, c.TemplatesRootPath+Path+"/"+ReleaseFile) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + filePaths = append(filePaths, &targetReleaseFileName) |
| 82 | + return executor.ExecuteGhActions(filePaths, c.Data) |
| 83 | +} |
0 commit comments