2014-04-09 01:22:48 -04:00
|
|
|
package vagrant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2014-04-09 01:22:48 -04:00
|
|
|
)
|
|
|
|
|
2014-04-30 02:03:52 -04:00
|
|
|
// These are the extensions of files and directories that are unnecessary for the function
|
2014-04-09 01:22:48 -04:00
|
|
|
// of a Parallels virtual machine.
|
2015-01-30 04:30:42 -05:00
|
|
|
var UnnecessaryFilesPatterns = []string{"\\.log$", "\\.backup$", "\\.Backup$", "\\.app/", "/Windows Disks/"}
|
2014-04-09 01:22:48 -04:00
|
|
|
|
|
|
|
type ParallelsProvider struct{}
|
|
|
|
|
|
|
|
func (p *ParallelsProvider) KeepInputArtifact() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func (p *ParallelsProvider) Process(ui packersdk.Ui, artifact packersdk.Artifact, dir string) (vagrantfile string, metadata map[string]interface{}, err error) {
|
2014-04-09 01:22:48 -04:00
|
|
|
// Create the metadata
|
|
|
|
metadata = map[string]interface{}{"provider": "parallels"}
|
|
|
|
|
|
|
|
// Copy all of the original contents into the temporary directory
|
|
|
|
for _, path := range artifact.Files() {
|
|
|
|
// If the file isn't critical to the function of the
|
|
|
|
// virtual machine, we get rid of it.
|
|
|
|
unnecessary := false
|
2014-04-30 02:03:52 -04:00
|
|
|
for _, unnecessaryPat := range UnnecessaryFilesPatterns {
|
|
|
|
if matched, _ := regexp.MatchString(unnecessaryPat, path); matched {
|
2014-04-09 01:22:48 -04:00
|
|
|
unnecessary = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if unnecessary {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpPath := filepath.ToSlash(path)
|
|
|
|
pathRe := regexp.MustCompile(`^(.+?)([^/]+\.pvm/.+?)$`)
|
|
|
|
matches := pathRe.FindStringSubmatch(tmpPath)
|
|
|
|
var pvmPath string
|
|
|
|
if matches != nil {
|
|
|
|
pvmPath = filepath.FromSlash(matches[2])
|
|
|
|
} else {
|
|
|
|
continue // Just copy a pvm
|
|
|
|
}
|
|
|
|
dstPath := filepath.Join(dir, pvmPath)
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Copying: %s", path))
|
|
|
|
if err = CopyContents(dstPath, path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|