<commit_before>// Copyright (C) 2017, 2018 <NAME>. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"go/doc"
	"path"
	"path/filepath"
	"strings"
)

type target struct {
	Target       string
	Phony        bool
	Dependencies []string
	MakeScript   string
}

type targetType interface {
	name() string
	help() string
	targets() ([]target, error)
}

type helpTarget struct {
	getTargetTypes func() []targetType
}

func createHelpTarget(getTargetTypes func() []targetType) targetType {
	return &helpTarget{getTargetTypes}
}

func (*helpTarget) name() string {
	return "help"
}

func (*helpTarget) help() string {
	return "Display this help message. Unless overridden by the '--" +
		maketargetOption + "' option, this is the default target."
}

func (ht *helpTarget) targets() ([]target, error) {
	script := `	@echo "Usage:"
	@echo "    make [target...]"
	@echo
	@echo "Global targets:"
`

	for _, t := range ht.getTargetTypes() {
		script += "\t@echo \"    " + t.name() + "\"\n"

		var buffer bytes.Buffer

		doc.ToText(&buffer, t.help(), "", "    ", 52)

		help := buffer.String()

		for _, l := range strings.Split(help, "\n") {
			if l != "" {
				script += "\t@echo \"        " + l + "\"\n"
			} else {
				script += "\t@echo\n"
			}
		}
	}

	return []target{{
		Target:     "help",
		Phony:      true,
		MakeScript: script}}, nil
}

type makeTargetData struct {
	targetName   string
	selection    packageDefinitionList
	workspaceDir string
	wp           *workspaceParams
}

func (mtd *makeTargetData) name() string {
	return mtd.targetName
}

func (mtd *makeTargetData) globalTarget() target {
	prefix := mtd.targetName + "_"

	var dependencies []string

	for _, pd := range mtd.selection {
		dependencies = append(dependencies, prefix+pd.PackageName)
	}

	return target{
		Target:       mtd.targetName,
		Phony:        true,
		Dependencies: dependencies}
}

type bootstrapTarget struct {
	makeTargetData
}

func createBootstrapTarget(selection packageDefinitionList,
	workspaceDir string, wp *workspaceParams) targetType {
	return &bootstrapTarget{makeTargetData{"bootstrap",
		selection, workspaceDir, wp}}
}

func (*bootstrapTarget) help() string {
	return "Unconditionally regenerate the 'configure' " +
		"scripts for the selected packages."
}

func (bt *bootstrapTarget) targets() ([]target, error) {
	globalTarget := bt.globalTarget()

	bootstrapTargets := []target{globalTarget}

	scriptTemplate := `	@echo "[bootstrap] %[1]s"
	@cd ` + path.Join(privateDirName, pkgDirName, "%[1]s") +
		" && ./autogen.sh\n"

	for i, pd := range bt.selection {
		bootstrapTargets = append(bootstrapTargets,
			target{
				Target: globalTarget.Dependencies[i],
				Phony:  true,
				MakeScript: fmt.Sprintf(scriptTemplate,
					pd.PackageName),
			},
			target{
				Target: path.Join(privateDirName, pkgDirName,
					pd.PackageName, "configure"),
				MakeScript: "	@$(MAKE) -s " +
					globalTarget.Dependencies[i] + "\n",
			})
	}

	return bootstrapTargets, nil
}

type configureTarget struct {
	makeTargetData
}

func createConfigureTarget(selection packageDefinitionList,
	workspaceDir string, wp *workspaceParams) targetType {
	return &configureTarget{makeTargetData{"configure",
		selection, workspaceDir, wp}}
}

func (*configureTarget) help() string {
	return "Configure the selected packages using the " +
		"current options specified in the 'conftab' file."
}

