<commit_before>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"os/user"
	"path/filepath"
	"regexp"
	"strings"
	"time"

	"v.io/x/devtools/internal/collect"
	"v.io/x/devtools/internal/envutil"
	"v.io/x/devtools/internal/tool"
	"v.io/x/devtools/internal/util"
	"v.io/x/lib/cmdline"
)

var (
	hostGoFlag   string
	targetGoFlag string
)

func init() {
	cmdGo.Flags.StringVar(&hostGoFlag, "host_go", "go", "Go command for the host platform.")
	cmdGo.Flags.StringVar(&targetGoFlag, "target_go", "go", "Go command for the target platform.")
	// The "v23 xgo" commands has the same flags as "v23 go".
	cmdXGo.Flags = cmdGo.Flags
}

// cmdGo represents the "v23 go" command.
var cmdGo = &cmdline.Command{
	Run:   runGo,
	Name:  "go",
	Short: "Execute the go tool using the vanadium environment",
	Long: `
Wrapper around the 'go' tool that can be used for compilation of
vanadium Go sources. It takes care of vanadium-specific setup, such as
setting up the Go specific environment variables or making sure that
VDL generated files are regenerated before compilation.

In particular, the tool invokes the following command before invoking
any go tool commands that compile vanadium Go code:

vdl generate -lang=go all
`,
	ArgsName: "<arg ...>",
	ArgsLong: "<arg ...> is a list of arguments for the go tool.",
}

func runGo(command *cmdline.Command, args []string) error {
	if len(args) == 0 {
		return command.UsageErrorf("not enough arguments")
	}
	ctx := tool.NewContextFromCommand(command, tool.ContextOpts{
		Color:   &colorFlag,
		DryRun:  &dryRunFlag,
		Verbose: &verboseFlag,
	})
	return runGoForPlatform(ctx, util.HostPlatform(), command, args)
}

// cmdXGo represents the "v23 xgo" command.
var cmdXGo = &cmdline.Command{
	Run:   runXGo,
	Name:  "xgo",
	Short: "Execute the go tool using the vanadium environment and cross-compilation",
	Long: `
Wrapper around the 'go' tool that can be used for cross-compilation of
vanadium Go sources. It takes care of vanadium-specific setup, such as
setting up the Go specific environment variables or making sure that
VDL generated files are regenerated before compilation.

In particular, the tool invokes the following command before invoking
any go tool commands that compile vanadium Go code:

vdl generate -lang=go all

`,
	ArgsName: "<platform> <arg ...>",
	ArgsLong: `
<platform> is the cross-compilation target and has the general format
<arch><sub>-<os> or <arch><sub>-<os>-<env> where:
- <arch> is the platform architecture (e.g. 386, amd64 or arm)
- <sub> is the platform sub-architecture (e.g. v6 or v7 for arm)
- <os> is the platform operating system (e.g. linux or darwin)
- <env> is the platform environment (e.g. gnu or android)

<arg ...> is a list of arguments for the go tool."
`,
}

func runXGo(command *cmdline.Command, args []string) error {
	if len(args) < 2 {
		return command.UsageErrorf("not enough arguments")
	}
	ctx := tool.NewContextFromCommand(command, tool.ContextOpts{
		Color:   &colorFlag,
		DryRun:  &dryRunFlag,
		Verbose: &verboseFlag,
	})
	platform, err := util.ParsePlatform(args[0])
	if err != nil {
		return err
	}
	return runGoForPlatform(ctx, platform, command, args[1:])
}

func setBuildInfoFlags(args []string, platform util.Platform) []string {
	const buildInfoPackage = "v.io/x/ref/lib/flags/buildinfo"

	userName := ""
	if currUser, err := user.Current(); err == nil {
		userName = currUser.Name
	}

	settings := []struct {
		variable, value string
	}{
		{"platform", platform.String()},
		{"username", userName},
		{"timestamp", time.Now().UTC().Format(time.RFC3339)},
	}
	flags := []string{}
	for _, s := range settings {
		flags = append(flags, "-X", buildInfoPackage+"."+s.variable, fmt.Sprintf("%q", s.value))
	}
	ldflags := "-ldflags=" + strings.Join(flags, " ")
	return append([]string{args[0], ldflags}, args[1:]...)
}

