-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (82 loc) · 2.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Dagger Composer Module
//
// This module provides a way to install composer dependencies in a directory for a
// PHP project. It uses the composer image to install the dependencies and returns the
// vendor directory with the dependencies installed.
package main
import (
"context"
"dagger/dagger-composer/internal/dagger"
"errors"
"fmt"
)
type Composer struct {
Version string // +private
Source *dagger.Directory // +private
CacheName string // +private
EnableCache bool // +private
}
func New(
// +optional
// +default="latest"
// The version of the composer image to use.
version string,
// +optional
// +defaultPath="."
// The directory that contains the composer.json/composer.lock files.
source *dagger.Directory,
// Enable or disable the composer cache.
// +optional
// +default=true
enableCache bool,
// The name to use for the cache volume
// +optional
// +default="composer"
cacheName string,
) *Composer {
return &Composer{
Version: version,
Source: source,
CacheName: cacheName,
EnableCache: enableCache,
}
}
// Takes a directory and installs composer dependencies.
func (m *Composer) Install(
ctx context.Context,
// +optional
// +default=["--prefer-dist", "--optimize-autoloader", "--ignore-platform-reqs"]
// Additional arguments to pass to the composer install command.
args []string) (*dagger.Directory, error) {
// make sure there is a composer.json file
if !exists(ctx, m.Source, "composer.json") {
return nil, errors.New("missing composer.json file in path")
}
// if there is no composer.lock, log it but continue
if !exists(ctx, m.Source, "composer.lock") {
// TODO(jasonmccallister) log to stdout in the dagger way
fmt.Println("composer.lock file not found in path")
}
ctr := dag.Container().
From("composer:"+m.Version).
WithMountedDirectory("/app", m.Source).
WithWorkdir("/app").
WithExec([]string{"mkdir", "/composer"}).
WithEnvVariable("COMPOSER_HOME", "/composer")
if m.EnableCache {
ctr = ctr.WithMountedCache("/composer",
dag.CacheVolume(m.CacheName),
)
}
exec := append([]string{"composer", "install"}, args...)
return ctr.
WithExec(exec).
Directory("/app/vendor"), nil
}
// can be simplified when this is completed: https://github.com/dagger/dagger/issues/6713
func exists(ctx context.Context, src *dagger.Directory, path string) bool {
if _, err := src.File(path).Sync(ctx); err != nil {
return false
}
return true
}