You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // licence that can be found in the LICENSE file.
  4. // The gomvpkg command moves go packages, updating import declarations.
  5. // See the -help message or Usage constant for details.
  6. package main
  7. import (
  8. "flag"
  9. "fmt"
  10. "go/build"
  11. "os"
  12. "golang.org/x/tools/go/buildutil"
  13. "golang.org/x/tools/refactor/rename"
  14. )
  15. var (
  16. fromFlag = flag.String("from", "", "Import path of package to be moved")
  17. toFlag = flag.String("to", "", "Destination import path for package")
  18. vcsMvCmdFlag = flag.String("vcs_mv_cmd", "", `A template for the version control system's "move directory" command, e.g. "git mv {{.Src}} {{.Dst}}`)
  19. helpFlag = flag.Bool("help", false, "show usage message")
  20. )
  21. func init() {
  22. flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
  23. }
  24. const Usage = `gomvpkg: moves a package, updating import declarations
  25. Usage:
  26. gomvpkg -from <path> -to <path> [-vcs_mv_cmd <template>]
  27. Flags:
  28. -from specifies the import path of the package to be moved
  29. -to specifies the destination import path
  30. -vcs_mv_cmd specifies a shell command to inform the version control system of a
  31. directory move. The argument is a template using the syntax of the
  32. text/template package. It has two fields: Src and Dst, the absolute
  33. paths of the directories.
  34. For example: "git mv {{.Src}} {{.Dst}}"
  35. gomvpkg determines the set of packages that might be affected, including all
  36. packages importing the 'from' package and any of its subpackages. It will move
  37. the 'from' package and all its subpackages to the destination path and update all
  38. imports of those packages to point to its new import path.
  39. gomvpkg rejects moves in which a package already exists at the destination import
  40. path, or in which a directory already exists at the location the package would be
  41. moved to.
  42. gomvpkg will not always be able to rename imports when a package's name is changed.
  43. Import statements may want further cleanup.
  44. gomvpkg's behavior is not defined if any of the packages to be moved are
  45. imported using dot imports.
  46. Examples:
  47. % gomvpkg -from myproject/foo -to myproject/bar
  48. Move the package with import path "myproject/foo" to the new path
  49. "myproject/bar".
  50. % gomvpkg -from myproject/foo -to myproject/bar -vcs_mv_cmd "git mv {{.Src}} {{.Dst}}"
  51. Move the package with import path "myproject/foo" to the new path
  52. "myproject/bar" using "git mv" to execute the directory move.
  53. `
  54. func main() {
  55. flag.Parse()
  56. if len(flag.Args()) > 0 {
  57. fmt.Fprintln(os.Stderr, "gomvpkg: surplus arguments.")
  58. os.Exit(1)
  59. }
  60. if *helpFlag || *fromFlag == "" || *toFlag == "" {
  61. fmt.Println(Usage)
  62. return
  63. }
  64. if err := rename.Move(&build.Default, *fromFlag, *toFlag, *vcsMvCmdFlag); err != nil {
  65. fmt.Fprintf(os.Stderr, "gomvpkg: %s.\n", err)
  66. os.Exit(1)
  67. }
  68. }