-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (107 loc) · 3.71 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"context"
"fmt"
"path"
"path/filepath"
)
func New(
// +optional
_sdkSourceDir *Directory,
) *DenoSdk {
sdkSourceDir := dag.Git("https://github.com/fluentci-io/daggerverse").Branch("main").Tree()
return &DenoSdk{
SDKSourceDir: sdkSourceDir.Directory("./deno-sdk"),
RequiredPaths: []string{
"**/deno.json",
"**/deno.lock",
},
}
}
type DenoSdk struct {
SDKSourceDir *Directory
RequiredPaths []string
}
const (
ModSourceDirPath = "/src"
RuntimeExecutablePath = "/usr/local/bin/runtime"
schemaPath = "/schema.json"
codegenVersion = "v0.2.1"
sdkSrc = "/sdk"
genDir = "sdk"
)
func (m *DenoSdk) ModuleRuntime(ctx context.Context, modSource *ModuleSource, introspectionJSON *File) (*Container, error) {
subPath, err := modSource.SourceSubpath(ctx)
if err != nil {
return nil, fmt.Errorf("could not load module config: %v", err)
}
modSubPath := filepath.Join(ModSourceDirPath, subPath)
return m.Base().
// Add template directory
WithMountedDirectory("/opt", dag.CurrentModule().Source().Directory(".")).
// Mount users' module
WithMountedDirectory(ModSourceDirPath, modSource.ContextDirectory()).
WithWorkdir(path.Join(ModSourceDirPath, subPath)).
WithMountedFile(schemaPath, introspectionJSON).
WithExec([]string{"sh", "-c", "ls -lha"}).
WithExec([]string{"sh", "-c", "codegen --module . --lang deno --introspection-json-path /schema.json"}, ContainerWithExecOpts{
ExperimentalPrivilegedNesting: true,
}).
WithExec([]string{"sh", "-c", "[ ! -f '/src/import_map.json' ] && wget -P /src https://cdn.jsdelivr.net/gh/fluent-ci-templates/base-pipeline@main/import_map.json; exit 0"}).
WithExec([]string{
"deno",
"install",
"-A",
"https://cdn.jsdelivr.net/gh/fluentci-io/daggerverse@7b41679/deno-sdk/sdk/src/mod/cli.ts",
"-n",
"runtime",
}).
WithWorkdir(ModSourceDirPath).
WithEntrypoint([]string{RuntimeExecutablePath, "--mod-path", path.Join(ModSourceDirPath, subPath)}).
WithLabel("io.dagger.module.config", modSubPath), nil
}
func (m *DenoSdk) Codegen(ctx context.Context, modSource *ModuleSource, introspectionJSON *File) (*GeneratedCode, error) {
subPath, err := modSource.SourceSubpath(ctx)
if err != nil {
return nil, fmt.Errorf("could not load module config: %v", err)
}
base := m.Base().
WithMountedDirectory(ModSourceDirPath, modSource.ContextDirectory()).
WithWorkdir(path.Join(ModSourceDirPath, subPath)).
WithMountedFile(schemaPath, introspectionJSON)
ctr := base.
WithExec([]string{"sh", "-c", "codegen --module . --lang deno --introspection-json-path /schema.json"}, ContainerWithExecOpts{
ExperimentalPrivilegedNesting: true,
})
codegen := ctr.WithDirectory(genDir, ctr.Directory(sdkSrc), ContainerWithDirectoryOpts{
Exclude: []string{},
}).
Directory(ModSourceDirPath)
return dag.GeneratedCode(codegen).
WithVCSGeneratedPaths([]string{
genDir + "/**",
}).
WithVCSIgnoredPaths([]string{
genDir,
}), nil
}
func (m *DenoSdk) CodegenBin() *File {
codegen := "codegen_" + codegenVersion + "_x86_64_unknown_linux-gnu.tar.gz"
return dag.Container().
From("alpine:latest").
WithExec([]string{"wget", "https://github.com/fluentci-io/codegen/releases/download/" + codegenVersion + "/" + codegen}).
WithExec([]string{"tar", "-xvf", codegen}).
File("/codegen")
}
func (m *DenoSdk) Base() *Container {
return m.denoBase().
WithDirectory("/sdk", m.SDKSourceDir).
WithFile("/usr/bin/codegen", m.CodegenBin())
}
func (m *DenoSdk) denoBase() *Container {
opts := ContainerOpts{}
return dag.Container(opts).
From("denoland/deno:alpine-1.40.0").
WithExec([]string{"apk", "add", "--no-cache", "git"}).
WithMountedCache("/deno-dir", dag.CacheVolume("moddenocache"))
}