<commit_before>package framework

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strings"

	"github.com/jarosser06/fastfood"
	"github.com/jarosser06/fastfood/common/fileutil"
	"github.com/jarosser06/fastfood/common/stringutil"
	"github.com/jarosser06/fastfood/framework/chef"
)

type Chef struct {
	cookbook chef.Cookbook
	options  fastfood.FrameworkOptions
}

// Initializes a framework with options
func (c *Chef) Init(frameworkOptions fastfood.FrameworkOptions) error {
	c.options = frameworkOptions
	if chef.PathIsCookbook(c.options.Destination) {
		var err error
		c.cookbook, err = chef.NewCookbookFromPath(c.options.Destination)
		if err != nil {
			return err
		}

		return nil
	}

	cpath := path.Join(c.options.Destination, c.options.Name)
	// Create cookbook
	if fileutil.FileExist(cpath) {
		var err error
		c.cookbook, err = chef.NewCookbookFromPath(cpath)
		if err != nil {
			return err
		}
	} else {
		c.cookbook = chef.NewCookbook(c.options.Destination, c.options.Name)
	}

	return nil
}

// Generates a basic cookbook structure
func (c *Chef) GenerateBase() ([]string, error) {
	// Generate the cookbook if it doesn't exist
	if !fileutil.FileExist(c.cookbook.Path) {
		err := os.Mkdir(c.cookbook.Path, 0755)
		if err != nil {
			return []string{}, fmt.Errorf(
				"error creating cookbook directory %s: %v",
				c.cookbook.Path,
				err,
			)
		}
	}

	err := c.genDirs(c.options.BaseDirs)
	if err != nil {
		return []string{}, err
	}

	var moddedFile []string
	for _, file := range c.options.BaseFiles {
		cfile := path.Join(c.cookbook.Path, file)
		// If the file already exists then dont overwrite it
		if fileutil.FileExist(cfile) {
			continue
		}

		tfile := path.Join(c.options.TemplateDir, "base", file)
		err := c.genBaseFile(cfile, tfile)
		if err != nil {
			return moddedFile, err
		}

		moddedFile = append(moddedFile, cfile)
	}

	return moddedFile, nil
}

// Generates a stencil
// Takes the stencil name to be generated
func (c *Chef) GenerateStencil(name string, stencilset fastfood.StencilSet, opts map[string]string) ([]string, error) {
	var moddedFiles []string
	opts = stencilset.MergeOpts(name, opts)
	chefOpts, err := c.chefOpts(name, stencilset)
	if err != nil {
		return moddedFiles, err
	}

	tOpts := struct {
		*fastfood.Helpers
		Cookbook chef.Cookbook
		Options  map[string]string
	}{
		Cookbook: c.cookbook,
		Options:  CapitalizeOptions(opts),
	}

	// Generate the directories
	c.genDirs(append(chefOpts.Directories))

	// Generate the files
	files := chefOpts.Files
	pfiles := chefOpts.Partials

	for cfile, tfile := range files {
		// Find any instance of <NAME> and replace with the name
		cfile = strings.Replace(cfile, "<NAME>", tOpts.Options["Name"], 1)
		tfile = path.Join(stencilset.BasePath, tfile)

		var pContent []string
		for _, partial := range pfiles {
			b, err := ioutil.ReadFile(path.Join(stencilset.BasePath, partial))
			if err != nil {
				return moddedFiles, fmt.Errorf("error %v occured while reading file %s", err, partial)
			}

			pContent = append(pContent, string(b))<commit_msg>if a path doesn't exist then create it before attempting to write a file<commit_after>package framework

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strings"

	"github.com/jarosser06/fastfood"
	"github.com/jarosser06/fastfood/common/fileutil"
	"github.com/jarosser06/fastfood/common/stringutil"
	"github.com/jarosser06/fastfood/framework/chef"
)

type Chef struct {
	cookbook chef.Cookbook
	options  fastfood.FrameworkOptions
}

// Initializes a framework with options
func (c *Chef) Init(frameworkOptions fastfood.FrameworkOptions) error {
	c.options = frameworkOptions
	if chef.PathIsCookbook(c.options.Destination) {
		var err error
		c.cookbook, err = chef.NewCookbookFromPath(c.options.Destination)
		if err != nil {
			return err
		}

		return nil
	}

	cpath := path.Join(c.options.Destination, c.options.Name)
	// Create cookbook
	if fileutil.FileExist(cpath) {
		var err error
		c.cookbook, err = chef.NewCookbookFromPath(cpath)
		if err != nil {
			return err
		}
	} else {
		c.cookbook = chef.NewCookbook(c.options.Destination, c.options.Name)
	}

	return nil
}

// Generates a basic cookbook structure
func (c *Chef) GenerateBase() ([]string, error) {
	// Generate the cookbook if it doesn't exist
	if !fileutil.FileExist(c.cookbook.Path) {
		err := os.Mkdir(c.cookbook.Path, 0755)
		if err != nil {
			return []string{}, fmt.Errorf(
				"error creating cookbook directory %s: %v",
				c.cookbook.Path,
				err,
			)
		}
	}

	err := c.genDirs(c.options.BaseDirs)
	if err != nil {
		return []string{}, err
	}

	var moddedFile []string
	for _, file := range c.options.BaseFiles {
		cfile := path.Join(c.cookbook.Path, file)
		// If the file already exists then dont overwrite it
		if fileutil.FileExist(cfile) {
			continue
		}

		tfile := path.Join(c.options.TemplateDir, "base", file)
		err := c.genBaseFile(cfile, tfile)
		if err != nil {
			return moddedFile, err
		}

		moddedFile = append(moddedFile, cfile)
	}

	return moddedFile, nil
}

// Generates a stencil
// Takes the stencil name to be generated
func (c *Chef) GenerateStencil(name string, stencilset fastfood.StencilSet, opts map[string]string) ([]string, error) {
	var moddedFiles []string
	opts = stencilset.MergeOpts(name, opts)
	chefOpts, err := c.chefOpts(name, stencilset)
	if err != nil {
		return moddedFiles, err
	}

	tOpts := struct {
		*fastfood.Helpers
		Cookbook chef.Cookbook
		Options  map[string]string
	}{
		Cookbook: c.cookbook,
		Options:  CapitalizeOptions(opts),
	}

	// Generate the directories
	c.genDirs(append(chefOpts.Directories))

	// Generate the files
	files := chefOpts.Files
	pfiles := chefOpts.Partials

	for cfile, tfile := range files {
		// Find any instance of <NAME> and replace with the name
		cfile = strings.Replace(cfile, "<NAME>", tOpts.Options["Name"], 1)
		tfile = path.Join(stencilset.BasePath, tfile)

		// Make sure the path exists before attempting to write the file
		c.genDirs([]string{path.Dir(cfile)})

		var pContent []string
		for _, partial := range pfiles {
			b, err := ioutil.ReadFile(path.Join(stencilset.BasePath, partial))
			if err != nil {
				return moddedFiles, fmt.Errorf("error %v occured while reading file %s", err, partial)
			}

			pContent = append(pContent, string(b))
{'fraction_non_alphanumeric': 0.1226866624122527, 'fraction_numerical': 0.004148053605615826, 'mean_word_length': 3.807515337423313, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2709847', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2225, 'n_words': 558}