func (ct *configureTarget) targets() ([]target, error) {
	globalTarget := ct.globalTarget()

	configureTargets := []target{globalTarget}

	privateDir := getPrivateDir(ct.workspaceDir)

	buildDir := getBuildDir(privateDir, ct.wp)

	relBuildDir, err := filepath.Rel(ct.workspaceDir, buildDir)
	if err != nil {
		relBuildDir = buildDir
	}

	pkgRootDir := getGeneratedPkgRootDir(privateDir)

	for i, pd := range ct.selection {
		pkgDir := path.Join(pkgRootDir, pd.PackageName)
		relPkgBuildDir := path.Join(relBuildDir, pd.PackageName)
		relPkgSrcDir, err := filepath.Rel(relPkgBuildDir, pkgDir)
		if err != nil {
			relPkgSrcDir, err = filepath.Abs(pkgDir)
			if err != nil {
				return nil, err
			}<commit_msg>Improve computation of configure script pathname<commit_after>// Copyright (C) 2017, 2018 <NAME>. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"go/doc"
	"path"
	"path/filepath"
	"strings"
)

type target struct {
	Target       string
	Phony        bool
	Dependencies []string
	MakeScript   string
}

type targetType interface {
	name() string
	help() string
	targets() ([]target, error)
}

type helpTarget struct {
	getTargetTypes func() []targetType
}

func createHelpTarget(getTargetTypes func() []targetType) targetType {
	return &helpTarget{getTargetTypes}
}

func (*helpTarget) name() string {
	return "help"
}

func (*helpTarget) help() string {
	return "Display this help message. Unless overridden by the '--" +
		maketargetOption + "' option, this is the default target."
}

func (ht *helpTarget) targets() ([]target, error) {
	script := `	@echo "Usage:"
	@echo "    make [target...]"
	@echo
	@echo "Global targets:"
`

	for _, t := range ht.getTargetTypes() {
		script += "\t@echo \"    " + t.name() + "\"\n"

		var buffer bytes.Buffer

		doc.ToText(&buffer, t.help(), "", "    ", 52)

		help := buffer.String()

		for _, l := range strings.Split(help, "\n") {
			if l != "" {
				script += "\t@echo \"        " + l + "\"\n"
			} else {
				script += "\t@echo\n"
			}
		}
	}

	return []target{{
		Target:     "help",
		Phony:      true,
		MakeScript: script}}, nil
}

type makeTargetData struct {
	targetName   string
	selection    packageDefinitionList
	workspaceDir string
	wp           *workspaceParams
}

func (mtd *makeTargetData) name() string {
	return mtd.targetName
}

func (mtd *makeTargetData) globalTarget() target {
	prefix := mtd.targetName + "_"

	var dependencies []string

	for _, pd := range mtd.selection {
		dependencies = append(dependencies, prefix+pd.PackageName)
	}

	return target{
		Target:       mtd.targetName,
		Phony:        true,
		Dependencies: dependencies}
}

type bootstrapTarget struct {
	makeTargetData
}

func createBootstrapTarget(selection packageDefinitionList,
	workspaceDir string, wp *workspaceParams) targetType {
	return &bootstrapTarget{makeTargetData{"bootstrap",
		selection, workspaceDir, wp}}
}

func (*bootstrapTarget) help() string {
	return "Unconditionally regenerate the 'configure' " +
		"scripts for the selected packages."
}

func (bt *bootstrapTarget) targets() ([]target, error) {
	globalTarget := bt.globalTarget()

	bootstrapTargets := []target{globalTarget}

	scriptTemplate := `	@echo "[bootstrap] %[1]s"
	@cd ` + path.Join(privateDirName, pkgDirName, "%[1]s") +
		" && ./autogen.sh\n"

	for i, pd := range bt.selection {
		bootstrapTargets = append(bootstrapTargets,
			target{
				Target: globalTarget.Dependencies[i],
				Phony:  true,
				MakeScript: fmt.Sprintf(scriptTemplate,
					pd.PackageName),
			},
			target{
				Target: path.Join(privateDirName, pkgDirName,
					pd.PackageName, "configure"),
				MakeScript: "	@$(MAKE) -s " +
					globalTarget.Dependencies[i] + "\n",
			})
	}

	return bootstrapTargets, nil
}

type configureTarget struct {
	makeTargetData
}

func createConfigureTarget(selection packageDefinitionList,
	workspaceDir string, wp *workspaceParams) targetType {
	return &configureTarget{makeTargetData{"configure",
		selection, workspaceDir, wp}}
}

func (*configureTarget) help() string {
	return "Configure the selected packages using the " +
		"current options specified in the 'conftab' file."
}

func (ct *configureTarget) targets() ([]target, error) {
	globalTarget := ct.globalTarget()

	configureTargets := []target{globalTarget}

	privateDir := getPrivateDir(ct.workspaceDir)

	buildDir := getBuildDir(privateDir, ct.wp)

	relBuildDir, err := filepath.Rel(ct.workspaceDir, buildDir)
	if err != nil {
		relBuildDir = buildDir
	}

	pkgRootDir := getGeneratedPkgRootDir(privateDir)

	for i, pd := range ct.selection {
		relPkgBuildDir := path.Join(relBuildDir, pd.PackageName)
		relPkgSrcDir, err :=
			filepath.Rel(path.Join(buildDir, pd.PackageName),
				path.Join(pkgRootDir, pd.PackageName))
		if err != nil {
			return nil, err
{'fraction_non_alphanumeric': 0.1310964649490713, 'fraction_numerical': 0.002875973636908328, 'mean_word_length': 3.926800472255018, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2515430', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2664, 'n_words': 722}