MCPcopy Create free account
hub / github.com/celer-pkg/celer / InstallFromPackage

Method InstallFromPackage

configs/port_install.go:348–427  ·  view source on GitHub ↗
(options InstallOptions)

Source from the content-addressed store, hash-verified

346}
347
348func (p *Port) InstallFromPackage(options InstallOptions) (bool, error) {
349 // No package no install.
350 if !fileio.PathExists(p.MatchedConfig.PortConfig.PackageDir) {
351 return false, nil
352 }
353
354 // Install dependencies.
355 if err := p.installDependencies(options); err != nil {
356 return false, err
357 }
358
359 // Check if have meta file in package, no meta file means the package is invalid.
360 var metaFile string
361 entities, err := os.ReadDir(p.MatchedConfig.PortConfig.PackageDir)
362 if err != nil {
363 return false, fmt.Errorf("failed to read package dir -> %w", err)
364 }
365 for _, entity := range entities {
366 if strings.HasSuffix(entity.Name(), ".meta") {
367 metaFile = filepath.Join(p.MatchedConfig.PortConfig.PackageDir, entity.Name())
368 break
369 }
370 }
371 if metaFile == "" {
372 suffix := expr.If(p.DevDep, " [dev]", "")
373 return false, fmt.Errorf("invalid package %s, since meta file is not found for %s", p.PackageDir, p.NameVersion()+suffix)
374 }
375
376 // Install from package if meta matches.
377 metaBytes, err := os.ReadFile(metaFile)
378 if err != nil {
379 return false, fmt.Errorf("failed to read package meta of %s -> %w", p.NameVersion(), err)
380 }
381 newMeta, err := p.buildMeta()
382 if err != nil {
383 return false, fmt.Errorf("failed to calculate meta of %s -> %w", p.NameVersion(), err)
384 }
385
386 // Remove outdated package.
387 localMeta := string(metaBytes)
388 if localMeta != newMeta {
389 color.Printf(color.Warning, "\n================ The outdated package of %s will be removed now. ================\n", p.NameVersion())
390
391 // Backup current installed meta file if it exists.
392 if fileio.PathExists(p.metaFile) {
393 metaFileBackup := filepath.Join(dirs.InstalledDir, "celer", "metas", "outdated", filepath.Base(p.metaFile))
394 if err := fileio.MkdirAll(filepath.Dir(metaFileBackup), os.ModePerm); err != nil {
395 return false, fmt.Errorf("failed to mkdir %s", filepath.Dir(metaFileBackup))
396 }
397 if err := fileio.CopyFile(p.metaFile, metaFileBackup); err != nil {
398 return false, fmt.Errorf("failed to backup meta file -> %w", err)
399 }
400 } else {
401 color.Printf(color.Warning, "installed meta file not found, skip backup: %s\n", p.metaFile)
402 }
403
404 // Remove outdated package and install from source again.
405 remoteOptions := RemoveOptions{

Calls 13

installDependenciesMethod · 0.95
NameVersionMethod · 0.95
buildMetaMethod · 0.95
RemoveMethod · 0.95
doInstallFromSourceMethod · 0.95
doInstallFromPackageMethod · 0.95
writeTraceFileMethod · 0.95
PathExistsFunction · 0.92
IfFunction · 0.92
PrintfFunction · 0.92
MkdirAllFunction · 0.92
CopyFileFunction · 0.92