示例#1
0
文件: compiler.go 项目: kego/ke
func Compile(ctx context.Context, source []byte, mapping bool) ([]byte, error) {
	vos := vosctx.FromContext(ctx)

	options := &build.Options{
		GOROOT:        vos.Getenv("GOROOT"),
		GOPATH:        vos.Getenv("GOPATH"),
		CreateMapFile: mapping,
	}
	s := build.NewSession(options)

	packages := make(map[string]*compiler.Archive)
	importContext := &compiler.ImportContext{
		Packages: s.Types,
		Import:   s.BuildImportPath,
	}

	fileSet := token.NewFileSet()

	file, err := parser.ParseFile(fileSet, "prog.go", source, parser.ParseComments)
	if err != nil {
		return nil, kerr.Wrap("NCYFEKGCWX", err)
	}

	mainPkg, err := compiler.Compile("main", []*ast.File{file}, fileSet, importContext, false)
	if err != nil {
		return nil, kerr.Wrap("KPHUKOLTBX", err)
	}
	packages["main"] = mainPkg

	bufCode := bytes.NewBuffer(nil)
	filter := &compiler.SourceMapFilter{Writer: bufCode}
	allPkgs, err := compiler.ImportDependencies(mainPkg, importContext.Import)
	if err != nil {
		return nil, kerr.Wrap("TIMQHFQTWL", err)
	}

	if mapping {
		bufMap := bytes.NewBuffer(nil)
		smap := &sourcemap.Map{File: "script.js"}
		filter.MappingCallback = build.NewMappingCallback(smap, options.GOROOT, options.GOPATH, false)
		if err := compiler.WriteProgramCode(allPkgs, filter); err != nil {
			return nil, kerr.Wrap("YKQEKRKBPL", err)
		}
		if err := smap.WriteTo(bufMap); err != nil {
			return nil, kerr.Wrap("VYQGYAAADG", err)
		}
		return bufMap.Bytes(), nil
	}

	if err := compiler.WriteProgramCode(allPkgs, filter); err != nil {
		return nil, kerr.Wrap("DPPVHCOTBQ", err)
	}
	if _, err := bufCode.WriteString("//# sourceMappingURL=script.js.map\n"); err != nil {
		return nil, kerr.Wrap("CXXKWQVGUI", err)
	}
	return bufCode.Bytes(), nil
}
示例#2
0
文件: initialise.go 项目: kego/ke
func Initialise(ctx context.Context, overrides OptionsInterface) (context.Context, context.CancelFunc, error) {
	if overrides == nil {
		overrides = Flags{}
	}
	options := overrides.getOptions()

	ctx, cancel := context.WithCancel(ctx)
	ctx = jsonctx.AutoContext(ctx)
	ctx = wgctx.NewContext(ctx)
	ctx = sysctx.NewContext(ctx)

	cmd := &cmdctx.Cmd{}

	vos := vosctx.FromContext(ctx)

	path := ""
	cmd.Edit = options.Edit
	cmd.Validate = options.Validate
	cmd.Update = options.Update
	cmd.Log = options.Log
	cmd.Debug = options.Debug
	cmd.Port = options.Port
	if options.Path == "" {
		dir, err := vos.Getwd()
		if err != nil {
			return nil, nil, kerr.Wrap("OKOLXAMBSJ", err)
		}
		p, err := packages.GetPackageFromDir(ctx, dir)
		if err != nil {
			return nil, nil, kerr.Wrap("ADNJKTLAWY", err)
		}
		path = p
	} else {
		path = options.Path
	}

	ctx = cmdctx.NewContext(ctx, cmd)

	pcache, err := parser.Parse(ctx, path)
	if err != nil {
		return nil, nil, kerr.Wrap("EBMBIBIKUF", err)
	}

	ctx = envctx.NewContext(ctx, pcache.Env)

	return ctx, cancel, nil
}
示例#3
0
文件: parser.go 项目: kego/ke
func GoGet(ctx context.Context, path string) error {
	vos := vosctx.FromContext(ctx)
	cmd := cmdctx.FromContext(ctx)
	cmd.Print("Running go get -d ")
	args := []string{"get", "-d"}
	if cmd.Update {
		cmd.Print("-u ")
		args = append(args, "-u")
	}
	cmd.Print(path, "...")
	args = append(args, path)
	exe := exec.Command("go", args...)
	exe.Env = vos.Environ()
	if combined, err := exe.CombinedOutput(); err != nil {
		if !strings.Contains(string(combined), "no buildable Go source files") {
			return kerr.New("NIKCKQAKUI", "%s: %s", err.Error(), combined)
		}
	}
	cmd.Println(" OK.")
	return nil
}
示例#4
0
文件: packages.go 项目: kego/ke
func GetCurrentGopath(ctx context.Context) string {
	vos := vosctx.FromContext(ctx)
	currentDir, _ := vos.Getwd()
	return gopackages.GetCurrentGopath(vos.Getenv("GOPATH"), currentDir)
}
示例#5
0
文件: packages.go 项目: kego/ke
func GetPackageFromDir(ctx context.Context, dir string) (string, error) {
	vos := vosctx.FromContext(ctx)
	return gopackages.GetPackageFromDir(vos.Getenv("GOPATH"), dir)
}
示例#6
0
文件: packages.go 项目: kego/ke
func GetDirFromEmptyPackage(ctx context.Context, path string) (string, error) {
	vos := vosctx.FromContext(ctx)
	return gopackages.GetDirFromEmptyPackage(vos.Getenv("GOPATH"), path)
}