This repository was archived by the owner on Mar 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
C# support foundations #161
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package csharp | ||
|
||
import ( | ||
"io" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/klothoplatform/klotho/pkg/core" | ||
"github.com/klothoplatform/klotho/pkg/lang" | ||
"github.com/smacker/go-tree-sitter/csharp" | ||
) | ||
|
||
var multilineCommentMarginRegexp = regexp.MustCompile(`(?m)^\s*[*]*[ \t]*`) // we need to use [ \t] instead of \s, because \s includes newlines in (?m) mode. | ||
|
||
var Language = core.SourceLanguage{ | ||
ID: core.LanguageId("csharp"), | ||
Sitter: csharp.GetLanguage(), | ||
CapabilityFinder: lang.NewCapabilityFinder("comment", lang.CompositePreprocessor( | ||
lang.RegexpRemovePreprocessor(`//\s*`), | ||
func(comment string) string { | ||
// Check for comments starting with `/*`. | ||
// If you don't find one, just return this comment unchanged. | ||
// If you do find one, snip off the start and end chars, as well as any `*`s that prefix a line | ||
// (this is a common style for giving the comment a left border). | ||
if !strings.HasPrefix(comment, "/*") { | ||
return comment | ||
} | ||
// The comment is something like: | ||
// /** foo | ||
// * bar | ||
// */ | ||
// | ||
// First, we'll trim the opening and closing slashes, to get it to: | ||
// ** foo | ||
// * bar | ||
// * | ||
// | ||
// Then, we'll use a regexp to remove an opening stretch of `*`s from each line | ||
comment = comment[1 : len(comment)-1] | ||
comment = multilineCommentMarginRegexp.ReplaceAllString(comment, "") | ||
// `/*`-style comments never combine with subsequent comments | ||
return comment | ||
}, | ||
)), | ||
TurnIntoComment: lang.MakeLineCommenter("// "), | ||
} | ||
|
||
func NewFile(path string, content io.Reader) (f *core.SourceFile, err error) { | ||
return core.NewSourceFile(path, content, Language) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package csharp | ||
|
||
import ( | ||
"github.com/klothoplatform/klotho/pkg/config" | ||
"github.com/klothoplatform/klotho/pkg/core" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ( | ||
CSharpPlugins struct { | ||
Plugins []core.Plugin | ||
} | ||
) | ||
|
||
func NewCSharpPlugins(cfg *config.Application, runtime Runtime) *CSharpPlugins { | ||
return &CSharpPlugins{ | ||
Plugins: []core.Plugin{}, | ||
} | ||
} | ||
|
||
func (c CSharpPlugins) Name() string { return "C#" } | ||
|
||
func (c CSharpPlugins) Transform(result *core.CompilationResult, deps *core.Dependencies) error { | ||
for _, p := range c.Plugins { | ||
log := zap.L().With(zap.String("plugin", p.Name())) | ||
log.Debug("starting") | ||
err := p.Transform(result, deps) | ||
if err != nil { | ||
return core.NewPluginError(p.Name(), err) | ||
} | ||
log.Debug("completed") | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package csharp | ||
|
||
import ( | ||
"github.com/klothoplatform/klotho/pkg/query" | ||
sitter "github.com/smacker/go-tree-sitter" | ||
) | ||
|
||
// DoQuery is a thin wrapper around `query.Exec` to use C# as the Language. | ||
func DoQuery(c *sitter.Node, q string) query.NextMatchFunc { | ||
return query.Exec(Language, c, q) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package csharp | ||
|
||
type ( | ||
Runtime interface { | ||
} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package aws_runtime | ||
|
||
import ( | ||
"github.com/klothoplatform/klotho/pkg/config" | ||
"github.com/klothoplatform/klotho/pkg/provider/aws" | ||
) | ||
|
||
type AwsRuntime struct { | ||
TemplateConfig aws.TemplateConfig | ||
Cfg *config.Application | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package runtimes | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/klothoplatform/klotho/pkg/config" | ||
"github.com/klothoplatform/klotho/pkg/lang/csharp" | ||
aws_runtime "github.com/klothoplatform/klotho/pkg/lang/csharp/runtimes/aws" | ||
"github.com/klothoplatform/klotho/pkg/provider" | ||
"github.com/klothoplatform/klotho/pkg/provider/aws" | ||
) | ||
|
||
func GetRuntime(cfg *config.Application) (csharp.Runtime, error) { | ||
switch cfg.Provider { | ||
case "gcp", "azure": | ||
// TODO GCP and Azure is hacked to be the same as AWS so we can generate a topology diagram, but the compilation won't work. | ||
fallthrough | ||
case "aws": | ||
return &aws_runtime.AwsRuntime{ | ||
Cfg: cfg, | ||
TemplateConfig: aws.TemplateConfig{ | ||
TemplateConfig: provider.TemplateConfig{ | ||
AppName: cfg.AppName, | ||
}, | ||
PayloadsBucketName: aws.SanitizeS3BucketName(cfg.AppName), | ||
}, | ||
}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("could not get C# runtime for provider: %v", cfg.Provider) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this definitely needs a better implementation.