<commit_before>package main

import (
	"flag"
	"fmt"
	"github.com/naoina/kocha"
	"go/build"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"text/template"
)

type buildCommand struct {
	flag *flag.FlagSet
	all  bool
}

func (c *buildCommand) Name() string {
	return "build"
}

func (c *buildCommand) Alias() string {
	return "b"
}

func (c *buildCommand) Short() string {
	return "build your application"
}

func (c *buildCommand) Usage() string {
	return fmt.Sprintf("%s [options] ENV", c.Name())
}

func (c *buildCommand) DefineFlags(fs *flag.FlagSet) {
	fs.BoolVar(&c.all, "a", false, "make the true all-in-one binary")
	c.flag = fs
}

func (c *buildCommand) Run() {
	env := c.flag.Arg(0)
	if env == "" {
		kocha.PanicOnError(c, "abort: no ENV given")
	}
	dir, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	appName := filepath.Base(dir)
	configPkg := c.Package(path.Join(appName, "config", env))
	controllersPkg := c.Package(path.Join(appName, "app", "controllers"))
	tmpDir, err := filepath.Abs("tmp")
	if err != nil {
		panic(err)
	}
	if err := os.Mkdir(tmpDir, 0755); err != nil && !os.IsExist(err) {
		kocha.PanicOnError(c, "abort: failed to create directory: %v", err)
	}
	_, filename, _, _ := runtime.Caller(0)
	baseDir := filepath.Dir(filename)
	skeletonDir := filepath.Join(baseDir, "skeleton", "build")
	mainTemplate, err := ioutil.ReadFile(filepath.Join(skeletonDir, "main.go"))
	if err != nil {
		panic(err)
	}
	mainFilePath := filepath.Join(tmpDir, "main.go")
	builderFilePath := filepath.Join(tmpDir, "builder.go")
	file, err := os.Create(builderFilePath)
	if err != nil {
		kocha.PanicOnError(c, "abort: failed to create file: %v", err)
	}
	defer file.Close()
	builderTemplatePath := filepath.Join(skeletonDir, "builder.go")
	t := template.Must(template.ParseFiles(builderTemplatePath))
	data := map[string]interface{}{
		"configImportPath":      configPkg.ImportPath,
		"controllersImportPath": controllersPkg.ImportPath,
		"mainTemplate":          string(mainTemplate),
		"mainFilePath":          mainFilePath,
		"resources":             c.collectResourcePaths(filepath.Join(dir, kocha.StaticDir)),
	}
	if err := t.Execute(file, data); err != nil {
		kocha.PanicOnError(c, "abort: failed to write file: %v", err)
	}
	c.execCmd("go", "run", builderFilePath)
	c.execCmd("go", "build", "-o", appName, mainFilePath)
	fmt.Printf("build all-in-one binary to %v\n", filepath.Join(dir, appName))
	fmt.Println(kocha.Green("Build successful"))
	if err := os.RemoveAll(tmpDir); err != nil {
		panic(err)
	}
}

func (c *buildCommand) Package(importPath string) *build.Package {
	pkg, err := build.Import(importPath, "", build.FindOnly)
	if err != nil {
		kocha.PanicOnError(c, "abort: cannot import `%s`: %v", importPath, err)
	}
	return pkg
}

func (c *buildCommand) execCmd(cmd string, args ...string) {
	command := exec.Command(cmd, args...)
	if msg, err := command.CombinedOutput(); err != nil {
		kocha.PanicOnError(c, "abort: build failed: %v\n%v", err, string(msg))
	}
}

func (c *buildCommand) collectResourcePaths(root string) map[string]string {
	result := make(map[string]string)
	filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.Name()[0] == '.' {
			if info.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}
		if info.IsDir() {
			return nil
		}
		rel, err := filepath.Rel(root, path)
		if err != nil {
			return err
		}
		result[rel] = path
		return nil
	})
	return result
}<commit_msg>Use filepath.ToSlash() to work on windows<commit_after>package main

import (
	"flag"
	"fmt"
	"github.com/naoina/kocha"
	"go/build"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"text/template"
)

type buildCommand struct {
	flag *flag.FlagSet
	all  bool
}

