func()

in packaging/linux/rpm/template.go [565:701]


func (w *specWrapper) Install() fmt.Stringer {
	b := &strings.Builder{}
	fmt.Fprintln(b, "%install")

	artifacts := w.Spec.GetArtifacts(w.Target)

	copyArtifact := func(root, p string, cfg *dalec.ArtifactConfig) {
		if cfg == nil {
			return
		}
		targetDir := filepath.Join(root, cfg.SubPath)
		fmt.Fprintln(b, "mkdir -p", targetDir)

		var targetPath string
		file := cfg.ResolveName(p)
		if !strings.Contains(file, "*") {
			targetPath = filepath.Join(targetDir, file)
		} else {
			targetPath = targetDir + "/"
		}
		fmt.Fprintln(b, "cp -r", p, targetPath)
		if cfg.Permissions.Perm() != 0 {
			fmt.Fprintf(b, "chmod %o %s\n", cfg.Permissions, targetPath)
		}
	}

	if len(artifacts.Binaries) > 0 {
		binKeys := dalec.SortMapKeys(artifacts.Binaries)
		for _, p := range binKeys {
			cfg := artifacts.Binaries[p]
			copyArtifact(`%{buildroot}/%{_bindir}`, p, &cfg)
		}
	}

	if len(artifacts.Manpages) > 0 {
		manKeys := dalec.SortMapKeys(artifacts.Manpages)
		for _, p := range manKeys {
			cfg := artifacts.Manpages[p]
			copyArtifact(`%{buildroot}/%{_mandir}`, p, &cfg)
		}
	}

	createArtifactDir := func(root, p string, cfg dalec.ArtifactDirConfig) {
		dir := filepath.Join(root, p)
		mkdirCmd := "mkdir"
		perms := cfg.Mode.Perm()
		if perms != 0 {
			mkdirCmd += fmt.Sprintf(" -m %o", cfg.Mode)
		}
		fmt.Fprintf(b, "%s -p %q\n", mkdirCmd, dir)
	}

	if artifacts.Directories != nil {
		configKeys := dalec.SortMapKeys(artifacts.Directories.Config)
		for _, p := range configKeys {
			cfg := artifacts.Directories.Config[p]
			createArtifactDir(`%{buildroot}/%{_sysconfdir}`, p, cfg)
		}

		stateKeys := dalec.SortMapKeys(artifacts.Directories.State)
		for _, p := range stateKeys {
			cfg := artifacts.Directories.State[p]
			createArtifactDir(`%{buildroot}/%{_sharedstatedir}`, p, cfg)
		}
	}

	if len(artifacts.DataDirs) > 0 {
		dataFileKeys := dalec.SortMapKeys(artifacts.DataDirs)
		for _, k := range dataFileKeys {
			df := artifacts.DataDirs[k]
			copyArtifact(`%{buildroot}/%{_datadir}`, k, &df)
		}
	}

	if artifacts.Libexec != nil {
		libexecFileKeys := dalec.SortMapKeys(artifacts.Libexec)
		for _, k := range libexecFileKeys {
			le := artifacts.Libexec[k]
			copyArtifact(`%{buildroot}/%{_libexecdir}`, k, &le)
		}
	}

	configKeys := dalec.SortMapKeys(artifacts.ConfigFiles)
	for _, c := range configKeys {
		cfg := artifacts.ConfigFiles[c]
		copyArtifact(`%{buildroot}/%{_sysconfdir}`, c, &cfg)
	}

	if artifacts.Systemd != nil {
		serviceKeys := dalec.SortMapKeys(artifacts.Systemd.Units)
		for _, p := range serviceKeys {
			cfg := artifacts.Systemd.Units[p]
			// must include systemd unit extension (.service, .socket, .timer, etc.) in name
			copyArtifact(`%{buildroot}/%{_unitdir}`, p, cfg.Artifact())
		}

		dropinKeys := dalec.SortMapKeys(artifacts.Systemd.Dropins)
		for _, d := range dropinKeys {
			cfg := artifacts.Systemd.Dropins[d]
			copyArtifact(`%{buildroot}/%{_unitdir}`, d, cfg.Artifact())
		}
	}

	docKeys := dalec.SortMapKeys(artifacts.Docs)
	for _, d := range docKeys {
		cfg := artifacts.Docs[d]
		root := filepath.Join(`%{buildroot}/%{_docdir}`, w.Name)
		copyArtifact(root, d, &cfg)
	}

	licenseKeys := dalec.SortMapKeys(artifacts.Licenses)
	for _, l := range licenseKeys {
		cfg := artifacts.Licenses[l]
		root := filepath.Join(`%{buildroot}/%{_licensedir}`, w.Name)
		copyArtifact(root, l, &cfg)
	}

	libs := dalec.SortMapKeys(artifacts.Libs)
	for _, l := range libs {
		cfg := artifacts.Libs[l]
		root := filepath.Join(`%{buildroot}/%{_libdir}`)
		copyArtifact(root, l, &cfg)
	}

	for _, l := range artifacts.Links {
		fmt.Fprintln(b, "mkdir -p", filepath.Dir(filepath.Join("%{buildroot}", l.Dest)))
		fmt.Fprintln(b, "ln -sf", l.Source, "%{buildroot}/"+l.Dest)
	}

	headersKeys := dalec.SortMapKeys(artifacts.Headers)
	for _, h := range headersKeys {
		cfg := artifacts.Headers[h]
		copyArtifact(`%{buildroot}/%{_includedir}`, h, &cfg)
	}
	b.WriteString("\n")
	return b
}