Skip to content

Commit

Permalink
feat: added license generation for dotnet
Browse files Browse the repository at this point in the history
  • Loading branch information
mahendraintelops committed Apr 20, 2024
1 parent a842898 commit 2849f22
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"cwd": "${workspaceRoot}",
"args": ["pullTemplates"]
"args": ["generate"]
}
]
}
27 changes: 16 additions & 11 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,26 @@ func GenerateCode() error {
return err
}

// assign absolute path to the license file Path if it's not
absPath, err := filepath.Abs(coreProject.License.Path)
if err != nil {
log.Errorf("error while getting absolute path [" + err.Error() + "]")
return err
}
coreProject.License.Path = absPath
// assign absolute path to the license file path if it's not (if supplied for the nodes)
for _, node := range coreProject.CompageJSON.Nodes {
absPath, err = filepath.Abs(node.License.Path)
if len(coreProject.License.Path) > 0 {
// assign absolute path to the license file Path if it's not
absPath, err := filepath.Abs(coreProject.License.Path)
if err != nil {
log.Errorf("error while getting absolute path [" + err.Error() + "]")
return err
}
node.License.Path = absPath
coreProject.License.Path = absPath
}

// assign absolute path to the license file path if it's not (if supplied for the nodes)
for _, node := range coreProject.CompageJSON.Nodes {
if len(node.License.Path) > 0 {
absPath, err := filepath.Abs(node.License.Path)
if err != nil {
log.Errorf("error while getting absolute path [" + err.Error() + "]")
return err
}
node.License.Path = absPath
}
}

// pull all required templates
Expand Down
13 changes: 13 additions & 0 deletions internal/languages/dotnet/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/intelops/compage/internal/languages/dotnet/integrations/docker"
"github.com/intelops/compage/internal/languages/dotnet/integrations/githubactions"
"github.com/intelops/compage/internal/languages/dotnet/integrations/kubernetes"
"github.com/intelops/compage/internal/languages/dotnet/integrations/license"
"github.com/intelops/compage/internal/languages/templates"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -113,6 +114,14 @@ func generateIntegrationConfig(dotNetValues *DotNetValues) error {
log.Errorf("error while getting the integrations copier [" + err.Error() + "]")
return err
}

// license files need to be generated for the whole project so, it should be here.
licenseCopier := m["license"].(*license.Copier)
if err = licenseCopier.CreateLicenseFiles(); err != nil {
log.Errorf("err : %s", err)
return err
}

// dockerfile needs to be generated for the whole project, so it should be here.
dockerCopier := m["docker"].(*docker.Copier)
if err = dockerCopier.CreateDockerFile(); err != nil {
Expand Down Expand Up @@ -159,6 +168,9 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{},
restServerPort = ""
}

// create dotnet specific licenseCopier
licenseCopier := license.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, dotNetValues.LDotNetLangNode.License)

// create dotnet specific dockerCopier
dockerCopier := docker.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, isRestServer, restServerPort)

Expand All @@ -172,5 +184,6 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{},
"docker": dockerCopier,
"k8s": k8sCopier,
"githubActions": githubActionsCopier,
"license": licenseCopier,
}, nil
}
56 changes: 56 additions & 0 deletions internal/languages/dotnet/integrations/license/copier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package license

import (
corenode "github.com/intelops/compage/internal/core/node"
"github.com/intelops/compage/internal/utils"
log "github.com/sirupsen/logrus"
)

// Copier integrations specific copier
type Copier struct {
NodeName string
NodeDirectoryName string
TemplatesRootPath string
License *corenode.License
Data map[string]interface{}
}

func NewCopier(gitRepositoryName, gitPlatformUserName, nodeName, nodeDirectoryName, templatesRootPath string, license *corenode.License) *Copier {
// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": gitRepositoryName,
"GitPlatformUserName": gitPlatformUserName,
}

return &Copier{
TemplatesRootPath: templatesRootPath,
NodeDirectoryName: nodeDirectoryName,
NodeName: nodeName,
Data: data,
License: license,
}
}

// CreateLicenseFiles creates the required directory and copies files from language template.
func (c Copier) CreateLicenseFiles() error {
destDirectory := c.NodeDirectoryName
if err := utils.CreateDirectories(destDirectory); err != nil {
log.Errorf("error while creating directories [" + err.Error() + "]")
return err
}
// copy license file if it's been supplied
if c.License != nil && len(c.License.URL) > 0 {
// read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow.
return utils.DownloadFile(c.NodeDirectoryName+"/LICENCE", c.License.URL)
} else if c.License != nil && len(c.License.Path) > 0 {
// local license file sent via config.yaml file.
// get the absolute path of the license file
_, err := utils.CopyFile(c.NodeDirectoryName+"/LICENCE", c.License.Path)
if err != nil {
log.Errorf("error while copying file [" + err.Error() + "]")
return err
}
}
// return from here as the license file has been copied
return nil
}
9 changes: 5 additions & 4 deletions internal/languages/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type LanguageNode struct {
Metadata map[string]interface{} `json:"metadata,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Language string `json:"language"`

RestConfig *corenode.RestConfig `json:"addRestConfig"`
GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"`
WsConfig *corenode.WsConfig `json:"wsConfig"`
License *corenode.License `json:"license"`
RestConfig *corenode.RestConfig `json:"addRestConfig"`
GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"`
WsConfig *corenode.WsConfig `json:"wsConfig"`
}

// NewLanguageNode converts node to LanguageNode struct
Expand All @@ -39,6 +39,7 @@ func NewLanguageNode(compageJSON *core.CompageJSON, node *corenode.Node) (*Langu
Metadata: node.Metadata,
Annotations: node.Annotations,
Language: node.Language,
License: node.License,
}

addRestConfig(node, languageNode)
Expand Down
3 changes: 1 addition & 2 deletions internal/utils/license-helper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"fmt"
log "github.com/sirupsen/logrus"
"io"
"net/http"
Expand Down Expand Up @@ -34,6 +33,6 @@ func DownloadFile(destination, src string) error {
defer func(file *os.File) {
_ = file.Close()
}(file)
fmt.Printf("Downloaded a file %s with size %d", src, size)
log.Debugf("Downloaded a file %s with size %d", src, size)
return nil
}

0 comments on commit 2849f22

Please sign in to comment.