示例#1
0
文件: main.go 项目: Kubuxu/gx-lua
func doRewrite(pkg *Package, cwd string, mapping map[string]string) error {
	rwm := func(in string) string {
		m, ok := mapping[in]
		if ok {
			return m
		}

		for k, v := range mapping {
			if strings.HasPrefix(in, k+"/") {
				nmapping := strings.Replace(in, k, v, 1)
				mapping[in] = nmapping
				return nmapping
			}
		}

		mapping[in] = in
		return in
	}

	filter := func(s string) bool {
		return strings.HasSuffix(s, ".go")
	}

	VLog("  - rewriting imports")
	err := rw.RewriteImports(cwd, rwm, filter)
	if err != nil {
		return err
	}
	VLog("  - finished!")

	return nil
}
示例#2
0
文件: importer.go 项目: Kubuxu/gx-lua
func doUpdate(dir, oldimp, newimp string) error {
	rwf := func(in string) string {
		if in == oldimp {
			return newimp
		}

		if strings.HasPrefix(in, oldimp+"/") {
			return strings.Replace(in, oldimp, newimp, 1)
		}

		return in
	}

	filter := func(in string) bool {
		return strings.HasSuffix(in, ".go") && !strings.HasPrefix(in, "vendor")
	}

	return rw.RewriteImports(dir, rwf, filter)
}
示例#3
0
文件: importer.go 项目: Kubuxu/gx-lua
func (i *Importer) rewriteImports(pkgpath string) error {

	filter := func(p string) bool {
		return !strings.HasPrefix(p, "vendor") &&
			!strings.HasPrefix(p, ".git") &&
			strings.HasSuffix(p, ".go") &&
			!strings.HasPrefix(p, "Godeps")
	}

	base := pkgpath[len(i.gopath)+5:]
	gdepath := base + "/Godeps/_workspace/src/"
	rwf := func(in string) string {
		if strings.HasPrefix(in, gdepath) {
			in = in[len(gdepath):]
		}

		if !i.rewrite {
			// if rewrite not specified, just fixup godeps paths
			return in
		}

		dep, ok := i.pkgs[in]
		if ok {
			return "gx/" + dep.Hash + "/" + dep.Name
		}

		parts := strings.Split(in, "/")
		if len(parts) > 3 {
			obase := strings.Join(parts[:3], "/")
			dep, bok := i.pkgs[obase]
			if !bok {
				return in
			}

			return strings.Replace(in, obase, "gx/"+dep.Hash+"/"+dep.Name, 1)
		}

		return in
	}

	return rw.RewriteImports(pkgpath, rwf, filter)
}