Skip to content

Commit 0c89684

Browse files
feat: added LICENSE to all generated files
1 parent 3d4b0c6 commit 0c89684

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

internal/handlers/projects.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/intelops/compage/internal/core"
77
corenode "github.com/intelops/compage/internal/core/node"
88
"github.com/intelops/compage/internal/integrations/deepsource"
9+
"github.com/intelops/compage/internal/integrations/license"
910
"github.com/intelops/compage/internal/integrations/readme"
1011
"github.com/intelops/compage/internal/languages"
1112
"github.com/intelops/compage/internal/languages/dotnet"
@@ -63,6 +64,16 @@ func Handle(coreProject *core.Project) error {
6364
return err
6465
}
6566

67+
// add LICENSE at project level
68+
licenseCopier, err := license.NewCopier(coreProject)
69+
if err != nil {
70+
return errors.New("license copier is nil")
71+
}
72+
if err = licenseCopier.CreateLicenseFiles(); err != nil {
73+
log.Errorf("error while creating LICENSE file [" + err.Error() + "]")
74+
return err
75+
}
76+
6677
return nil
6778
}
6879

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package license
2+
3+
import (
4+
"github.com/intelops/compage/internal/core"
5+
"github.com/intelops/compage/internal/languages/executor"
6+
"github.com/intelops/compage/internal/utils"
7+
log "github.com/sirupsen/logrus"
8+
)
9+
10+
const File = "LICENSE.tmpl"
11+
12+
// Copier integrations specific copier
13+
type Copier struct {
14+
ProjectDirectoryName string
15+
GitRepositoryName string
16+
TemplatesRootPath string
17+
Data map[string]interface{}
18+
}
19+
20+
func NewCopier(project *core.Project) (*Copier, error) {
21+
// retrieve project named directory
22+
//gitPlatformUserName, gitRepositoryName, projectDirectoryName, templatesRootPath string
23+
// populate map to replace templates
24+
data := map[string]interface{}{
25+
"GitRepositoryName": project.GitRepositoryName,
26+
"GitPlatformUserName": project.GitPlatformUserName,
27+
}
28+
29+
templatesRootPath, err := utils.GetTemplatesRootPath("common-templates")
30+
if err != nil {
31+
log.Errorf("error while getting the project root path [" + err.Error() + "]")
32+
return nil, err
33+
}
34+
return &Copier{
35+
// TODO change this path to constant. Add language specific analysers in a generic way later.
36+
TemplatesRootPath: templatesRootPath,
37+
ProjectDirectoryName: utils.GetProjectDirectoryName(project.Name),
38+
GitRepositoryName: project.GitRepositoryName,
39+
Data: data,
40+
}, nil
41+
}
42+
43+
// CreateLicenseFiles creates the required directory and copies files from language template.
44+
func (c Copier) CreateLicenseFiles() error {
45+
destDirectory := c.ProjectDirectoryName
46+
if err := utils.CreateDirectories(destDirectory); err != nil {
47+
log.Errorf("error while creating directories [" + err.Error() + "]")
48+
return err
49+
}
50+
51+
var filePaths []*string
52+
// copy deployment files to generated license config files
53+
targetLicenseFileName := c.ProjectDirectoryName + "/" + File
54+
_, err := utils.CopyFile(targetLicenseFileName, c.TemplatesRootPath+"/"+File)
55+
if err != nil {
56+
log.Errorf("error while copying file [" + err.Error() + "]")
57+
return err
58+
}
59+
filePaths = append(filePaths, &targetLicenseFileName)
60+
return executor.Execute(filePaths, c.Data)
61+
}

0 commit comments

Comments
 (0)