<commit_before>package generate

import (
	"errors"
	"os"
	"path/filepath"
	"strings"

	"github.com/gobuffalo/buffalo/generators/resource"
	"github.com/gobuffalo/envy"
	"github.com/gobuffalo/makr"
	"github.com/markbates/inflect"
	"github.com/spf13/cobra"
)

const resourceExamples = `$ buffalo g resource users
Generates:

- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go
- migrations/2016020216301234_create_users.up.fizz
- migrations/2016020216301234_create_users.down.fizz

$ buffalo g resource users --skip-migration
Generates:

- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go

$ buffalo g resource users --skip-model
Generates:

- actions/users.go
- actions/users_test.go

$ buffalo g resource users --use-model
Generates:

- actions/users.go
- actions/users_test.go`

// SkipResourceMigration allows to generate a resource without the migration.
var SkipResourceMigration = false

// SkipResourceModel allows to generate a resource without the model and Migration.
var SkipResourceModel = false

// UseResourceModel allows to generate a resource with a working model.
var UseResourceModel = ""

// ResourceMimeType allows to generate a typed resource (HTML by default, JSON...).
var ResourceMimeType = "html"

// ResourceCmd generates a new actions/resource file and a stub test.
var ResourceCmd = &cobra.Command{
	Use:     "resource [name]",
	Example: resourceExamples,
	Aliases: []string{"r"},
	Short:   "Generates a new actions/resource file",
	RunE: func(cmd *cobra.Command, args []string) error {
		var name, modelName string

		// Allow overwriting modelName with the --use-model flag
		// buffalo generate resource users --use-model people
		if UseResourceModel != "" {
			modelName = inflect.Pluralize(UseResourceModel)
		}

		if len(args) == 0 {
			if UseResourceModel == "" {
				return errors.New("you must specify a resource name")
			}
			// When there is no resource name given and --use-model flag is set
			name = UseResourceModel
		} else {
			// When resource name is specified
			name = inflect.Pluralize(args[0])
			// If there is no --use-model flag set use the resource to create the model
			if modelName == "" {
				modelName = name
			}
		}

		if ResourceMimeType != "html" && ResourceMimeType != "json" && ResourceMimeType != "xml" {
			return errors.New("invalid resource type, you need to choose between \"html\", \"xml\" and \"json\"")
		}

		modelProps := getModelPropertiesFromArgs(args)

		data := makr.Data{
			"name":             name,
			"singular":         inflect.Singularize(name),
			"plural":           name,
			"camel":            inflect.Camelize(name),
			"under":            inflect.Underscore(name),
			"underSingular":    inflect.Singularize(inflect.Underscore(name)),
			"downFirstCap":     inflect.CamelizeDownFirst(name),
			"model":            inflect.Singularize(inflect.Camelize(modelName)),
			"modelPlural":      inflect.Camelize(modelName),
			"modelUnder":       inflect.Singularize(inflect.Underscore(modelName)),<commit_msg>simplifying conditions on the generate resource command<commit_after>package generate

import (
	"errors"
	"os"
	"path/filepath"
	"strings"

	"github.com/gobuffalo/buffalo/generators/resource"
	"github.com/gobuffalo/envy"
	"github.com/gobuffalo/makr"
	"github.com/markbates/inflect"
	"github.com/spf13/cobra"
)

const resourceExamples = `$ buffalo g resource users
Generates:

- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go
- migrations/2016020216301234_create_users.up.fizz
- migrations/2016020216301234_create_users.down.fizz

$ buffalo g resource users --skip-migration
Generates:

- actions/users.go
- actions/users_test.go
- models/user.go
- models/user_test.go

$ buffalo g resource users --skip-model
Generates:

- actions/users.go
- actions/users_test.go

$ buffalo g resource users --use-model
Generates:

- actions/users.go
- actions/users_test.go`

// SkipResourceMigration allows to generate a resource without the migration.
var SkipResourceMigration = false

// SkipResourceModel allows to generate a resource without the model and Migration.
var SkipResourceModel = false

// UseResourceModel allows to generate a resource with a working model.
var UseResourceModel = ""

// ResourceMimeType allows to generate a typed resource (HTML by default, JSON...).
var ResourceMimeType = "html"

// ResourceCmd generates a new actions/resource file and a stub test.
var ResourceCmd = &cobra.Command{
	Use:     "resource [name]",
	Example: resourceExamples,
	Aliases: []string{"r"},
	Short:   "Generates a new actions/resource file",
	RunE: func(cmd *cobra.Command, args []string) error {
		var name, modelName string

		if len(args) == 0 && UseResourceModel == "" {
			return errors.New("you must specify a resource name")
		}

		name = inflect.Pluralize(args[0])

		// Allow overwriting modelName with the --use-model flag
		// buffalo generate resource users --use-model people
		if UseResourceModel != "" {
			modelName = inflect.Pluralize(UseResourceModel)
			name = UseResourceModel
		}

		if modelName == "" {
			modelName = name
		}

		if ResourceMimeType != "html" && ResourceMimeType != "json" && ResourceMimeType != "xml" {
			return errors.New("invalid resource type, you need to choose between \"html\", \"xml\" and \"json\"")
		}

		modelProps := getModelPropertiesFromArgs(args)

		data := makr.Data{
			"name":             name,
			"singular":         inflect.Singularize(name),
			"plural":           name,
			"camel":            inflect.Camelize(name),
			"under":            inflect.Underscore(name),
			"underSingular":    inflect.Singularize(inflect.Underscore(name)),
			"downFirstCap":     inflect.CamelizeDownFirst(name),
			"model":            inflect.Singularize(inflect.Camelize(modelName)),
			"modelPlural":      inflect.Camelize(modelName),
			"modelUnder":       inflect.Singularize(inflect.Underscore(modelName)),
{'fraction_non_alphanumeric': 0.1164983164983165, 'fraction_numerical': 0.012121212121212121, 'mean_word_length': 3.64140625, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1534005', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1880, 'n_words': 476}