Пример #1
0
func (self *file) Run(ctx scaffolt.Context, dryrun bool) (err error) {

	source := filepath.Join(ctx.Source(), InterpolateOrDefault(randomString(20), self.desc.Source, ctx))
	target := filepath.Join(ctx.Target(), InterpolateOrDefault(randomString(20), self.desc.Target, ctx))

	hasContent := self.desc.Content != ""
	self.l.Printf("Creating file: %s", target)
	if dryrun {
		return nil
	}
	if self.desc.Interpolate {
		var str string
		if hasContent {
			str, err = Interpolate(randomString(20)+"name", self.desc.Content, ctx)
		} else {
			str, err = InterpolateFile(source, ctx)
		}

		if err == nil {
			err = createFileBytes(target, []byte(str), false)
		}

	} else {
		if hasContent {
			err = createFileBytes(target, []byte(self.desc.Content), false)
		} else {
			err = createFile(target, source, false)
		}
	}

	return err
}
Пример #2
0
func (self *engine) Run(path string, ctx scaffolt.Context) error {
	/*val, err := self.engine.ToValue(ctx)
	if err != nil {
		return err
	}*/
	c, _ := self.engine.ToValue(&context{ctx, self.engine})
	//self.engine.Set("context", c)
	value, err := self.engine.Require(path, ctx.Source())

	if err != nil {
		return err
	}

	if value.IsFunction() {
		_, err := value.Call(otto.UndefinedValue(), c)
		if err != nil {
			return err
		}
	} else if value.IsObject() {
		v := value.Object()
		prop := "run"
		if Contains(v.Keys(), "run") {
			prop = "run"
		} else if Contains(v.Keys(), "Run") {
			prop = "Run"
		}
		_, err := v.Call(prop, c)
		if err != nil {
			return err
		}
	}

	return nil
}
Пример #3
0
func Interpolate(name, str string, ctx scaffolt.Context) (string, error) {
	t, err := template.New(name).Parse(str)

	if err != nil {
		return "", err
	}
	var buf bytes.Buffer
	err = t.Execute(&buf, ctx.Locals())
	if err != nil {
		return "", err
	}
	return string(buf.Bytes()), nil
}
Пример #4
0
func (self *Questions) Run(ctx scaffolt.Context) error {

	for _, q := range self.desc {
		w := getWidget(q.Name, q, ctx)
		if w == nil {
			return fmt.Errorf("No widget: %s", q.Type)
		}
		ctx.Set(q.Name, w.Run())

		for _, file := range q.Files {
			ctx.Generator().AddFile(file)
		}
	}

	return nil
}
Пример #5
0
func (self *task) Run(ctx scaffolt.Context) error {
	ctx.Set(self.desc.Name, dict.NewMap())
	ctx = &taskContext{ctx, self.desc.Name}

	if err := self.runHook(Before, ctx); err != nil {
		return err
	}
	self.l.Printf("Running questions")
	if err := self.questions.Run(ctx); err != nil {
		return err
	}
	self.l.Printf("Running files")
	if err := self.runFiles(ctx); err != nil {
		return err
	}

	if err := self.runHook(After, ctx); err != nil {
		return err
	}

	return nil
}