Skip to content

Commit 2849f22

Browse files
feat: added license generation for dotnet
1 parent a842898 commit 2849f22

File tree

6 files changed

+92
-18
lines changed

6 files changed

+92
-18
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"mode": "debug",
99
"program": "${workspaceRoot}/main.go",
1010
"cwd": "${workspaceRoot}",
11-
"args": ["pullTemplates"]
11+
"args": ["generate"]
1212
}
1313
]
1414
}

cmd/generate.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,26 @@ func GenerateCode() error {
6262
return err
6363
}
6464

65-
// assign absolute path to the license file Path if it's not
66-
absPath, err := filepath.Abs(coreProject.License.Path)
67-
if err != nil {
68-
log.Errorf("error while getting absolute path [" + err.Error() + "]")
69-
return err
70-
}
71-
coreProject.License.Path = absPath
72-
// assign absolute path to the license file path if it's not (if supplied for the nodes)
73-
for _, node := range coreProject.CompageJSON.Nodes {
74-
absPath, err = filepath.Abs(node.License.Path)
65+
if len(coreProject.License.Path) > 0 {
66+
// assign absolute path to the license file Path if it's not
67+
absPath, err := filepath.Abs(coreProject.License.Path)
7568
if err != nil {
7669
log.Errorf("error while getting absolute path [" + err.Error() + "]")
7770
return err
7871
}
79-
node.License.Path = absPath
72+
coreProject.License.Path = absPath
73+
}
74+
75+
// assign absolute path to the license file path if it's not (if supplied for the nodes)
76+
for _, node := range coreProject.CompageJSON.Nodes {
77+
if len(node.License.Path) > 0 {
78+
absPath, err := filepath.Abs(node.License.Path)
79+
if err != nil {
80+
log.Errorf("error while getting absolute path [" + err.Error() + "]")
81+
return err
82+
}
83+
node.License.Path = absPath
84+
}
8085
}
8186

8287
// pull all required templates

internal/languages/dotnet/generator.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/intelops/compage/internal/languages/dotnet/integrations/docker"
1212
"github.com/intelops/compage/internal/languages/dotnet/integrations/githubactions"
1313
"github.com/intelops/compage/internal/languages/dotnet/integrations/kubernetes"
14+
"github.com/intelops/compage/internal/languages/dotnet/integrations/license"
1415
"github.com/intelops/compage/internal/languages/templates"
1516
"github.com/intelops/compage/internal/utils"
1617
log "github.com/sirupsen/logrus"
@@ -113,6 +114,14 @@ func generateIntegrationConfig(dotNetValues *DotNetValues) error {
113114
log.Errorf("error while getting the integrations copier [" + err.Error() + "]")
114115
return err
115116
}
117+
118+
// license files need to be generated for the whole project so, it should be here.
119+
licenseCopier := m["license"].(*license.Copier)
120+
if err = licenseCopier.CreateLicenseFiles(); err != nil {
121+
log.Errorf("err : %s", err)
122+
return err
123+
}
124+
116125
// dockerfile needs to be generated for the whole project, so it should be here.
117126
dockerCopier := m["docker"].(*docker.Copier)
118127
if err = dockerCopier.CreateDockerFile(); err != nil {
@@ -159,6 +168,9 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{},
159168
restServerPort = ""
160169
}
161170

171+
// create dotnet specific licenseCopier
172+
licenseCopier := license.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, dotNetValues.LDotNetLangNode.License)
173+
162174
// create dotnet specific dockerCopier
163175
dockerCopier := docker.NewCopier(gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, dotNetTemplatesRootPath, isRestServer, restServerPort)
164176

