makr

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2019 License: MIT Imports: 15 Imported by: 478

README

makr

GoDoc

Makr is a file generation system for Go.

Usage

Execute a command
// Execute a npm install command.
g := makr.New()
g.Add(makr.NewCommand(exec.Command("npm", "install")))
err = g.Run(".", makr.Data{})
if err != nil {
    // Error!
}
Execute Go commands
// Get golang.org/x/tools/cmd/goimports package, update it if it's already in GOPATH.
g := makr.New()
g.Add(makr.NewCommand(makr.GoGet("golang.org/x/tools/cmd/goimports", "-u")))
err = g.Run(".", makr.Data{})
if err != nil {
    // Error!
}

Available Go commands:

  • GoGet
  • GoInstall
  • GoFmt
Create a file from a golang template string
s := "my file contents"
g := makr.New()
g.Add(makr.NewFile("file.txt", s))
err = g.Run(".", makr.Data{})
if err != nil {
    // Error!
}

Chained usage

s = `
{
  "name": "buffalo",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "babel-cli": "~6.24.1",
    "babel-core": "~6.25.0",
    "babel-loader": "~7.0.0",
    "babel-preset-env": "~1.5.2",
    "bootstrap-sass": "~3.3.7",
    "clean-webpack-plugin": "~0.1.17",
    "copy-webpack-plugin": "~4.0.1",
    "css-loader": "~0.28.4",
    "expose-loader": "~0.7.3",
    "extract-text-webpack-plugin": "2.1.2",
    "file-loader": "~0.11.2",
    "font-awesome": "~4.7.0",
    "gopherjs-loader": "^0.0.1",
    "highlightjs": "^9.10.0",
    "jquery": "~3.2.1",
    "jquery-ujs": "~1.2.2",
    "node-sass": "~4.7.2",
    "npm-install-webpack-plugin": "4.0.4",
    "path": "~0.12.7",
    "sass-loader": "~6.0.5",
    "style-loader": "~0.18.2",
    "uglifyjs-webpack-plugin": "~0.4.6",
    "url-loader": "~0.5.9",
    "webpack": "~2.3.0",
    "webpack-manifest-plugin": "~1.2.1"
  }
}
`
// Create a package.json file, then execute npm install
g := makr.New()
g.Add(makr.NewFile("package.json", s))
g.Add(makr.NewCommand(exec.Command("npm", "install")))

err = g.Run(".", makr.Data{})
if err != nil {
    // Error!
}

Documentation

Index

Constants

View Source
const Version = "v1.2.0"

Version of makr

Variables

View Source
var Helpers = map[string]interface{}{
	"js_escape":   template.JSEscapeString,
	"html_escape": template.HTMLEscapeString,
	"json":        toJSON,
	"upcase":      strings.ToUpper,
	"downcase":    strings.ToLower,
}

Helpers that are automatically injected into templates.

Functions

func GoFmt

func GoFmt(files ...string) *exec.Cmd

GoFmt is command that will use `goimports` if available, or fail back to `gofmt` otherwise.

func GoGet

func GoGet(pkg string, opts ...string) *exec.Cmd

GoGet downloads and installs packages and dependencies

func GoInstall

func GoInstall(pkg string, opts ...string) *exec.Cmd

GoInstall compiles and installs packages and dependencies

Types

type Command

type Command struct {
	Should  ShouldFunc
	Command *exec.Cmd
}

Command wraps an exec.Cmd to be run

func NewCommand

func NewCommand(cmd *exec.Cmd) Command

NewCommand ready to run

func (Command) Run

func (c Command) Run(rootPath string, data Data) error

Run the generator

type Data

type Data map[string]interface{}

Data to be passed into generators

type File

type File struct {
	Path          string
	Template      string
	TemplateFuncs template.FuncMap
	Permission    os.FileMode
	Should        ShouldFunc
}

File to generate

func NewFile

func NewFile(path string, t string) File

NewFile set up with sensible defaults

func (File) Run

func (f File) Run(rootPath string, data Data) error

Run the generator

type Func

type Func struct {
	Should ShouldFunc
	Runner RunFn
}

Func to be run as a generator

func (Func) Run

func (f Func) Run(rootPath string, data Data) error

Run the generator

type Generator

type Generator struct {
	Runners []Runnable
	Should  ShouldFunc
	Data    Data
}

Generator is the top level construct that holds all of the Runnables

func New

func New() *Generator

New Generator

func (*Generator) Add

func (g *Generator) Add(r Runnable)

Add a Runnable generator to the list

func (*Generator) Fmt added in v1.1.0

func (g *Generator) Fmt(rootPath string)

Fmt formats Go code using `goimports` if available, or `gofmt` otherwise.

func (*Generator) Run

func (g *Generator) Run(rootPath string, data Data) error

Run all of the generators

type RemoteFile

type RemoteFile struct {
	RemotePath string
	File
}

RemoteFile represents a remote file (URL) that you want to pull down and write into the project

func (RemoteFile) Run

func (f RemoteFile) Run(rootPath string, data Data) error

Run the generator

type RunFn

type RunFn func(string, Data) error

RunFn interface for a function that can be used as a runner

type Runnable

type Runnable interface {
	Run(string, Data) error
}

Runnable interface must be implemented to be considered a runnable generator

type ShouldFunc

type ShouldFunc func(Data) bool

ShouldFunc decides whether a generator should be run or not

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL