diff --git a/doc/github.go b/doc/github.go index b911a8e1b..f25916cb9 100644 --- a/doc/github.go +++ b/doc/github.go @@ -15,7 +15,6 @@ import ( "regexp" "strings" - "github.com/GPMGo/gpm/models" "github.com/GPMGo/gpm/utils" ) @@ -29,7 +28,7 @@ func SetGithubCredentials(id, secret string) { githubCred = "client_id=" + id + "&client_secret=" + secret } -func GetGithubDoc(client *http.Client, match map[string]string, commit string) (*models.PkgInfo, error) { +func GetGithubDoc(client *http.Client, match map[string]string, commit string) (*Package, error) { SetGithubCredentials("1862bcb265171f37f36c", "308d71ab53ccd858416cfceaed52d5d5b7d53c5f") match["cred"] = githubCred @@ -88,11 +87,17 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) ( // Create destination directory os.Mkdir(installPath, os.ModePerm) - files := make([]*source, 0, len(r.File)) + dirs := make([]string, 0, 5) for _, f := range r.File { - srcName := f.FileInfo().Name()[strings.Index(f.FileInfo().Name(), "/")+1:] - fmt.Printf("Unzipping %s...", srcName) - fn := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1) + absPath := strings.Replace(f.FileInfo().Name(), shaName, installPath, 1) + fmt.Printf("Unzipping %s...", absPath) + + // Check if it is directory or not. + if strings.HasSuffix(absPath, "/") { + // Directory. + dirs = append(dirs, absPath) + continue + } // Get files from archive rc, err := f.Open() @@ -101,38 +106,38 @@ func GetGithubDoc(client *http.Client, match map[string]string, commit string) ( } // Create diretory before create file - os.MkdirAll(path.Dir(fn), os.ModePerm) + os.MkdirAll(path.Dir(absPath), os.ModePerm) // Write data to file - fw, _ := os.Create(fn) + fw, _ := os.Create(absPath) if err != nil { return nil, err } - _, err = io.Copy(fw, rc) + n, err := io.Copy(fw, rc) if err != nil { return nil, err } + fmt.Println(n) - localF, _ := os.Open(fn) + /*localF, _ := os.Open(absPath) fbytes := make([]byte, f.FileInfo().Size()) n, _ := localF.Read(fbytes) - fmt.Println(n) // Check if Go source file. - if n > 0 && strings.HasSuffix(fn, ".go") { + if n > 0 && strings.HasSuffix(absPath, ".go") { files = append(files, &source{ name: srcName, data: fbytes, }) - } + }*/ } - w := &walker{ - pinfo: &models.PkgInfo{ - Path: importPath, - Commit: commit, - }, + pkg := &Package{ + ImportPath: importPath, + AbsPath: installPath, + Commit: commit, + Dirs: dirs, } - return w.build(files) + return pkg, nil } diff --git a/doc/struct.go b/doc/struct.go index 85ce42714..b04be3093 100644 --- a/doc/struct.go +++ b/doc/struct.go @@ -8,10 +8,24 @@ import ( "go/token" "os" "time" - - "github.com/GPMGo/gpm/models" ) +// Package represents a package. +type Package struct { + // Package import path. + ImportPath string + AbsPath string + + // Revision tag and project tags. + Commit string + + // Imports. + Imports []string + + // Directories. + Dirs []string +} + // source is source code file. type source struct { name string @@ -29,7 +43,7 @@ func (s *source) Sys() interface{} { return nil } // walker holds the state used when building the documentation. type walker struct { - pinfo *models.PkgInfo - srcs map[string]*source // Source files. - fset *token.FileSet + pkg *Package + srcs map[string]*source // Source files. + fset *token.FileSet } diff --git a/doc/walker.go b/doc/walker.go index aebb8e317..b52327cd5 100644 --- a/doc/walker.go +++ b/doc/walker.go @@ -91,7 +91,7 @@ func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, e var buildPicPattern = regexp.MustCompile(`\[+!+\[+([a-zA-Z ]*)+\]+\(+[a-zA-z]+://[^\s]*`) // build generates data from source files. -func (w *walker) build(srcs []*source) (*models.PkgInfo, error) { +/*func (w *walker) build(srcs []*source) (*models.PkgInfo, error) { // Set created time. w.pinfo.Created = time.Now().UTC() @@ -125,7 +125,6 @@ func (w *walker) build(srcs []*source) (*models.PkgInfo, error) { if nogo { err = nil } else { - fmt.Println(w.pinfo) return w.pinfo, errors.New("doc.walker.build(): " + err.Error()) } } @@ -147,3 +146,4 @@ func (w *walker) build(srcs []*source) (*models.PkgInfo, error) { // beego.Info("doc.walker.build(", pdoc.ImportPath, "), Goroutine #", runtime.NumGoroutine()) return w.pinfo, err } +*/ diff --git a/install.go b/install.go index 6a5060a8a..07e23fa9a 100644 --- a/install.go +++ b/install.go @@ -12,12 +12,12 @@ import ( "strings" "github.com/GPMGo/gpm/doc" - "github.com/GPMGo/gpm/models" "github.com/GPMGo/gpm/utils" ) var ( isHasGit, isHasHg bool + downloadCache map[string]bool // Saves packages that have downloaded. ) var cmdInstall = &Command{ @@ -25,6 +25,7 @@ var cmdInstall = &Command{ } func init() { + downloadCache = make(map[string]bool) cmdInstall.Run = runInstall cmdInstall.Flags = map[string]bool{ "-p": false, @@ -107,10 +108,13 @@ func downloadPackage(path, commit string) { } fmt.Printf("Downloading package: %s.\n", path) - _, err := pureDownload(path, commit) + pkg, err := pureDownload(path, commit) if err != nil { fmt.Printf("Fail to download package(%s) with error: %s.\n", path, err) } else { + fmt.Println(pkg) + fmt.Printf("Checking imports(%s).\n", path) + fmt.Printf("Installing package: %s.\n", path) } } @@ -133,7 +137,7 @@ func checkGoGetFlags() (args []string) { type service struct { pattern *regexp.Regexp prefix string - get func(*http.Client, map[string]string, string) (*models.PkgInfo, error) + get func(*http.Client, map[string]string, string) (*doc.Package, error) } // services is the list of source code control services handled by gopkgdoc. @@ -145,7 +149,7 @@ var services = []*service{ } // pureDownload downloads package without control control. -func pureDownload(path, commit string) (pinfo *models.PkgInfo, err error) { +func pureDownload(path, commit string) (pinfo *doc.Package, err error) { for _, s := range services { if s.get == nil || !strings.HasPrefix(path, s.prefix) { continue diff --git a/models/models.go b/models/models.go index ec709833c..579351a8d 100644 --- a/models/models.go +++ b/models/models.go @@ -27,6 +27,7 @@ const ( type PkgInfo struct { Id int64 Path string `qbs:"index"` // Import path of package. + AbsPath string Imports []string Note string Created time.Time `qbs:"index"` // Time when information last updated.