Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new language #173

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@
[submodule "core/templates/compage-template-typescript"]
path = core/templates/compage-template-typescript
url = https://github.com/intelops/compage-template-typescript.git
branch = template-v1
branch = template-v1
[submodule "core/templates/compage-template-dotnet"]
path = core/templates/compage-template-dotnet
url = https://github.com/intelops/compage-template-dotnet.git
2 changes: 1 addition & 1 deletion app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
related details.
- It's an Express.js based REST server and gRPC client to a core component.
- The sqlite is used as a database for development. To use cassandra, you need to set the following environment
variables in your terminal along with a `environment DB_TYPE='cassandra'`.
variables in your terminal along with a `export DB_TYPE='cassandra'`.

#### Use cassandra as a database

Expand Down
27 changes: 15 additions & 12 deletions app/src/integrations/simple-git/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@
branchName = repositoryBranch + '-' + projectVersion;
}

// checkoutLocalBranch checks out local branch with name supplied
await git.checkoutLocalBranch(branchName)
.then(
(success: any) => {
Logger.debug(`git checkoutLocalBranch succeeded: ${JSON.stringify(success)}`);
}, (failure: any) => {
Logger.debug(`git checkoutLocalBranch failed: ${JSON.stringify(failure)}`);
error = `git checkoutLocalBranch failed: ${JSON.stringify(failure)}`;
});
if (error.length > 0) {
return error;
const branchSummary = await git.branch();
const isBranchPresent = branchSummary?.branches[branchName];
if (!isBranchPresent) {
// checkoutLocalBranch checks out local branch with name supplied
await git.checkoutLocalBranch(branchName)
.then(
(success: any) => {
Logger.debug(`git checkoutLocalBranch succeeded: ${JSON.stringify(success)}`);
}, (failure: any) => {
Logger.debug(`git checkoutLocalBranch failed: ${JSON.stringify(failure)}`);
error = `git checkoutLocalBranch failed: ${JSON.stringify(failure)}`;
});
if (error.length > 0) {
return error;
}

Check warning on line 59 in app/src/integrations/simple-git/common.ts

View check run for this annotation

Codecov / codecov/patch

app/src/integrations/simple-git/common.ts#L45-L59

Added lines #L45 - L59 were not covered by tests
}

// Finally, push to online repository
await git.push('origin', branchName, {'--force': null})
.then((success: any) => {
Expand Down
2 changes: 1 addition & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- It's a Golang based gRPC server to app component.

#### How to run this component?
- Navigate to core directory [cd app] from root directory of compage.
- Navigate to core directory [cd core] from root directory of compage.
- Fire below set of commands in sequence to initialize the git submodules.
- `git submodule init`
- `git submodule update --remote`
Expand Down
8 changes: 8 additions & 0 deletions core/internal/handlers/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/intelops/compage/core/internal/integrations/deepsource"
"github.com/intelops/compage/core/internal/integrations/readme"
"github.com/intelops/compage/core/internal/languages"
"github.com/intelops/compage/core/internal/languages/dotnet"
"github.com/intelops/compage/core/internal/languages/golang"
"github.com/intelops/compage/core/internal/languages/java"
"github.com/intelops/compage/core/internal/languages/javascript"
Expand Down Expand Up @@ -135,6 +136,13 @@ func runLanguageProcess(languageNode *languages.LanguageNode, languageCtx contex
log.Debugf("err : %s", err1)
return err1
}
} else if languageNode.Language == languages.DotNet {
// add values(LanguageNode and configs from coreProject) to context.
dotnetCtx := dotnet.AddValuesToContext(languageCtx)
if err1 := dotnet.Process(dotnetCtx); err1 != nil {
log.Debugf("err : %s", err1)
return err1
}
}
return nil
}
33 changes: 33 additions & 0 deletions core/internal/languages/dotnet/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dotnet

import (
"context"
"github.com/intelops/compage/core/internal/languages"
)

type contextKey string

func (c contextKey) String() string {
return string(c)
}

var (
contextKeyDotNetContextVars = contextKey("DotNetContextVars")
)

type DotNetValues struct {
Values *languages.Values
LDotNetLangNode *LDotNetLangNode
}

func AddValuesToContext(ctx context.Context) context.Context {
values := ctx.Value(languages.ContextKeyLanguageContextVars).(languages.Values)
v := DotNetValues{
Values: &values,
LDotNetLangNode: &LDotNetLangNode{
LanguageNode: values.LanguageNode,
},
}

return context.WithValue(ctx, contextKeyDotNetContextVars, v)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package dotnet_mvc_framework

import (
"github.com/gertd/go-pluralize"
"github.com/intelops/compage/core/internal/languages/executor"
log "github.com/sirupsen/logrus"

"strings"

"github.com/intelops/compage/core/internal/utils"
)

const RestServerPath = "/pkg/rest/server"

// Copier Language specific *Copier
type Copier struct {
NodeDirectoryName string
TemplatesRootPath string
Data map[string]interface{}
IsRestServer bool
RestServerPort string
PluralizeClient *pluralize.Client
}

func NewCopier(gitPlatformURL, gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, templatesRootPath string) *Copier {

pluralizeClient := pluralize.NewClient()

// populate map to replace templates
data := map[string]interface{}{
"GitRepositoryName": gitRepositoryName,
"NodeName": strings.ToLower(nodeName),
"GitPlatformUserName": gitPlatformUserName,
"GitPlatformURL": strings.Replace(gitPlatformURL, "https://", "", -1),
}

return &Copier{
TemplatesRootPath: templatesRootPath,
NodeDirectoryName: nodeDirectoryName,
Data: data,
PluralizeClient: pluralizeClient,
}
}

// createRestServerDirectories creates rest server directories.
func (c *Copier) createRestServerDirectories() error {
rootDirectory := c.NodeDirectoryName
if err := utils.CreateDirectories(rootDirectory); err != nil {
log.Debugf("error creating root directory: %v", err)
return err
}
return nil
}

// CreateRestConfigs creates/copies relevant files to generated project
func (c *Copier) CreateRestConfigs() error {
if err := c.CreateRestServer(); err != nil {
log.Debugf("error creating rest server: %v", err)
return err
}
return nil
}

// CreateRestServer creates/copies relevant files to generated project
func (c *Copier) CreateRestServer() error {
// if the node is server, add server code
if c.IsRestServer {
// create directories for controller, service, dao, models
if err := c.createRestServerDirectories(); err != nil {
log.Debugf("error creating rest server directories: %v", err)
return err
}
}
return nil
}

// CreateRootLevelFiles copies all root level files at language template.
func (c *Copier) CreateRootLevelFiles() error {
err := utils.CopyFiles(c.NodeDirectoryName, c.TemplatesRootPath)
if err != nil {
return err
}
_, files, err0 := utils.GetDirectoriesAndFilePaths(c.NodeDirectoryName)
if err0 != nil {
return err0
}
return executor.Execute(files, c.Data)
}
63 changes: 63 additions & 0 deletions core/internal/languages/dotnet/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dotnet

import (
"context"
"fmt"
"github.com/intelops/compage/core/internal/languages"
dotnetmvcframework "github.com/intelops/compage/core/internal/languages/dotnet/frameworks/dotnet-mvc-framework"
"github.com/intelops/compage/core/internal/languages/templates"
log "github.com/sirupsen/logrus"
)

// Generate generates dotnet specific code according to config passed
func Generate(ctx context.Context) error {
// extract goNode
goValues := ctx.Value(contextKeyDotNetContextVars).(DotNetValues)
// rest config
err := generateRESTConfig(ctx, &goValues)
if err != nil {
log.Debugf("err : %s", err)
return err
}

return nil
}

func generateRESTConfig(ctx context.Context, dotNetValues *DotNetValues) error {
n := dotNetValues.LDotNetLangNode
if n.RestConfig != nil {
// check for the templates
if n.RestConfig.Template == templates.Compage {
if n.RestConfig.Framework == NetMVCFramework {
dotNetMVCFrameworkCopier := getDotNetMVCFrameworkCopier(dotNetValues)
if n.RestConfig.Server != nil {
if err := dotNetMVCFrameworkCopier.CreateRestServer(); err != nil {
log.Debugf("err : %s", err)
return err
}
}
// copy all files at root level, fire this at last
if err := dotNetMVCFrameworkCopier.CreateRootLevelFiles(); err != nil {
log.Debugf("err : %s", err)
return err
}
} else {
return fmt.Errorf("unsupported framework %s for template %s for language %s", n.RestConfig.Framework, n.RestConfig.Template, n.Language)
}
}
}

return nil
}

func getDotNetMVCFrameworkCopier(dotNetValues *DotNetValues) *dotnetmvcframework.Copier {
gitPlatformURL := dotNetValues.Values.Get(languages.GitPlatformURL)
gitPlatformUserName := dotNetValues.Values.Get(languages.GitPlatformUserName)
gitRepositoryName := dotNetValues.Values.Get(languages.GitRepositoryName)
nodeName := dotNetValues.Values.Get(languages.NodeName)
nodeDirectoryName := dotNetValues.Values.NodeDirectoryName
path := GetDotNetTemplatesRootPath() + "/frameworks/" + NetMVCFramework

copier := dotnetmvcframework.NewCopier(gitPlatformURL, gitPlatformUserName, gitRepositoryName, nodeName, nodeDirectoryName, path)
return copier
}
26 changes: 26 additions & 0 deletions core/internal/languages/dotnet/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dotnet

import (
"github.com/intelops/compage/core/internal/languages"
"github.com/intelops/compage/core/internal/utils"
)

// TemplatesPath directory of template files
const TemplatesPath = "templates/compage-template-dotnet"
const NetMVCFramework = "dotnet-mvc-framework"

var templatesRootPath = utils.GetProjectRootPath(TemplatesPath)

// LDotNetLangNode language specific struct.
type LDotNetLangNode struct {
*languages.LanguageNode
}

// FillDefaults constructor function
func (n *LDotNetLangNode) FillDefaults() error {
return nil
}

func GetDotNetTemplatesRootPath() string {
return templatesRootPath
}
24 changes: 24 additions & 0 deletions core/internal/languages/dotnet/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dotnet

import (
"context"
log "github.com/sirupsen/logrus"
)

func Process(ctx context.Context) error {
dotNetValues := ctx.Value(contextKeyDotNetContextVars).(DotNetValues)

// fills default config for DotNet
if err := dotNetValues.LDotNetLangNode.FillDefaults(); err != nil {
log.Debugf("err : %s", err)
return err
}

// generate DotNet project
if err := Generate(ctx); err != nil {
log.Debugf("err : %s", err)
return err
}

return nil
}
1 change: 1 addition & 0 deletions core/internal/languages/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const Rust = "rust"
const JavaScript = "javascript"
const TypeScript = "typescript"
const Ruby = "ruby"
const DotNet = "dotnet"

type LanguageNode struct {
ID string `json:"ID"`
Expand Down
4 changes: 2 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var (

func main() {
// grpc server configuration
// Initialize the exporter
// Initializes the exporter
var grpcTraceProvider *sdktrace.TracerProvider
if len(serviceName) > 0 && len(collectorURL) > 0 {
// add opentel
// add open telemetry tracing
grpcTraceProvider = config.InitGrpcTracer(serviceName, collectorURL, insecure)
}
defer func() {
Expand Down
1 change: 1 addition & 0 deletions core/templates/compage-template-dotnet
Loading
Loading