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

check for annotated file as entrypoint #225

Merged
merged 2 commits into from
Feb 14, 2023
Merged
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
27 changes: 14 additions & 13 deletions pkg/lang/javascript/plugin_nodejs_executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,31 @@ func (l NodeJSExecutable) Transform(result *core.CompilationResult, dependencies
unit.Executable.Type = core.ExecutableTypeNodeJS

var err error
if len(unit.Executable.Entrypoints) > 0 {
err = refreshSourceFiles(unit)
if err != nil {
return err
for _, file := range unit.FilesOfLang(js) {
for _, annot := range file.Annotations() {
cap := annot.Capability
if cap.Name == annotation.ExecutionUnitCapability && cap.ID == unit.Name {
unit.AddEntrypoint(file)
}
}
refreshUpstreamEntrypoints(unit)
}

if len(unit.Executable.Entrypoints) == 0 {
err = addEntrypointFromPackageJson(packageJson, unit)
if err != nil {
return core.WrapErrf(err, "entrypoint resolution from package.json failed for execution unit: %s", unit.Name)
}
}

if len(unit.Executable.Entrypoints) == 0 {
resolveDefaultEntrypoint(unit)
}
if len(unit.Executable.Entrypoints) == 0 {
resolveDefaultEntrypoint(unit)
}

err = refreshSourceFiles(unit)
if err != nil {
return err
}
refreshUpstreamEntrypoints(unit)
err = refreshSourceFiles(unit)
if err != nil {
return err
}
refreshUpstreamEntrypoints(unit)
}
return nil
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/lang/javascript/plugin_nodejs_executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,58 @@ func TestNodeJSExecutable_Transform(t *testing.T) {
},
},
},
{
name: "annotated file is added",
otherFiles: map[string]string{"package.json": `{ "main" : "" }`},
units: []*core.ExecutionUnit{
execUnit("unit1",
taggedFile{path: "expose.js", content: `
/* @klotho::expose {
* id = "gateway"
* }
*/
const index = require('./entrypoint');`,
},
taggedFile{path: "entrypoint.js", content: "const mod = require('./module')"},
taggedFile{path: "module.js"}),
execUnit("unit2",
taggedFile{path: "expose.js", content: `
/* @klotho::expose {
* id = "gateway"
* }
*/
const index = require('./entrypoint');`,
},
taggedFile{path: "index.js", content: `
/* @klotho::execution_unit {
* id = "unit2"
* }
*/
const mod = require('./module')"}
`, tag: "source"},
taggedFile{path: "module.js"}),
},
expectedUnits: map[string]expectedUnit{
"unit1": {
executableType: core.ExecutableTypeNodeJS,
expectedFiles: map[string][]string{
"allFiles": {"package.json", "entrypoint.js", "module.js", "expose.js"},
"resources": {"package.json"},
"sourceFiles": {},
"entrypoints": {},
},
},
"unit2": {
executableType: core.ExecutableTypeNodeJS,
expectedFiles: map[string][]string{
"allFiles": {"package.json", "index.js", "module.js", "expose.js"},
"resources": {"package.json"},
"sourceFiles": {"index.js", "module.js"},
"entrypoints": {"index.js"},
},
},
},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/lang/python/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/smacker/go-tree-sitter/python"
)

const py = core.LanguageId("python")

var Language = core.SourceLanguage{
ID: core.LanguageId("python"),
ID: py,
Sitter: python.GetLanguage(),
CapabilityFinder: lang.NewCapabilityFinder("comment", lang.RegexpRemovePreprocessor(`^#\s*`)),
TurnIntoComment: lang.MakeLineCommenter("# "),
Expand Down
21 changes: 11 additions & 10 deletions pkg/lang/python/plugin_python_executable.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,24 @@ func (l PythonExecutable) Transform(result *core.CompilationResult, dependencies
unit.AddResource(requirementsTxt.Clone())
unit.Executable.Type = core.ExecutableTypePython

if len(unit.Executable.Entrypoints) > 0 {
err := refreshSourceFiles(unit)
if err != nil {
return err
for _, file := range unit.FilesOfLang(py) {
for _, annot := range file.Annotations() {
cap := annot.Capability
if cap.Name == annotation.ExecutionUnitCapability && cap.ID == unit.Name {
unit.AddEntrypoint(file)
}
}
refreshUpstreamEntrypoints(unit)
}

if len(unit.Executable.Entrypoints) == 0 {
resolveDefaultEntrypoint(unit)
}

err := refreshSourceFiles(unit)
if err != nil {
return err
}
refreshUpstreamEntrypoints(unit)
err := refreshSourceFiles(unit)
if err != nil {
return err
}
refreshUpstreamEntrypoints(unit)
}
return nil
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/lang/python/plugin_python_executable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,54 @@ func TestPythonExecutable_Transform(t *testing.T) {
},
},
},
{
name: "annotated file is added",
otherFiles: map[string]string{"requirements.txt": ""},
units: []*core.ExecutionUnit{
execUnit("unit1",
taggedFile{path: "app/expose.py", content: `
# @klotho::expose {
# id = "gateway"
# }
import app.entrypoint`,
},
taggedFile{path: "app/entrypoint.py", content: `
# @klotho::execution_unit { id = "unit1" }
import app.module`},
taggedFile{path: "app/module.py"}),
execUnit("unit2",
taggedFile{path: "app/expose.py", content: `
# @klotho::expose {
# id = "gateway"
# }
import app.main`,
},
taggedFile{path: "app/main.py", content: `
# @klotho::execution_unit { id = "unit2" }
import app.module`},
taggedFile{path: "app/module.py"}),
},
expectedUnits: map[string]expectedUnit{
"unit1": {
executableType: core.ExecutableTypePython,
expectedFiles: map[string][]string{
"allFiles": {"requirements.txt", "app/entrypoint.py", "app/module.py", "app/expose.py"},
"resources": {"requirements.txt"},
"sourceFiles": {"app/entrypoint.py", "app/module.py", "app/expose.py"},
"entrypoints": {"app/entrypoint.py", "app/expose.py"},
},
},
"unit2": {
executableType: core.ExecutableTypePython,
expectedFiles: map[string][]string{
"allFiles": {"requirements.txt", "app/main.py", "app/module.py", "app/expose.py"},
"resources": {"requirements.txt"},
"sourceFiles": {"app/main.py", "app/module.py", "app/expose.py"},
"entrypoints": {"app/main.py", "app/expose.py"},
},
},
},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
Expand Down