/
sdk_php.go
115 lines (100 loc) · 3.02 KB
/
sdk_php.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/dagger/dagger/ci/internal/dagger"
"github.com/dagger/dagger/ci/util"
)
const (
phpSDKPath = "sdk/php"
phpSDKGeneratedDir = "generated"
phpSDKVersionFile = "src/Connection/version.php"
)
type PHPSDK struct {
Dagger *Dagger // +private
}
// Lint the PHP SDK
func (t PHPSDK) Lint(ctx context.Context) error {
return util.DiffDirectoryF(ctx, t.Dagger.Source, t.Generate, filepath.Join(phpSDKPath, phpSDKGeneratedDir))
}
// Test the PHP SDK
func (t PHPSDK) Test(ctx context.Context) error {
return fmt.Errorf("not implemented")
}
// Regenerate the PHP SDK API
func (t PHPSDK) Generate(ctx context.Context) (*Directory, error) {
installer, err := t.Dagger.installer(ctx, "sdk-php-generate")
if err != nil {
return nil, err
}
generated := t.phpBase().
With(installer).
With(util.ShellCmds(
fmt.Sprintf("rm -f %s/*.php", phpSDKGeneratedDir),
"ls -lha",
"$_EXPERIMENTAL_DAGGER_CLI_BIN run ./codegen",
)).
Directory(".")
return dag.Directory().WithDirectory(phpSDKPath, generated), nil
}
// Publish the PHP SDK
func (t PHPSDK) Publish(
ctx context.Context,
tag string,
// +optional
dryRun bool,
// +optional
// +default="https://github.com/dagger/dagger-php-sdk.git"
gitRepo string,
// +optional
// +default="dagger-ci"
gitUserName string,
// +optional
// +default="hello@dagger.io"
gitUserEmail string,
// +optional
githubToken *Secret,
) error {
return gitPublish(ctx, gitPublishOpts{
source: "https://github.com/dagger/dagger.git",
sourcePath: "sdk/php/",
sourceTag: tag,
dest: gitRepo,
destTag: strings.TrimPrefix(tag, "sdk/php/"),
username: gitUserName,
email: gitUserEmail,
githubToken: githubToken,
dryRun: dryRun,
})
}
// Bump the PHP SDK's Engine dependency
func (t PHPSDK) Bump(ctx context.Context, version string) (*Directory, error) {
version = strings.TrimPrefix(version, "v")
content := fmt.Sprintf(`<?php /* Code generated by dagger. DO NOT EDIT. */ return '%s';
`, version)
dir := dag.Directory().WithNewFile(filepath.Join(phpSDKPath, phpSDKVersionFile), content)
return dir, nil
}
// phpBase returns a PHP container with the PHP SDK source files
// added and dependencies installed.
func (t PHPSDK) phpBase() *dagger.Container {
src := t.Dagger.Source.Directory(phpSDKPath)
return dag.Container().
From("php:8.2-zts-bookworm").
WithExec([]string{"apt-get", "update"}).
WithExec([]string{"apt-get", "install", "-y", "git", "unzip"}).
WithFile("/usr/bin/composer", dag.Container().
From("composer:2").
File("/usr/bin/composer"),
).
WithMountedCache("/root/.composer", dag.CacheVolume("composer-cache-8.2-zts-bookworm")).
WithEnvVariable("COMPOSER_HOME", "/root/.composer").
WithEnvVariable("COMPOSER_ALLOW_SUPERUSER", "1").
WithWorkdir(fmt.Sprintf("/%s", phpSDKPath)).
WithFile("composer.json", src.File("composer.json")).
WithFile("composer.lock", src.File("composer.lock")).
WithExec([]string{"composer", "install"}).
WithDirectory(".", src)
}