UnpackWebappBundle unpacks webapp bundle for a given plugin id on disk.
(id string)
| 526 | |
| 527 | // UnpackWebappBundle unpacks webapp bundle for a given plugin id on disk. |
| 528 | func (env *Environment) UnpackWebappBundle(id string) (*model.Manifest, error) { |
| 529 | plugins, err := env.Available() |
| 530 | if err != nil { |
| 531 | return nil, errors.New("Unable to get available plugins") |
| 532 | } |
| 533 | var manifest *model.Manifest |
| 534 | for _, p := range plugins { |
| 535 | if p.Manifest != nil && p.Manifest.Id == id { |
| 536 | if manifest != nil { |
| 537 | return nil, fmt.Errorf("multiple plugins found: %v", id) |
| 538 | } |
| 539 | manifest = p.Manifest |
| 540 | } |
| 541 | } |
| 542 | if manifest == nil { |
| 543 | return nil, fmt.Errorf("plugin not found: %v", id) |
| 544 | } |
| 545 | |
| 546 | bundlePath := filepath.Clean(manifest.Webapp.BundlePath) |
| 547 | if bundlePath == "" || bundlePath[0] == '.' { |
| 548 | return nil, fmt.Errorf("invalid webapp bundle path") |
| 549 | } |
| 550 | bundlePath = filepath.Join(env.pluginDir, id, bundlePath) |
| 551 | destinationPath := filepath.Join(env.webappPluginDir, id) |
| 552 | |
| 553 | if err = os.RemoveAll(destinationPath); err != nil { |
| 554 | return nil, errors.Wrapf(err, "unable to remove old webapp bundle directory: %v", destinationPath) |
| 555 | } |
| 556 | |
| 557 | if err = utils.CopyDir(filepath.Dir(bundlePath), destinationPath); err != nil { |
| 558 | return nil, errors.Wrapf(err, "unable to copy webapp bundle directory: %v", id) |
| 559 | } |
| 560 | |
| 561 | sourceBundleFilepath := filepath.Join(destinationPath, filepath.Base(bundlePath)) |
| 562 | |
| 563 | sourceBundleFileContents, err := os.ReadFile(sourceBundleFilepath) |
| 564 | if err != nil { |
| 565 | return nil, errors.Wrapf(err, "unable to read webapp bundle: %v", id) |
| 566 | } |
| 567 | |
| 568 | hash := fnv.New64a() |
| 569 | if _, err = hash.Write(sourceBundleFileContents); err != nil { |
| 570 | return nil, errors.Wrapf(err, "unable to generate hash for webapp bundle: %v", id) |
| 571 | } |
| 572 | manifest.Webapp.BundleHash = hash.Sum([]byte{}) |
| 573 | |
| 574 | if err = os.Rename( |
| 575 | sourceBundleFilepath, |
| 576 | filepath.Join(destinationPath, fmt.Sprintf("%s_%x_bundle.js", id, manifest.Webapp.BundleHash)), |
| 577 | ); err != nil { |
| 578 | return nil, errors.Wrapf(err, "unable to rename webapp bundle: %v", id) |
| 579 | } |
| 580 | |
| 581 | return manifest, nil |
| 582 | } |
| 583 | |
| 584 | // HooksForPlugin returns the hooks API for the plugin with the given id. |
| 585 | // |