Exemple #1
0
func rewriteImports() (rewritten bool) {
	filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
		if err != nil || info.IsDir() {
			return nil
		}
		fset := token.NewFileSet()
		f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
		if err != nil {
			return nil
		}
		for _, im := range f.Imports {
			for _, site := range ImportSites {
				v, err := strconv.Unquote(im.Path.Value)
				if err != nil {
					log.Fatal(err)
				}
				// don't replace ourself
				if strings.HasPrefix(v, relpath+"/") {
					continue
				}
				if strings.HasPrefix(v, site+"/") {
					target := ThirdParty + "/" + v
					if !*relative {
						target = relpath + "/" + target
					}
					if *verbose {
						log.Printf("rewrite %s: %s -> %s\n", path, v, target)
					}
					if *dryrun {
						break
					}
					if !astutil.RewriteImport(fset, f, v, target) {
						log.Fatalf("%s: could not rewrite %s", path, im.Path.Value)
					}
					rewritten = true
					break
				}
			}
		}
		var buf bytes.Buffer
		if err := printer.Fprint(&buf, fset, f); err != nil {
			log.Fatal(err)
		}
		fb, err := format.Source(buf.Bytes())
		if err != nil {
			log.Fatal(err)
		}
		if err := ioutil.WriteFile(path, fb, info.Mode()); err != nil {
			log.Fatal(err)
		}
		return nil
	})
	return
}
Exemple #2
0
func rewriteImports(fset *token.FileSet, pkg *build.Package, files map[string]*ast.File, rewrites map[string]string, st *rewriteState, opts *rewriteOptions) error {
	for k, v := range files {
		rewritten := make(map[string]string)
		imports := astutil.Imports(fset, v)
		for _, group := range imports {
			for _, imp := range group {
				if unquoted, err := strconv.Unquote(imp.Path.Value); err == nil {
					for rk, rv := range rewrites {
						if !strings.HasPrefix(unquoted, rk) {
							continue
						}
						newImport := strings.Replace(unquoted, rk, rv, 1)
						if !opts.DryRun {
							if err := st.DownloadImport(newImport, opts); err != nil {
								fmt.Fprintf(os.Stderr, "couldn't download %s, using original", newImport)
								continue
							}
						}
						rewritten[unquoted] = newImport
					}
				}
			}
		}
		if len(rewritten) == 0 {
			continue
		}
		if opts.DryRun || opts.Verbose {
			if opts.DryRun {
				fmt.Printf("would rewrite %d imports in %s:\n", len(rewritten), k)
			} else {
				fmt.Printf("rewrite %d imports in %s:\n", len(rewritten), k)
			}
			for ik, iv := range rewritten {
				fmt.Printf("\t%s => %s\n", ik, iv)
			}
			if opts.DryRun {
				continue
			}
		}
		for ik, iv := range rewritten {
			astutil.RewriteImport(fset, v, ik, iv)
		}
		// Same as go fmt
		cfg := &printer.Config{
			Tabwidth: 8,
			Mode:     printer.UseSpaces | printer.TabIndent,
		}
		var buf bytes.Buffer
		var data []byte
		var st os.FileInfo
		var err error
		if err = cfg.Fprint(&buf, fset, v); err == nil {
			if data, err = format.Source(buf.Bytes()); err == nil {
				if st, err = os.Stat(k); err == nil {
					err = ioutil.WriteFile(k, data, st.Mode())
				}
			}
		}
		if err != nil {
			fmt.Fprintf(os.Stderr, "error rewriting file %s: %s\n", k, err)
		}

	}
	return nil
}