1
0
mirror of https://github.com/golang/go synced 2024-11-13 17:50:23 -07:00

goinstall: process dependencies for package main

Currently to install a command, you have to manually
goinstall each of the remote packages that it depends on.
This patch lets goinstall P work where P is
contains files in package main.
It does not actually build the package, but
it installs all of its dependencies and prints a message
to that effect.

R=rsc
CC=golang-dev
https://golang.org/cl/1301043
This commit is contained in:
Roger Peppe 2010-06-21 11:01:20 -07:00 committed by Russ Cox
parent 5cb4f82bbc
commit 3ce29380d5
3 changed files with 30 additions and 16 deletions

View File

@ -122,7 +122,7 @@ func install(pkg, parent string) {
} }
// Install prerequisites. // Install prerequisites.
files, m, err := goFiles(dir) files, m, pkgname, err := goFiles(dir, parent == "")
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s: %s: %s\n", argv0, pkg, err) fmt.Fprintf(os.Stderr, "%s: %s: %s\n", argv0, pkg, err)
errors = true errors = true
@ -138,6 +138,14 @@ func install(pkg, parent string) {
for p := range m { for p := range m {
install(p, pkg) install(p, pkg)
} }
if pkgname == "main" {
if !errors {
fmt.Fprintf(os.Stderr, "%s: %s's dependencies are installed.\n", argv0, pkg)
}
errors = true
visit[pkg] = done
return
}
// Install this package. // Install this package.
if !errors { if !errors {

View File

@ -35,11 +35,10 @@ func domake(dir, pkg string, local bool) os.Error {
// installing as package pkg. It includes all *.go files in the directory // installing as package pkg. It includes all *.go files in the directory
// except those in package main and those ending in _test.go. // except those in package main and those ending in _test.go.
func makeMakefile(dir, pkg string) ([]byte, os.Error) { func makeMakefile(dir, pkg string) ([]byte, os.Error) {
files, _, err := goFiles(dir) files, _, _, err := goFiles(dir, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var buf bytes.Buffer var buf bytes.Buffer
if err := makefileTemplate.Execute(&makedata{pkg, files}, &buf); err != nil { if err := makefileTemplate.Execute(&makedata{pkg, files}, &buf); err != nil {
return nil, err return nil, err

View File

@ -16,25 +16,25 @@ import (
"go/parser" "go/parser"
) )
// goFiles returns a list of the *.go source files in dir, // goFiles returns a list of the *.go source files in dir, excluding
// excluding those in package main or ending in _test.go. // those in package main (unless allowMain is true) or ending in
// It also returns a map giving the packages imported // _test.go. It also returns a map giving the packages imported by
// by those files. The map keys are the imported paths. // those files, and the package name.
// The key's value is one file that imports that path. // The map keys are the imported paths. The key's value
func goFiles(dir string) (files []string, imports map[string]string, err os.Error) { // is one file that imports that path.
func goFiles(dir string, allowMain bool) (files []string, imports map[string]string, pkgName string, err os.Error) {
f, err := os.Open(dir, os.O_RDONLY, 0) f, err := os.Open(dir, os.O_RDONLY, 0)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, "", err
} }
dirs, err := f.Readdir(-1) dirs, err := f.Readdir(-1)
f.Close() f.Close()
if err != nil { if err != nil {
return nil, nil, err return nil, nil, "", err
} }
files = make([]string, 0, len(dirs)) files = make([]string, 0, len(dirs))
imports = make(map[string]string) imports = make(map[string]string)
pkgName := ""
for i := range dirs { for i := range dirs {
d := &dirs[i] d := &dirs[i]
if !strings.HasSuffix(d.Name, ".go") || strings.HasSuffix(d.Name, "_test.go") { if !strings.HasSuffix(d.Name, ".go") || strings.HasSuffix(d.Name, "_test.go") {
@ -43,16 +43,23 @@ func goFiles(dir string) (files []string, imports map[string]string, err os.Erro
filename := path.Join(dir, d.Name) filename := path.Join(dir, d.Name)
pf, err := parser.ParseFile(filename, nil, nil, parser.ImportsOnly) pf, err := parser.ParseFile(filename, nil, nil, parser.ImportsOnly)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, "", err
} }
s := string(pf.Name.Name()) s := string(pf.Name.Name())
if s == "main" { if s == "main" && !allowMain {
continue continue
} }
if pkgName == "" { if pkgName == "" {
pkgName = s pkgName = s
} else if pkgName != s { } else if pkgName != s {
return nil, nil, os.ErrorString("multiple package names in " + dir) // Only if all files in the directory are in package main
// do we return pkgName=="main".
// A mix of main and another package reverts
// to the original (allowMain=false) behaviour.
if allowMain && pkgName == "main" {
return goFiles(dir, false)
}
return nil, nil, "", os.ErrorString("multiple package names in " + dir)
} }
n := len(files) n := len(files)
files = files[0 : n+1] files = files[0 : n+1]
@ -68,5 +75,5 @@ func goFiles(dir string) (files []string, imports map[string]string, err os.Erro
} }
} }
} }
return files, imports, nil return files, imports, pkgName, nil
} }