Example #1
0
// Build returns the zipped contents of the function.
func (f *Function) Build() (io.Reader, error) {
	f.Log.Debugf("creating build")

	if err := f.RunHook("build"); err != nil {
		return nil, err
	}

	buf := new(bytes.Buffer)
	zip := archive.NewZipWriter(buf)

	if r, ok := f.runtime.(runtime.CompiledRuntime); ok {
		f.Log.Debugf("compiling")
		if err := r.Build(f.Path); err != nil {
			return nil, fmt.Errorf("compiling: %s", err)
		}
	}

	if f.env != nil {
		f.Log.Debugf("adding .env.json")

		b, err := json.Marshal(f.env)
		if err != nil {
			return nil, err
		}

		zip.AddBytes(".env.json", b)
	}

	if f.runtime.Shimmed() {
		f.Log.Debugf("adding nodejs shim")
		zip.AddBytes("index.js", shim.MustAsset("index.js"))
		zip.AddBytes("byline.js", shim.MustAsset("byline.js"))
	}

	files, err := utils.LoadFiles(f.Path, f.IgnoredPatterns)
	if err != nil {
		return nil, err
	}

	for path, file := range files {
		f.Log.WithField("file", path).Debug("add file")
		if err := zip.AddFile(path, file); err != nil {
			return nil, err
		}

		if err := file.Close(); err != nil {
			return nil, err
		}
	}

	if err := zip.Close(); err != nil {
		return nil, err
	}

	return buf, nil
}
Example #2
0
File: shim.go Project: pilwon/apex
// addShim saves nodejs shim.
func (p *Plugin) addShim(fn *function.Function) error {
	fn.Log.Debug("add shim")

	if err := ioutil.WriteFile(filepath.Join(fn.Path, "index.js"), shim.MustAsset("index.js"), 0666); err != nil {
		return err
	}

	if err := ioutil.WriteFile(filepath.Join(fn.Path, "byline.js"), shim.MustAsset("byline.js"), 0666); err != nil {
		return err
	}

	return nil
}
Example #3
0
// Build adds the nodejs shim files.
func (p *Plugin) Build(fn *function.Function, zip *archive.Archive) error {
	if fn.Shim {
		fn.Log.Debug("add shim")

		if err := zip.AddBytes("index.js", shim.MustAsset("index.js")); err != nil {
			return err
		}

		if err := zip.AddBytes("byline.js", shim.MustAsset("byline.js")); err != nil {
			return err
		}
	}

	return nil
}
Example #4
0
// Zip returns the zipped contents of the function.
func (f *Function) Zip() (io.Reader, error) {
	buf := new(bytes.Buffer)
	zip := archive.NewZipWriter(buf)

	if r, ok := f.runtime.(runtime.CompiledRuntime); ok {
		f.Log.Debugf("compiling")
		if err := r.Build(f.Path); err != nil {
			return nil, fmt.Errorf("compiling: %s", err)
		}
	}

	if f.env != nil {
		f.Log.Debugf("adding .env.json")

		b, err := json.Marshal(f.env)
		if err != nil {
			return nil, err
		}

		zip.AddBytes(".env.json", b)
	}

	if f.runtime.Shimmed() {
		f.Log.Debugf("adding nodejs shim")
		zip.AddBytes("index.js", shim.MustAsset("index.js"))
		zip.AddBytes("byline.js", shim.MustAsset("byline.js"))
	}

	for path, file := range f.files {
		if err := zip.AddFile(path, file); err != nil {
			return nil, err
		}
		defer file.Close()
	}

	if err := zip.Close(); err != nil {
		return nil, err
	}

	return buf, nil
}