Skip to content

Commit

Permalink
Fix auto dependencies (#206)
Browse files Browse the repository at this point in the history
* Code optimizations

* Fix tests

* Fix dependency generation
  • Loading branch information
marcauberer committed Nov 4, 2021
1 parent 110b94a commit 0981938
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
13 changes: 3 additions & 10 deletions src/pass/generate/gen_copy_volumes.go
Expand Up @@ -21,24 +21,17 @@ var copyVolumeMockable = copyVolume
func GenerateCopyVolumes(project *model.CGProject, selected *model.SelectedTemplates) {
infoLogger.Println("Copying volumes ...")
spinner := startProcess("Copying volumes ...")
// Copy volumes
// Copy volumes and build contexts
for _, template := range selected.GetAll() {
for _, volume := range template.Volumes {
srcPath := filepath.Clean(getPredefinedServicesPath() + "/" + template.Type + "/" + template.Name + "/" + volume.DefaultValue)
srcPath := filepath.Clean(template.Dir + "/" + volume.DefaultValue)
dstPath := filepath.Clean(project.Composition.WorkingDir + project.Vars[volume.Variable])
infoLogger.Println("Copying volume from '" + srcPath + "' to '" + dstPath + "'")
copyVolumeMockable(srcPath, dstPath)
}
}
// Change volume and build context paths in composition to the new ones
// Change volume paths in composition to the new ones
for serviceIndex, service := range project.Composition.Services {
// Build contexts
if service.Build != nil && strings.Contains(service.Build.Context, getPredefinedServicesPath()) {
dstPath := service.Build.Context[len(getPredefinedServicesPath()):]
dstPath = project.Composition.WorkingDir + strings.Join(strings.Split(dstPath, "/")[3:], "/")
project.Composition.Services[serviceIndex].Build.Context = dstPath
}
// Volumes
for volumeIndex, volume := range service.Volumes {
if strings.Contains(volume.Source, getPredefinedServicesPath()) {
dstPath := volume.Source[len(getPredefinedServicesPath()):]
Expand Down
6 changes: 4 additions & 2 deletions src/pass/generate/gen_copy_volumes_test.go
Expand Up @@ -28,7 +28,7 @@ func TestGenerateCopyVolumes1(t *testing.T) {
Services: spec.Services{
{
Build: &spec.BuildConfig{
Context: templatesPath + "/frontend/angular/frontend-angular",
Context: "./angular",
},
Volumes: []spec.ServiceVolumeConfig{
{
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestGenerateCopyVolumes1(t *testing.T) {
Services: spec.Services{
{
Build: &spec.BuildConfig{
Context: "./frontend-angular",
Context: "./angular",
},
Volumes: []spec.ServiceVolumeConfig{
{
Expand Down Expand Up @@ -93,6 +93,7 @@ func TestGenerateCopyVolumes1(t *testing.T) {
{
Name: "angular",
Type: "frontend",
Dir: templatesPath + "/frontend/angular",
Volumes: []model.Volume{
{
DefaultValue: "./frontend-angular",
Expand All @@ -109,6 +110,7 @@ func TestGenerateCopyVolumes1(t *testing.T) {
{
Name: "postgres",
Type: "database",
Dir: templatesPath + "/database/postgres",
Volumes: []model.Volume{
{
DefaultValue: "./database-postgres",
Expand Down
16 changes: 12 additions & 4 deletions src/pass/generate/gen_generate.go
Expand Up @@ -82,10 +82,6 @@ func generateService(
project.LoadFromDir(template.Dir),
project.LoadFromComposeFile("service.yml"),
)
// Change to build context path to contain more information
if service.Build != nil && service.Build.Context != "" {
service.Build.Context = template.Dir + "/" + service.Build.Context
}
// Add env variables for proxy questions
for varName := range proj.ProxyVars[template.Name] {
varValue := proj.ProxyVars[template.Name][varName]
Expand All @@ -98,6 +94,18 @@ func generateService(
for labelName := range proj.ProxyLabels[template.Name] {
service.Labels[labelName] = proj.ProxyLabels[template.Name][labelName]
}
// Add dependency groups depending on the template type
switch templateType {
case model.TemplateTypeFrontend:
service.DependsOn = make(types.DependsOnConfig)
service.DependsOn[model.TemplateTypeBackend] = types.ServiceDependency{}
case model.TemplateTypeBackend:
service.DependsOn = make(types.DependsOnConfig)
service.DependsOn[model.TemplateTypeDatabase] = types.ServiceDependency{}
case model.TemplateTypeDbAdmin:
service.DependsOn = make(types.DependsOnConfig)
service.DependsOn[model.TemplateTypeDatabase] = types.ServiceDependency{}
}
// Add service to the project
proj.Composition.Services = append(proj.Composition.Services, *service)
// Add child readme files
Expand Down

0 comments on commit 0981938

Please sign in to comment.