func (c *buildCommand) Name() string {
	return "build"
}

func (c *buildCommand) Alias() string {
	return "b"
}

func (c *buildCommand) Short() string {
	return "build your application"
}

func (c *buildCommand) Usage() string {
	return fmt.Sprintf("%s [options] ENV", c.Name())
}

func (c *buildCommand) DefineFlags(fs *flag.FlagSet) {
	fs.BoolVar(&c.all, "a", false, "make the true all-in-one binary")
	c.flag = fs
}

func (c *buildCommand) Run() {
	env := c.flag.Arg(0)
	if env == "" {
		kocha.PanicOnError(c, "abort: no ENV given")
	}
	dir, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	appName := filepath.Base(dir)
	configPkg := c.Package(path.Join(appName, "config", env))
	controllersPkg := c.Package(path.Join(appName, "app", "controllers"))
	tmpDir, err := filepath.Abs("tmp")
	if err != nil {
		panic(err)
	}
	if err := os.Mkdir(tmpDir, 0755); err != nil && !os.IsExist(err) {
		kocha.PanicOnError(c, "abort: failed to create directory: %v", err)
	}
	_, filename, _, _ := runtime.Caller(0)
	baseDir := filepath.Dir(filename)
	skeletonDir := filepath.Join(baseDir, "skeleton", "build")
	mainTemplate, err := ioutil.ReadFile(filepath.Join(skeletonDir, "main.go"))
	if err != nil {
		panic(err)
	}
	mainFilePath := filepath.ToSlash(filepath.Join(tmpDir, "main.go"))
	builderFilePath := filepath.ToSlash(filepath.Join(tmpDir, "builder.go"))
	file, err := os.Create(builderFilePath)
	if err != nil {
		kocha.PanicOnError(c, "abort: failed to create file: %v", err)
	}
	defer file.Close()
	builderTemplatePath := filepath.ToSlash(filepath.Join(skeletonDir, "builder.go"))
	t := template.Must(template.ParseFiles(builderTemplatePath))
	data := map[string]interface{}{
		"configImportPath":      configPkg.ImportPath,
		"controllersImportPath": controllersPkg.ImportPath,
		"mainTemplate":          string(mainTemplate),
		"mainFilePath":          mainFilePath,
		"resources":             c.collectResourcePaths(filepath.Join(dir, kocha.StaticDir)),
	}
	if err := t.Execute(file, data); err != nil {
		kocha.PanicOnError(c, "abort: failed to write file: %v", err)
	}
	file.Close()
	c.execCmd("go", "run", builderFilePath)
	c.execCmd("go", "build", "-o", appName, mainFilePath)
	fmt.Printf("build all-in-one binary to %v\n", filepath.Join(dir, appName))
	fmt.Println(kocha.Green("Build successful"))
	if err := os.RemoveAll(tmpDir); err != nil {
		panic(err)
	}
}

func (c *buildCommand) Package(importPath string) *build.Package {
	pkg, err := build.Import(importPath, "", build.FindOnly)
	if err != nil {
		kocha.PanicOnError(c, "abort: cannot import `%s`: %v", importPath, err)
	}
	return pkg
}

func (c *buildCommand) execCmd(cmd string, args ...string) {
	command := exec.Command(cmd, args...)
	if msg, err := command.CombinedOutput(); err != nil {
		kocha.PanicOnError(c, "abort: build failed: %v\n%v", err, string(msg))
	}
}

func (c *buildCommand) collectResourcePaths(root string) map[string]string {
	result := make(map[string]string)
	filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.Name()[0] == '.' {
			if info.IsDir() {
				return filepath.SkipDir
			}
			return nil
		}
		if info.IsDir() {
			return nil
		}
		rel, err := filepath.Rel(root, path)
		if err != nil {
			return err
		}
		result[rel] = filepath.ToSlash(path)
		return nil
	})
	return result
}
{'fraction_non_alphanumeric': 0.16764623565631123, 'fraction_numerical': 0.0019591379792891126, 'mean_word_length': 4.171490593342981, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, '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': '1301167', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2679, 'n_words': 624}