Skip to content

Commit 8045218

Browse files
committed
add VersionedNamesForPlatform and PackageNamesForPlatform
1 parent 1398df1 commit 8045218

File tree

5 files changed

+33
-43
lines changed

5 files changed

+33
-43
lines changed

internal/devconfig/packages.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ type Packages struct {
3232
Collection []Package `json:"-,omitempty"`
3333
}
3434

35-
// TODO savil: rm
36-
func (pkgs *Packages) Kind() int {
37-
return int(pkgs.jsonKind)
38-
}
39-
4035
// VersionedNames returns a list of package names with versions.
4136
// NOTE: if the package is unversioned, the version will be omitted (doesn't default to @latest).
4237
//
@@ -249,14 +244,25 @@ func NewPackage(name string, values map[string]any) Package {
249244
}
250245
}
251246

252-
// TODO savil: rm
253-
func (p *Package) Name() string {
254-
return p.name
255-
}
256-
257-
// TODO savil: rm
258-
func (p *Package) Kind() int {
259-
return int(p.kind)
247+
// enabledOnPlatform returns whether the package is enabled on the given platform.
248+
// If the package has a list of platforms, it is enabled only on those platforms.
249+
// If the package has a list of excluded platforms, it is enabled on all platforms
250+
// except those.
251+
func (p *Package) enabledOnPlatform(platform string) bool {
252+
if len(p.Platforms) > 0 {
253+
for _, plt := range p.Platforms {
254+
if plt == platform {
255+
return true
256+
}
257+
}
258+
return false
259+
}
260+
for _, plt := range p.ExcludedPlatforms {
261+
if plt == platform {
262+
return false
263+
}
264+
}
265+
return true
260266
}
261267

262268
func (p *Package) VersionedName() string {
@@ -268,8 +274,11 @@ func (p *Package) VersionedName() string {
268274
}
269275

270276
func (p *Package) IsEnabledOnPlatform() (bool, error) {
271-
// TODO savil. Next PR will update this implementation
272-
return true, nil
277+
platform, err := nix.System()
278+
if err != nil {
279+
return false, errors.WithStack(err)
280+
}
281+
return p.enabledOnPlatform(platform), nil
273282
}
274283

275284
func (p *Package) UnmarshalJSON(data []byte) error {
@@ -290,10 +299,6 @@ func (p *Package) UnmarshalJSON(data []byte) error {
290299

291300
*p = Package(*alias)
292301
p.kind = regular
293-
294-
// TODO savil. needed?
295-
//p.Platforms = alias.Platforms
296-
//p.ExcludedPlatforms = alias.ExcludedPlatforms
297302
return nil
298303
}
299304

internal/devconfig/packages_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,3 @@ func TestParseVersionedName(t *testing.T) {
209209
})
210210
}
211211
}
212-
213-
func TestConvertToKind(t *testing.T) {
214-
testCase := Packages{
215-
jsonKind: jsonList,
216-
Collection: packagesFromLegacyList([]string{"python", "hello@latest", "[email protected]"}),
217-
}
218-
219-
expected := Packages{
220-
jsonKind: jsonMap,
221-
Collection: []Package{
222-
NewVersionOnlyPackage("python", "" /*version*/),
223-
NewVersionOnlyPackage("hello", "latest"),
224-
NewVersionOnlyPackage("go", "1.20"),
225-
},
226-
}
227-
228-
testCase.convertToKind(jsonMap)
229-
if !reflect.DeepEqual(testCase, expected) {
230-
t.Errorf("expected: %+v, got: %+v", expected, testCase)
231-
}
232-
}

internal/devpkg/package.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,13 @@ func (p *Package) Versioned() string {
416416
}
417417

418418
func (p *Package) IsLegacy() bool {
419-
return p.IsDevboxPackage() && !p.isVersioned() && p.lockfile.Get(p.Raw).GetSource() == ""
419+
if !p.IsDevboxPackage() || p.isVersioned() {
420+
return false
421+
}
422+
if p.IsInstallable() {
423+
return false
424+
}
425+
return p.lockfile.Get(p.Raw).GetSource() == ""
420426
}
421427

422428
func (p *Package) LegacyToVersioned() string {

internal/impl/packages.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func (d *Devbox) Add(ctx context.Context, platform, excludePlatform string, pkgs
4848
addedPackageNames := []string{}
4949
existingPackageNames := d.ConfigPackageNames()
5050
for _, pkg := range pkgs {
51-
5251
// If exact versioned package is already in the config, skip.
5352
if slices.Contains(existingPackageNames, pkg.Versioned()) {
5453
addedPackageNames = append(addedPackageNames, pkg.Versioned())

internal/nix/nix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ var nixPlatforms = []string{
177177
"x86_64-darwin",
178178
"x86_64-linux",
179179
// not technically supported, but should work?
180-
// ref.
180+
// ref. https://nixos.wiki/wiki/Nix_on_ARM
181+
// ref. https://github.com/jetpack-io/devbox/pull/1300
181182
"armv7l-linux",
182183
}
183184

0 commit comments

Comments
 (0)