<commit_before>package build

import (
	"fmt"
	"path/filepath"

	"sourcegraph.com/sourcegraph/srcgraph/buildstore"
	"sourcegraph.com/sourcegraph/srcgraph/config"
	"sourcegraph.com/sourcegraph/srcgraph/dep2"
	"sourcegraph.com/sourcegraph/srcgraph/unit"
	"sourcegraph.com/sourcegraph/srcgraph/util2/makefile"
)

func init() {
	RegisterRuleMaker("dep", makeDepRules)
	buildstore.RegisterDataType("raw_deps.v0", []*dep2.RawDependency{})
	buildstore.RegisterDataType("resolved_deps.v0", []*dep2.ResolvedDep{})
}

func makeDepRules(c *config.Repository, dataDir string, existing []makefile.Rule) ([]makefile.Rule, error) {
	if len(c.SourceUnits) == 0 {
		return nil, nil
	}

	resolveRule := &ResolveDepsRule{dataDir, nil}

	rules := []makefile.Rule{resolveRule}
	for _, u := range c.SourceUnits {
		rule := &ListSourceUnitDepsRule{dataDir, u}
		rules = append(rules, rule)
		resolveRule.rawDepLists = append(resolveRule.rawDepLists, rule.Target())
	}

	return rules, nil
}

type ResolveDepsRule struct {
	dataDir     string
	rawDepLists []string
}

func (r *ResolveDepsRule) Target() string {
	return filepath.Join(r.dataDir, RepositoryCommitDataFilename([]*dep2.ResolvedDep{}))
}

func (r *ResolveDepsRule) Prereqs() []string { return r.rawDepLists }

func (r *ResolveDepsRule) Recipes() []string {
	return []string{"srcgraph -v resolve-deps -json $^ 1> $@"}
}

type ListSourceUnitDepsRule struct {
	dataDir string
	unit    unit.SourceUnit
}

func (r *ListSourceUnitDepsRule) Target() string {
	return filepath.Join(r.dataDir, SourceUnitDataFilename([]*dep2.RawDependency{}, r.unit))
}

func (r *ListSourceUnitDepsRule) Prereqs() []string {
	return r.unit.Paths()
}

func (r *ListSourceUnitDepsRule) Recipes() []string {
	return []string{
		"mkdir -p `dirname \"$@\"`",
		fmt.Sprintf("srcgraph -v list-deps -json %q 1> $@", unit.MakeID(r.unit)),
	}
}
<commit_msg>resolve deps at source unit level<commit_after>package build

import (
	"fmt"
	"path/filepath"

	"sourcegraph.com/sourcegraph/srcgraph/buildstore"
	"sourcegraph.com/sourcegraph/srcgraph/config"
	"sourcegraph.com/sourcegraph/srcgraph/dep2"
	"sourcegraph.com/sourcegraph/srcgraph/unit"
	"sourcegraph.com/sourcegraph/srcgraph/util2/makefile"
)

func init() {
	RegisterRuleMaker("dep", makeDepRules)
	buildstore.RegisterDataType("raw_deps.v0", []*dep2.RawDependency{})
	buildstore.RegisterDataType("resolved_deps.v0", []*dep2.ResolvedDep{})
}

func makeDepRules(c *config.Repository, dataDir string, existing []makefile.Rule) ([]makefile.Rule, error) {
	if len(c.SourceUnits) == 0 {
		return nil, nil
	}

	var rules []makefile.Rule
	for _, u := range c.SourceUnits {
		rawDepRule := &ListSourceUnitDepsRule{dataDir, u}
		rules = append(rules, rawDepRule)
		rules = append(rules, &ResolveDepsRule{dataDir, u, rawDepRule.Target()})
	}

	return rules, nil
}

type ResolveDepsRule struct {
	dataDir       string
	unit          unit.SourceUnit
	RawDepsOutput string
}

func (r *ResolveDepsRule) Target() string {
	return filepath.Join(r.dataDir, SourceUnitDataFilename([]*dep2.ResolvedDep{}, r.unit))
}

func (r *ResolveDepsRule) Prereqs() []string { return []string{r.RawDepsOutput} }

func (r *ResolveDepsRule) Recipes() []string {
	return []string{"srcgraph -v resolve-deps -json $^ 1> $@"}
}

type ListSourceUnitDepsRule struct {
	dataDir string
	unit    unit.SourceUnit
}

func (r *ListSourceUnitDepsRule) Target() string {
	return filepath.Join(r.dataDir, SourceUnitDataFilename([]*dep2.RawDependency{}, r.unit))
}

func (r *ListSourceUnitDepsRule) Prereqs() []string {
	return r.unit.Paths()
}

func (r *ListSourceUnitDepsRule) Recipes() []string {
	return []string{
		"mkdir -p `dirname \"$@\"`",
		fmt.Sprintf("srcgraph -v list-deps -json %q 1> $@", unit.MakeID(r.unit)),
	}
}

{'fraction_non_alphanumeric': 0.14832663989290495, 'fraction_numerical': 0.005890227576974565, 'mean_word_length': 5.321489001692047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7265931', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1342, 'n_words': 262}