func runGoForPlatform(ctx *tool.Context, platform util.Platform, command *cmdline.Command, args []string) error {<commit_msg>x/devtools: Move x/ref/lib/flags/buildinfo to x/lib/buildinfo<commit_after>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"os/user"
	"path/filepath"
	"regexp"
	"strings"
	"time"

	"v.io/x/devtools/internal/collect"
	"v.io/x/devtools/internal/envutil"
	"v.io/x/devtools/internal/tool"
	"v.io/x/devtools/internal/util"
	"v.io/x/lib/cmdline"
)

var (
	hostGoFlag   string
	targetGoFlag string
)

func init() {
	cmdGo.Flags.StringVar(&hostGoFlag, "host_go", "go", "Go command for the host platform.")
	cmdGo.Flags.StringVar(&targetGoFlag, "target_go", "go", "Go command for the target platform.")
	// The "v23 xgo" commands has the same flags as "v23 go".
	cmdXGo.Flags = cmdGo.Flags
}

// cmdGo represents the "v23 go" command.
var cmdGo = &cmdline.Command{
	Run:   runGo,
	Name:  "go",
	Short: "Execute the go tool using the vanadium environment",
	Long: `
Wrapper around the 'go' tool that can be used for compilation of
vanadium Go sources. It takes care of vanadium-specific setup, such as
setting up the Go specific environment variables or making sure that
VDL generated files are regenerated before compilation.

In particular, the tool invokes the following command before invoking
any go tool commands that compile vanadium Go code:

vdl generate -lang=go all
`,
	ArgsName: "<arg ...>",
	ArgsLong: "<arg ...> is a list of arguments for the go tool.",
}

func runGo(command *cmdline.Command, args []string) error {
	if len(args) == 0 {
		return command.UsageErrorf("not enough arguments")
	}
	ctx := tool.NewContextFromCommand(command, tool.ContextOpts{
		Color:   &colorFlag,
		DryRun:  &dryRunFlag,
		Verbose: &verboseFlag,
	})
	return runGoForPlatform(ctx, util.HostPlatform(), command, args)
}

// cmdXGo represents the "v23 xgo" command.
var cmdXGo = &cmdline.Command{
	Run:   runXGo,
	Name:  "xgo",
	Short: "Execute the go tool using the vanadium environment and cross-compilation",
	Long: `
Wrapper around the 'go' tool that can be used for cross-compilation of
vanadium Go sources. It takes care of vanadium-specific setup, such as
setting up the Go specific environment variables or making sure that
VDL generated files are regenerated before compilation.

In particular, the tool invokes the following command before invoking
any go tool commands that compile vanadium Go code:

vdl generate -lang=go all

`,
	ArgsName: "<platform> <arg ...>",
	ArgsLong: `
<platform> is the cross-compilation target and has the general format
<arch><sub>-<os> or <arch><sub>-<os>-<env> where:
- <arch> is the platform architecture (e.g. 386, amd64 or arm)
- <sub> is the platform sub-architecture (e.g. v6 or v7 for arm)
- <os> is the platform operating system (e.g. linux or darwin)
- <env> is the platform environment (e.g. gnu or android)

<arg ...> is a list of arguments for the go tool."
`,
}

func runXGo(command *cmdline.Command, args []string) error {
	if len(args) < 2 {
		return command.UsageErrorf("not enough arguments")
	}
	ctx := tool.NewContextFromCommand(command, tool.ContextOpts{
		Color:   &colorFlag,
		DryRun:  &dryRunFlag,
		Verbose: &verboseFlag,
	})
	platform, err := util.ParsePlatform(args[0])
	if err != nil {
		return err
	}
	return runGoForPlatform(ctx, platform, command, args[1:])
}

func setBuildInfoFlags(args []string, platform util.Platform) []string {
	const buildInfoPackage = "v.io/x/lib/buildinfo"

	userName := ""
	if currUser, err := user.Current(); err == nil {
		userName = currUser.Name
	}

	settings := []struct {
		variable, value string
	}{
		{"platform", platform.String()},
		{"username", userName},
		{"timestamp", time.Now().UTC().Format(time.RFC3339)},
	}
	flags := []string{}
	for _, s := range settings {
		flags = append(flags, "-X", buildInfoPackage+"."+s.variable, fmt.Sprintf("%q", s.value))
	}
	ldflags := "-ldflags=" + strings.Join(flags, " ")
	return append([]string{args[0], ldflags}, args[1:]...)
}

func runGoForPlatform(ctx *tool.Context, platform util.Platform, command *cmdline.Command, args []string) error {
{'fraction_non_alphanumeric': 0.12950859356088115, 'fraction_numerical': 0.007020091987412249, 'mean_word_length': 4.229746835443038, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, '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': '2032087', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2712, 'n_words': 924}