github.com/goplus/gop@v1.2.6/_gendeps.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package gop
    18  
    19  import (
    20  	"fmt"
    21  	"go/token"
    22  	"io/fs"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  
    27  	"github.com/goplus/gop/parser"
    28  	"github.com/goplus/mod/gopmod"
    29  	"github.com/goplus/mod/modfetch"
    30  
    31  	astmod "github.com/goplus/gop/ast/mod"
    32  )
    33  
    34  // -----------------------------------------------------------------------------
    35  
    36  func GenDepMods(mod *gopmod.Module, dir string, recursively bool) (ret map[string]struct{}, err error) {
    37  	modBase := mod.Path()
    38  	ret = make(map[string]struct{})
    39  	for _, r := range mod.Opt.Import {
    40  		ret[r.ClassfileMod] = struct{}{}
    41  	}
    42  	err = HandleDeps(mod, dir, recursively, func(pkgPath string) {
    43  		modPath, _ := modfetch.Split(pkgPath, modBase)
    44  		if modPath != "" && modPath != modBase {
    45  			ret[modPath] = struct{}{}
    46  		}
    47  	})
    48  	return
    49  }
    50  
    51  func HandleDeps(mod *gopmod.Module, dir string, recursively bool, h func(pkgPath string)) (err error) {
    52  	g := depsGen{
    53  		deps: astmod.Deps{HandlePkg: h},
    54  		mod:  mod,
    55  		fset: token.NewFileSet(),
    56  	}
    57  	if recursively {
    58  		err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
    59  			if err == nil && d.IsDir() {
    60  				if strings.HasPrefix(d.Name(), "_") { // skip _
    61  					return filepath.SkipDir
    62  				}
    63  				err = g.gen(path)
    64  				if err != nil {
    65  					fmt.Fprintln(os.Stderr, err)
    66  				}
    67  			}
    68  			return err
    69  		})
    70  	} else {
    71  		err = g.gen(dir)
    72  	}
    73  	return
    74  }
    75  
    76  type depsGen struct {
    77  	deps astmod.Deps
    78  	mod  *gopmod.Module
    79  	fset *token.FileSet
    80  }
    81  
    82  func (p depsGen) gen(dir string) (err error) {
    83  	pkgs, err := parser.ParseDirEx(p.fset, dir, parser.Config{
    84  		ClassKind: p.mod.ClassKind,
    85  		Mode:      parser.ImportsOnly,
    86  	})
    87  	if err != nil {
    88  		return
    89  	}
    90  
    91  	for _, pkg := range pkgs {
    92  		p.deps.Load(pkg, false)
    93  	}
    94  	return
    95  }
    96  
    97  // -----------------------------------------------------------------------------