@@ -172,5 +184,6 @@ func getIntegrationsCopier(dotNetValues *DotNetValues) (map[string]interface{},
172184
"docker": dockerCopier,
173185
"k8s": k8sCopier,
174186
"githubActions": githubActionsCopier,
187+
"license": licenseCopier,
175188
}, nil
176189
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package license
2+
3+
import (
4+
corenode "github.com/intelops/compage/internal/core/node"
5+
"github.com/intelops/compage/internal/utils"
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
// Copier integrations specific copier
10+
type Copier struct {
11+
NodeName string
12+
NodeDirectoryName string
13+
TemplatesRootPath string
14+
License *corenode.License
15+
Data map[string]interface{}
16+
}
17+
18+
func NewCopier(gitRepositoryName, gitPlatformUserName, nodeName, nodeDirectoryName, templatesRootPath string, license *corenode.License) *Copier {
19+
// populate map to replace templates
20+
data := map[string]interface{}{
21+
"GitRepositoryName": gitRepositoryName,
22+
"GitPlatformUserName": gitPlatformUserName,
23+
}
24+
25+
return &Copier{
26+
TemplatesRootPath: templatesRootPath,
27+
NodeDirectoryName: nodeDirectoryName,
28+
NodeName: nodeName,
29+
Data: data,
30+
License: license,
31+
}
32+
}
33+
34+
// CreateLicenseFiles creates the required directory and copies files from language template.
35+
func (c Copier) CreateLicenseFiles() error {
36+
destDirectory := c.NodeDirectoryName
37+
if err := utils.CreateDirectories(destDirectory); err != nil {
38+
log.Errorf("error while creating directories [" + err.Error() + "]")
39+
return err
40+
}
41+
// copy license file if it's been supplied
42+
if c.License != nil && len(c.License.URL) > 0 {
43+
// read file from url in c.License.URL. This is applicable for both config.yaml file and ui flow.
44+
return utils.DownloadFile(c.NodeDirectoryName+"/LICENCE", c.License.URL)
45+
} else if c.License != nil && len(c.License.Path) > 0 {
46+
// local license file sent via config.yaml file.
47+
// get the absolute path of the license file
48+
_, err := utils.CopyFile(c.NodeDirectoryName+"/LICENCE", c.License.Path)
49+
if err != nil {
50+
log.Errorf("error while copying file [" + err.Error() + "]")
51+
return err
52+
}
53+
}
54+
// return from here as the license file has been copied
55+
return nil
56+
}

internal/languages/languages.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ type LanguageNode struct {
2525
Metadata map[string]interface{} `json:"metadata,omitempty"`
2626
Annotations map[string]string `json:"annotations,omitempty"`
2727
Language string `json:"language"`
28-
29-
RestConfig *corenode.RestConfig `json:"addRestConfig"`
30-
GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"`
31-
WsConfig *corenode.WsConfig `json:"wsConfig"`
28+
License *corenode.License `json:"license"`
29+
RestConfig *corenode.RestConfig `json:"addRestConfig"`
30+
GrpcConfig *corenode.GrpcConfig `json:"grpcConfig"`
31+
WsConfig *corenode.WsConfig `json:"wsConfig"`
3232
}
3333

3434
// NewLanguageNode converts node to LanguageNode struct
@@ -39,6 +39,7 @@ func NewLanguageNode(compageJSON *core.CompageJSON, node *corenode.Node) (*Langu
3939
Metadata: node.Metadata,
4040
Annotations: node.Annotations,
4141
Language: node.Language,
42+
License: node.License,
4243
}
4344

4445
addRestConfig(node, languageNode)

internal/utils/license-helper.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package utils
22

33
import (
4-
"fmt"
54
log "github.com/sirupsen/logrus"
65
"io"
76
"net/http"
@@ -34,6 +33,6 @@ func DownloadFile(destination, src string) error {
3433
defer func(file *os.File) {
3534
_ = file.Close()
3635
}(file)
37-
fmt.Printf("Downloaded a file %s with size %d", src, size)
36+
log.Debugf("Downloaded a file %s with size %d", src, size)
3837
return nil
3938
}

0 commit comments

Comments
 (0)