-
Notifications
You must be signed in to change notification settings - Fork 9
/
module.go
110 lines (90 loc) · 3.23 KB
/
module.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package pkggraph
import (
"fmt"
"io/fs"
"path/filepath"
"time"
"golang.org/x/net/context"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/git"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/runtime"
)
type Module struct {
Workspace *schema.Workspace
loadedFrom *schema.Workspace_LoadedFrom
// If not empty, this is an external module.
version string
}
type MutableModule interface {
ModuleName() string // The module that this workspace corresponds to.
ReadWriteFS() fnfs.ReadWriteFS
}
func NewModule(w *schema.Workspace, lf *schema.Workspace_LoadedFrom, version string) *Module {
return &Module{
Workspace: w,
loadedFrom: lf,
version: version,
}
}
// Implements fnerrors.Location.
func (mod *Module) ErrorLocation() string {
if mod.IsExternal() {
return mod.Workspace.ModuleName
}
return mod.loadedFrom.AbsPath
}
func (mod *Module) Abs() string { return mod.loadedFrom.AbsPath }
func (mod *Module) DefinitionFile() string { return mod.loadedFrom.DefinitionFile }
func (mod *Module) ModuleName() string { return mod.Workspace.ModuleName }
func (mod *Module) Version() string { return mod.version }
func (mod *Module) ChangeTrigger(rel string, excludes []string) compute.Computable[any] { return nil }
// An external module is downloaded from a remote location and stored in the cache. It always has a version.
func (mod *Module) IsExternal() bool { return mod.version != "" }
func (mod *Module) ReadOnlyFS(rel ...string) fs.FS {
return fnfs.Local(mod.Abs(), rel...)
}
func (mod *Module) ReadWriteFS() fnfs.ReadWriteFS {
if mod.IsExternal() {
return fnfs.Local(mod.Abs()).(fnfs.ReadWriteFS) // LocalFS has a Write, which fails Writes.
}
return fnfs.ReadWriteLocalFS(mod.Abs())
}
func (mod *Module) MakeLocation(relPath string) Location {
cl := filepath.Clean(relPath)
pkg := mod.Workspace.ModuleName
if cl != "." {
pkg += "/" + cl
}
return Location{
Module: mod,
PackageName: schema.PackageName(pkg),
relPath: cl,
}
}
func (mod *Module) RootLocation() Location {
return mod.MakeLocation(".")
}
func (mod *Module) VCS(ctx context.Context) (*runtime.BuildVCS, error) {
t := time.Now()
status, err := git.FetchStatus(ctx, mod.Abs())
if err != nil {
return nil, err
}
if status.Revision == "" {
fmt.Fprintf(console.Debug(ctx), "module.vcs: %s: none detected\n", mod.ModuleName())
return nil, nil
}
fmt.Fprintf(console.Debug(ctx), "module.vcs: %s: revision %s (uncommited: %v) commit_time %v (took %v)\n",
mod.ModuleName(), status.Revision, status.Uncommitted, status.CommitTime, time.Since(t))
return &runtime.BuildVCS{
Revision: status.Revision,
CommitTime: status.CommitTime.Format(time.RFC3339),
Uncommitted: status.Uncommitted,
}, nil
}