Skip to content

Commit

Permalink
CORE-2170 support python-api template
Browse files Browse the repository at this point in the history
  • Loading branch information
baksetercx committed Dec 27, 2024
1 parent de6e985 commit c01b545
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ issues:
- linters:
- gochecknoglobals
text: "Dotnet.* is a global variable"
- linters:
- gochecknoglobals
text: "Python.* is a global variable"
- linters:
- gochecknoglobals
text: "Templates is a global variable"
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func generateDockerfile(
}

return dockerfile, buildContext, nil
} else if strings.HasSuffix(projectFile, "go.mod") {
} else if projectFile == "go.mod" {
dockerfile, buildContext, err := generateDockerfileForGo(
projectFile,
applicationName,
Expand All @@ -49,7 +49,7 @@ func generateDockerfile(
}

return dockerfile, buildContext, nil
} else if strings.HasSuffix(projectFile, "uv.lock") {
} else if projectFile == "pyproject.toml" {
dockerfile, buildContext, err := generateDockerfileForPython(
projectFile,
directory,
Expand Down
8 changes: 4 additions & 4 deletions pkg/build/generate_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/3lvia/cli/pkg/utils"
)

const DefaultPythonVersion = "3.13"

type DockerfileVariablesPython struct {
PythonVersion string
}
Expand Down Expand Up @@ -52,8 +54,6 @@ func generateDockerfileForPython(
}

func getPythonVersion(directories ...string) string {
const defaultPythonVersion = "3.13"

// removes duplicates
slices.Sort(directories)
directories = slices.Compact(directories)
Expand Down Expand Up @@ -121,9 +121,9 @@ func getPythonVersion(directories ...string) string {
style.PrintWarning(
fmt.Sprintf(
"Did not find any .python-version files, using default version %s.",
defaultPythonVersion,
DefaultPythonVersion,
),
)

return defaultPythonVersion
return DefaultPythonVersion
}
4 changes: 2 additions & 2 deletions pkg/build/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func TestGeneratePythonDockerfile1(t *testing.T) {
expectedBuildContext := "."

const (
projectFile = "uv.lock"
projectFile = "pyproject.toml"
applicationName = "demo-api-python"
)

Expand Down Expand Up @@ -345,7 +345,7 @@ func TestGeneratePythonDockerfile2(t *testing.T) {
}

tempDir := t.TempDir()
projectFile := filepath.Join(tempDir, "uv.lock")
projectFile := filepath.Join(tempDir, "pyproject.toml")
expectedBuildContext := tempDir

const applicationName = "demo-api-python"
Expand Down
82 changes: 61 additions & 21 deletions pkg/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"strings"

"github.com/3lvia/cli/pkg/build"
"github.com/3lvia/cli/pkg/command"
"github.com/3lvia/cli/pkg/githubactions"
"github.com/3lvia/cli/pkg/shared"
Expand All @@ -28,11 +29,13 @@ var (
// Dotnet8WebApp = Template{"dotnet8-webapp"}.
Dotnet8Worker = Template{"dotnet8-worker"}
// Go Template = Template{"go"}.
PythonAPI = Template{"python-api"}
Templates = enum.New(
Dotnet8WebAPI,
// Dotnet8WebApp,
Dotnet8Worker,
// Go,
PythonAPI,
)
)

Expand Down Expand Up @@ -83,6 +86,10 @@ var Command *cli.Command = &cli.Command{
Usage: "The root directory of your GitHub repository." +
" The path specified will be prepended to '.github/workflows'.",
},
&cli.StringFlag{
Name: "python-version",
Usage: "The version of Python to use for the project. Only applicable for Python templates.",
},
},
Action: Create,
}
Expand Down Expand Up @@ -118,6 +125,11 @@ func Create(ctx context.Context, c *cli.Command) error {

defaultBranch := c.String("default-branch")
nonInteractive := c.Bool("non-interactive")
pythonVersion := c.String("python-version")

if template != PythonAPI && c.IsSet("python-version") {
style.PrintWarning("Argument 'python-version' is only applicable for Python templates.")
}

checkCoooiecutterInstalledOutput := checkCookiecutterInstalledCommand(nil)
if command.IsError(checkCoooiecutterInstalledOutput) {
Expand Down Expand Up @@ -150,9 +162,9 @@ func Create(ctx context.Context, c *cli.Command) error {
outputDirectory,
applicationName,
systemName,
pythonVersion,
nil,
)

if command.IsError(cookiecutterOutput) {
return cli.Exit("Failed to create project.", 1)
}
Expand All @@ -166,6 +178,13 @@ func Create(ctx context.Context, c *cli.Command) error {
return cli.Exit(err, 1)
}

if template == PythonAPI {
uvSyncOutput := uvSyncCommand(projectDirectory, nil)
if command.IsError(uvSyncOutput) {
return cli.Exit("Failed to generate uv.lock file.", 1)
}
}

githubActionsDirectory := func() string {
if c.IsSet("github-actions-directory") {
return c.String("github-actions-directory")
Expand Down Expand Up @@ -214,10 +233,8 @@ func getProjectDirectoryForTemplate(
outputDirectory,
toPascalCaseWithoutHyphens(applicationName),
), nil
/*
case Go:
return path.Join(outputDirectory, applicationName), nil
*/
case PythonAPI /*, Go*/ :
return path.Join(outputDirectory, applicationName), nil
default:
return "", fmt.Errorf("Could not find project directory for template '%s'", template)
}
Expand All @@ -234,6 +251,8 @@ func getProjectFileForTemplate(
case Go:
return "go.mod", nil
*/
case PythonAPI:
return "pyproject.toml", nil
default:
return "", fmt.Errorf("Could not find project file for template '%s'", template)
}
Expand All @@ -244,25 +263,31 @@ func cookiecutterCommand(
outputDirectory string,
applicationName string,
systemName string,
pythonVersion string,
options *command.RunOptions,
) command.Output {
return command.Run(
*exec.Command(
"cookiecutter",
"gh:3lvia/application-templates",
"--directory",
template.Value,
"--output-dir",
outputDirectory,
"--no-input",
"application_name="+applicationName,
"application_name_pascal_case="+toPascalCaseWithoutHyphens(applicationName),
"system_name="+systemName,
// TODO: is this needed?
"base_dir=./",
),
options,
cmd := *exec.Command(
"cookiecutter",
"gh:3lvia/application-templates",
"--directory",
template.Value,
"--output-dir",
outputDirectory,
"--no-input",
"application_name="+applicationName,
"application_name_pascal_case="+toPascalCaseWithoutHyphens(applicationName),
"system_name="+systemName,
)

if template == PythonAPI {
if pythonVersion == "" {
cmd.Args = append(cmd.Args, "python_version="+build.DefaultPythonVersion)
} else {
cmd.Args = append(cmd.Args, "python_version="+pythonVersion)
}
}

return command.Run(cmd, options)
}

func checkCookiecutterInstalledCommand(
Expand Down Expand Up @@ -303,3 +328,18 @@ func installCookiecutterCommand(
options,
)
}

func uvSyncCommand(
projectDirectory string,
options *command.RunOptions,
) command.Output {
return command.Run(
*exec.Command(
"uv",
"sync",
"--directory",
projectDirectory,
),
options,
)
}
21 changes: 21 additions & 0 deletions pkg/githubactions/githubactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ func getLanguageFromProjectFile(projectFile string) (string, error) {
return "go", nil
}

if projectFile == "pyproject.toml" {
return "python", nil
}

if strings.Contains(projectFile, "Dockerfile") {
return "dockerfile", nil
}
Expand Down Expand Up @@ -362,6 +366,23 @@ func getExampleWorkflowFileURL(language string, runtimeCloudProvider string) (st
)
}

// Python
if language == "python" && runtimeCloudProvider == "aks" {
return exampleWorkflowBaseURL + "/build-deploy-python.yml", nil
}

if language == "python" && runtimeCloudProvider == "gke" {
return exampleWorkflowBaseURL + "/build-deploy-python-google.yml", nil
}

if language == "python" && runtimeCloudProvider == "iss" {
return "",
fmt.Errorf("Example workflow is not implemented yet for language '%s' and runtime cloud provider '%s'",
language,
runtimeCloudProvider,
)
}

// Dockerfile
if language == "dockerfile" && runtimeCloudProvider == "aks" {
return exampleWorkflowBaseURL + "/build-deploy-dockerfile.yml", nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/shared/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ProjectFileFlag() *cli.StringFlag {
Name: "project-file",
Aliases: []string{"f"},
Usage: "The project file to use. We currently support .NET (*.csproj), Go (go.mod)," +
" Python with uv (uv.lock) or a generic Docker project (Dockerfile).",
" Python with uv (pyproject.toml) or a generic Docker project (Dockerfile).",
}
}

Expand Down

0 comments on commit c01b545

Please sign in to comment.